H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2 | * |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 6 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 10 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 17 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * preproc.c macro preprocessor for the Netwide Assembler |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 38 | /* Typical flow of text through preproc |
| 39 | * |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 40 | * pp_getline gets tokenized lines, either |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 41 | * |
| 42 | * from a macro expansion |
| 43 | * |
| 44 | * or |
| 45 | * { |
| 46 | * read_line gets raw text from stdmacpos, or predef, or current input file |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 47 | * tokenize converts to tokens |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 48 | * } |
| 49 | * |
| 50 | * expand_mmac_params is used to expand %1 etc., unless a macro is being |
| 51 | * defined or a false conditional is being processed |
| 52 | * (%0, %1, %+1, %-1, %%foo |
| 53 | * |
| 54 | * do_directive checks for directives |
| 55 | * |
| 56 | * expand_smacro is used to expand single line macros |
| 57 | * |
| 58 | * expand_mmacro is used to expand multi-line macros |
| 59 | * |
| 60 | * detoken is used to convert the line back to text |
| 61 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 62 | |
H. Peter Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 63 | #include "compiler.h" |
| 64 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 65 | #include <stdio.h> |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 66 | #include <stdarg.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 67 | #include <stdlib.h> |
| 68 | #include <stddef.h> |
| 69 | #include <string.h> |
| 70 | #include <ctype.h> |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 71 | #include <limits.h> |
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 | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 83 | #include "listing.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 84 | |
| 85 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 86 | typedef struct MMacro MMacro; |
| 87 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 88 | typedef struct Context Context; |
| 89 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 90 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 91 | typedef struct Line Line; |
| 92 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 93 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 94 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 97 | * Note on the storage of both SMacro and MMacros: the hash table |
| 98 | * indexes them case-insensitively, and we then have to go through a |
| 99 | * linked list of potential case aliases (and, for MMacros, parameter |
| 100 | * ranges); this is to preserve the matching semantics of the earlier |
| 101 | * code. If the number of case aliases for a specific macro is a |
| 102 | * performance issue, you may want to reconsider your coding style. |
| 103 | */ |
| 104 | |
| 105 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 106 | * Store the definition of a single-line macro. |
| 107 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 108 | struct SMacro { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 109 | SMacro *next; |
| 110 | char *name; |
| 111 | bool casesense; |
| 112 | bool in_progress; |
| 113 | unsigned int nparam; |
| 114 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 118 | * Store the definition of a multi-line macro. This is also used to |
| 119 | * store the interiors of `%rep...%endrep' blocks, which are |
| 120 | * effectively self-re-invoking multi-line macros which simply |
| 121 | * don't have a name or bother to appear in the hash tables. %rep |
| 122 | * blocks are signified by having a NULL `name' field. |
| 123 | * |
| 124 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 125 | * isn't merely boolean, but gives the number of repeats left to |
| 126 | * run. |
| 127 | * |
| 128 | * The `next' field is used for storing MMacros in hash tables; the |
| 129 | * `next_active' field is for stacking them on istk entries. |
| 130 | * |
| 131 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 132 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 133 | */ |
| 134 | struct MMacro { |
| 135 | MMacro *next; |
| 136 | MMacroInvocation *prev; /* previous invocation */ |
| 137 | char *name; |
| 138 | int nparam_min, nparam_max; |
| 139 | bool casesense; |
| 140 | bool plus; /* is the last parameter greedy? */ |
| 141 | bool nolist; /* is this macro listing-inhibited? */ |
| 142 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 143 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 144 | Token *dlist; /* All defaults as one list */ |
| 145 | Token **defaults; /* Parameter default pointers */ |
| 146 | int ndefs; /* number of default parameters */ |
| 147 | Line *expansion; |
| 148 | |
| 149 | MMacro *next_active; |
| 150 | MMacro *rep_nest; /* used for nesting %rep */ |
| 151 | Token **params; /* actual parameters */ |
| 152 | Token *iline; /* invocation line */ |
| 153 | unsigned int nparam, rotate; |
| 154 | int *paramlen; |
| 155 | uint64_t unique; |
| 156 | int lineno; /* Current line number on expansion */ |
| 157 | uint64_t condcnt; /* number of if blocks... */ |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | /* Store the definition of a multi-line macro, as defined in a |
| 162 | * previous recursive macro expansion. |
| 163 | */ |
| 164 | struct MMacroInvocation { |
| 165 | MMacroInvocation *prev; /* previous invocation */ |
| 166 | Token **params; /* actual parameters */ |
| 167 | Token *iline; /* invocation line */ |
| 168 | unsigned int nparam, rotate; |
| 169 | int *paramlen; |
| 170 | uint64_t unique; |
| 171 | uint64_t condcnt; |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 176 | * The context stack is composed of a linked list of these. |
| 177 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 178 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 179 | Context *next; |
| 180 | char *name; |
| 181 | struct hash_table localmac; |
| 182 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | /* |
| 186 | * This is the internal form which we break input lines up into. |
| 187 | * Typically stored in linked lists. |
| 188 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 189 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 190 | * necessarily used as-is, but is intended to denote the number of |
| 191 | * the substituted parameter. So in the definition |
| 192 | * |
| 193 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 194 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 195 | * the token representing `x' will have its type changed to |
| 196 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 197 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 198 | * |
| 199 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 200 | * which doesn't need quotes around it. Used in the pre-include |
| 201 | * mechanism as an alternative to trying to find a sensible type of |
| 202 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 203 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 204 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 205 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 206 | TOK_PREPROC_ID, TOK_STRING, |
| 207 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 208 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 209 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 210 | TOK_PASTE, /* %+ */ |
| 211 | TOK_INDIRECT, /* %[...] */ |
| 212 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 213 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 216 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 217 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 218 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 219 | struct tokseq_match { |
| 220 | int mask_head; |
| 221 | int mask_tail; |
| 222 | }; |
| 223 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 224 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 225 | Token *next; |
| 226 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 227 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 228 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 229 | size_t len; /* scratch length field */ |
| 230 | } a; /* Auxiliary data */ |
| 231 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 235 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 236 | * these, which is essentially a container to allow several linked |
| 237 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 238 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 239 | * Note that in this module, linked lists are treated as stacks |
| 240 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 241 | * `expansion' field in MMacro structures, so that the linked list, |
| 242 | * if walked, would give the macro lines in reverse order; this |
| 243 | * means that we can walk the list when expanding a macro, and thus |
| 244 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 245 | * order (so that when popped back off they are in the right |
| 246 | * order). It may seem cockeyed, and it relies on my design having |
| 247 | * an even number of steps in, but it works... |
| 248 | * |
| 249 | * Some of these structures, rather than being actual lines, are |
| 250 | * markers delimiting the end of the expansion of a given macro. |
| 251 | * This is for use in the cycle-tracking and %rep-handling code. |
| 252 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 253 | * others have `finishes' NULL, but `first' may still be NULL if |
| 254 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 255 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 256 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 257 | Line *next; |
| 258 | MMacro *finishes; |
| 259 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 263 | * To handle an arbitrary level of file inclusion, we maintain a |
| 264 | * stack (ie linked list) of these things. |
| 265 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 266 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 267 | Include *next; |
| 268 | FILE *fp; |
| 269 | Cond *conds; |
| 270 | Line *expansion; |
| 271 | char *fname; |
| 272 | int lineno, lineinc; |
| 273 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 277 | * Include search path. This is simply a list of strings which get |
| 278 | * prepended, in turn, to the name of an include file, in an |
| 279 | * attempt to find the file if it's not in the current directory. |
| 280 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 281 | struct IncPath { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 282 | IncPath *next; |
| 283 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 287 | * Conditional assembly: we maintain a separate stack of these for |
| 288 | * each level of file inclusion. (The only reason we keep the |
| 289 | * stacks separate is to ensure that a stray `%endif' in a file |
| 290 | * included from within the true branch of a `%if' won't terminate |
| 291 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 292 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 293 | struct Cond { |
| 294 | Cond *next; |
| 295 | int state; |
| 296 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 297 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 298 | /* |
| 299 | * These states are for use just after %if or %elif: IF_TRUE |
| 300 | * means the condition has evaluated to truth so we are |
| 301 | * currently emitting, whereas IF_FALSE means we are not |
| 302 | * currently emitting but will start doing so if a %else comes |
| 303 | * up. In these states, all directives are admissible: %elif, |
| 304 | * %else and %endif. (And of course %if.) |
| 305 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 306 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 307 | /* |
| 308 | * These states come up after a %else: ELSE_TRUE means we're |
| 309 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 310 | * any %elif or %else will cause an error. |
| 311 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 312 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 313 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 314 | * These states mean that we're not emitting now, and also that |
| 315 | * nothing until %endif will be emitted at all. COND_DONE is |
| 316 | * used when we've had our moment of emission |
| 317 | * and have now started seeing %elifs. COND_NEVER is used when |
| 318 | * the condition construct in question is contained within a |
| 319 | * non-emitting branch of a larger condition construct, |
| 320 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 321 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 322 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 323 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 324 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 325 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 326 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 327 | * These defines are used as the possible return values for do_directive |
| 328 | */ |
| 329 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 330 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 331 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 332 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 333 | * This define sets the upper limit for smacro and recursive mmacro |
| 334 | * expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 335 | */ |
| 336 | #define DEADMAN_LIMIT (1 << 20) |
| 337 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 338 | /* max reps */ |
| 339 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 340 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 341 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 342 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 343 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 344 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 345 | * ones, so we need a different enum... |
| 346 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 347 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 348 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 349 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 350 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 351 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 352 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 353 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 354 | 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] | 355 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 356 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 357 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 358 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 359 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 360 | 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] | 361 | 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] | 362 | }; |
| 363 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 364 | /* |
| 365 | * Directive names. |
| 366 | */ |
| 367 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 368 | static int is_condition(enum preproc_token arg) |
| 369 | { |
| 370 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 371 | } |
| 372 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 373 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 374 | * conditional compilation directives. Using the NASM pre-processor does |
| 375 | * not work, so we look for them specifically from the following list and |
| 376 | * then jam in the equivalent NASM directive into the input stream. |
| 377 | */ |
| 378 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 379 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 380 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 381 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 382 | }; |
| 383 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 384 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 385 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 386 | "ifndef", "include", "local" |
| 387 | }; |
| 388 | |
| 389 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 390 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 391 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 392 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 393 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 394 | static Context *cstk; |
| 395 | static Include *istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 396 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 397 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 398 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 399 | static StrList **dephead, **deptail; /* Dependency list */ |
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 uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 402 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 403 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 404 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 405 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 406 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 407 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 408 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 409 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | * The current set of single-line macros we have defined. |
| 413 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 414 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 415 | |
| 416 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 417 | * The multi-line macro we are currently defining, or the %rep |
| 418 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 419 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 420 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 421 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 422 | static uint64_t nested_mac_count; |
| 423 | static uint64_t nested_rep_count; |
| 424 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 425 | /* |
| 426 | * The number of macro parameters to allocate space for at a time. |
| 427 | */ |
| 428 | #define PARAM_DELTA 16 |
| 429 | |
| 430 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 431 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 432 | * 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] | 433 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 434 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 435 | |
| 436 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 437 | * The extra standard macros that come from the object format, if |
| 438 | * any. |
| 439 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 440 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 441 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 442 | |
| 443 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 444 | * Tokens are allocated in blocks to improve speed |
| 445 | */ |
| 446 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 447 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 448 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 449 | Blocks *next; |
| 450 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 451 | }; |
| 452 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 453 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 454 | |
| 455 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 456 | * Forward declarations. |
| 457 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 458 | static Token *expand_mmac_params(Token * tline); |
| 459 | static Token *expand_smacro(Token * tline); |
| 460 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 461 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 462 | static void make_tok_num(Token * tok, int64_t val); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 463 | static void pp_verror(int severity, const char *fmt, va_list ap); |
| 464 | static vefunc real_verror; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 465 | static void *new_Block(size_t size); |
| 466 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 467 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 468 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 469 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 473 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 474 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 475 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 476 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 477 | #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] | 478 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 479 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 480 | * nasm_unquote with error if the string contains NUL characters. |
| 481 | * If the string contains NUL characters, issue an error and return |
| 482 | * the C len, i.e. truncate at the NUL. |
| 483 | */ |
| 484 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 485 | { |
| 486 | size_t len = nasm_unquote(qstr, NULL); |
| 487 | size_t clen = strlen(qstr); |
| 488 | |
| 489 | if (len != clen) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 490 | nasm_error(ERR_NONFATAL, "NUL character in `%s' directive", |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 491 | pp_directives[directive]); |
| 492 | |
| 493 | return clen; |
| 494 | } |
| 495 | |
| 496 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 497 | * In-place reverse a list of tokens. |
| 498 | */ |
| 499 | static Token *reverse_tokens(Token *t) |
| 500 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 501 | Token *prev = NULL; |
| 502 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 503 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 504 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 505 | next = t->next; |
| 506 | t->next = prev; |
| 507 | prev = t; |
| 508 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 509 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 510 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 511 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 515 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 516 | * front of them. We do it here because I could not find any other |
| 517 | * place to do it for the moment, and it is a hack (ideally it would |
| 518 | * be nice to be able to use the NASM pre-processor to do it). |
| 519 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 520 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 521 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 522 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 523 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 524 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 525 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 526 | |
| 527 | /* Binary search for the directive name */ |
| 528 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 529 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 530 | q = nasm_skip_word(p); |
| 531 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 532 | if (len) { |
| 533 | oldchar = p[len]; |
| 534 | p[len] = 0; |
| 535 | while (j - i > 1) { |
| 536 | k = (j + i) / 2; |
| 537 | m = nasm_stricmp(p, tasm_directives[k]); |
| 538 | if (m == 0) { |
| 539 | /* We have found a directive, so jam a % in front of it |
| 540 | * so that NASM will then recognise it as one if it's own. |
| 541 | */ |
| 542 | p[len] = oldchar; |
| 543 | len = strlen(p); |
| 544 | oldline = line; |
| 545 | line = nasm_malloc(len + 2); |
| 546 | line[0] = '%'; |
| 547 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 548 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 549 | * NASM does not recognise IFDIFI, so we convert |
| 550 | * it to %if 0. This is not used in NASM |
| 551 | * compatible code, but does need to parse for the |
| 552 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 553 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 554 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 555 | } else { |
| 556 | memcpy(line + 1, p, len + 1); |
| 557 | } |
| 558 | nasm_free(oldline); |
| 559 | return line; |
| 560 | } else if (m < 0) { |
| 561 | j = k; |
| 562 | } else |
| 563 | i = k; |
| 564 | } |
| 565 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 566 | } |
| 567 | return line; |
| 568 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 569 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 570 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 571 | * The pre-preprocessing stage... This function translates line |
| 572 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 573 | * flags') into NASM preprocessor line number indications (`%line |
| 574 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 575 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 576 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 577 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 578 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 579 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 580 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 581 | if (line[0] == '#' && line[1] == ' ') { |
| 582 | oldline = line; |
| 583 | fname = oldline + 2; |
| 584 | lineno = atoi(fname); |
| 585 | fname += strspn(fname, "0123456789 "); |
| 586 | if (*fname == '"') |
| 587 | fname++; |
| 588 | fnlen = strcspn(fname, "\""); |
| 589 | line = nasm_malloc(20 + fnlen); |
| 590 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 591 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 592 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 593 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 594 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 595 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 599 | * Free a linked list of tokens. |
| 600 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 601 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 602 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 603 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 604 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Free a linked list of lines. |
| 609 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 610 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 611 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 612 | Line *l, *tmp; |
| 613 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 614 | free_tlist(l->first); |
| 615 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 620 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 621 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 622 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 623 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 624 | nasm_free(m->name); |
| 625 | free_tlist(m->dlist); |
| 626 | nasm_free(m->defaults); |
| 627 | free_llist(m->expansion); |
| 628 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 632 | * Free all currently defined macros, and free the hash tables |
| 633 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 634 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 635 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 636 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 637 | const char *key; |
| 638 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 639 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 640 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 641 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 642 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 643 | nasm_free(s->name); |
| 644 | free_tlist(s->expansion); |
| 645 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 646 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 647 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 648 | hash_free(smt); |
| 649 | } |
| 650 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 651 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 652 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 653 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 654 | const char *key; |
| 655 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 656 | |
| 657 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 658 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 659 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 660 | list_for_each_safe(m ,tmp, m) |
| 661 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 662 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 663 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static void free_macros(void) |
| 667 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 668 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 669 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Initialize the hash tables |
| 674 | */ |
| 675 | static void init_macros(void) |
| 676 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 677 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 678 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 682 | * Pop the context stack. |
| 683 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 684 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 685 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 686 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 687 | |
| 688 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 689 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 690 | nasm_free(c->name); |
| 691 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 692 | } |
| 693 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 694 | /* |
| 695 | * Search for a key in the hash index; adding it if necessary |
| 696 | * (in which case we initialize the data pointer to NULL.) |
| 697 | */ |
| 698 | static void ** |
| 699 | hash_findi_add(struct hash_table *hash, const char *str) |
| 700 | { |
| 701 | struct hash_insert hi; |
| 702 | void **r; |
| 703 | char *strx; |
| 704 | |
| 705 | r = hash_findi(hash, str, &hi); |
| 706 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 707 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 708 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 709 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 710 | return hash_add(&hi, strx, NULL); |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Like hash_findi, but returns the data element rather than a pointer |
| 715 | * to it. Used only when not adding a new element, hence no third |
| 716 | * argument. |
| 717 | */ |
| 718 | static void * |
| 719 | hash_findix(struct hash_table *hash, const char *str) |
| 720 | { |
| 721 | void **p; |
| 722 | |
| 723 | p = hash_findi(hash, str, NULL); |
| 724 | return p ? *p : NULL; |
| 725 | } |
| 726 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 727 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 728 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 729 | * if there no more left -- return NULL |
| 730 | */ |
| 731 | static char *line_from_stdmac(void) |
| 732 | { |
| 733 | unsigned char c; |
| 734 | const unsigned char *p = stdmacpos; |
| 735 | char *line, *q; |
| 736 | size_t len = 0; |
| 737 | |
| 738 | if (!stdmacpos) |
| 739 | return NULL; |
| 740 | |
| 741 | while ((c = *p++)) { |
| 742 | if (c >= 0x80) |
| 743 | len += pp_directives_len[c - 0x80] + 1; |
| 744 | else |
| 745 | len++; |
| 746 | } |
| 747 | |
| 748 | line = nasm_malloc(len + 1); |
| 749 | q = line; |
| 750 | while ((c = *stdmacpos++)) { |
| 751 | if (c >= 0x80) { |
| 752 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 753 | q += pp_directives_len[c - 0x80]; |
| 754 | *q++ = ' '; |
| 755 | } else { |
| 756 | *q++ = c; |
| 757 | } |
| 758 | } |
| 759 | stdmacpos = p; |
| 760 | *q = '\0'; |
| 761 | |
| 762 | if (!*stdmacpos) { |
| 763 | /* This was the last of the standard macro chain... */ |
| 764 | stdmacpos = NULL; |
| 765 | if (any_extrastdmac) { |
| 766 | stdmacpos = extrastdmac; |
| 767 | any_extrastdmac = false; |
| 768 | } else if (do_predef) { |
| 769 | Line *pd, *l; |
| 770 | Token *head, **tail, *t; |
| 771 | |
| 772 | /* |
| 773 | * Nasty hack: here we push the contents of |
| 774 | * `predef' on to the top-level expansion stack, |
| 775 | * since this is the most convenient way to |
| 776 | * implement the pre-include and pre-define |
| 777 | * features. |
| 778 | */ |
| 779 | list_for_each(pd, predef) { |
| 780 | head = NULL; |
| 781 | tail = &head; |
| 782 | list_for_each(t, pd->first) { |
| 783 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 784 | tail = &(*tail)->next; |
| 785 | } |
| 786 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 787 | l = nasm_malloc(sizeof(Line)); |
| 788 | l->next = istk->expansion; |
| 789 | l->first = head; |
| 790 | l->finishes = NULL; |
| 791 | |
| 792 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 793 | } |
| 794 | do_predef = false; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return line; |
| 799 | } |
| 800 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 801 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 802 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 803 | unsigned int size, c, next; |
| 804 | const unsigned int delta = 512; |
| 805 | const unsigned int pad = 8; |
| 806 | unsigned int nr_cont = 0; |
| 807 | bool cont = false; |
| 808 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 809 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 810 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 811 | p = line_from_stdmac(); |
| 812 | if (p) |
| 813 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 814 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 815 | size = delta; |
| 816 | p = buffer = nasm_malloc(size); |
| 817 | |
| 818 | for (;;) { |
| 819 | c = fgetc(istk->fp); |
| 820 | if ((int)(c) == EOF) { |
| 821 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 822 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 823 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 824 | |
| 825 | switch (c) { |
| 826 | case '\r': |
| 827 | next = fgetc(istk->fp); |
| 828 | if (next != '\n') |
| 829 | ungetc(next, istk->fp); |
| 830 | if (cont) { |
| 831 | cont = false; |
| 832 | continue; |
| 833 | } |
| 834 | break; |
| 835 | |
| 836 | case '\n': |
| 837 | if (cont) { |
| 838 | cont = false; |
| 839 | continue; |
| 840 | } |
| 841 | break; |
| 842 | |
| 843 | case '\\': |
| 844 | next = fgetc(istk->fp); |
| 845 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 846 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 847 | cont = true; |
| 848 | nr_cont++; |
| 849 | continue; |
| 850 | } |
| 851 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 852 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 853 | |
| 854 | if (c == '\r' || c == '\n') { |
| 855 | *p++ = 0; |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | if (p >= (buffer + size - pad)) { |
| 860 | buffer = nasm_realloc(buffer, size + delta); |
| 861 | p = buffer + size - pad; |
| 862 | size += delta; |
| 863 | } |
| 864 | |
| 865 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 868 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 869 | nasm_free(buffer); |
| 870 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 873 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 874 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 875 | |
| 876 | /* |
| 877 | * Handle spurious ^Z, which may be inserted into source files |
| 878 | * by some file transfer utilities. |
| 879 | */ |
| 880 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 881 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 882 | lfmt->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 883 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 884 | return buffer; |
| 885 | } |
| 886 | |
| 887 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 888 | * 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] | 889 | * don't need to parse the value out of e.g. numeric tokens: we |
| 890 | * simply split one string into many. |
| 891 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 892 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 893 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 894 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 895 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 896 | Token *list = NULL; |
| 897 | Token *t, **tail = &list; |
| 898 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 899 | while (*line) { |
| 900 | p = line; |
| 901 | if (*p == '%') { |
| 902 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 903 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 904 | p++; |
| 905 | type = TOK_PASTE; |
| 906 | } else if (nasm_isdigit(*p) || |
| 907 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 908 | do { |
| 909 | p++; |
| 910 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 911 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 912 | type = TOK_PREPROC_ID; |
| 913 | } else if (*p == '{') { |
| 914 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 915 | while (*p) { |
| 916 | if (*p == '}') |
| 917 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 918 | p[-1] = *p; |
| 919 | p++; |
| 920 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 921 | if (*p != '}') |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 922 | nasm_error(ERR_WARNING | ERR_PASS1, |
| 923 | "unterminated %%{ construct"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 924 | p[-1] = '\0'; |
| 925 | if (*p) |
| 926 | p++; |
| 927 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 928 | } else if (*p == '[') { |
| 929 | int lvl = 1; |
| 930 | line += 2; /* Skip the leading %[ */ |
| 931 | p++; |
| 932 | while (lvl && (c = *p++)) { |
| 933 | switch (c) { |
| 934 | case ']': |
| 935 | lvl--; |
| 936 | break; |
| 937 | case '%': |
| 938 | if (*p == '[') |
| 939 | lvl++; |
| 940 | break; |
| 941 | case '\'': |
| 942 | case '\"': |
| 943 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 944 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 945 | break; |
| 946 | default: |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | p--; |
| 951 | if (*p) |
| 952 | *p++ = '\0'; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 953 | if (lvl) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 954 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 955 | "unterminated %%[ construct"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 956 | type = TOK_INDIRECT; |
| 957 | } else if (*p == '?') { |
| 958 | type = TOK_PREPROC_Q; /* %? */ |
| 959 | p++; |
| 960 | if (*p == '?') { |
| 961 | type = TOK_PREPROC_QQ; /* %?? */ |
| 962 | p++; |
| 963 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 964 | } else if (*p == '!') { |
| 965 | type = TOK_PREPROC_ID; |
| 966 | p++; |
| 967 | if (isidchar(*p)) { |
| 968 | do { |
| 969 | p++; |
| 970 | } |
| 971 | while (isidchar(*p)); |
| 972 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 973 | p = nasm_skip_string(p); |
| 974 | if (*p) |
| 975 | p++; |
| 976 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 977 | nasm_error(ERR_NONFATAL|ERR_PASS1, |
| 978 | "unterminated %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 979 | } else { |
| 980 | /* %! without string or identifier */ |
| 981 | type = TOK_OTHER; /* Legacy behavior... */ |
| 982 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 983 | } else if (isidchar(*p) || |
| 984 | ((*p == '!' || *p == '%' || *p == '$') && |
| 985 | isidchar(p[1]))) { |
| 986 | do { |
| 987 | p++; |
| 988 | } |
| 989 | while (isidchar(*p)); |
| 990 | type = TOK_PREPROC_ID; |
| 991 | } else { |
| 992 | type = TOK_OTHER; |
| 993 | if (*p == '%') |
| 994 | p++; |
| 995 | } |
| 996 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 997 | type = TOK_ID; |
| 998 | p++; |
| 999 | while (*p && isidchar(*p)) |
| 1000 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1001 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1002 | /* |
| 1003 | * A string token. |
| 1004 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1005 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1006 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1007 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1008 | if (*p) { |
| 1009 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1010 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1011 | nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1012 | /* Handling unterminated strings by UNV */ |
| 1013 | /* type = -1; */ |
| 1014 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1015 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1016 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1017 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1018 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1019 | bool is_hex = false; |
| 1020 | bool is_float = false; |
| 1021 | bool has_e = false; |
| 1022 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1023 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1024 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1025 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1026 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1027 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1028 | if (*p == '$') { |
| 1029 | p++; |
| 1030 | is_hex = true; |
| 1031 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1032 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1033 | for (;;) { |
| 1034 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1035 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1036 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1037 | has_e = true; |
| 1038 | if (*p == '+' || *p == '-') { |
| 1039 | /* |
| 1040 | * e can only be followed by +/- if it is either a |
| 1041 | * prefixed hex number or a floating-point number |
| 1042 | */ |
| 1043 | p++; |
| 1044 | is_float = true; |
| 1045 | } |
| 1046 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1047 | is_hex = true; |
| 1048 | } else if (c == 'P' || c == 'p') { |
| 1049 | is_float = true; |
| 1050 | if (*p == '+' || *p == '-') |
| 1051 | p++; |
| 1052 | } else if (isnumchar(c) || c == '_') |
| 1053 | ; /* just advance */ |
| 1054 | else if (c == '.') { |
| 1055 | /* |
| 1056 | * we need to deal with consequences of the legacy |
| 1057 | * parser, like "1.nolist" being two tokens |
| 1058 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1059 | * a shot for now. In the future, we probably need |
| 1060 | * a flex-based scanner with proper pattern matching |
| 1061 | * to do it as well as it can be done. Nothing in |
| 1062 | * the world is going to help the person who wants |
| 1063 | * 0x123.p16 interpreted as two tokens, though. |
| 1064 | */ |
| 1065 | r = p; |
| 1066 | while (*r == '_') |
| 1067 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1068 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1069 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1070 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1071 | (*r == 'p' || *r == 'P')) { |
| 1072 | p = r; |
| 1073 | is_float = true; |
| 1074 | } else |
| 1075 | break; /* Terminate the token */ |
| 1076 | } else |
| 1077 | break; |
| 1078 | } |
| 1079 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1080 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1081 | if (p == line+1 && *line == '$') { |
| 1082 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1083 | } else { |
| 1084 | if (has_e && !is_hex) { |
| 1085 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1086 | is_float = true; |
| 1087 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1088 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1089 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1090 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1091 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1092 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1093 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1094 | /* |
| 1095 | * Whitespace just before end-of-line is discarded by |
| 1096 | * pretending it's a comment; whitespace just before a |
| 1097 | * comment gets lumped into the comment. |
| 1098 | */ |
| 1099 | if (!*p || *p == ';') { |
| 1100 | type = TOK_COMMENT; |
| 1101 | while (*p) |
| 1102 | p++; |
| 1103 | } |
| 1104 | } else if (*p == ';') { |
| 1105 | type = TOK_COMMENT; |
| 1106 | while (*p) |
| 1107 | p++; |
| 1108 | } else { |
| 1109 | /* |
| 1110 | * Anything else is an operator of some kind. We check |
| 1111 | * for all the double-character operators (>>, <<, //, |
| 1112 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1113 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1114 | */ |
| 1115 | type = TOK_OTHER; |
| 1116 | if ((p[0] == '>' && p[1] == '>') || |
| 1117 | (p[0] == '<' && p[1] == '<') || |
| 1118 | (p[0] == '/' && p[1] == '/') || |
| 1119 | (p[0] == '<' && p[1] == '=') || |
| 1120 | (p[0] == '>' && p[1] == '=') || |
| 1121 | (p[0] == '=' && p[1] == '=') || |
| 1122 | (p[0] == '!' && p[1] == '=') || |
| 1123 | (p[0] == '<' && p[1] == '>') || |
| 1124 | (p[0] == '&' && p[1] == '&') || |
| 1125 | (p[0] == '|' && p[1] == '|') || |
| 1126 | (p[0] == '^' && p[1] == '^')) { |
| 1127 | p++; |
| 1128 | } |
| 1129 | p++; |
| 1130 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1131 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1132 | /* Handling unterminated string by UNV */ |
| 1133 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1134 | { |
| 1135 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1136 | t->text[p-line] = *line; |
| 1137 | tail = &t->next; |
| 1138 | } |
| 1139 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1140 | if (type != TOK_COMMENT) { |
| 1141 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1142 | tail = &t->next; |
| 1143 | } |
| 1144 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1145 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1146 | return list; |
| 1147 | } |
| 1148 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1149 | /* |
| 1150 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1151 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1152 | * deleted only all at once by the delete_Blocks function. |
| 1153 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1154 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1155 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1156 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1157 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1158 | /* first, get to the end of the linked list */ |
| 1159 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1160 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1161 | /* now allocate the requested chunk */ |
| 1162 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1163 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1164 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame] | 1165 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1166 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * this function deletes all managed blocks of memory |
| 1171 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1172 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1173 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1174 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1175 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1176 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1177 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1178 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1179 | * free it. |
| 1180 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1181 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1182 | if (b->chunk) |
| 1183 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1184 | a = b; |
| 1185 | b = b->next; |
| 1186 | if (a != &blocks) |
| 1187 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1188 | } |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 1189 | memset(&blocks, 0, sizeof(blocks)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1190 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1191 | |
| 1192 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1193 | * 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] | 1194 | * 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] | 1195 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1196 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1197 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1198 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1199 | { |
| 1200 | Token *t; |
| 1201 | int i; |
| 1202 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1203 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1204 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1205 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1206 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1207 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1208 | } |
| 1209 | t = freeTokens; |
| 1210 | freeTokens = t->next; |
| 1211 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1212 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1213 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1214 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1215 | t->text = NULL; |
| 1216 | } else { |
| 1217 | if (txtlen == 0) |
| 1218 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1219 | t->text = nasm_malloc(txtlen+1); |
| 1220 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1221 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1222 | } |
| 1223 | return t; |
| 1224 | } |
| 1225 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1226 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1227 | { |
| 1228 | Token *next = t->next; |
| 1229 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1230 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1231 | freeTokens = t; |
| 1232 | return next; |
| 1233 | } |
| 1234 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1235 | /* |
| 1236 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1237 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1238 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1239 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1240 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1241 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1242 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1243 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1244 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1245 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1246 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1247 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1248 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1249 | char *v; |
| 1250 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1251 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1252 | v = t->text + 2; |
| 1253 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1254 | size_t len = nasm_unquote(v, NULL); |
| 1255 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1256 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1257 | if (len != clen) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1258 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
| 1259 | "NUL character in %%! string"); |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1260 | v = NULL; |
| 1261 | } |
| 1262 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1263 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1264 | if (v) { |
| 1265 | char *p = getenv(v); |
| 1266 | if (!p) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1267 | nasm_error(ERR_NONFATAL | ERR_PASS1, |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1268 | "nonexistent environment variable `%s'", v); |
| 1269 | p = ""; |
| 1270 | } |
| 1271 | t->text = nasm_strdup(p); |
| 1272 | } |
| 1273 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1274 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1275 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1276 | /* Expand local macros here and not during preprocessing */ |
| 1277 | if (expand_locals && |
| 1278 | t->type == TOK_PREPROC_ID && t->text && |
| 1279 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1280 | const char *q; |
| 1281 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1282 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1283 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1284 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1285 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1286 | p = nasm_strcat(buffer, q); |
| 1287 | nasm_free(t->text); |
| 1288 | t->text = p; |
| 1289 | } |
| 1290 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1291 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1292 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1293 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1294 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1295 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1296 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1297 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1298 | |
| 1299 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1300 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1301 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1302 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1303 | q = t->text; |
| 1304 | while (*q) |
| 1305 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1306 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1307 | } |
| 1308 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1309 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1310 | return line; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1314 | * A scanner, suitable for use by the expression evaluator, which |
| 1315 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1316 | * the first token in the line to be passed in as its private_data |
| 1317 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1318 | * |
| 1319 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1320 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1321 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1322 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1323 | Token **tlineptr = private_data; |
| 1324 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1325 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1326 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1327 | do { |
| 1328 | tline = *tlineptr; |
| 1329 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1330 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1331 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1334 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1335 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1336 | tokval->t_charptr = tline->text; |
| 1337 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1338 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1339 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1340 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1341 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1342 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1343 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1344 | p = tokval->t_charptr = tline->text; |
| 1345 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1346 | tokval->t_charptr++; |
| 1347 | return tokval->t_type = TOKEN_ID; |
| 1348 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1349 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1350 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1351 | if (r >= p+MAX_KEYWORD) |
| 1352 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1353 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1354 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1355 | *s = '\0'; |
| 1356 | /* right, so we have an identifier sitting in temp storage. now, |
| 1357 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1358 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1361 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1362 | bool rn_error; |
| 1363 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1364 | tokval->t_charptr = tline->text; |
| 1365 | if (rn_error) |
| 1366 | return tokval->t_type = TOKEN_ERRNUM; |
| 1367 | else |
| 1368 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1369 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1370 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1371 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1372 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1375 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1376 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1377 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1378 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1379 | tokval->t_charptr = tline->text; |
| 1380 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1381 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1382 | if (ep[0] != bq || ep[1] != '\0') |
| 1383 | return tokval->t_type = TOKEN_ERRSTR; |
| 1384 | else |
| 1385 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1388 | if (tline->type == TOK_OTHER) { |
| 1389 | if (!strcmp(tline->text, "<<")) |
| 1390 | return tokval->t_type = TOKEN_SHL; |
| 1391 | if (!strcmp(tline->text, ">>")) |
| 1392 | return tokval->t_type = TOKEN_SHR; |
| 1393 | if (!strcmp(tline->text, "//")) |
| 1394 | return tokval->t_type = TOKEN_SDIV; |
| 1395 | if (!strcmp(tline->text, "%%")) |
| 1396 | return tokval->t_type = TOKEN_SMOD; |
| 1397 | if (!strcmp(tline->text, "==")) |
| 1398 | return tokval->t_type = TOKEN_EQ; |
| 1399 | if (!strcmp(tline->text, "<>")) |
| 1400 | return tokval->t_type = TOKEN_NE; |
| 1401 | if (!strcmp(tline->text, "!=")) |
| 1402 | return tokval->t_type = TOKEN_NE; |
| 1403 | if (!strcmp(tline->text, "<=")) |
| 1404 | return tokval->t_type = TOKEN_LE; |
| 1405 | if (!strcmp(tline->text, ">=")) |
| 1406 | return tokval->t_type = TOKEN_GE; |
| 1407 | if (!strcmp(tline->text, "&&")) |
| 1408 | return tokval->t_type = TOKEN_DBL_AND; |
| 1409 | if (!strcmp(tline->text, "^^")) |
| 1410 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1411 | if (!strcmp(tline->text, "||")) |
| 1412 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * We have no other options: just return the first character of |
| 1417 | * the token text. |
| 1418 | */ |
| 1419 | return tokval->t_type = tline->text[0]; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1423 | * Compare a string to the name of an existing macro; this is a |
| 1424 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1425 | * depending on the value of the `casesense' parameter. |
| 1426 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1427 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1428 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1429 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1433 | * Compare a string to the name of an existing macro; this is a |
| 1434 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1435 | * depending on the value of the `casesense' parameter. |
| 1436 | */ |
| 1437 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1438 | { |
| 1439 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1440 | } |
| 1441 | |
| 1442 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1443 | * Return the Context structure associated with a %$ token. Return |
| 1444 | * NULL, having _already_ reported an error condition, if the |
| 1445 | * context stack isn't deep enough for the supplied number of $ |
| 1446 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1447 | * |
| 1448 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1449 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1450 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1451 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1452 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1453 | Context *ctx; |
| 1454 | int i; |
| 1455 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1456 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1457 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1458 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1459 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1460 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1461 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1462 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1463 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1464 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1467 | name += 2; |
| 1468 | ctx = cstk; |
| 1469 | i = 0; |
| 1470 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1471 | name++; |
| 1472 | i++; |
| 1473 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1474 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1475 | if (!ctx) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1476 | nasm_error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1477 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1478 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1479 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1480 | |
| 1481 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1482 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1483 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1484 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1488 | * Check to see if a file is already in a string list |
| 1489 | */ |
| 1490 | static bool in_list(const StrList *list, const char *str) |
| 1491 | { |
| 1492 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1493 | if (!strcmp(list->str, str)) |
| 1494 | return true; |
| 1495 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1496 | } |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1501 | * Open an include file. This routine must always return a valid |
| 1502 | * file pointer if it returns - it's responsible for throwing an |
| 1503 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1504 | * the include path one by one until it finds the file or reaches |
| 1505 | * the end of the path. |
| 1506 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1507 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1508 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1509 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1510 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1511 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1512 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1513 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1514 | size_t prefix_len = 0; |
| 1515 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1516 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1517 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1518 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1519 | memcpy(sl->str, prefix, prefix_len); |
| 1520 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1521 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1522 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1523 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1524 | **dtail = sl; |
| 1525 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1526 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1527 | nasm_free(sl); |
| 1528 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1529 | if (fp) |
| 1530 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1531 | if (!ip) { |
| 1532 | if (!missing_ok) |
| 1533 | break; |
| 1534 | prefix = NULL; |
| 1535 | } else { |
| 1536 | prefix = ip->path; |
| 1537 | ip = ip->next; |
| 1538 | } |
| 1539 | if (prefix) { |
| 1540 | prefix_len = strlen(prefix); |
| 1541 | } else { |
| 1542 | /* -MG given and file not found */ |
| 1543 | if (dhead && !in_list(*dhead, file)) { |
| 1544 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1545 | sl->next = NULL; |
| 1546 | strcpy(sl->str, file); |
| 1547 | **dtail = sl; |
| 1548 | *dtail = &sl->next; |
| 1549 | } |
| 1550 | return NULL; |
| 1551 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1552 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1553 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1554 | nasm_error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1555 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1559 | * 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] | 1560 | * 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] | 1561 | * return true if _any_ single-line macro of that name is defined. |
| 1562 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1563 | * `nparam' or no parameters is defined. |
| 1564 | * |
| 1565 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1566 | * defined, or nparam is -1, the address of the definition structure |
| 1567 | * 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] | 1568 | * is NULL, no action will be taken regarding its contents, and no |
| 1569 | * error will occur. |
| 1570 | * |
| 1571 | * Note that this is also called with nparam zero to resolve |
| 1572 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1573 | * |
| 1574 | * If you already know which context macro belongs to, you can pass |
| 1575 | * the context pointer as first parameter; if you won't but name begins |
| 1576 | * with %$ the context will be automatically computed. If all_contexts |
| 1577 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1578 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1579 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1580 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1581 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1582 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1583 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1584 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1585 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1586 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1587 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1588 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1589 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1590 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1591 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1592 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1593 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1594 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1595 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1596 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1597 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1598 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1599 | while (m) { |
| 1600 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1601 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1602 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1603 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1604 | *defn = m; |
| 1605 | else |
| 1606 | *defn = NULL; |
| 1607 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1608 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1609 | } |
| 1610 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1611 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1612 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1613 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | * Count and mark off the parameters in a multi-line macro call. |
| 1618 | * This is called both from within the multi-line macro expansion |
| 1619 | * code, and also to mark off the default parameters when provided |
| 1620 | * in a %macro definition line. |
| 1621 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1622 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1623 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1624 | int paramsize, brace; |
| 1625 | |
| 1626 | *nparam = paramsize = 0; |
| 1627 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1628 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1629 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1630 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1631 | paramsize += PARAM_DELTA; |
| 1632 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1633 | } |
| 1634 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1635 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1636 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1637 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1638 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1639 | if (brace) { |
| 1640 | while (brace && (t = t->next) != NULL) { |
| 1641 | if (tok_is_(t, "{")) |
| 1642 | brace++; |
| 1643 | else if (tok_is_(t, "}")) |
| 1644 | brace--; |
| 1645 | } |
| 1646 | |
| 1647 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1648 | /* |
| 1649 | * Now we've found the closing brace, look further |
| 1650 | * for the comma. |
| 1651 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1652 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1653 | skip_white_(t); |
| 1654 | if (tok_isnt_(t, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1655 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1656 | "braces do not enclose all of macro parameter"); |
| 1657 | while (tok_isnt_(t, ",")) |
| 1658 | t = t->next; |
| 1659 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1660 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1661 | } else { |
| 1662 | while (tok_isnt_(t, ",")) |
| 1663 | t = t->next; |
| 1664 | } |
| 1665 | if (t) { /* got a comma/brace */ |
| 1666 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1667 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1672 | * Determine whether one of the various `if' conditions is true or |
| 1673 | * not. |
| 1674 | * |
| 1675 | * We must free the tline we get passed. |
| 1676 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1677 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1678 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1679 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1680 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1681 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1682 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1683 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1684 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1685 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1686 | |
| 1687 | origline = tline; |
| 1688 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1689 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1690 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1691 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1692 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1693 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1694 | if (!tline) |
| 1695 | break; |
| 1696 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1697 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1698 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1699 | free_tlist(origline); |
| 1700 | return -1; |
| 1701 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1702 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1703 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1704 | tline = tline->next; |
| 1705 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1706 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1707 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1708 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1709 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1710 | while (tline) { |
| 1711 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1712 | if (!tline || (tline->type != TOK_ID && |
| 1713 | (tline->type != TOK_PREPROC_ID || |
| 1714 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1715 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1716 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1717 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1718 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1719 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1720 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1721 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1722 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1723 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1724 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1725 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1726 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1727 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1728 | while (tline) { |
| 1729 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1730 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1731 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1732 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1733 | tline->text[1] != '!'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1734 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1735 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1736 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1737 | goto fail; |
| 1738 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1739 | p = tline->text; |
| 1740 | if (tline->type == TOK_PREPROC_ID) |
| 1741 | p += 2; /* Skip leading %! */ |
| 1742 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1743 | nasm_unquote_cstr(p, ct); |
| 1744 | if (getenv(p)) |
| 1745 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1746 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1747 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1748 | break; |
| 1749 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1750 | case PPC_IFIDN: |
| 1751 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1752 | tline = expand_smacro(tline); |
| 1753 | t = tt = tline; |
| 1754 | while (tok_isnt_(tt, ",")) |
| 1755 | tt = tt->next; |
| 1756 | if (!tt) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1757 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1758 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1759 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1760 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1761 | } |
| 1762 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1763 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1764 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1765 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1766 | nasm_error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1767 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1768 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1769 | } |
| 1770 | if (t->type == TOK_WHITESPACE) { |
| 1771 | t = t->next; |
| 1772 | continue; |
| 1773 | } |
| 1774 | if (tt->type == TOK_WHITESPACE) { |
| 1775 | tt = tt->next; |
| 1776 | continue; |
| 1777 | } |
| 1778 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1779 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1780 | break; |
| 1781 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1782 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1783 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1784 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1785 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1786 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1787 | if (l1 != l2) { |
| 1788 | j = false; |
| 1789 | break; |
| 1790 | } |
| 1791 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1792 | j = false; |
| 1793 | break; |
| 1794 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1795 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1796 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1797 | break; |
| 1798 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1799 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1800 | t = t->next; |
| 1801 | tt = tt->next; |
| 1802 | } |
| 1803 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1804 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1805 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1806 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1807 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1808 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1809 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1810 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1811 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1812 | skip_white_(tline); |
| 1813 | tline = expand_id(tline); |
| 1814 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1815 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1816 | "`%s' expects a macro name", pp_directives[ct]); |
| 1817 | goto fail; |
| 1818 | } |
| 1819 | searching.name = nasm_strdup(tline->text); |
| 1820 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1821 | searching.plus = false; |
| 1822 | searching.nolist = false; |
| 1823 | searching.in_progress = 0; |
| 1824 | searching.max_depth = 0; |
| 1825 | searching.rep_nest = NULL; |
| 1826 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1827 | searching.nparam_max = INT_MAX; |
| 1828 | tline = expand_smacro(tline->next); |
| 1829 | skip_white_(tline); |
| 1830 | if (!tline) { |
| 1831 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1832 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1833 | "`%s' expects a parameter count or nothing", |
| 1834 | pp_directives[ct]); |
| 1835 | } else { |
| 1836 | searching.nparam_min = searching.nparam_max = |
| 1837 | readnum(tline->text, &j); |
| 1838 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1839 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1840 | "unable to parse parameter count `%s'", |
| 1841 | tline->text); |
| 1842 | } |
| 1843 | if (tline && tok_is_(tline->next, "-")) { |
| 1844 | tline = tline->next->next; |
| 1845 | if (tok_is_(tline, "*")) |
| 1846 | searching.nparam_max = INT_MAX; |
| 1847 | else if (!tok_type_(tline, TOK_NUMBER)) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1848 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1849 | "`%s' expects a parameter count after `-'", |
| 1850 | pp_directives[ct]); |
| 1851 | else { |
| 1852 | searching.nparam_max = readnum(tline->text, &j); |
| 1853 | if (j) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1854 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1855 | "unable to parse parameter count `%s'", |
| 1856 | tline->text); |
| 1857 | if (searching.nparam_min > searching.nparam_max) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1858 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1859 | "minimum parameter count exceeds maximum"); |
| 1860 | } |
| 1861 | } |
| 1862 | if (tline && tok_is_(tline->next, "+")) { |
| 1863 | tline = tline->next; |
| 1864 | searching.plus = true; |
| 1865 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1866 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1867 | while (mmac) { |
| 1868 | if (!strcmp(mmac->name, searching.name) && |
| 1869 | (mmac->nparam_min <= searching.nparam_max |
| 1870 | || searching.plus) |
| 1871 | && (searching.nparam_min <= mmac->nparam_max |
| 1872 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1873 | found = true; |
| 1874 | break; |
| 1875 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1876 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1877 | } |
| 1878 | if (tline && tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1879 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1880 | "trailing garbage after %%ifmacro ignored"); |
| 1881 | nasm_free(searching.name); |
| 1882 | j = found; |
| 1883 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1884 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1885 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1886 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1887 | needtype = TOK_ID; |
| 1888 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1889 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1890 | needtype = TOK_NUMBER; |
| 1891 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1892 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1893 | needtype = TOK_STRING; |
| 1894 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1895 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1896 | iftype: |
| 1897 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1898 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1899 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1900 | (needtype == TOK_NUMBER && |
| 1901 | tok_type_(t, TOK_OTHER) && |
| 1902 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1903 | !t->text[1])) |
| 1904 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1905 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1906 | j = tok_type_(t, needtype); |
| 1907 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1908 | |
| 1909 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1910 | t = tline = expand_smacro(tline); |
| 1911 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1912 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1913 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1914 | j = false; |
| 1915 | if (t) { |
| 1916 | t = t->next; /* Skip the actual token */ |
| 1917 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1918 | t = t->next; |
| 1919 | j = !t; /* Should be nothing left */ |
| 1920 | } |
| 1921 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1922 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1923 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1924 | t = tline = expand_smacro(tline); |
| 1925 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1926 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1927 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1928 | j = !t; /* Should be empty */ |
| 1929 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1930 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1931 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1932 | t = tline = expand_smacro(tline); |
| 1933 | tptr = &t; |
| 1934 | tokval.t_type = TOKEN_INVALID; |
| 1935 | evalresult = evaluate(ppscan, tptr, &tokval, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1936 | NULL, pass | CRITICAL, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1937 | if (!evalresult) |
| 1938 | return -1; |
| 1939 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1940 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1941 | "trailing garbage after expression ignored"); |
| 1942 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1943 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1944 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1945 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1946 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1947 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1948 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 1949 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1950 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1951 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1952 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1953 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1954 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1955 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1956 | |
| 1957 | free_tlist(origline); |
| 1958 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1959 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1960 | fail: |
| 1961 | free_tlist(origline); |
| 1962 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1966 | * Common code for defining an smacro |
| 1967 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1968 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1969 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1970 | { |
| 1971 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1972 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1973 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1974 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1975 | if (!smac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1976 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1977 | "single-line macro `%s' defined both with and" |
| 1978 | " without parameters", mname); |
| 1979 | /* |
| 1980 | * Some instances of the old code considered this a failure, |
| 1981 | * some others didn't. What is the right thing to do here? |
| 1982 | */ |
| 1983 | free_tlist(expansion); |
| 1984 | return false; /* Failure */ |
| 1985 | } else { |
| 1986 | /* |
| 1987 | * We're redefining, so we have to take over an |
| 1988 | * existing SMacro structure. This means freeing |
| 1989 | * what was already in it. |
| 1990 | */ |
| 1991 | nasm_free(smac->name); |
| 1992 | free_tlist(smac->expansion); |
| 1993 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1994 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1995 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 1996 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1997 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1998 | smac->next = *smhead; |
| 1999 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2000 | } |
| 2001 | smac->name = nasm_strdup(mname); |
| 2002 | smac->casesense = casesense; |
| 2003 | smac->nparam = nparam; |
| 2004 | smac->expansion = expansion; |
| 2005 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2006 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * Undefine an smacro |
| 2011 | */ |
| 2012 | static void undef_smacro(Context *ctx, const char *mname) |
| 2013 | { |
| 2014 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2015 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2016 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2017 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2018 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2019 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2020 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2021 | /* |
| 2022 | * We now have a macro name... go hunt for it. |
| 2023 | */ |
| 2024 | sp = smhead; |
| 2025 | while ((s = *sp) != NULL) { |
| 2026 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2027 | *sp = s->next; |
| 2028 | nasm_free(s->name); |
| 2029 | free_tlist(s->expansion); |
| 2030 | nasm_free(s); |
| 2031 | } else { |
| 2032 | sp = &s->next; |
| 2033 | } |
| 2034 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2038 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2039 | * Parse a mmacro specification. |
| 2040 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2041 | 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] | 2042 | { |
| 2043 | bool err; |
| 2044 | |
| 2045 | tline = tline->next; |
| 2046 | skip_white_(tline); |
| 2047 | tline = expand_id(tline); |
| 2048 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2049 | nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2050 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2051 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2052 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2053 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2054 | def->name = nasm_strdup(tline->text); |
| 2055 | def->plus = false; |
| 2056 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2057 | def->in_progress = 0; |
| 2058 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2059 | def->nparam_min = 0; |
| 2060 | def->nparam_max = 0; |
| 2061 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2062 | tline = expand_smacro(tline->next); |
| 2063 | skip_white_(tline); |
| 2064 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2065 | nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2066 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2067 | def->nparam_min = def->nparam_max = |
| 2068 | readnum(tline->text, &err); |
| 2069 | if (err) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2070 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2071 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2072 | } |
| 2073 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2074 | tline = tline->next->next; |
| 2075 | if (tok_is_(tline, "*")) { |
| 2076 | def->nparam_max = INT_MAX; |
| 2077 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2078 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2079 | "`%s' expects a parameter count after `-'", directive); |
| 2080 | } else { |
| 2081 | def->nparam_max = readnum(tline->text, &err); |
| 2082 | if (err) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2083 | nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2084 | tline->text); |
| 2085 | } |
| 2086 | if (def->nparam_min > def->nparam_max) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2087 | nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2088 | } |
| 2089 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2090 | } |
| 2091 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2092 | tline = tline->next; |
| 2093 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2094 | } |
| 2095 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2096 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2097 | tline = tline->next; |
| 2098 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2099 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2100 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2101 | /* |
| 2102 | * Handle default parameters. |
| 2103 | */ |
| 2104 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2105 | def->dlist = tline->next; |
| 2106 | tline->next = NULL; |
| 2107 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2108 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2109 | def->dlist = NULL; |
| 2110 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2111 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2112 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2113 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2114 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2115 | !def->plus) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2116 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2117 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2118 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2119 | return true; |
| 2120 | } |
| 2121 | |
| 2122 | |
| 2123 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2124 | * Decode a size directive |
| 2125 | */ |
| 2126 | static int parse_size(const char *str) { |
| 2127 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2128 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2129 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2130 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2131 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2132 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2135 | /** |
| 2136 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2137 | * Find out if a line contains a preprocessor directive, and deal |
| 2138 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2139 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2140 | * If a directive _is_ found, it is the responsibility of this routine |
| 2141 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2142 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2143 | * @param tline a pointer to the current tokeninzed line linked list |
| 2144 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2145 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2146 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2147 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2148 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2149 | enum preproc_token i; |
| 2150 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2151 | bool err; |
| 2152 | int nparam; |
| 2153 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2154 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2155 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2156 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2157 | char *p, *pp; |
| 2158 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2159 | Include *inc; |
| 2160 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2161 | Cond *cond; |
| 2162 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2163 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2164 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2165 | struct tokenval tokval; |
| 2166 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2167 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2168 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2169 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2170 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2171 | |
| 2172 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2173 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2174 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2175 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2176 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2177 | || tline->text[1] == '!')) |
| 2178 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2179 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2180 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2181 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2182 | /* |
| 2183 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2184 | * since they are known to be buggy at moment, we need to fix them |
| 2185 | * in future release (2.09-2.10) |
| 2186 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2187 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2188 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2189 | tline->text); |
| 2190 | return NO_DIRECTIVE_FOUND; |
| 2191 | } |
| 2192 | |
| 2193 | /* |
| 2194 | * If we're in a non-emitting branch of a condition construct, |
| 2195 | * or walking to the end of an already terminated %rep block, |
| 2196 | * we should ignore all directives except for condition |
| 2197 | * directives. |
| 2198 | */ |
| 2199 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2200 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2201 | return NO_DIRECTIVE_FOUND; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | * If we're defining a macro or reading a %rep block, we should |
| 2206 | * ignore all directives except for %macro/%imacro (which nest), |
| 2207 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2208 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2209 | */ |
| 2210 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2211 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2212 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2213 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2214 | return NO_DIRECTIVE_FOUND; |
| 2215 | } |
| 2216 | |
| 2217 | if (defining) { |
| 2218 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2219 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2220 | nested_mac_count++; |
| 2221 | return NO_DIRECTIVE_FOUND; |
| 2222 | } else if (nested_mac_count > 0) { |
| 2223 | if (i == PP_ENDMACRO) { |
| 2224 | nested_mac_count--; |
| 2225 | return NO_DIRECTIVE_FOUND; |
| 2226 | } |
| 2227 | } |
| 2228 | if (!defining->name) { |
| 2229 | if (i == PP_REP) { |
| 2230 | nested_rep_count++; |
| 2231 | return NO_DIRECTIVE_FOUND; |
| 2232 | } else if (nested_rep_count > 0) { |
| 2233 | if (i == PP_ENDREP) { |
| 2234 | nested_rep_count--; |
| 2235 | return NO_DIRECTIVE_FOUND; |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | } |
| 2240 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2241 | switch (i) { |
| 2242 | case PP_INVALID: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2243 | nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2244 | tline->text); |
| 2245 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2246 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2247 | case PP_STACKSIZE: |
| 2248 | /* Directive to tell NASM what the default stack size is. The |
| 2249 | * default is for a 16-bit stack, and this can be overriden with |
| 2250 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2251 | */ |
| 2252 | tline = tline->next; |
| 2253 | if (tline && tline->type == TOK_WHITESPACE) |
| 2254 | tline = tline->next; |
| 2255 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2256 | nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2257 | free_tlist(origline); |
| 2258 | return DIRECTIVE_FOUND; |
| 2259 | } |
| 2260 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2261 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2262 | StackSize = 4; |
| 2263 | StackPointer = "ebp"; |
| 2264 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2265 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2266 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2267 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2268 | StackSize = 8; |
| 2269 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2270 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2271 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2272 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2273 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2274 | * far function call. |
| 2275 | */ |
| 2276 | StackSize = 2; |
| 2277 | StackPointer = "bp"; |
| 2278 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2279 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2280 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2281 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2282 | * far function call. We don't support near functions. |
| 2283 | */ |
| 2284 | StackSize = 2; |
| 2285 | StackPointer = "bp"; |
| 2286 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2287 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2288 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2289 | nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2290 | free_tlist(origline); |
| 2291 | return DIRECTIVE_FOUND; |
| 2292 | } |
| 2293 | free_tlist(origline); |
| 2294 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2295 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2296 | case PP_ARG: |
| 2297 | /* TASM like ARG directive to define arguments to functions, in |
| 2298 | * the following form: |
| 2299 | * |
| 2300 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2301 | */ |
| 2302 | offset = ArgOffset; |
| 2303 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2304 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2305 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2306 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2307 | /* Find the argument name */ |
| 2308 | tline = tline->next; |
| 2309 | if (tline && tline->type == TOK_WHITESPACE) |
| 2310 | tline = tline->next; |
| 2311 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2312 | nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2313 | free_tlist(origline); |
| 2314 | return DIRECTIVE_FOUND; |
| 2315 | } |
| 2316 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2317 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2318 | /* Find the argument size type */ |
| 2319 | tline = tline->next; |
| 2320 | if (!tline || tline->type != TOK_OTHER |
| 2321 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2322 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2323 | "Syntax error processing `%%arg' directive"); |
| 2324 | free_tlist(origline); |
| 2325 | return DIRECTIVE_FOUND; |
| 2326 | } |
| 2327 | tline = tline->next; |
| 2328 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2329 | nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2330 | free_tlist(origline); |
| 2331 | return DIRECTIVE_FOUND; |
| 2332 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2333 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2334 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2335 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2336 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2337 | size = parse_size(tt->text); |
| 2338 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2339 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2340 | "Invalid size type for `%%arg' missing directive"); |
| 2341 | free_tlist(tt); |
| 2342 | free_tlist(origline); |
| 2343 | return DIRECTIVE_FOUND; |
| 2344 | } |
| 2345 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2346 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2347 | /* Round up to even stack slots */ |
| 2348 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2349 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2350 | /* Now define the macro for the argument */ |
| 2351 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2352 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2353 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2354 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2355 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2356 | /* Move to the next argument in the list */ |
| 2357 | tline = tline->next; |
| 2358 | if (tline && tline->type == TOK_WHITESPACE) |
| 2359 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2360 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2361 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2362 | free_tlist(origline); |
| 2363 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2364 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2365 | case PP_LOCAL: |
| 2366 | /* TASM like LOCAL directive to define local variables for a |
| 2367 | * function, in the following form: |
| 2368 | * |
| 2369 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2370 | * |
| 2371 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2372 | * required by TASM to define the local parameter size (and used |
| 2373 | * by the TASM macro package). |
| 2374 | */ |
| 2375 | offset = LocalOffset; |
| 2376 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2377 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2378 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2379 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2380 | /* Find the argument name */ |
| 2381 | tline = tline->next; |
| 2382 | if (tline && tline->type == TOK_WHITESPACE) |
| 2383 | tline = tline->next; |
| 2384 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2385 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2386 | "`%%local' missing argument parameter"); |
| 2387 | free_tlist(origline); |
| 2388 | return DIRECTIVE_FOUND; |
| 2389 | } |
| 2390 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2391 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2392 | /* Find the argument size type */ |
| 2393 | tline = tline->next; |
| 2394 | if (!tline || tline->type != TOK_OTHER |
| 2395 | || tline->text[0] != ':') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2396 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2397 | "Syntax error processing `%%local' directive"); |
| 2398 | free_tlist(origline); |
| 2399 | return DIRECTIVE_FOUND; |
| 2400 | } |
| 2401 | tline = tline->next; |
| 2402 | if (!tline || tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2403 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2404 | "`%%local' missing size type parameter"); |
| 2405 | free_tlist(origline); |
| 2406 | return DIRECTIVE_FOUND; |
| 2407 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2408 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2409 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2410 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2411 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2412 | size = parse_size(tt->text); |
| 2413 | if (!size) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2414 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2415 | "Invalid size type for `%%local' missing directive"); |
| 2416 | free_tlist(tt); |
| 2417 | free_tlist(origline); |
| 2418 | return DIRECTIVE_FOUND; |
| 2419 | } |
| 2420 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2421 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2422 | /* Round up to even stack slots */ |
| 2423 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2424 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2425 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2426 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2427 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2428 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2429 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2430 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2431 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2432 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2433 | snprintf(directive, sizeof(directive), |
| 2434 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2435 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2436 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2437 | /* Move to the next argument in the list */ |
| 2438 | tline = tline->next; |
| 2439 | if (tline && tline->type == TOK_WHITESPACE) |
| 2440 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2441 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2442 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2443 | free_tlist(origline); |
| 2444 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2445 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2446 | case PP_CLEAR: |
| 2447 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2448 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2449 | "trailing garbage after `%%clear' ignored"); |
| 2450 | free_macros(); |
| 2451 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2452 | free_tlist(origline); |
| 2453 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2454 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2455 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2456 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2457 | skip_white_(t); |
| 2458 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2459 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2460 | nasm_error(ERR_NONFATAL, "`%%depend' expects a file name"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2461 | free_tlist(origline); |
| 2462 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2463 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2464 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2465 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2466 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2467 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2468 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2469 | nasm_unquote_cstr(p, i); |
| 2470 | if (dephead && !in_list(*dephead, p)) { |
| 2471 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2472 | sl->next = NULL; |
| 2473 | strcpy(sl->str, p); |
| 2474 | *deptail = sl; |
| 2475 | deptail = &sl->next; |
| 2476 | } |
| 2477 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2478 | return DIRECTIVE_FOUND; |
| 2479 | |
| 2480 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2481 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2482 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2483 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2484 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2485 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2486 | nasm_error(ERR_NONFATAL, "`%%include' expects a file name"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2487 | free_tlist(origline); |
| 2488 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2489 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2490 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2491 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2492 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2493 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2494 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2495 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2496 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2497 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2498 | inc->conds = NULL; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2499 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2500 | if (!inc->fp) { |
| 2501 | /* -MG given but file not found */ |
| 2502 | nasm_free(inc); |
| 2503 | } else { |
| 2504 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2505 | inc->lineno = src_set_linnum(0); |
| 2506 | inc->lineinc = 1; |
| 2507 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2508 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2509 | istk = inc; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 2510 | lfmt->uplevel(LIST_INCLUDE); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2511 | } |
| 2512 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2513 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2514 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2515 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2516 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2517 | static macros_t *use_pkg; |
| 2518 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2519 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2520 | tline = tline->next; |
| 2521 | skip_white_(tline); |
| 2522 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2523 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2524 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2525 | tline->type != TOK_INTERNAL_STRING && |
| 2526 | tline->type != TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2527 | nasm_error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2528 | free_tlist(origline); |
| 2529 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2530 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2531 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2532 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2533 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2534 | if (tline->type == TOK_STRING) |
| 2535 | nasm_unquote_cstr(tline->text, i); |
| 2536 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2537 | if (!use_pkg) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2538 | nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2539 | else |
| 2540 | 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] | 2541 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2542 | /* Not already included, go ahead and include it */ |
| 2543 | stdmacpos = use_pkg; |
| 2544 | } |
| 2545 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2546 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2547 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2548 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2549 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2550 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2551 | tline = tline->next; |
| 2552 | skip_white_(tline); |
| 2553 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2554 | if (tline) { |
| 2555 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2556 | nasm_error(ERR_NONFATAL, "`%s' expects a context identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2557 | pp_directives[i]); |
| 2558 | free_tlist(origline); |
| 2559 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2560 | } |
| 2561 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2562 | nasm_error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2563 | "trailing garbage after `%s' ignored", |
| 2564 | pp_directives[i]); |
| 2565 | p = nasm_strdup(tline->text); |
| 2566 | } else { |
| 2567 | p = NULL; /* Anonymous */ |
| 2568 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2569 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2570 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2571 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2572 | ctx->next = cstk; |
| 2573 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2574 | ctx->name = p; |
| 2575 | ctx->number = unique++; |
| 2576 | cstk = ctx; |
| 2577 | } else { |
| 2578 | /* %pop or %repl */ |
| 2579 | if (!cstk) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2580 | nasm_error(ERR_NONFATAL, "`%s': context stack is empty", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2581 | pp_directives[i]); |
| 2582 | } else if (i == PP_POP) { |
| 2583 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2584 | nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2585 | "expected %s", |
| 2586 | cstk->name ? cstk->name : "anonymous", p); |
| 2587 | else |
| 2588 | ctx_pop(); |
| 2589 | } else { |
| 2590 | /* i == PP_REPL */ |
| 2591 | nasm_free(cstk->name); |
| 2592 | cstk->name = p; |
| 2593 | p = NULL; |
| 2594 | } |
| 2595 | nasm_free(p); |
| 2596 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2597 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2598 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2599 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2600 | severity = ERR_FATAL; |
| 2601 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2602 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2603 | severity = ERR_NONFATAL; |
| 2604 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2605 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2606 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2607 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2608 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2609 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2610 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2611 | /* Only error out if this is the final pass */ |
| 2612 | if (pass != 2 && i != PP_FATAL) |
| 2613 | return DIRECTIVE_FOUND; |
| 2614 | |
| 2615 | tline->next = expand_smacro(tline->next); |
| 2616 | tline = tline->next; |
| 2617 | skip_white_(tline); |
| 2618 | t = tline ? tline->next : NULL; |
| 2619 | skip_white_(t); |
| 2620 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2621 | /* The line contains only a quoted string */ |
| 2622 | p = tline->text; |
| 2623 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2624 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2625 | } else { |
| 2626 | /* Not a quoted string, or more than a quoted string */ |
| 2627 | p = detoken(tline, false); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2628 | nasm_error(severity, "%s", p); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2629 | nasm_free(p); |
| 2630 | } |
| 2631 | free_tlist(origline); |
| 2632 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2633 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2634 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2635 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2636 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2637 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2638 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2639 | j = if_condition(tline->next, i); |
| 2640 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2641 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2642 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2643 | cond = nasm_malloc(sizeof(Cond)); |
| 2644 | cond->next = istk->conds; |
| 2645 | cond->state = j; |
| 2646 | istk->conds = cond; |
| 2647 | if(istk->mstk) |
| 2648 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2649 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2650 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2651 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2652 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2653 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2654 | nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2655 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2656 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2657 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2658 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2659 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2660 | case COND_DONE: |
| 2661 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2662 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2663 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2664 | case COND_ELSE_TRUE: |
| 2665 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2666 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2667 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2668 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2669 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2670 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2671 | case COND_IF_FALSE: |
| 2672 | /* |
| 2673 | * IMPORTANT: In the case of %if, we will already have |
| 2674 | * called expand_mmac_params(); however, if we're |
| 2675 | * processing an %elif we must have been in a |
| 2676 | * non-emitting mode, which would have inhibited |
| 2677 | * the normal invocation of expand_mmac_params(). |
| 2678 | * Therefore, we have to do it explicitly here. |
| 2679 | */ |
| 2680 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2681 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2682 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2683 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2684 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2685 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2686 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2687 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2688 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2689 | case PP_ELSE: |
| 2690 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2691 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2692 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2693 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2694 | nasm_fatal(0, "`%%else: no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2695 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2696 | case COND_IF_TRUE: |
| 2697 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2698 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2699 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2700 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2701 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2702 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2703 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2704 | case COND_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2705 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2706 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2707 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2708 | case COND_ELSE_TRUE: |
| 2709 | case COND_ELSE_FALSE: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2710 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2711 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2712 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2713 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2714 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2715 | free_tlist(origline); |
| 2716 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2717 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2718 | case PP_ENDIF: |
| 2719 | if (tline->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2720 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND, |
| 2721 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2722 | if (!istk->conds) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2723 | nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2724 | cond = istk->conds; |
| 2725 | istk->conds = cond->next; |
| 2726 | nasm_free(cond); |
| 2727 | if(istk->mstk) |
| 2728 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2729 | free_tlist(origline); |
| 2730 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2731 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2732 | case PP_RMACRO: |
| 2733 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2734 | case PP_MACRO: |
| 2735 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2736 | if (defining) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2737 | nasm_error(ERR_FATAL, "`%s': already defining a macro", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2738 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2739 | return DIRECTIVE_FOUND; |
| 2740 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2741 | defining = nasm_malloc(sizeof(MMacro)); |
| 2742 | defining->max_depth = |
| 2743 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
| 2744 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2745 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2746 | nasm_free(defining); |
| 2747 | defining = NULL; |
| 2748 | return DIRECTIVE_FOUND; |
| 2749 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2750 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2751 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2752 | while (mmac) { |
| 2753 | if (!strcmp(mmac->name, defining->name) && |
| 2754 | (mmac->nparam_min <= defining->nparam_max |
| 2755 | || defining->plus) |
| 2756 | && (defining->nparam_min <= mmac->nparam_max |
| 2757 | || mmac->plus)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2758 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2759 | "redefining multi-line macro `%s'", defining->name); |
| 2760 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2761 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2762 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2763 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2764 | free_tlist(origline); |
| 2765 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2766 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2767 | case PP_ENDM: |
| 2768 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2769 | if (! (defining && defining->name)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2770 | nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2771 | return DIRECTIVE_FOUND; |
| 2772 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2773 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2774 | defining->next = *mmhead; |
| 2775 | *mmhead = defining; |
| 2776 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2777 | free_tlist(origline); |
| 2778 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2779 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2780 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2781 | /* |
| 2782 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2783 | * macro-end marker for a macro with a name. Then we |
| 2784 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2785 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2786 | list_for_each(l, istk->expansion) |
| 2787 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2788 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2789 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2790 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2791 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2792 | * Remove all conditional entries relative to this |
| 2793 | * macro invocation. (safe to do in this context) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2794 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2795 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2796 | cond = istk->conds; |
| 2797 | istk->conds = cond->next; |
| 2798 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2799 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2800 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2801 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2802 | nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2803 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2804 | free_tlist(origline); |
| 2805 | return DIRECTIVE_FOUND; |
| 2806 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2807 | case PP_UNMACRO: |
| 2808 | case PP_UNIMACRO: |
| 2809 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2810 | MMacro **mmac_p; |
| 2811 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2812 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2813 | spec.casesense = (i == PP_UNMACRO); |
| 2814 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2815 | return DIRECTIVE_FOUND; |
| 2816 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2817 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2818 | while (mmac_p && *mmac_p) { |
| 2819 | mmac = *mmac_p; |
| 2820 | if (mmac->casesense == spec.casesense && |
| 2821 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2822 | mmac->nparam_min == spec.nparam_min && |
| 2823 | mmac->nparam_max == spec.nparam_max && |
| 2824 | mmac->plus == spec.plus) { |
| 2825 | *mmac_p = mmac->next; |
| 2826 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2827 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2828 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | free_tlist(origline); |
| 2832 | free_tlist(spec.dlist); |
| 2833 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2836 | case PP_ROTATE: |
| 2837 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2838 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2839 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2840 | free_tlist(origline); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2841 | nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2842 | return DIRECTIVE_FOUND; |
| 2843 | } |
| 2844 | t = expand_smacro(tline->next); |
| 2845 | tline->next = NULL; |
| 2846 | free_tlist(origline); |
| 2847 | tline = t; |
| 2848 | tptr = &t; |
| 2849 | tokval.t_type = TOKEN_INVALID; |
| 2850 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2851 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2852 | free_tlist(tline); |
| 2853 | if (!evalresult) |
| 2854 | return DIRECTIVE_FOUND; |
| 2855 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2856 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2857 | "trailing garbage after expression ignored"); |
| 2858 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2859 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2860 | return DIRECTIVE_FOUND; |
| 2861 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2862 | mmac = istk->mstk; |
| 2863 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2864 | mmac = mmac->next_active; |
| 2865 | if (!mmac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2866 | nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2867 | } else if (mmac->nparam == 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2868 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2869 | "`%%rotate' invoked within macro without parameters"); |
| 2870 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2871 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2872 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2873 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2874 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2875 | rotate += mmac->nparam; |
| 2876 | |
| 2877 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2878 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2879 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2880 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2881 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2882 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2883 | do { |
| 2884 | tline = tline->next; |
| 2885 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2886 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2887 | if (tok_type_(tline, TOK_ID) && |
| 2888 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2889 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2890 | do { |
| 2891 | tline = tline->next; |
| 2892 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2893 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2894 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2895 | if (tline) { |
| 2896 | t = expand_smacro(tline); |
| 2897 | tptr = &t; |
| 2898 | tokval.t_type = TOKEN_INVALID; |
| 2899 | evalresult = |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2900 | evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2901 | if (!evalresult) { |
| 2902 | free_tlist(origline); |
| 2903 | return DIRECTIVE_FOUND; |
| 2904 | } |
| 2905 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2906 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2907 | "trailing garbage after expression ignored"); |
| 2908 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2909 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2910 | return DIRECTIVE_FOUND; |
| 2911 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2912 | count = reloc_value(evalresult); |
| 2913 | if (count >= REP_LIMIT) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2914 | nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2915 | count = 0; |
| 2916 | } else |
| 2917 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2918 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2919 | nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2920 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2921 | } |
| 2922 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2923 | |
| 2924 | tmp_defining = defining; |
| 2925 | defining = nasm_malloc(sizeof(MMacro)); |
| 2926 | defining->prev = NULL; |
| 2927 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2928 | defining->casesense = false; |
| 2929 | defining->plus = false; |
| 2930 | defining->nolist = nolist; |
| 2931 | defining->in_progress = count; |
| 2932 | defining->max_depth = 0; |
| 2933 | defining->nparam_min = defining->nparam_max = 0; |
| 2934 | defining->defaults = NULL; |
| 2935 | defining->dlist = NULL; |
| 2936 | defining->expansion = NULL; |
| 2937 | defining->next_active = istk->mstk; |
| 2938 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2939 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2940 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2941 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2942 | if (!defining || defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2943 | nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2944 | return DIRECTIVE_FOUND; |
| 2945 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2946 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2947 | /* |
| 2948 | * Now we have a "macro" defined - although it has no name |
| 2949 | * and we won't be entering it in the hash tables - we must |
| 2950 | * push a macro-end marker for it on to istk->expansion. |
| 2951 | * After that, it will take care of propagating itself (a |
| 2952 | * macro-end marker line for a macro which is really a %rep |
| 2953 | * block will cause the macro to be re-expanded, complete |
| 2954 | * with another macro-end marker to ensure the process |
| 2955 | * continues) until the whole expansion is forcibly removed |
| 2956 | * from istk->expansion by a %exitrep. |
| 2957 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2958 | l = nasm_malloc(sizeof(Line)); |
| 2959 | l->next = istk->expansion; |
| 2960 | l->finishes = defining; |
| 2961 | l->first = NULL; |
| 2962 | istk->expansion = l; |
| 2963 | |
| 2964 | istk->mstk = defining; |
| 2965 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 2966 | lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2967 | tmp_defining = defining; |
| 2968 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2969 | free_tlist(origline); |
| 2970 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2971 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2972 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2973 | /* |
| 2974 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2975 | * macro-end marker for a macro with no name. Then we set |
| 2976 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2977 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2978 | list_for_each(l, istk->expansion) |
| 2979 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2980 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2981 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2982 | if (l) |
| 2983 | l->finishes->in_progress = 1; |
| 2984 | else |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 2985 | nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2986 | free_tlist(origline); |
| 2987 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2988 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2989 | case PP_XDEFINE: |
| 2990 | case PP_IXDEFINE: |
| 2991 | case PP_DEFINE: |
| 2992 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2993 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2994 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2995 | tline = tline->next; |
| 2996 | skip_white_(tline); |
| 2997 | tline = expand_id(tline); |
| 2998 | if (!tline || (tline->type != TOK_ID && |
| 2999 | (tline->type != TOK_PREPROC_ID || |
| 3000 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3001 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3002 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3003 | free_tlist(origline); |
| 3004 | return DIRECTIVE_FOUND; |
| 3005 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3006 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3007 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3008 | last = tline; |
| 3009 | param_start = tline = tline->next; |
| 3010 | nparam = 0; |
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 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3013 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3014 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3015 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3016 | if (tok_is_(tline, "(")) { |
| 3017 | /* |
| 3018 | * This macro has parameters. |
| 3019 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3020 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3021 | tline = tline->next; |
| 3022 | while (1) { |
| 3023 | skip_white_(tline); |
| 3024 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3025 | nasm_error(ERR_NONFATAL, "parameter identifier expected"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3026 | free_tlist(origline); |
| 3027 | return DIRECTIVE_FOUND; |
| 3028 | } |
| 3029 | if (tline->type != TOK_ID) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3030 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3031 | "`%s': parameter identifier expected", |
| 3032 | tline->text); |
| 3033 | free_tlist(origline); |
| 3034 | return DIRECTIVE_FOUND; |
| 3035 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3036 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3037 | tline = tline->next; |
| 3038 | skip_white_(tline); |
| 3039 | if (tok_is_(tline, ",")) { |
| 3040 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3041 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3042 | if (!tok_is_(tline, ")")) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3043 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3044 | "`)' expected to terminate macro template"); |
| 3045 | free_tlist(origline); |
| 3046 | return DIRECTIVE_FOUND; |
| 3047 | } |
| 3048 | break; |
| 3049 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3050 | } |
| 3051 | last = tline; |
| 3052 | tline = tline->next; |
| 3053 | } |
| 3054 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3055 | last = tline, tline = tline->next; |
| 3056 | macro_start = NULL; |
| 3057 | last->next = NULL; |
| 3058 | t = tline; |
| 3059 | while (t) { |
| 3060 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3061 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3062 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3063 | !strcmp(tt->text, t->text)) |
| 3064 | t->type = tt->type; |
| 3065 | } |
| 3066 | tt = t->next; |
| 3067 | t->next = macro_start; |
| 3068 | macro_start = t; |
| 3069 | t = tt; |
| 3070 | } |
| 3071 | /* |
| 3072 | * Good. We now have a macro name, a parameter count, and a |
| 3073 | * token list (in reverse order) for an expansion. We ought |
| 3074 | * to be OK just to create an SMacro, store it, and let |
| 3075 | * free_tlist have the rest of the line (which we have |
| 3076 | * carefully re-terminated after chopping off the expansion |
| 3077 | * from the end). |
| 3078 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3079 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3080 | free_tlist(origline); |
| 3081 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3082 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3083 | case PP_UNDEF: |
| 3084 | tline = tline->next; |
| 3085 | skip_white_(tline); |
| 3086 | tline = expand_id(tline); |
| 3087 | if (!tline || (tline->type != TOK_ID && |
| 3088 | (tline->type != TOK_PREPROC_ID || |
| 3089 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3090 | nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3091 | free_tlist(origline); |
| 3092 | return DIRECTIVE_FOUND; |
| 3093 | } |
| 3094 | if (tline->next) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3095 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3096 | "trailing garbage after macro name ignored"); |
| 3097 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3098 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3099 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3100 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3101 | undef_smacro(ctx, mname); |
| 3102 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3103 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3104 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3105 | case PP_DEFSTR: |
| 3106 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3107 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3108 | |
| 3109 | tline = tline->next; |
| 3110 | skip_white_(tline); |
| 3111 | tline = expand_id(tline); |
| 3112 | if (!tline || (tline->type != TOK_ID && |
| 3113 | (tline->type != TOK_PREPROC_ID || |
| 3114 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3115 | nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3116 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3117 | free_tlist(origline); |
| 3118 | return DIRECTIVE_FOUND; |
| 3119 | } |
| 3120 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3121 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3122 | last = tline; |
| 3123 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3124 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3125 | |
| 3126 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3127 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3128 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3129 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3130 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3131 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3132 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3133 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3134 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3135 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3136 | |
| 3137 | /* |
| 3138 | * We now have a macro name, an implicit parameter count of |
| 3139 | * zero, and a string token to use as an expansion. Create |
| 3140 | * and store an SMacro. |
| 3141 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3142 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3143 | free_tlist(origline); |
| 3144 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3145 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3146 | case PP_DEFTOK: |
| 3147 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3148 | casesense = (i == PP_DEFTOK); |
| 3149 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3150 | tline = tline->next; |
| 3151 | skip_white_(tline); |
| 3152 | tline = expand_id(tline); |
| 3153 | if (!tline || (tline->type != TOK_ID && |
| 3154 | (tline->type != TOK_PREPROC_ID || |
| 3155 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3156 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3157 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3158 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3159 | free_tlist(origline); |
| 3160 | return DIRECTIVE_FOUND; |
| 3161 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3162 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3163 | last = tline; |
| 3164 | tline = expand_smacro(tline->next); |
| 3165 | last->next = NULL; |
| 3166 | |
| 3167 | t = tline; |
| 3168 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3169 | t = t->next; |
| 3170 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3171 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3172 | nasm_error(ERR_NONFATAL, |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3173 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3174 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3175 | free_tlist(tline); |
| 3176 | free_tlist(origline); |
| 3177 | return DIRECTIVE_FOUND; |
| 3178 | } |
| 3179 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3180 | /* |
| 3181 | * Convert the string to a token stream. Note that smacros |
| 3182 | * are stored with the token stream reversed, so we have to |
| 3183 | * reverse the output of tokenize(). |
| 3184 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3185 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3186 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3187 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3188 | /* |
| 3189 | * We now have a macro name, an implicit parameter count of |
| 3190 | * zero, and a numeric token to use as an expansion. Create |
| 3191 | * and store an SMacro. |
| 3192 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3193 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3194 | free_tlist(tline); |
| 3195 | free_tlist(origline); |
| 3196 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3197 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3198 | case PP_PATHSEARCH: |
| 3199 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3200 | FILE *fp; |
| 3201 | StrList *xsl = NULL; |
| 3202 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3203 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3204 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3205 | |
| 3206 | tline = tline->next; |
| 3207 | skip_white_(tline); |
| 3208 | tline = expand_id(tline); |
| 3209 | if (!tline || (tline->type != TOK_ID && |
| 3210 | (tline->type != TOK_PREPROC_ID || |
| 3211 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3212 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3213 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3214 | free_tlist(origline); |
| 3215 | return DIRECTIVE_FOUND; |
| 3216 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3217 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3218 | last = tline; |
| 3219 | tline = expand_smacro(tline->next); |
| 3220 | last->next = NULL; |
| 3221 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3222 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3223 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3224 | t = t->next; |
| 3225 | |
| 3226 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3227 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3228 | nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3229 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3230 | free_tlist(origline); |
| 3231 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3232 | } |
| 3233 | if (t->next) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3234 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3235 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3236 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3237 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3238 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3239 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3240 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3241 | if (fp) { |
| 3242 | p = xsl->str; |
| 3243 | fclose(fp); /* Don't actually care about the file */ |
| 3244 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3245 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3246 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3247 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3248 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3249 | macro_start->a.mac = NULL; |
| 3250 | if (xsl) |
| 3251 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3252 | |
| 3253 | /* |
| 3254 | * We now have a macro name, an implicit parameter count of |
| 3255 | * zero, and a string token to use as an expansion. Create |
| 3256 | * and store an SMacro. |
| 3257 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3258 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3259 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3260 | free_tlist(origline); |
| 3261 | return DIRECTIVE_FOUND; |
| 3262 | } |
| 3263 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3264 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3265 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3266 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3267 | tline = tline->next; |
| 3268 | skip_white_(tline); |
| 3269 | tline = expand_id(tline); |
| 3270 | if (!tline || (tline->type != TOK_ID && |
| 3271 | (tline->type != TOK_PREPROC_ID || |
| 3272 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3273 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3274 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3275 | free_tlist(origline); |
| 3276 | return DIRECTIVE_FOUND; |
| 3277 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3278 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3279 | last = tline; |
| 3280 | tline = expand_smacro(tline->next); |
| 3281 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3282 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3283 | t = tline; |
| 3284 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3285 | t = t->next; |
| 3286 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3287 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3288 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3289 | "`%%strlen` requires string as second parameter"); |
| 3290 | free_tlist(tline); |
| 3291 | free_tlist(origline); |
| 3292 | return DIRECTIVE_FOUND; |
| 3293 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3294 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3295 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3296 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3297 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3298 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3299 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3300 | /* |
| 3301 | * We now have a macro name, an implicit parameter count of |
| 3302 | * zero, and a numeric token to use as an expansion. Create |
| 3303 | * and store an SMacro. |
| 3304 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3305 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3306 | free_tlist(tline); |
| 3307 | free_tlist(origline); |
| 3308 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3309 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3310 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3311 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3312 | |
| 3313 | tline = tline->next; |
| 3314 | skip_white_(tline); |
| 3315 | tline = expand_id(tline); |
| 3316 | if (!tline || (tline->type != TOK_ID && |
| 3317 | (tline->type != TOK_PREPROC_ID || |
| 3318 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3319 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3320 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3321 | free_tlist(origline); |
| 3322 | return DIRECTIVE_FOUND; |
| 3323 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3324 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3325 | last = tline; |
| 3326 | tline = expand_smacro(tline->next); |
| 3327 | last->next = NULL; |
| 3328 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3329 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3330 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3331 | switch (t->type) { |
| 3332 | case TOK_WHITESPACE: |
| 3333 | break; |
| 3334 | case TOK_STRING: |
| 3335 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3336 | break; |
| 3337 | case TOK_OTHER: |
| 3338 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3339 | break; |
| 3340 | /* else fall through */ |
| 3341 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3342 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3343 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3344 | free_tlist(tline); |
| 3345 | free_tlist(origline); |
| 3346 | return DIRECTIVE_FOUND; |
| 3347 | } |
| 3348 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3349 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3350 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3351 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3352 | if (t->type == TOK_STRING) { |
| 3353 | memcpy(p, t->text, t->a.len); |
| 3354 | p += t->a.len; |
| 3355 | } |
| 3356 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3357 | |
| 3358 | /* |
| 3359 | * We now have a macro name, an implicit parameter count of |
| 3360 | * zero, and a numeric token to use as an expansion. Create |
| 3361 | * and store an SMacro. |
| 3362 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3363 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3364 | macro_start->text = nasm_quote(pp, len); |
| 3365 | nasm_free(pp); |
| 3366 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3367 | free_tlist(tline); |
| 3368 | free_tlist(origline); |
| 3369 | return DIRECTIVE_FOUND; |
| 3370 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3371 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3372 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3373 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3374 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3375 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3376 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3377 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3378 | tline = tline->next; |
| 3379 | skip_white_(tline); |
| 3380 | tline = expand_id(tline); |
| 3381 | if (!tline || (tline->type != TOK_ID && |
| 3382 | (tline->type != TOK_PREPROC_ID || |
| 3383 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3384 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3385 | "`%%substr' expects a macro identifier as first parameter"); |
| 3386 | free_tlist(origline); |
| 3387 | return DIRECTIVE_FOUND; |
| 3388 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3389 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3390 | last = tline; |
| 3391 | tline = expand_smacro(tline->next); |
| 3392 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3393 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3394 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3395 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3396 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3397 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3398 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3399 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3400 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3401 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3402 | "`%%substr` requires string as second parameter"); |
| 3403 | free_tlist(tline); |
| 3404 | free_tlist(origline); |
| 3405 | return DIRECTIVE_FOUND; |
| 3406 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3407 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3408 | tt = t->next; |
| 3409 | tptr = &tt; |
| 3410 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3411 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3412 | if (!evalresult) { |
| 3413 | free_tlist(tline); |
| 3414 | free_tlist(origline); |
| 3415 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3416 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3417 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3418 | free_tlist(tline); |
| 3419 | free_tlist(origline); |
| 3420 | return DIRECTIVE_FOUND; |
| 3421 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3422 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3423 | |
| 3424 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3425 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3426 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3427 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3428 | } else { |
| 3429 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3430 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3431 | if (!evalresult) { |
| 3432 | free_tlist(tline); |
| 3433 | free_tlist(origline); |
| 3434 | return DIRECTIVE_FOUND; |
| 3435 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3436 | nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3437 | free_tlist(tline); |
| 3438 | free_tlist(origline); |
| 3439 | return DIRECTIVE_FOUND; |
| 3440 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3441 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3442 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3443 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3444 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3445 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3446 | /* make start and count being in range */ |
| 3447 | if (start < 0) |
| 3448 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3449 | if (count < 0) |
| 3450 | count = len + count + 1 - start; |
| 3451 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3452 | count = len - start; |
| 3453 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3454 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3455 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3456 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3457 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3458 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3459 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3460 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3461 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3462 | /* |
| 3463 | * We now have a macro name, an implicit parameter count of |
| 3464 | * zero, and a numeric token to use as an expansion. Create |
| 3465 | * and store an SMacro. |
| 3466 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3467 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3468 | free_tlist(tline); |
| 3469 | free_tlist(origline); |
| 3470 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3471 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3472 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3473 | case PP_ASSIGN: |
| 3474 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3475 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3476 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3477 | tline = tline->next; |
| 3478 | skip_white_(tline); |
| 3479 | tline = expand_id(tline); |
| 3480 | if (!tline || (tline->type != TOK_ID && |
| 3481 | (tline->type != TOK_PREPROC_ID || |
| 3482 | tline->text[1] != '$'))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3483 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3484 | "`%%%sassign' expects a macro identifier", |
| 3485 | (i == PP_IASSIGN ? "i" : "")); |
| 3486 | free_tlist(origline); |
| 3487 | return DIRECTIVE_FOUND; |
| 3488 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3489 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3490 | last = tline; |
| 3491 | tline = expand_smacro(tline->next); |
| 3492 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3493 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3494 | t = tline; |
| 3495 | tptr = &t; |
| 3496 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3497 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3498 | free_tlist(tline); |
| 3499 | if (!evalresult) { |
| 3500 | free_tlist(origline); |
| 3501 | return DIRECTIVE_FOUND; |
| 3502 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3503 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3504 | if (tokval.t_type) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3505 | nasm_error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3506 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3507 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3508 | if (!is_simple(evalresult)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3509 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3510 | "non-constant value given to `%%%sassign'", |
| 3511 | (i == PP_IASSIGN ? "i" : "")); |
| 3512 | free_tlist(origline); |
| 3513 | return DIRECTIVE_FOUND; |
| 3514 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3515 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3516 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3517 | macro_start->next = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3518 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3519 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3520 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3521 | /* |
| 3522 | * We now have a macro name, an implicit parameter count of |
| 3523 | * zero, and a numeric token to use as an expansion. Create |
| 3524 | * and store an SMacro. |
| 3525 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3526 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3527 | free_tlist(origline); |
| 3528 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3529 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3530 | case PP_LINE: |
| 3531 | /* |
| 3532 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3533 | */ |
| 3534 | tline = tline->next; |
| 3535 | skip_white_(tline); |
| 3536 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3537 | nasm_error(ERR_NONFATAL, "`%%line' expects line number"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3538 | free_tlist(origline); |
| 3539 | return DIRECTIVE_FOUND; |
| 3540 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3541 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3542 | m = 1; |
| 3543 | tline = tline->next; |
| 3544 | if (tok_is_(tline, "+")) { |
| 3545 | tline = tline->next; |
| 3546 | if (!tok_type_(tline, TOK_NUMBER)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3547 | nasm_error(ERR_NONFATAL, "`%%line' expects line increment"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3548 | free_tlist(origline); |
| 3549 | return DIRECTIVE_FOUND; |
| 3550 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3551 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3552 | tline = tline->next; |
| 3553 | } |
| 3554 | skip_white_(tline); |
| 3555 | src_set_linnum(k); |
| 3556 | istk->lineinc = m; |
| 3557 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3558 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3559 | } |
| 3560 | free_tlist(origline); |
| 3561 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3562 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3563 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3564 | nasm_error(ERR_FATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3565 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3566 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3567 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3568 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3572 | * Ensure that a macro parameter contains a condition code and |
| 3573 | * nothing else. Return the condition code index if so, or -1 |
| 3574 | * otherwise. |
| 3575 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3576 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3577 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3578 | Token *tt; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3579 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3580 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3581 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3582 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3583 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3584 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3585 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3586 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3587 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3588 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3589 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3590 | |
Cyrill Gorcunov | 1945639 | 2012-05-02 00:18:56 +0400 | [diff] [blame] | 3591 | return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3594 | /* |
| 3595 | * This routines walks over tokens strem and hadnles tokens |
| 3596 | * pasting, if @handle_explicit passed then explicit pasting |
| 3597 | * term is handled, otherwise -- implicit pastings only. |
| 3598 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3599 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3600 | size_t mnum, bool handle_explicit) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3601 | { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3602 | Token *tok, *next, **prev_next, **prev_nonspace; |
| 3603 | bool pasted = false; |
| 3604 | char *buf, *p; |
| 3605 | size_t len, i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3606 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3607 | /* |
| 3608 | * The last token before pasting. We need it |
| 3609 | * to be able to connect new handled tokens. |
| 3610 | * In other words if there were a tokens stream |
| 3611 | * |
| 3612 | * A -> B -> C -> D |
| 3613 | * |
| 3614 | * and we've joined tokens B and C, the resulting |
| 3615 | * stream should be |
| 3616 | * |
| 3617 | * A -> BC -> D |
| 3618 | */ |
| 3619 | tok = *head; |
| 3620 | prev_next = NULL; |
| 3621 | |
| 3622 | if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE)) |
| 3623 | prev_nonspace = head; |
| 3624 | else |
| 3625 | prev_nonspace = NULL; |
| 3626 | |
| 3627 | while (tok && (next = tok->next)) { |
| 3628 | |
| 3629 | switch (tok->type) { |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3630 | case TOK_WHITESPACE: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3631 | /* Zap redundant whitespaces */ |
| 3632 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3633 | next = delete_Token(next); |
| 3634 | tok->next = next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3635 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3636 | |
| 3637 | case TOK_PASTE: |
| 3638 | /* Explicit pasting */ |
| 3639 | if (!handle_explicit) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3640 | break; |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3641 | next = delete_Token(tok); |
| 3642 | |
| 3643 | while (tok_type_(next, TOK_WHITESPACE)) |
| 3644 | next = delete_Token(next); |
| 3645 | |
| 3646 | if (!pasted) |
| 3647 | pasted = true; |
| 3648 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3649 | /* Left pasting token is start of line */ |
| 3650 | if (!prev_nonspace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3651 | nasm_error(ERR_FATAL, "No lvalue found on pasting"); |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3652 | |
Cyrill Gorcunov | 8b5c9fb | 2013-02-04 01:24:54 +0400 | [diff] [blame] | 3653 | /* |
| 3654 | * No ending token, this might happen in two |
| 3655 | * cases |
| 3656 | * |
| 3657 | * 1) There indeed no right token at all |
| 3658 | * 2) There is a bare "%define ID" statement, |
| 3659 | * and @ID does expand to whitespace. |
| 3660 | * |
| 3661 | * So technically we need to do a grammar analysis |
| 3662 | * in another stage of parsing, but for now lets don't |
| 3663 | * change the behaviour people used to. Simply allow |
| 3664 | * whitespace after paste token. |
| 3665 | */ |
| 3666 | if (!next) { |
| 3667 | /* |
| 3668 | * Zap ending space tokens and that's all. |
| 3669 | */ |
| 3670 | tok = (*prev_nonspace)->next; |
| 3671 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3672 | tok = delete_Token(tok); |
| 3673 | tok = *prev_nonspace; |
| 3674 | tok->next = NULL; |
| 3675 | break; |
| 3676 | } |
| 3677 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3678 | tok = *prev_nonspace; |
| 3679 | while (tok_type_(tok, TOK_WHITESPACE)) |
| 3680 | tok = delete_Token(tok); |
| 3681 | len = strlen(tok->text); |
| 3682 | len += strlen(next->text); |
| 3683 | |
| 3684 | p = buf = nasm_malloc(len + 1); |
| 3685 | strcpy(p, tok->text); |
| 3686 | p = strchr(p, '\0'); |
| 3687 | strcpy(p, next->text); |
| 3688 | |
| 3689 | delete_Token(tok); |
| 3690 | |
| 3691 | tok = tokenize(buf); |
| 3692 | nasm_free(buf); |
| 3693 | |
| 3694 | *prev_nonspace = tok; |
| 3695 | while (tok && tok->next) |
| 3696 | tok = tok->next; |
| 3697 | |
| 3698 | tok->next = delete_Token(next); |
| 3699 | |
| 3700 | /* Restart from pasted tokens head */ |
| 3701 | tok = *prev_nonspace; |
| 3702 | break; |
| 3703 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3704 | default: |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3705 | /* implicit pasting */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3706 | for (i = 0; i < mnum; i++) { |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3707 | if (!(PP_CONCAT_MATCH(tok, m[i].mask_head))) |
| 3708 | continue; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3709 | |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3710 | len = 0; |
| 3711 | while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) { |
| 3712 | len += strlen(next->text); |
| 3713 | next = next->next; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 3714 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3715 | |
| 3716 | /* No match */ |
| 3717 | if (tok == next) |
| 3718 | break; |
| 3719 | |
| 3720 | len += strlen(tok->text); |
| 3721 | p = buf = nasm_malloc(len + 1); |
| 3722 | |
| 3723 | while (tok != next) { |
| 3724 | strcpy(p, tok->text); |
| 3725 | p = strchr(p, '\0'); |
| 3726 | tok = delete_Token(tok); |
| 3727 | } |
| 3728 | |
| 3729 | tok = tokenize(buf); |
| 3730 | nasm_free(buf); |
| 3731 | |
| 3732 | if (prev_next) |
| 3733 | *prev_next = tok; |
| 3734 | else |
| 3735 | *head = tok; |
| 3736 | |
| 3737 | /* |
| 3738 | * Connect pasted into original stream, |
| 3739 | * ie A -> new-tokens -> B |
| 3740 | */ |
| 3741 | while (tok && tok->next) |
| 3742 | tok = tok->next; |
| 3743 | tok->next = next; |
| 3744 | |
| 3745 | if (!pasted) |
| 3746 | pasted = true; |
| 3747 | |
| 3748 | /* Restart from pasted tokens head */ |
| 3749 | tok = prev_next ? *prev_next : *head; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3750 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3751 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3752 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3753 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3754 | |
| 3755 | prev_next = &tok->next; |
| 3756 | |
| 3757 | if (tok->next && |
| 3758 | !tok_type_(tok->next, TOK_WHITESPACE) && |
| 3759 | !tok_type_(tok->next, TOK_PASTE)) |
| 3760 | prev_nonspace = prev_next; |
| 3761 | |
| 3762 | tok = tok->next; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3763 | } |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 3764 | |
| 3765 | return pasted; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3766 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3767 | |
| 3768 | /* |
| 3769 | * expands to a list of tokens from %{x:y} |
| 3770 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3771 | static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3772 | { |
| 3773 | Token *t = tline, **tt, *tm, *head; |
| 3774 | char *pos; |
| 3775 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3776 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3777 | pos = strchr(tline->text, ':'); |
| 3778 | nasm_assert(pos); |
| 3779 | |
| 3780 | lst = atoi(pos + 1); |
| 3781 | fst = atoi(tline->text + 1); |
| 3782 | |
| 3783 | /* |
| 3784 | * only macros params are accounted so |
| 3785 | * if someone passes %0 -- we reject such |
| 3786 | * value(s) |
| 3787 | */ |
| 3788 | if (lst == 0 || fst == 0) |
| 3789 | goto err; |
| 3790 | |
| 3791 | /* the values should be sane */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3792 | if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) || |
| 3793 | (lst > (int)mac->nparam || lst < (-(int)mac->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3794 | goto err; |
| 3795 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3796 | fst = fst < 0 ? fst + (int)mac->nparam + 1: fst; |
| 3797 | lst = lst < 0 ? lst + (int)mac->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3798 | |
| 3799 | /* counted from zero */ |
| 3800 | fst--, lst--; |
| 3801 | |
| 3802 | /* |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3803 | * It will be at least one token. Note we |
| 3804 | * need to scan params until separator, otherwise |
| 3805 | * only first token will be passed. |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3806 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3807 | tm = mac->params[(fst + mac->rotate) % mac->nparam]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3808 | head = new_Token(NULL, tm->type, tm->text, 0); |
| 3809 | tt = &head->next, tm = tm->next; |
| 3810 | while (tok_isnt_(tm, ",")) { |
| 3811 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3812 | *tt = t, tt = &t->next, tm = tm->next; |
| 3813 | } |
| 3814 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3815 | if (fst < lst) { |
| 3816 | for (i = fst + 1; i <= lst; i++) { |
| 3817 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3818 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3819 | j = (i + mac->rotate) % mac->nparam; |
| 3820 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3821 | while (tok_isnt_(tm, ",")) { |
| 3822 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3823 | *tt = t, tt = &t->next, tm = tm->next; |
| 3824 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3825 | } |
| 3826 | } else { |
| 3827 | for (i = fst - 1; i >= lst; i--) { |
| 3828 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 3829 | *tt = t, tt = &t->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3830 | j = (i + mac->rotate) % mac->nparam; |
| 3831 | tm = mac->params[j]; |
Cyrill Gorcunov | e75331c | 2013-11-09 12:02:15 +0400 | [diff] [blame] | 3832 | while (tok_isnt_(tm, ",")) { |
| 3833 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 3834 | *tt = t, tt = &t->next, tm = tm->next; |
| 3835 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | *last = tt; |
| 3840 | return head; |
| 3841 | |
| 3842 | err: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3843 | nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3844 | &tline->text[1]); |
| 3845 | return tline; |
| 3846 | } |
| 3847 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3848 | /* |
| 3849 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3850 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3851 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3852 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3853 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3854 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3855 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3856 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3857 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3858 | |
| 3859 | tail = &thead; |
| 3860 | thead = NULL; |
| 3861 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3862 | while (tline) { |
| 3863 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 3864 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 3865 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 3866 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3867 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3868 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3869 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3870 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3871 | int i; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3872 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3873 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3874 | t = tline; |
| 3875 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3876 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3877 | mac = istk->mstk; |
| 3878 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3879 | mac = mac->next_active; |
| 3880 | if (!mac) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3881 | nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3882 | } else { |
| 3883 | pos = strchr(t->text, ':'); |
| 3884 | if (!pos) { |
| 3885 | switch (t->text[1]) { |
| 3886 | /* |
| 3887 | * We have to make a substitution of one of the |
| 3888 | * forms %1, %-1, %+1, %%foo, %0. |
| 3889 | */ |
| 3890 | case '0': |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3891 | type = TOK_NUMBER; |
| 3892 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3893 | text = nasm_strdup(tmpbuf); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3894 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3895 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3896 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3897 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3898 | mac->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3899 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3900 | break; |
| 3901 | case '-': |
| 3902 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3903 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3904 | tt = NULL; |
| 3905 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3906 | if (mac->nparam > 1) |
| 3907 | n = (n + mac->rotate) % mac->nparam; |
| 3908 | tt = mac->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3909 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3910 | cc = find_cc(tt); |
| 3911 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3912 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3913 | "macro parameter %d is not a condition code", |
| 3914 | n + 1); |
| 3915 | text = NULL; |
| 3916 | } else { |
| 3917 | type = TOK_ID; |
| 3918 | if (inverse_ccs[cc] == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3919 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3920 | "condition code `%s' is not invertible", |
| 3921 | conditions[cc]); |
| 3922 | text = NULL; |
| 3923 | } else |
| 3924 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 3925 | } |
| 3926 | break; |
| 3927 | case '+': |
| 3928 | n = atoi(t->text + 2) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3929 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3930 | tt = NULL; |
| 3931 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3932 | if (mac->nparam > 1) |
| 3933 | n = (n + mac->rotate) % mac->nparam; |
| 3934 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3935 | } |
| 3936 | cc = find_cc(tt); |
| 3937 | if (cc == -1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 3938 | nasm_error(ERR_NONFATAL, |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3939 | "macro parameter %d is not a condition code", |
| 3940 | n + 1); |
| 3941 | text = NULL; |
| 3942 | } else { |
| 3943 | type = TOK_ID; |
| 3944 | text = nasm_strdup(conditions[cc]); |
| 3945 | } |
| 3946 | break; |
| 3947 | default: |
| 3948 | n = atoi(t->text + 1) - 1; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3949 | if (n >= mac->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3950 | tt = NULL; |
| 3951 | else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3952 | if (mac->nparam > 1) |
| 3953 | n = (n + mac->rotate) % mac->nparam; |
| 3954 | tt = mac->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3955 | } |
| 3956 | if (tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3957 | for (i = 0; i < mac->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3958 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 3959 | tail = &(*tail)->next; |
| 3960 | tt = tt->next; |
| 3961 | } |
| 3962 | } |
| 3963 | text = NULL; /* we've done it here */ |
| 3964 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3965 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3966 | } else { |
| 3967 | /* |
| 3968 | * seems we have a parameters range here |
| 3969 | */ |
| 3970 | Token *head, **last; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3971 | head = expand_mmac_params_range(mac, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3972 | if (head != t) { |
| 3973 | *tail = head; |
| 3974 | *last = tline; |
| 3975 | tline = head; |
| 3976 | text = NULL; |
| 3977 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3978 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 3979 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3980 | if (!text) { |
| 3981 | delete_Token(t); |
| 3982 | } else { |
| 3983 | *tail = t; |
| 3984 | tail = &t->next; |
| 3985 | t->type = type; |
| 3986 | nasm_free(t->text); |
| 3987 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3988 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3989 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3990 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3991 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3992 | } else if (tline->type == TOK_INDIRECT) { |
| 3993 | t = tline; |
| 3994 | tline = tline->next; |
| 3995 | tt = tokenize(t->text); |
| 3996 | tt = expand_mmac_params(tt); |
| 3997 | tt = expand_smacro(tt); |
| 3998 | *tail = tt; |
| 3999 | while (tt) { |
| 4000 | tt->a.mac = NULL; /* Necessary? */ |
| 4001 | tail = &tt->next; |
| 4002 | tt = tt->next; |
| 4003 | } |
| 4004 | delete_Token(t); |
| 4005 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4006 | } else { |
| 4007 | t = *tail = tline; |
| 4008 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4009 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4010 | tail = &t->next; |
| 4011 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4012 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4013 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4014 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4015 | if (changed) { |
| 4016 | const struct tokseq_match t[] = { |
| 4017 | { |
| 4018 | PP_CONCAT_MASK(TOK_ID) | |
| 4019 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4020 | PP_CONCAT_MASK(TOK_ID) | |
| 4021 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4022 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4023 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4024 | }, |
| 4025 | { |
| 4026 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4027 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4028 | } |
| 4029 | }; |
| 4030 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4031 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4032 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4033 | return thead; |
| 4034 | } |
| 4035 | |
| 4036 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4037 | * Expand all single-line macro calls made in the given line. |
| 4038 | * Return the expanded version of the line. The original is deemed |
| 4039 | * to be destroyed in the process. (In reality we'll just move |
| 4040 | * Tokens from input to output a lot of the time, rather than |
| 4041 | * actually bothering to destroy and replicate.) |
| 4042 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4043 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4044 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4045 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4046 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4047 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4048 | Token **params; |
| 4049 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4050 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4051 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4052 | Token *org_tline = tline; |
| 4053 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4054 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4055 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4056 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4057 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4058 | /* |
| 4059 | * Trick: we should avoid changing the start token pointer since it can |
| 4060 | * be contained in "next" field of other token. Because of this |
| 4061 | * we allocate a copy of first token and work with it; at the end of |
| 4062 | * routine we copy it back |
| 4063 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4064 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4065 | tline = new_Token(org_tline->next, org_tline->type, |
| 4066 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4067 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4068 | nasm_free(org_tline->text); |
| 4069 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4072 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4073 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4074 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4075 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4076 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4077 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4078 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4079 | if (!--deadman) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4080 | nasm_error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4081 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4082 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4083 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4084 | if ((mname = tline->text)) { |
| 4085 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4086 | if (tline->type == TOK_ID) { |
| 4087 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4088 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 4089 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4090 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4091 | } else |
| 4092 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4093 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4094 | /* |
| 4095 | * We've hit an identifier. As in is_mmacro below, we first |
| 4096 | * check whether the identifier is a single-line macro at |
| 4097 | * all, then think about checking for parameters if |
| 4098 | * necessary. |
| 4099 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4100 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4101 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4102 | break; |
| 4103 | if (m) { |
| 4104 | mstart = tline; |
| 4105 | params = NULL; |
| 4106 | paramsize = NULL; |
| 4107 | if (m->nparam == 0) { |
| 4108 | /* |
| 4109 | * Simple case: the macro is parameterless. Discard the |
| 4110 | * one token that the macro call took, and push the |
| 4111 | * expansion back on the to-do stack. |
| 4112 | */ |
| 4113 | if (!m->expansion) { |
| 4114 | if (!strcmp("__FILE__", m->name)) { |
| 4115 | int32_t num = 0; |
| 4116 | char *file = NULL; |
| 4117 | src_get(&num, &file); |
| 4118 | tline->text = nasm_quote(file, strlen(file)); |
| 4119 | tline->type = TOK_STRING; |
| 4120 | nasm_free(file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4121 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4122 | } |
| 4123 | if (!strcmp("__LINE__", m->name)) { |
| 4124 | nasm_free(tline->text); |
| 4125 | make_tok_num(tline, src_get_linnum()); |
| 4126 | continue; |
| 4127 | } |
| 4128 | if (!strcmp("__BITS__", m->name)) { |
| 4129 | nasm_free(tline->text); |
| 4130 | make_tok_num(tline, globalbits); |
| 4131 | continue; |
| 4132 | } |
| 4133 | tline = delete_Token(tline); |
| 4134 | continue; |
| 4135 | } |
| 4136 | } else { |
| 4137 | /* |
| 4138 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4139 | * exists and takes parameters. We must find the |
| 4140 | * parameters in the call, count them, find the SMacro |
| 4141 | * that corresponds to that form of the macro call, and |
| 4142 | * substitute for the parameters when we expand. What a |
| 4143 | * pain. |
| 4144 | */ |
| 4145 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4146 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4147 | do { |
| 4148 | t = tline->next; |
| 4149 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4150 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4151 | t->text = NULL; |
| 4152 | t = tline->next = delete_Token(t); |
| 4153 | } |
| 4154 | tline = t; |
| 4155 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4156 | if (!tok_is_(tline, "(")) { |
| 4157 | /* |
| 4158 | * This macro wasn't called with parameters: ignore |
| 4159 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4160 | */ |
| 4161 | tline = mstart; |
| 4162 | m = NULL; |
| 4163 | } else { |
| 4164 | int paren = 0; |
| 4165 | int white = 0; |
| 4166 | brackets = 0; |
| 4167 | nparam = 0; |
| 4168 | sparam = PARAM_DELTA; |
| 4169 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4170 | params[0] = tline->next; |
| 4171 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4172 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4173 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4174 | /* |
| 4175 | * For some unusual expansions |
| 4176 | * which concatenates function call |
| 4177 | */ |
| 4178 | t = tline->next; |
| 4179 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4180 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4181 | t->text = NULL; |
| 4182 | t = tline->next = delete_Token(t); |
| 4183 | } |
| 4184 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4185 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4186 | if (!tline) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4187 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4188 | "macro call expects terminating `)'"); |
| 4189 | break; |
| 4190 | } |
| 4191 | if (tline->type == TOK_WHITESPACE |
| 4192 | && brackets <= 0) { |
| 4193 | if (paramsize[nparam]) |
| 4194 | white++; |
| 4195 | else |
| 4196 | params[nparam] = tline->next; |
| 4197 | continue; /* parameter loop */ |
| 4198 | } |
| 4199 | if (tline->type == TOK_OTHER |
| 4200 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4201 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4202 | if (ch == ',' && !paren && brackets <= 0) { |
| 4203 | if (++nparam >= sparam) { |
| 4204 | sparam += PARAM_DELTA; |
| 4205 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4206 | sparam * sizeof(Token *)); |
| 4207 | paramsize = nasm_realloc(paramsize, |
| 4208 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4209 | } |
| 4210 | params[nparam] = tline->next; |
| 4211 | paramsize[nparam] = 0; |
| 4212 | white = 0; |
| 4213 | continue; /* parameter loop */ |
| 4214 | } |
| 4215 | if (ch == '{' && |
| 4216 | (brackets > 0 || (brackets == 0 && |
| 4217 | !paramsize[nparam]))) |
| 4218 | { |
| 4219 | if (!(brackets++)) { |
| 4220 | params[nparam] = tline->next; |
| 4221 | continue; /* parameter loop */ |
| 4222 | } |
| 4223 | } |
| 4224 | if (ch == '}' && brackets > 0) |
| 4225 | if (--brackets == 0) { |
| 4226 | brackets = -1; |
| 4227 | continue; /* parameter loop */ |
| 4228 | } |
| 4229 | if (ch == '(' && !brackets) |
| 4230 | paren++; |
| 4231 | if (ch == ')' && brackets <= 0) |
| 4232 | if (--paren < 0) |
| 4233 | break; |
| 4234 | } |
| 4235 | if (brackets < 0) { |
| 4236 | brackets = 0; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4237 | nasm_error(ERR_NONFATAL, "braces do not " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4238 | "enclose all of macro parameter"); |
| 4239 | } |
| 4240 | paramsize[nparam] += white + 1; |
| 4241 | white = 0; |
| 4242 | } /* parameter loop */ |
| 4243 | nparam++; |
| 4244 | while (m && (m->nparam != nparam || |
| 4245 | mstrcmp(m->name, mname, |
| 4246 | m->casesense))) |
| 4247 | m = m->next; |
| 4248 | if (!m) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4249 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4250 | "macro `%s' exists, " |
| 4251 | "but not taking %d parameters", |
| 4252 | mstart->text, nparam); |
| 4253 | } |
| 4254 | } |
| 4255 | if (m && m->in_progress) |
| 4256 | m = NULL; |
| 4257 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4258 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4259 | * Design question: should we handle !tline, which |
| 4260 | * indicates missing ')' here, or expand those |
| 4261 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4262 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4263 | */ |
| 4264 | nasm_free(params); |
| 4265 | nasm_free(paramsize); |
| 4266 | tline = mstart; |
| 4267 | } else { |
| 4268 | /* |
| 4269 | * Expand the macro: we are placed on the last token of the |
| 4270 | * call, so that we can easily split the call from the |
| 4271 | * following tokens. We also start by pushing an SMAC_END |
| 4272 | * token for the cycle removal. |
| 4273 | */ |
| 4274 | t = tline; |
| 4275 | if (t) { |
| 4276 | tline = t->next; |
| 4277 | t->next = NULL; |
| 4278 | } |
| 4279 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4280 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4281 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4282 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4283 | list_for_each(t, m->expansion) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4284 | if (t->type >= TOK_SMAC_PARAM) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4285 | Token *pcopy = tline, **ptail = &pcopy; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4286 | Token *ttt, *pt; |
| 4287 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4288 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4289 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 4290 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4291 | while (--i >= 0) { |
| 4292 | pt = *ptail = new_Token(tline, ttt->type, |
| 4293 | ttt->text, 0); |
| 4294 | ptail = &pt->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4295 | ttt = ttt->next; |
| 4296 | } |
| 4297 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4298 | } else if (t->type == TOK_PREPROC_Q) { |
| 4299 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4300 | tline = tt; |
| 4301 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4302 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4303 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4304 | } else { |
| 4305 | tt = new_Token(tline, t->type, t->text, 0); |
| 4306 | tline = tt; |
| 4307 | } |
| 4308 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4309 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4310 | /* |
| 4311 | * Having done that, get rid of the macro call, and clean |
| 4312 | * up the parameters. |
| 4313 | */ |
| 4314 | nasm_free(params); |
| 4315 | nasm_free(paramsize); |
| 4316 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4317 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4318 | continue; /* main token loop */ |
| 4319 | } |
| 4320 | } |
| 4321 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4322 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4323 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4324 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4325 | tline = delete_Token(tline); |
| 4326 | } else { |
| 4327 | t = *tail = tline; |
| 4328 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4329 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4330 | t->next = NULL; |
| 4331 | tail = &t->next; |
| 4332 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4335 | /* |
| 4336 | * 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] | 4337 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4338 | * TOK_IDs should be concatenated. |
| 4339 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4340 | * them (without white spaces in between). |
| 4341 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4342 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4343 | const struct tokseq_match t[] = { |
| 4344 | { |
| 4345 | PP_CONCAT_MASK(TOK_ID) | |
| 4346 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4347 | PP_CONCAT_MASK(TOK_ID) | |
| 4348 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4349 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4350 | } |
| 4351 | }; |
| 4352 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4353 | /* |
| 4354 | * If we concatenated something, *and* we had previously expanded |
| 4355 | * an actual macro, scan the lines again for macros... |
| 4356 | */ |
| 4357 | tline = thead; |
| 4358 | expanded = false; |
| 4359 | goto again; |
| 4360 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4361 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4362 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4363 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4364 | if (org_tline) { |
| 4365 | if (thead) { |
| 4366 | *org_tline = *thead; |
| 4367 | /* since we just gave text to org_line, don't free it */ |
| 4368 | thead->text = NULL; |
| 4369 | delete_Token(thead); |
| 4370 | } else { |
| 4371 | /* the expression expanded to empty line; |
| 4372 | we can't return NULL for some reasons |
| 4373 | we just set the line to a single WHITESPACE token. */ |
| 4374 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4375 | org_tline->text = NULL; |
| 4376 | org_tline->type = TOK_WHITESPACE; |
| 4377 | } |
| 4378 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4381 | return thead; |
| 4382 | } |
| 4383 | |
| 4384 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4385 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4386 | * right before they are fetched in. The reason is that there can be |
| 4387 | * identifiers consisting of several subparts. We consider that if there |
| 4388 | * are more than one element forming the name, user wants a expansion, |
| 4389 | * otherwise it will be left as-is. Example: |
| 4390 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4391 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4392 | * |
| 4393 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4394 | * will suck it and define the corresponding value. Other case: |
| 4395 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4396 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4397 | * |
| 4398 | * In this case user wants name to be expanded *before* %define starts |
| 4399 | * working, so we'll expand %$abc into something (if it has a value; |
| 4400 | * otherwise it will be left as-is) then concatenate all successive |
| 4401 | * PP_IDs into one. |
| 4402 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4403 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4404 | { |
| 4405 | Token *cur, *oldnext = NULL; |
| 4406 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4407 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4408 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4409 | |
| 4410 | cur = tline; |
| 4411 | while (cur->next && |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4412 | (cur->next->type == TOK_ID || |
| 4413 | cur->next->type == TOK_PREPROC_ID |
| 4414 | || cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4415 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4416 | |
| 4417 | /* If identifier consists of just one token, don't expand */ |
| 4418 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4419 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4420 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4421 | if (cur) { |
| 4422 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4423 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4426 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4427 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4428 | if (cur) { |
| 4429 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4430 | cur = tline; |
| 4431 | while (cur && cur->next) |
| 4432 | cur = cur->next; |
| 4433 | if (cur) |
| 4434 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | return tline; |
| 4438 | } |
| 4439 | |
| 4440 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4441 | * Determine whether the given line constitutes a multi-line macro |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4442 | * 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] | 4443 | * to check for an initial label - that's taken care of in |
| 4444 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4445 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4446 | * name is easy to find. |
| 4447 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4448 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4449 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4450 | MMacro *head, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4451 | Token **params; |
| 4452 | int nparam; |
| 4453 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4454 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4455 | |
| 4456 | /* |
| 4457 | * Efficiency: first we see if any macro exists with the given |
| 4458 | * name. If not, we can return NULL immediately. _Then_ we |
| 4459 | * count the parameters, and then we look further along the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4460 | * list if necessary to find the proper MMacro. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4461 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4462 | list_for_each(m, head) |
| 4463 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4464 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4465 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4466 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4467 | |
| 4468 | /* |
| 4469 | * OK, we have a potential macro. Count and demarcate the |
| 4470 | * parameters. |
| 4471 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4472 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4473 | |
| 4474 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4475 | * 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] | 4476 | * structure that handles this number. |
| 4477 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4478 | while (m) { |
| 4479 | if (m->nparam_min <= nparam |
| 4480 | && (m->plus || nparam <= m->nparam_max)) { |
| 4481 | /* |
| 4482 | * This one is right. Just check if cycle removal |
| 4483 | * prohibits us using it before we actually celebrate... |
| 4484 | */ |
| 4485 | if (m->in_progress > m->max_depth) { |
| 4486 | if (m->max_depth > 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4487 | nasm_error(ERR_WARNING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4488 | "reached maximum recursion depth of %i", |
| 4489 | m->max_depth); |
| 4490 | } |
| 4491 | nasm_free(params); |
| 4492 | return NULL; |
| 4493 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4494 | /* |
| 4495 | * It's right, and we can use it. Add its default |
| 4496 | * parameters to the end of our list if necessary. |
| 4497 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4498 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4499 | params = |
| 4500 | nasm_realloc(params, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4501 | ((m->nparam_min + m->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4502 | 1) * sizeof(*params))); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4503 | while (nparam < m->nparam_min + m->ndefs) { |
| 4504 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4505 | nparam++; |
| 4506 | } |
| 4507 | } |
| 4508 | /* |
| 4509 | * If we've gone over the maximum parameter count (and |
| 4510 | * we're in Plus mode), ignore parameters beyond |
| 4511 | * nparam_max. |
| 4512 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4513 | if (m->plus && nparam > m->nparam_max) |
| 4514 | nparam = m->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4515 | /* |
| 4516 | * Then terminate the parameter list, and leave. |
| 4517 | */ |
| 4518 | if (!params) { /* need this special case */ |
| 4519 | params = nasm_malloc(sizeof(*params)); |
| 4520 | nparam = 0; |
| 4521 | } |
| 4522 | params[nparam] = NULL; |
| 4523 | *params_array = params; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4524 | return m; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4525 | } |
| 4526 | /* |
| 4527 | * This one wasn't right: look for the next one with the |
| 4528 | * same name. |
| 4529 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4530 | list_for_each(m, m->next) |
| 4531 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4532 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | /* |
| 4536 | * After all that, we didn't find one with the right number of |
| 4537 | * parameters. Issue a warning, and fail to expand the macro. |
| 4538 | */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4539 | nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4540 | "macro `%s' exists, but not taking %d parameters", |
| 4541 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4542 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4543 | return NULL; |
| 4544 | } |
| 4545 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4546 | |
| 4547 | /* |
| 4548 | * Save MMacro invocation specific fields in |
| 4549 | * preparation for a recursive macro expansion |
| 4550 | */ |
| 4551 | static void push_mmacro(MMacro *m) |
| 4552 | { |
| 4553 | MMacroInvocation *i; |
| 4554 | |
| 4555 | i = nasm_malloc(sizeof(MMacroInvocation)); |
| 4556 | i->prev = m->prev; |
| 4557 | i->params = m->params; |
| 4558 | i->iline = m->iline; |
| 4559 | i->nparam = m->nparam; |
| 4560 | i->rotate = m->rotate; |
| 4561 | i->paramlen = m->paramlen; |
| 4562 | i->unique = m->unique; |
| 4563 | i->condcnt = m->condcnt; |
| 4564 | m->prev = i; |
| 4565 | } |
| 4566 | |
| 4567 | |
| 4568 | /* |
| 4569 | * Restore MMacro invocation specific fields that were |
| 4570 | * saved during a previous recursive macro expansion |
| 4571 | */ |
| 4572 | static void pop_mmacro(MMacro *m) |
| 4573 | { |
| 4574 | MMacroInvocation *i; |
| 4575 | |
| 4576 | if (m->prev) { |
| 4577 | i = m->prev; |
| 4578 | m->prev = i->prev; |
| 4579 | m->params = i->params; |
| 4580 | m->iline = i->iline; |
| 4581 | m->nparam = i->nparam; |
| 4582 | m->rotate = i->rotate; |
| 4583 | m->paramlen = i->paramlen; |
| 4584 | m->unique = i->unique; |
| 4585 | m->condcnt = i->condcnt; |
| 4586 | nasm_free(i); |
| 4587 | } |
| 4588 | } |
| 4589 | |
| 4590 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4591 | /* |
| 4592 | * Expand the multi-line macro call made by the given line, if |
| 4593 | * 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] | 4594 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4595 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4596 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4597 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4598 | Token *startline = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4599 | Token *label = NULL; |
| 4600 | int dont_prepend = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4601 | Token **params, *t, *tt; |
| 4602 | MMacro *m; |
| 4603 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4604 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4605 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4606 | |
| 4607 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4608 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4609 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4610 | 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] | 4611 | return 0; |
| 4612 | m = is_mmacro(t, ¶ms); |
| 4613 | if (m) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4614 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4615 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4616 | Token *last; |
| 4617 | /* |
| 4618 | * We have an id which isn't a macro call. We'll assume |
| 4619 | * it might be a label; we'll also check to see if a |
| 4620 | * colon follows it. Then, if there's another id after |
| 4621 | * that lot, we'll check it again for macro-hood. |
| 4622 | */ |
| 4623 | label = last = t; |
| 4624 | t = t->next; |
| 4625 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4626 | last = t, t = t->next; |
| 4627 | if (tok_is_(t, ":")) { |
| 4628 | dont_prepend = 1; |
| 4629 | last = t, t = t->next; |
| 4630 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4631 | last = t, t = t->next; |
| 4632 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4633 | if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, ¶ms))) |
| 4634 | return 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4635 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4636 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4637 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4638 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4639 | |
| 4640 | /* |
| 4641 | * Fix up the parameters: this involves stripping leading and |
| 4642 | * trailing whitespace, then stripping braces if they are |
| 4643 | * present. |
| 4644 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4645 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4646 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4647 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4648 | for (i = 0; params[i]; i++) { |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4649 | int brace = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4650 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4651 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4652 | t = params[i]; |
| 4653 | skip_white_(t); |
| 4654 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4655 | t = t->next, brace++, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4656 | params[i] = t; |
| 4657 | paramlen[i] = 0; |
| 4658 | while (t) { |
| 4659 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4660 | break; /* ... because we have hit a comma */ |
| 4661 | if (comma && t->type == TOK_WHITESPACE |
| 4662 | && tok_is_(t->next, ",")) |
| 4663 | break; /* ... or a space then a comma */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4664 | if (brace && t->type == TOK_OTHER) { |
| 4665 | if (t->text[0] == '{') |
| 4666 | brace++; /* ... or a nested opening brace */ |
| 4667 | else if (t->text[0] == '}') |
| 4668 | if (!--brace) |
| 4669 | break; /* ... or a brace */ |
| 4670 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4671 | t = t->next; |
| 4672 | paramlen[i]++; |
| 4673 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 4674 | if (brace) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4675 | nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4679 | * OK, we have a MMacro structure together with a set of |
| 4680 | * parameters. We must now go through the expansion and push |
| 4681 | * copies of each Line on to istk->expansion. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4682 | * parameter tokens and macro-local tokens doesn't get done |
| 4683 | * until the single-line macro substitution process; this is |
| 4684 | * because delaying them allows us to change the semantics |
| 4685 | * later through %rotate. |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4686 | * |
| 4687 | * First, push an end marker on to istk->expansion, mark this |
| 4688 | * macro as in progress, and set up its invocation-specific |
| 4689 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4690 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4691 | ll = nasm_malloc(sizeof(Line)); |
| 4692 | ll->next = istk->expansion; |
| 4693 | ll->finishes = m; |
| 4694 | ll->first = NULL; |
| 4695 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4696 | |
| 4697 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4698 | * Save the previous MMacro expansion in the case of |
| 4699 | * macro recursion |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4700 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4701 | if (m->max_depth && m->in_progress) |
| 4702 | push_mmacro(m); |
| 4703 | |
| 4704 | m->in_progress ++; |
| 4705 | m->params = params; |
| 4706 | m->iline = tline; |
| 4707 | m->nparam = nparam; |
| 4708 | m->rotate = 0; |
| 4709 | m->paramlen = paramlen; |
| 4710 | m->unique = unique++; |
| 4711 | m->lineno = 0; |
| 4712 | m->condcnt = 0; |
| 4713 | |
| 4714 | m->next_active = istk->mstk; |
| 4715 | istk->mstk = m; |
| 4716 | |
| 4717 | list_for_each(l, m->expansion) { |
| 4718 | Token **tail; |
| 4719 | |
| 4720 | ll = nasm_malloc(sizeof(Line)); |
| 4721 | ll->finishes = NULL; |
| 4722 | ll->next = istk->expansion; |
| 4723 | istk->expansion = ll; |
| 4724 | tail = &ll->first; |
| 4725 | |
| 4726 | list_for_each(t, l->first) { |
| 4727 | Token *x = t; |
| 4728 | switch (t->type) { |
| 4729 | case TOK_PREPROC_Q: |
| 4730 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4731 | break; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4732 | case TOK_PREPROC_QQ: |
| 4733 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4734 | break; |
| 4735 | case TOK_PREPROC_ID: |
| 4736 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4737 | dont_prepend = -1; |
| 4738 | x = label; |
| 4739 | if (!x) |
| 4740 | continue; |
| 4741 | } |
| 4742 | /* fall through */ |
| 4743 | default: |
| 4744 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4745 | break; |
| 4746 | } |
| 4747 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4748 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4749 | *tail = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4750 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4751 | |
| 4752 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4753 | * If we had a label, push it on as the first line of |
| 4754 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4755 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4756 | if (label) { |
| 4757 | if (dont_prepend < 0) |
| 4758 | free_tlist(startline); |
| 4759 | else { |
| 4760 | ll = nasm_malloc(sizeof(Line)); |
| 4761 | ll->finishes = NULL; |
| 4762 | ll->next = istk->expansion; |
| 4763 | istk->expansion = ll; |
| 4764 | ll->first = startline; |
| 4765 | if (!dont_prepend) { |
| 4766 | while (label->next) |
| 4767 | label = label->next; |
| 4768 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4769 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4770 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4771 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4772 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 4773 | lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4774 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4775 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4778 | /* |
| 4779 | * This function adds macro names to error messages, and suppresses |
| 4780 | * them if necessary. |
| 4781 | */ |
| 4782 | 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] | 4783 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4784 | char buff[BUFSIZ]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4785 | MMacro *mmac = NULL; |
| 4786 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4787 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4788 | /* |
| 4789 | * If we're in a dead branch of IF or something like it, ignore the error. |
| 4790 | * However, because %else etc are evaluated in the state context |
| 4791 | * of the previous branch, errors might get lost: |
| 4792 | * %if 0 ... %else trailing garbage ... %endif |
| 4793 | * So %else etc should set the ERR_PP_PRECOND flag. |
| 4794 | */ |
| 4795 | if ((severity & ERR_MASK) < ERR_FATAL && |
| 4796 | istk && istk->conds && |
| 4797 | ((severity & ERR_PP_PRECOND) ? |
| 4798 | istk->conds->state == COND_NEVER : |
| 4799 | emitting(istk->conds->state))) |
| 4800 | return; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4801 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4802 | /* get %macro name */ |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4803 | if (!(severity & ERR_NOFILE) && istk && istk->mstk) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4804 | mmac = istk->mstk; |
| 4805 | /* but %rep blocks should be skipped */ |
| 4806 | while (mmac && !mmac->name) |
| 4807 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4808 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4809 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4810 | if (mmac) { |
| 4811 | vsnprintf(buff, sizeof(buff), fmt, arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4812 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4813 | nasm_set_verror(real_verror); |
| 4814 | nasm_error(severity, "(%s:%d) %s", |
| 4815 | mmac->name, mmac->lineno - delta, buff); |
| 4816 | nasm_set_verror(pp_verror); |
| 4817 | } else { |
| 4818 | real_verror(severity, fmt, arg); |
| 4819 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4822 | static void |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4823 | pp_reset(char *file, int apass, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4824 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4825 | Token *t; |
| 4826 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4827 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4828 | istk = nasm_malloc(sizeof(Include)); |
| 4829 | istk->next = NULL; |
| 4830 | istk->conds = NULL; |
| 4831 | istk->expansion = NULL; |
| 4832 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4833 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4834 | istk->fname = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4835 | src_set_fname(nasm_strdup(file)); |
| 4836 | src_set_linnum(0); |
| 4837 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4838 | if (!istk->fp) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4839 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4840 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4841 | nested_mac_count = 0; |
| 4842 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4843 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4844 | unique = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4845 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4846 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4847 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4848 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4849 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4850 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 4851 | do_predef = true; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4852 | |
| 4853 | /* |
| 4854 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4855 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4856 | * we can set __PASS__ accordingly. |
| 4857 | */ |
| 4858 | pass = apass > 2 ? 2 : apass; |
| 4859 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4860 | dephead = deptail = deplist; |
| 4861 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4862 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 4863 | sl->next = NULL; |
| 4864 | strcpy(sl->str, file); |
| 4865 | *deptail = sl; |
| 4866 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4867 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4868 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4869 | /* |
| 4870 | * Define the __PASS__ macro. This is defined here unlike |
| 4871 | * all the other builtins, because it is special -- it varies between |
| 4872 | * passes. |
| 4873 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4874 | t = nasm_malloc(sizeof(*t)); |
| 4875 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4876 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4877 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4878 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4881 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4882 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4883 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4884 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4885 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4886 | real_verror = nasm_set_verror(pp_verror); |
| 4887 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4888 | while (1) { |
| 4889 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4890 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4891 | * buffer or from the input file. |
| 4892 | */ |
| 4893 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4894 | while (istk->expansion && istk->expansion->finishes) { |
| 4895 | Line *l = istk->expansion; |
| 4896 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 4897 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4898 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4899 | /* |
| 4900 | * This is a macro-end marker for a macro with no |
| 4901 | * name, which means it's not really a macro at all |
| 4902 | * but a %rep block, and the `in_progress' field is |
| 4903 | * more than 1, meaning that we still need to |
| 4904 | * repeat. (1 means the natural last repetition; 0 |
| 4905 | * means termination by %exitrep.) We have |
| 4906 | * therefore expanded up to the %endrep, and must |
| 4907 | * push the whole block on to the expansion buffer |
| 4908 | * again. We don't bother to remove the macro-end |
| 4909 | * marker: we'd only have to generate another one |
| 4910 | * if we did. |
| 4911 | */ |
| 4912 | l->finishes->in_progress--; |
| 4913 | list_for_each(l, l->finishes->expansion) { |
| 4914 | Token *t, *tt, **tail; |
| 4915 | |
| 4916 | ll = nasm_malloc(sizeof(Line)); |
| 4917 | ll->next = istk->expansion; |
| 4918 | ll->finishes = NULL; |
| 4919 | ll->first = NULL; |
| 4920 | tail = &ll->first; |
| 4921 | |
| 4922 | list_for_each(t, l->first) { |
| 4923 | if (t->text || t->type == TOK_WHITESPACE) { |
| 4924 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 4925 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4926 | } |
| 4927 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4928 | |
| 4929 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4930 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4931 | } else { |
| 4932 | /* |
| 4933 | * Check whether a `%rep' was started and not ended |
| 4934 | * within this macro expansion. This can happen and |
| 4935 | * should be detected. It's a fatal error because |
| 4936 | * I'm too confused to work out how to recover |
| 4937 | * sensibly from it. |
| 4938 | */ |
| 4939 | if (defining) { |
| 4940 | if (defining->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4941 | nasm_panic(0, "defining with name in expansion"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4942 | else if (istk->mstk->name) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 4943 | nasm_fatal(0, "`%%rep' without `%%endrep' within" |
| 4944 | " expansion of macro `%s'", |
| 4945 | istk->mstk->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4946 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4947 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4948 | /* |
| 4949 | * FIXME: investigate the relationship at this point between |
| 4950 | * istk->mstk and l->finishes |
| 4951 | */ |
| 4952 | { |
| 4953 | MMacro *m = istk->mstk; |
| 4954 | istk->mstk = m->next_active; |
| 4955 | if (m->name) { |
| 4956 | /* |
| 4957 | * This was a real macro call, not a %rep, and |
| 4958 | * therefore the parameter information needs to |
| 4959 | * be freed. |
| 4960 | */ |
| 4961 | if (m->prev) { |
| 4962 | pop_mmacro(m); |
| 4963 | l->finishes->in_progress --; |
| 4964 | } else { |
| 4965 | nasm_free(m->params); |
| 4966 | free_tlist(m->iline); |
| 4967 | nasm_free(m->paramlen); |
| 4968 | l->finishes->in_progress = 0; |
| 4969 | } |
| 4970 | } else |
| 4971 | free_mmacro(m); |
| 4972 | } |
| 4973 | istk->expansion = l->next; |
| 4974 | nasm_free(l); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 4975 | lfmt->downlevel(LIST_MACRO); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4976 | } |
| 4977 | } |
| 4978 | while (1) { /* until we get a line we can use */ |
| 4979 | |
| 4980 | if (istk->expansion) { /* from a macro expansion */ |
| 4981 | char *p; |
| 4982 | Line *l = istk->expansion; |
| 4983 | if (istk->mstk) |
| 4984 | istk->mstk->lineno++; |
| 4985 | tline = l->first; |
| 4986 | istk->expansion = l->next; |
| 4987 | nasm_free(l); |
| 4988 | p = detoken(tline, false); |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 4989 | lfmt->line(LIST_MACRO, p); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4990 | nasm_free(p); |
| 4991 | break; |
| 4992 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4993 | line = read_line(); |
| 4994 | if (line) { /* from the current input file */ |
| 4995 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4996 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4997 | nasm_free(line); |
| 4998 | break; |
| 4999 | } |
| 5000 | /* |
| 5001 | * The current file has ended; work down the istk |
| 5002 | */ |
| 5003 | { |
| 5004 | Include *i = istk; |
| 5005 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5006 | if (i->conds) { |
| 5007 | /* nasm_error can't be conditionally suppressed */ |
| 5008 | nasm_error(ERR_FATAL, |
| 5009 | "expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5010 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5011 | /* only set line and file name if there's a next node */ |
| 5012 | if (i->next) { |
| 5013 | src_set_linnum(i->lineno); |
Cyrill Gorcunov | d34a108 | 2011-03-07 11:23:08 +0300 | [diff] [blame] | 5014 | nasm_free(src_set_fname(nasm_strdup(i->fname))); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5015 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5016 | istk = i->next; |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame^] | 5017 | lfmt->downlevel(LIST_INCLUDE); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5018 | nasm_free(i); |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5019 | if (!istk) { |
| 5020 | line = NULL; |
| 5021 | goto done; |
| 5022 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5023 | if (istk->expansion && istk->expansion->finishes) |
| 5024 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5025 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5026 | } |
| 5027 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5028 | /* |
| 5029 | * We must expand MMacro parameters and MMacro-local labels |
| 5030 | * _before_ we plunge into directive processing, to cope |
| 5031 | * with things like `%define something %1' such as STRUC |
| 5032 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5033 | * those tokens should be left alone to go into the |
| 5034 | * definition; and unless we're in a non-emitting |
| 5035 | * condition, in which case we don't want to meddle with |
| 5036 | * anything. |
| 5037 | */ |
| 5038 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5039 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5040 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5041 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5042 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5043 | /* |
| 5044 | * Check the line to see if it's a preprocessor directive. |
| 5045 | */ |
| 5046 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5047 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5048 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5049 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5050 | * We're defining a multi-line macro. We emit nothing |
| 5051 | * at all, and just |
| 5052 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5053 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5054 | Line *l = nasm_malloc(sizeof(Line)); |
| 5055 | l->next = defining->expansion; |
| 5056 | l->first = tline; |
| 5057 | l->finishes = NULL; |
| 5058 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5059 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5060 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5061 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5062 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5063 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5064 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5065 | * directive so we keep our place correctly. |
| 5066 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5067 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5068 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5069 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5070 | /* |
| 5071 | * We're in a %rep block which has been terminated, so |
| 5072 | * we're walking through to the %endrep without |
| 5073 | * emitting anything. Emit nothing at all, not even a |
| 5074 | * blank line: when we emerge from the %rep block we'll |
| 5075 | * give a line-number directive so we keep our place |
| 5076 | * correctly. |
| 5077 | */ |
| 5078 | free_tlist(tline); |
| 5079 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5080 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5081 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5082 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5083 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5084 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5085 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5086 | line = detoken(tline, true); |
| 5087 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5088 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5089 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5090 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5091 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5092 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5093 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5094 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5095 | done: |
| 5096 | nasm_set_verror(real_verror); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5097 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5100 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5101 | { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5102 | real_verror = nasm_set_verror(pp_verror); |
| 5103 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5104 | if (defining) { |
| 5105 | if (defining->name) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5106 | nasm_error(ERR_NONFATAL, |
| 5107 | "end of file while still defining macro `%s'", |
| 5108 | defining->name); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5109 | } else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5110 | nasm_error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5111 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5112 | |
| 5113 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5114 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5115 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5116 | |
| 5117 | nasm_set_verror(real_verror); |
| 5118 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5119 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5120 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5121 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5122 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5123 | Include *i = istk; |
| 5124 | istk = istk->next; |
| 5125 | fclose(i->fp); |
| 5126 | nasm_free(i->fname); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5127 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5128 | } |
| 5129 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5130 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5131 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5132 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5133 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5134 | free_llist(predef); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5135 | predef = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5136 | delete_Blocks(); |
Cyrill Gorcunov | dae24d7 | 2014-06-28 10:17:39 +0400 | [diff] [blame] | 5137 | freeTokens = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5138 | while ((i = ipath)) { |
| 5139 | ipath = i->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5140 | if (i->path) |
| 5141 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5142 | nasm_free(i); |
| 5143 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5144 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5147 | static void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5148 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5149 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5150 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5151 | i = nasm_malloc(sizeof(IncPath)); |
| 5152 | i->path = path ? nasm_strdup(path) : NULL; |
| 5153 | i->next = NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5154 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5155 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5156 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5157 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5158 | j = j->next; |
| 5159 | j->next = i; |
| 5160 | } else { |
| 5161 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5162 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5163 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5164 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5165 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5166 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5167 | Token *inc, *space, *name; |
| 5168 | Line *l; |
| 5169 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5170 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5171 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5172 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5173 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5174 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5175 | l->next = predef; |
| 5176 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5177 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5178 | predef = l; |
| 5179 | } |
| 5180 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5181 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5182 | { |
| 5183 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5184 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5185 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5186 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5187 | real_verror = nasm_set_verror(pp_verror); |
| 5188 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5189 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5190 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5191 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5192 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5193 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5194 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5195 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5196 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5197 | |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5198 | if (space->next->type != TOK_PREPROC_ID && |
| 5199 | space->next->type != TOK_ID) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5200 | nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition); |
Cyrill Gorcunov | 6d42e9b | 2015-02-08 11:07:17 +0300 | [diff] [blame] | 5201 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5202 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5203 | l->next = predef; |
| 5204 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5205 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5206 | predef = l; |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 5207 | |
| 5208 | nasm_set_verror(real_verror); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5211 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5212 | { |
| 5213 | Token *def, *space; |
| 5214 | Line *l; |
| 5215 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5216 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5217 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5218 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5219 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5220 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5221 | l->next = predef; |
| 5222 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5223 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5224 | predef = l; |
| 5225 | } |
| 5226 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5227 | static void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5228 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5229 | extrastdmac = macros; |
| 5230 | } |
| 5231 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5232 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5233 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5234 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5235 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5236 | tok->text = nasm_strdup(numbuf); |
| 5237 | tok->type = TOK_NUMBER; |
| 5238 | } |
| 5239 | |
Cyrill Gorcunov | 86b2ad0 | 2011-07-01 10:38:25 +0400 | [diff] [blame] | 5240 | struct preproc_ops nasmpp = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5241 | pp_reset, |
| 5242 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5243 | pp_cleanup, |
| 5244 | pp_extra_stdmac, |
| 5245 | pp_pre_define, |
| 5246 | pp_pre_undefine, |
| 5247 | pp_pre_include, |
| 5248 | pp_include_path |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5249 | }; |