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 | * |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3 | * Copyright 1996-2014 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 | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 83 | |
| 84 | typedef struct SMacro SMacro; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 85 | typedef struct MMacro MMacro; |
| 86 | typedef struct MMacroInvocation MMacroInvocation; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 87 | typedef struct Context Context; |
| 88 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 89 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 90 | typedef struct Line Line; |
| 91 | typedef struct Include Include; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 92 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 93 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 96 | * Note on the storage of both SMacro and MMacros: the hash table |
| 97 | * indexes them case-insensitively, and we then have to go through a |
| 98 | * linked list of potential case aliases (and, for MMacros, parameter |
| 99 | * ranges); this is to preserve the matching semantics of the earlier |
| 100 | * code. If the number of case aliases for a specific macro is a |
| 101 | * performance issue, you may want to reconsider your coding style. |
| 102 | */ |
| 103 | |
| 104 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 105 | * Store the definition of a single-line macro. |
| 106 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 107 | struct SMacro { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 108 | SMacro *next; |
| 109 | char *name; |
| 110 | bool casesense; |
| 111 | bool in_progress; |
| 112 | unsigned int nparam; |
| 113 | Token *expansion; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 117 | * Store the definition of a multi-line macro. This is also used to |
| 118 | * store the interiors of `%rep...%endrep' blocks, which are |
| 119 | * effectively self-re-invoking multi-line macros which simply |
| 120 | * don't have a name or bother to appear in the hash tables. %rep |
| 121 | * blocks are signified by having a NULL `name' field. |
| 122 | * |
| 123 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 124 | * isn't merely boolean, but gives the number of repeats left to |
| 125 | * run. |
| 126 | * |
| 127 | * The `next' field is used for storing MMacros in hash tables; the |
| 128 | * `next_active' field is for stacking them on istk entries. |
| 129 | * |
| 130 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 131 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 132 | */ |
| 133 | struct MMacro { |
| 134 | MMacro *next; |
| 135 | MMacroInvocation *prev; /* previous invocation */ |
| 136 | char *name; |
| 137 | int nparam_min, nparam_max; |
| 138 | bool casesense; |
| 139 | bool plus; /* is the last parameter greedy? */ |
| 140 | bool nolist; /* is this macro listing-inhibited? */ |
| 141 | int64_t in_progress; /* is this macro currently being expanded? */ |
| 142 | int32_t max_depth; /* maximum number of recursive expansions allowed */ |
| 143 | Token *dlist; /* All defaults as one list */ |
| 144 | Token **defaults; /* Parameter default pointers */ |
| 145 | int ndefs; /* number of default parameters */ |
| 146 | Line *expansion; |
| 147 | |
| 148 | MMacro *next_active; |
| 149 | MMacro *rep_nest; /* used for nesting %rep */ |
| 150 | Token **params; /* actual parameters */ |
| 151 | Token *iline; /* invocation line */ |
| 152 | unsigned int nparam, rotate; |
| 153 | int *paramlen; |
| 154 | uint64_t unique; |
| 155 | int lineno; /* Current line number on expansion */ |
| 156 | uint64_t condcnt; /* number of if blocks... */ |
| 157 | }; |
| 158 | |
| 159 | |
| 160 | /* Store the definition of a multi-line macro, as defined in a |
| 161 | * previous recursive macro expansion. |
| 162 | */ |
| 163 | struct MMacroInvocation { |
| 164 | MMacroInvocation *prev; /* previous invocation */ |
| 165 | Token **params; /* actual parameters */ |
| 166 | Token *iline; /* invocation line */ |
| 167 | unsigned int nparam, rotate; |
| 168 | int *paramlen; |
| 169 | uint64_t unique; |
| 170 | uint64_t condcnt; |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 175 | * The context stack is composed of a linked list of these. |
| 176 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 177 | struct Context { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 178 | Context *next; |
| 179 | char *name; |
| 180 | struct hash_table localmac; |
| 181 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * This is the internal form which we break input lines up into. |
| 186 | * Typically stored in linked lists. |
| 187 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 188 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 189 | * necessarily used as-is, but is intended to denote the number of |
| 190 | * the substituted parameter. So in the definition |
| 191 | * |
| 192 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 193 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 194 | * the token representing `x' will have its type changed to |
| 195 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 196 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 197 | * |
| 198 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 199 | * which doesn't need quotes around it. Used in the pre-include |
| 200 | * mechanism as an alternative to trying to find a sensible type of |
| 201 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 202 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 203 | enum pp_token_type { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 204 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 205 | TOK_PREPROC_ID, TOK_STRING, |
| 206 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 207 | TOK_INTERNAL_STRING, |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 208 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 209 | TOK_PASTE, /* %+ */ |
| 210 | TOK_INDIRECT, /* %[...] */ |
| 211 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 212 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 215 | #define PP_CONCAT_MASK(x) (1 << (x)) |
Cyrill Gorcunov | 1cf9b31 | 2012-08-04 10:51:58 +0400 | [diff] [blame] | 216 | #define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask) |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 217 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 218 | struct tokseq_match { |
| 219 | int mask_head; |
| 220 | int mask_tail; |
| 221 | }; |
| 222 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 223 | struct Token { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 224 | Token *next; |
| 225 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 226 | union { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 227 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 228 | size_t len; /* scratch length field */ |
| 229 | } a; /* Auxiliary data */ |
| 230 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 234 | * Multi-line macro definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 235 | * these, which is essentially a container to allow several linked |
| 236 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 237 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 238 | * Note that in this module, linked lists are treated as stacks |
| 239 | * wherever possible. For this reason, Lines are _pushed_ on to the |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 240 | * `expansion' field in MMacro structures, so that the linked list, |
| 241 | * if walked, would give the macro lines in reverse order; this |
| 242 | * means that we can walk the list when expanding a macro, and thus |
| 243 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 244 | * order (so that when popped back off they are in the right |
| 245 | * order). It may seem cockeyed, and it relies on my design having |
| 246 | * an even number of steps in, but it works... |
| 247 | * |
| 248 | * Some of these structures, rather than being actual lines, are |
| 249 | * markers delimiting the end of the expansion of a given macro. |
| 250 | * This is for use in the cycle-tracking and %rep-handling code. |
| 251 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 252 | * others have `finishes' NULL, but `first' may still be NULL if |
| 253 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 254 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 255 | struct Line { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 256 | Line *next; |
| 257 | MMacro *finishes; |
| 258 | Token *first; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 262 | * To handle an arbitrary level of file inclusion, we maintain a |
| 263 | * stack (ie linked list) of these things. |
| 264 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 265 | struct Include { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 266 | Include *next; |
| 267 | FILE *fp; |
| 268 | Cond *conds; |
| 269 | Line *expansion; |
| 270 | char *fname; |
| 271 | int lineno, lineinc; |
| 272 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 276 | * Include search path. This is simply a list of strings which get |
| 277 | * prepended, in turn, to the name of an include file, in an |
| 278 | * attempt to find the file if it's not in the current directory. |
| 279 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 280 | struct IncPath { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 281 | IncPath *next; |
| 282 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 286 | * Conditional assembly: we maintain a separate stack of these for |
| 287 | * each level of file inclusion. (The only reason we keep the |
| 288 | * stacks separate is to ensure that a stray `%endif' in a file |
| 289 | * included from within the true branch of a `%if' won't terminate |
| 290 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 291 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 292 | struct Cond { |
| 293 | Cond *next; |
| 294 | int state; |
| 295 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 296 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 297 | /* |
| 298 | * These states are for use just after %if or %elif: IF_TRUE |
| 299 | * means the condition has evaluated to truth so we are |
| 300 | * currently emitting, whereas IF_FALSE means we are not |
| 301 | * currently emitting but will start doing so if a %else comes |
| 302 | * up. In these states, all directives are admissible: %elif, |
| 303 | * %else and %endif. (And of course %if.) |
| 304 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 305 | COND_IF_TRUE, COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 306 | /* |
| 307 | * These states come up after a %else: ELSE_TRUE means we're |
| 308 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 309 | * any %elif or %else will cause an error. |
| 310 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 311 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 312 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 313 | * These states mean that we're not emitting now, and also that |
| 314 | * nothing until %endif will be emitted at all. COND_DONE is |
| 315 | * used when we've had our moment of emission |
| 316 | * and have now started seeing %elifs. COND_NEVER is used when |
| 317 | * the condition construct in question is contained within a |
| 318 | * non-emitting branch of a larger condition construct, |
| 319 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 320 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 321 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 322 | }; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 323 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 324 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 325 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 326 | * These defines are used as the possible return values for do_directive |
| 327 | */ |
| 328 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 329 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 330 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 331 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 332 | * This define sets the upper limit for smacro and recursive mmacro |
| 333 | * expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 334 | */ |
| 335 | #define DEADMAN_LIMIT (1 << 20) |
| 336 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 337 | /* max reps */ |
| 338 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 339 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 340 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 341 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 342 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 343 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 344 | * ones, so we need a different enum... |
| 345 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 346 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 347 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 348 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 349 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 350 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 351 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 352 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 353 | 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] | 354 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 355 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 356 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 357 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 358 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 359 | 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] | 360 | 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] | 361 | }; |
| 362 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 363 | /* |
| 364 | * Directive names. |
| 365 | */ |
| 366 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
| 367 | static int is_condition(enum preproc_token arg) |
| 368 | { |
| 369 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
| 370 | } |
| 371 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 372 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 373 | * conditional compilation directives. Using the NASM pre-processor does |
| 374 | * not work, so we look for them specifically from the following list and |
| 375 | * then jam in the equivalent NASM directive into the input stream. |
| 376 | */ |
| 377 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 378 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 379 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 380 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 381 | }; |
| 382 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 383 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 384 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 385 | "ifndef", "include", "local" |
| 386 | }; |
| 387 | |
| 388 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 389 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 390 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 391 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 392 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 393 | static Context *cstk; |
| 394 | static Include *istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 395 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 396 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 397 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 398 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 399 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 400 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 401 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 402 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 403 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 404 | |
| 405 | static ListGen *list; |
| 406 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 407 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 408 | * The current set of multi-line macros we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 409 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 410 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * The current set of single-line macros we have defined. |
| 414 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 415 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 416 | |
| 417 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 418 | * The multi-line macro we are currently defining, or the %rep |
| 419 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 420 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 421 | static MMacro *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 422 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 423 | static uint64_t nested_mac_count; |
| 424 | static uint64_t nested_rep_count; |
| 425 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 426 | /* |
| 427 | * The number of macro parameters to allocate space for at a time. |
| 428 | */ |
| 429 | #define PARAM_DELTA 16 |
| 430 | |
| 431 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 432 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 433 | * 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] | 434 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 435 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 436 | |
| 437 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 438 | * The extra standard macros that come from the object format, if |
| 439 | * any. |
| 440 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 441 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 442 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 443 | |
| 444 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 445 | * Tokens are allocated in blocks to improve speed |
| 446 | */ |
| 447 | #define TOKEN_BLOCKSIZE 4096 |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 448 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 449 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 450 | Blocks *next; |
| 451 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 454 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 455 | |
| 456 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 457 | * Forward declarations. |
| 458 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 459 | static Token *expand_mmac_params(Token * tline); |
| 460 | static Token *expand_smacro(Token * tline); |
| 461 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 462 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 463 | static void make_tok_num(Token * tok, int64_t val); |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 464 | static void error(int severity, const char *fmt, ...); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 465 | static void error_precond(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 466 | static void *new_Block(size_t size); |
| 467 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 468 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 469 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 470 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 471 | |
| 472 | /* |
| 473 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 474 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 475 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 476 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 477 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 478 | #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] | 479 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 480 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 481 | * nasm_unquote with error if the string contains NUL characters. |
| 482 | * If the string contains NUL characters, issue an error and return |
| 483 | * the C len, i.e. truncate at the NUL. |
| 484 | */ |
| 485 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 486 | { |
| 487 | size_t len = nasm_unquote(qstr, NULL); |
| 488 | size_t clen = strlen(qstr); |
| 489 | |
| 490 | if (len != clen) |
| 491 | error(ERR_NONFATAL, "NUL character in `%s' directive", |
| 492 | pp_directives[directive]); |
| 493 | |
| 494 | return clen; |
| 495 | } |
| 496 | |
| 497 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 498 | * In-place reverse a list of tokens. |
| 499 | */ |
| 500 | static Token *reverse_tokens(Token *t) |
| 501 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 502 | Token *prev = NULL; |
| 503 | Token *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 504 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 505 | while (t) { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 506 | next = t->next; |
| 507 | t->next = prev; |
| 508 | prev = t; |
| 509 | t = next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 510 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 511 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 512 | return prev; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 516 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 517 | * front of them. We do it here because I could not find any other |
| 518 | * place to do it for the moment, and it is a hack (ideally it would |
| 519 | * be nice to be able to use the NASM pre-processor to do it). |
| 520 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 521 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 522 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 523 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 524 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 525 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 526 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 527 | |
| 528 | /* Binary search for the directive name */ |
| 529 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 530 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 531 | q = nasm_skip_word(p); |
| 532 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 533 | if (len) { |
| 534 | oldchar = p[len]; |
| 535 | p[len] = 0; |
| 536 | while (j - i > 1) { |
| 537 | k = (j + i) / 2; |
| 538 | m = nasm_stricmp(p, tasm_directives[k]); |
| 539 | if (m == 0) { |
| 540 | /* We have found a directive, so jam a % in front of it |
| 541 | * so that NASM will then recognise it as one if it's own. |
| 542 | */ |
| 543 | p[len] = oldchar; |
| 544 | len = strlen(p); |
| 545 | oldline = line; |
| 546 | line = nasm_malloc(len + 2); |
| 547 | line[0] = '%'; |
| 548 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 549 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 550 | * NASM does not recognise IFDIFI, so we convert |
| 551 | * it to %if 0. This is not used in NASM |
| 552 | * compatible code, but does need to parse for the |
| 553 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 554 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 555 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 556 | } else { |
| 557 | memcpy(line + 1, p, len + 1); |
| 558 | } |
| 559 | nasm_free(oldline); |
| 560 | return line; |
| 561 | } else if (m < 0) { |
| 562 | j = k; |
| 563 | } else |
| 564 | i = k; |
| 565 | } |
| 566 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 567 | } |
| 568 | return line; |
| 569 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 570 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 571 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 572 | * The pre-preprocessing stage... This function translates line |
| 573 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 574 | * flags') into NASM preprocessor line number indications (`%line |
| 575 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 576 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 577 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 578 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 579 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 580 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 581 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 582 | if (line[0] == '#' && line[1] == ' ') { |
| 583 | oldline = line; |
| 584 | fname = oldline + 2; |
| 585 | lineno = atoi(fname); |
| 586 | fname += strspn(fname, "0123456789 "); |
| 587 | if (*fname == '"') |
| 588 | fname++; |
| 589 | fnlen = strcspn(fname, "\""); |
| 590 | line = nasm_malloc(20 + fnlen); |
| 591 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 592 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 593 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 594 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 595 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 596 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 600 | * Free a linked list of tokens. |
| 601 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 602 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 603 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 604 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 605 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Free a linked list of lines. |
| 610 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 611 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 612 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 613 | Line *l, *tmp; |
| 614 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 615 | free_tlist(l->first); |
| 616 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 621 | * Free an MMacro |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 622 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 623 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 624 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 625 | nasm_free(m->name); |
| 626 | free_tlist(m->dlist); |
| 627 | nasm_free(m->defaults); |
| 628 | free_llist(m->expansion); |
| 629 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 633 | * Free all currently defined macros, and free the hash tables |
| 634 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 635 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 636 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 637 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 638 | const char *key; |
| 639 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 640 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 641 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 642 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 643 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 644 | nasm_free(s->name); |
| 645 | free_tlist(s->expansion); |
| 646 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 647 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 648 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 649 | hash_free(smt); |
| 650 | } |
| 651 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 652 | static void free_mmacro_table(struct hash_table *mmt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 653 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 654 | MMacro *m, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 655 | const char *key; |
| 656 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 657 | |
| 658 | it = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 659 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 660 | nasm_free((void *)key); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 661 | list_for_each_safe(m ,tmp, m) |
| 662 | free_mmacro(m); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 663 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 664 | hash_free(mmt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static void free_macros(void) |
| 668 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 669 | free_smacro_table(&smacros); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 670 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Initialize the hash tables |
| 675 | */ |
| 676 | static void init_macros(void) |
| 677 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 678 | hash_init(&smacros, HASH_LARGE); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 679 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 683 | * Pop the context stack. |
| 684 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 685 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 686 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 687 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 688 | |
| 689 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 690 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 691 | nasm_free(c->name); |
| 692 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 693 | } |
| 694 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 695 | /* |
| 696 | * Search for a key in the hash index; adding it if necessary |
| 697 | * (in which case we initialize the data pointer to NULL.) |
| 698 | */ |
| 699 | static void ** |
| 700 | hash_findi_add(struct hash_table *hash, const char *str) |
| 701 | { |
| 702 | struct hash_insert hi; |
| 703 | void **r; |
| 704 | char *strx; |
| 705 | |
| 706 | r = hash_findi(hash, str, &hi); |
| 707 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 708 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 709 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 710 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 711 | return hash_add(&hi, strx, NULL); |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | * Like hash_findi, but returns the data element rather than a pointer |
| 716 | * to it. Used only when not adding a new element, hence no third |
| 717 | * argument. |
| 718 | */ |
| 719 | static void * |
| 720 | hash_findix(struct hash_table *hash, const char *str) |
| 721 | { |
| 722 | void **p; |
| 723 | |
| 724 | p = hash_findi(hash, str, NULL); |
| 725 | return p ? *p : NULL; |
| 726 | } |
| 727 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 728 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 729 | * read line from standart macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 730 | * if there no more left -- return NULL |
| 731 | */ |
| 732 | static char *line_from_stdmac(void) |
| 733 | { |
| 734 | unsigned char c; |
| 735 | const unsigned char *p = stdmacpos; |
| 736 | char *line, *q; |
| 737 | size_t len = 0; |
| 738 | |
| 739 | if (!stdmacpos) |
| 740 | return NULL; |
| 741 | |
| 742 | while ((c = *p++)) { |
| 743 | if (c >= 0x80) |
| 744 | len += pp_directives_len[c - 0x80] + 1; |
| 745 | else |
| 746 | len++; |
| 747 | } |
| 748 | |
| 749 | line = nasm_malloc(len + 1); |
| 750 | q = line; |
| 751 | while ((c = *stdmacpos++)) { |
| 752 | if (c >= 0x80) { |
| 753 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 754 | q += pp_directives_len[c - 0x80]; |
| 755 | *q++ = ' '; |
| 756 | } else { |
| 757 | *q++ = c; |
| 758 | } |
| 759 | } |
| 760 | stdmacpos = p; |
| 761 | *q = '\0'; |
| 762 | |
| 763 | if (!*stdmacpos) { |
| 764 | /* This was the last of the standard macro chain... */ |
| 765 | stdmacpos = NULL; |
| 766 | if (any_extrastdmac) { |
| 767 | stdmacpos = extrastdmac; |
| 768 | any_extrastdmac = false; |
| 769 | } else if (do_predef) { |
| 770 | Line *pd, *l; |
| 771 | Token *head, **tail, *t; |
| 772 | |
| 773 | /* |
| 774 | * Nasty hack: here we push the contents of |
| 775 | * `predef' on to the top-level expansion stack, |
| 776 | * since this is the most convenient way to |
| 777 | * implement the pre-include and pre-define |
| 778 | * features. |
| 779 | */ |
| 780 | list_for_each(pd, predef) { |
| 781 | head = NULL; |
| 782 | tail = &head; |
| 783 | list_for_each(t, pd->first) { |
| 784 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 785 | tail = &(*tail)->next; |
| 786 | } |
| 787 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 788 | l = nasm_malloc(sizeof(Line)); |
| 789 | l->next = istk->expansion; |
| 790 | l->first = head; |
| 791 | l->finishes = NULL; |
| 792 | |
| 793 | istk->expansion = l; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 794 | } |
| 795 | do_predef = false; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | return line; |
| 800 | } |
| 801 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 802 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 803 | { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 804 | unsigned int size, c, next; |
| 805 | const unsigned int delta = 512; |
| 806 | const unsigned int pad = 8; |
| 807 | unsigned int nr_cont = 0; |
| 808 | bool cont = false; |
| 809 | char *buffer, *p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 810 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 811 | /* Standart macros set (predefined) goes first */ |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 812 | p = line_from_stdmac(); |
| 813 | if (p) |
| 814 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 815 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 816 | size = delta; |
| 817 | p = buffer = nasm_malloc(size); |
| 818 | |
| 819 | for (;;) { |
| 820 | c = fgetc(istk->fp); |
| 821 | if ((int)(c) == EOF) { |
| 822 | p[0] = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 823 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 824 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 825 | |
| 826 | switch (c) { |
| 827 | case '\r': |
| 828 | next = fgetc(istk->fp); |
| 829 | if (next != '\n') |
| 830 | ungetc(next, istk->fp); |
| 831 | if (cont) { |
| 832 | cont = false; |
| 833 | continue; |
| 834 | } |
| 835 | break; |
| 836 | |
| 837 | case '\n': |
| 838 | if (cont) { |
| 839 | cont = false; |
| 840 | continue; |
| 841 | } |
| 842 | break; |
| 843 | |
| 844 | case '\\': |
| 845 | next = fgetc(istk->fp); |
| 846 | ungetc(next, istk->fp); |
Cyrill Gorcunov | 490f85e | 2012-12-27 20:02:17 +0400 | [diff] [blame] | 847 | if (next == '\r' || next == '\n') { |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 848 | cont = true; |
| 849 | nr_cont++; |
| 850 | continue; |
| 851 | } |
| 852 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 853 | } |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 854 | |
| 855 | if (c == '\r' || c == '\n') { |
| 856 | *p++ = 0; |
| 857 | break; |
| 858 | } |
| 859 | |
| 860 | if (p >= (buffer + size - pad)) { |
| 861 | buffer = nasm_realloc(buffer, size + delta); |
| 862 | p = buffer + size - pad; |
| 863 | size += delta; |
| 864 | } |
| 865 | |
| 866 | *p++ = (unsigned char)c; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 869 | if (p == buffer) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 870 | nasm_free(buffer); |
| 871 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 874 | src_set_linnum(src_get_linnum() + istk->lineinc + |
Cyrill Gorcunov | f1fe4fd | 2012-10-27 19:27:18 +0400 | [diff] [blame] | 875 | (nr_cont * istk->lineinc)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * Handle spurious ^Z, which may be inserted into source files |
| 879 | * by some file transfer utilities. |
| 880 | */ |
| 881 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 882 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 883 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 884 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 885 | return buffer; |
| 886 | } |
| 887 | |
| 888 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 889 | * Tokenize a line of text. This is a very simple process since we |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 890 | * don't need to parse the value out of e.g. numeric tokens: we |
| 891 | * simply split one string into many. |
| 892 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 893 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 894 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 895 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 896 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 897 | Token *list = NULL; |
| 898 | Token *t, **tail = &list; |
| 899 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 900 | while (*line) { |
| 901 | p = line; |
| 902 | if (*p == '%') { |
| 903 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 904 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 905 | p++; |
| 906 | type = TOK_PASTE; |
| 907 | } else if (nasm_isdigit(*p) || |
| 908 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 909 | do { |
| 910 | p++; |
| 911 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 912 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 913 | type = TOK_PREPROC_ID; |
| 914 | } else if (*p == '{') { |
| 915 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 916 | while (*p) { |
| 917 | if (*p == '}') |
| 918 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 919 | p[-1] = *p; |
| 920 | p++; |
| 921 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 922 | if (*p != '}') |
| 923 | error(ERR_WARNING | ERR_PASS1, "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) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 954 | error(ERR_NONFATAL, "unterminated %[ construct"); |
| 955 | type = TOK_INDIRECT; |
| 956 | } else if (*p == '?') { |
| 957 | type = TOK_PREPROC_Q; /* %? */ |
| 958 | p++; |
| 959 | if (*p == '?') { |
| 960 | type = TOK_PREPROC_QQ; /* %?? */ |
| 961 | p++; |
| 962 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 963 | } else if (*p == '!') { |
| 964 | type = TOK_PREPROC_ID; |
| 965 | p++; |
| 966 | if (isidchar(*p)) { |
| 967 | do { |
| 968 | p++; |
| 969 | } |
| 970 | while (isidchar(*p)); |
| 971 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 972 | p = nasm_skip_string(p); |
| 973 | if (*p) |
| 974 | p++; |
| 975 | else |
| 976 | error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string"); |
| 977 | } else { |
| 978 | /* %! without string or identifier */ |
| 979 | type = TOK_OTHER; /* Legacy behavior... */ |
| 980 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 981 | } else if (isidchar(*p) || |
| 982 | ((*p == '!' || *p == '%' || *p == '$') && |
| 983 | isidchar(p[1]))) { |
| 984 | do { |
| 985 | p++; |
| 986 | } |
| 987 | while (isidchar(*p)); |
| 988 | type = TOK_PREPROC_ID; |
| 989 | } else { |
| 990 | type = TOK_OTHER; |
| 991 | if (*p == '%') |
| 992 | p++; |
| 993 | } |
| 994 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 995 | type = TOK_ID; |
| 996 | p++; |
| 997 | while (*p && isidchar(*p)) |
| 998 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 999 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1000 | /* |
| 1001 | * A string token. |
| 1002 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1003 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1004 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1005 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1006 | if (*p) { |
| 1007 | p++; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1008 | } else { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1009 | error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1010 | /* Handling unterminated strings by UNV */ |
| 1011 | /* type = -1; */ |
| 1012 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1013 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1014 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1015 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1016 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1017 | bool is_hex = false; |
| 1018 | bool is_float = false; |
| 1019 | bool has_e = false; |
| 1020 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1021 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1022 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1023 | * A numeric token. |
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 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1026 | if (*p == '$') { |
| 1027 | p++; |
| 1028 | is_hex = true; |
| 1029 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1030 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1031 | for (;;) { |
| 1032 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1033 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1034 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1035 | has_e = true; |
| 1036 | if (*p == '+' || *p == '-') { |
| 1037 | /* |
| 1038 | * e can only be followed by +/- if it is either a |
| 1039 | * prefixed hex number or a floating-point number |
| 1040 | */ |
| 1041 | p++; |
| 1042 | is_float = true; |
| 1043 | } |
| 1044 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1045 | is_hex = true; |
| 1046 | } else if (c == 'P' || c == 'p') { |
| 1047 | is_float = true; |
| 1048 | if (*p == '+' || *p == '-') |
| 1049 | p++; |
| 1050 | } else if (isnumchar(c) || c == '_') |
| 1051 | ; /* just advance */ |
| 1052 | else if (c == '.') { |
| 1053 | /* |
| 1054 | * we need to deal with consequences of the legacy |
| 1055 | * parser, like "1.nolist" being two tokens |
| 1056 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1057 | * a shot for now. In the future, we probably need |
| 1058 | * a flex-based scanner with proper pattern matching |
| 1059 | * to do it as well as it can be done. Nothing in |
| 1060 | * the world is going to help the person who wants |
| 1061 | * 0x123.p16 interpreted as two tokens, though. |
| 1062 | */ |
| 1063 | r = p; |
| 1064 | while (*r == '_') |
| 1065 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1066 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1067 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1068 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1069 | (*r == 'p' || *r == 'P')) { |
| 1070 | p = r; |
| 1071 | is_float = true; |
| 1072 | } else |
| 1073 | break; /* Terminate the token */ |
| 1074 | } else |
| 1075 | break; |
| 1076 | } |
| 1077 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1078 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1079 | if (p == line+1 && *line == '$') { |
| 1080 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1081 | } else { |
| 1082 | if (has_e && !is_hex) { |
| 1083 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1084 | is_float = true; |
| 1085 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1086 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1087 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1088 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1089 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1090 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1091 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1092 | /* |
| 1093 | * Whitespace just before end-of-line is discarded by |
| 1094 | * pretending it's a comment; whitespace just before a |
| 1095 | * comment gets lumped into the comment. |
| 1096 | */ |
| 1097 | if (!*p || *p == ';') { |
| 1098 | type = TOK_COMMENT; |
| 1099 | while (*p) |
| 1100 | p++; |
| 1101 | } |
| 1102 | } else if (*p == ';') { |
| 1103 | type = TOK_COMMENT; |
| 1104 | while (*p) |
| 1105 | p++; |
| 1106 | } else { |
| 1107 | /* |
| 1108 | * Anything else is an operator of some kind. We check |
| 1109 | * for all the double-character operators (>>, <<, //, |
| 1110 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1111 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1112 | */ |
| 1113 | type = TOK_OTHER; |
| 1114 | if ((p[0] == '>' && p[1] == '>') || |
| 1115 | (p[0] == '<' && p[1] == '<') || |
| 1116 | (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++; |
| 1126 | } |
| 1127 | p++; |
| 1128 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1129 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1130 | /* Handling unterminated string by UNV */ |
| 1131 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1132 | { |
| 1133 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1134 | t->text[p-line] = *line; |
| 1135 | tail = &t->next; |
| 1136 | } |
| 1137 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1138 | if (type != TOK_COMMENT) { |
| 1139 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1140 | tail = &t->next; |
| 1141 | } |
| 1142 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1143 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1144 | return list; |
| 1145 | } |
| 1146 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1147 | /* |
| 1148 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1149 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1150 | * deleted only all at once by the delete_Blocks function. |
| 1151 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1152 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1153 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1154 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1155 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1156 | /* first, get to the end of the linked list */ |
| 1157 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1158 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1159 | /* now allocate the requested chunk */ |
| 1160 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1161 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1162 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | c31767c | 2014-06-28 02:22:17 +0400 | [diff] [blame^] | 1163 | b->next = nasm_zalloc(sizeof(Blocks)); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1164 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * this function deletes all managed blocks of memory |
| 1169 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1170 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1171 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1172 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1173 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1174 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1175 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1176 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1177 | * free it. |
| 1178 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1179 | while (b) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1180 | if (b->chunk) |
| 1181 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1182 | a = b; |
| 1183 | b = b->next; |
| 1184 | if (a != &blocks) |
| 1185 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1186 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1187 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1190 | * 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] | 1191 | * 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] | 1192 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1193 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1194 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1195 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1196 | { |
| 1197 | Token *t; |
| 1198 | int i; |
| 1199 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1200 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1201 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1202 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1203 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1204 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1205 | } |
| 1206 | t = freeTokens; |
| 1207 | freeTokens = t->next; |
| 1208 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1209 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1210 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1211 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1212 | t->text = NULL; |
| 1213 | } else { |
| 1214 | if (txtlen == 0) |
| 1215 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1216 | t->text = nasm_malloc(txtlen+1); |
| 1217 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1218 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1219 | } |
| 1220 | return t; |
| 1221 | } |
| 1222 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1223 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1224 | { |
| 1225 | Token *next = t->next; |
| 1226 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1227 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1228 | freeTokens = t; |
| 1229 | return next; |
| 1230 | } |
| 1231 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1232 | /* |
| 1233 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1234 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1235 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1236 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1237 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1238 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1239 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1240 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1241 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1242 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1243 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1244 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1245 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1246 | char *v; |
| 1247 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1248 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1249 | v = t->text + 2; |
| 1250 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1251 | size_t len = nasm_unquote(v, NULL); |
| 1252 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1253 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1254 | if (len != clen) { |
| 1255 | error(ERR_NONFATAL | ERR_PASS1, |
| 1256 | "NUL character in %! string"); |
| 1257 | v = NULL; |
| 1258 | } |
| 1259 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1260 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1261 | if (v) { |
| 1262 | char *p = getenv(v); |
| 1263 | if (!p) { |
| 1264 | error(ERR_NONFATAL | ERR_PASS1, |
| 1265 | "nonexistent environment variable `%s'", v); |
| 1266 | p = ""; |
| 1267 | } |
| 1268 | t->text = nasm_strdup(p); |
| 1269 | } |
| 1270 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1271 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1272 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1273 | /* Expand local macros here and not during preprocessing */ |
| 1274 | if (expand_locals && |
| 1275 | t->type == TOK_PREPROC_ID && t->text && |
| 1276 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1277 | const char *q; |
| 1278 | char *p; |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1279 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1280 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1281 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1282 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1283 | p = nasm_strcat(buffer, q); |
| 1284 | nasm_free(t->text); |
| 1285 | t->text = p; |
| 1286 | } |
| 1287 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1288 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1289 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1290 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1291 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1292 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1293 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1294 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1295 | |
| 1296 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1297 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1298 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1299 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1300 | q = t->text; |
| 1301 | while (*q) |
| 1302 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1303 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1304 | } |
| 1305 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1306 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1307 | return line; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1311 | * A scanner, suitable for use by the expression evaluator, which |
| 1312 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1313 | * the first token in the line to be passed in as its private_data |
| 1314 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1315 | * |
| 1316 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1317 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1318 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1319 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1320 | Token **tlineptr = private_data; |
| 1321 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1322 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1323 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1324 | do { |
| 1325 | tline = *tlineptr; |
| 1326 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1327 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1328 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1329 | |
| 1330 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1331 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1332 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1333 | tokval->t_charptr = tline->text; |
| 1334 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1335 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1336 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1337 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1338 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1339 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1340 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1341 | p = tokval->t_charptr = tline->text; |
| 1342 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1343 | tokval->t_charptr++; |
| 1344 | return tokval->t_type = TOKEN_ID; |
| 1345 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1346 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1347 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1348 | if (r >= p+MAX_KEYWORD) |
| 1349 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1350 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1351 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1352 | *s = '\0'; |
| 1353 | /* right, so we have an identifier sitting in temp storage. now, |
| 1354 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1355 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1358 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1359 | bool rn_error; |
| 1360 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1361 | tokval->t_charptr = tline->text; |
| 1362 | if (rn_error) |
| 1363 | return tokval->t_type = TOKEN_ERRNUM; |
| 1364 | else |
| 1365 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1366 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1367 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1368 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1369 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1372 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1373 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1374 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1375 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1376 | tokval->t_charptr = tline->text; |
| 1377 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1378 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1379 | if (ep[0] != bq || ep[1] != '\0') |
| 1380 | return tokval->t_type = TOKEN_ERRSTR; |
| 1381 | else |
| 1382 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1385 | if (tline->type == TOK_OTHER) { |
| 1386 | if (!strcmp(tline->text, "<<")) |
| 1387 | return tokval->t_type = TOKEN_SHL; |
| 1388 | if (!strcmp(tline->text, ">>")) |
| 1389 | return tokval->t_type = TOKEN_SHR; |
| 1390 | if (!strcmp(tline->text, "//")) |
| 1391 | return tokval->t_type = TOKEN_SDIV; |
| 1392 | if (!strcmp(tline->text, "%%")) |
| 1393 | return tokval->t_type = TOKEN_SMOD; |
| 1394 | if (!strcmp(tline->text, "==")) |
| 1395 | return tokval->t_type = TOKEN_EQ; |
| 1396 | if (!strcmp(tline->text, "<>")) |
| 1397 | return tokval->t_type = TOKEN_NE; |
| 1398 | if (!strcmp(tline->text, "!=")) |
| 1399 | return tokval->t_type = TOKEN_NE; |
| 1400 | if (!strcmp(tline->text, "<=")) |
| 1401 | return tokval->t_type = TOKEN_LE; |
| 1402 | if (!strcmp(tline->text, ">=")) |
| 1403 | return tokval->t_type = TOKEN_GE; |
| 1404 | if (!strcmp(tline->text, "&&")) |
| 1405 | return tokval->t_type = TOKEN_DBL_AND; |
| 1406 | if (!strcmp(tline->text, "^^")) |
| 1407 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1408 | if (!strcmp(tline->text, "||")) |
| 1409 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * We have no other options: just return the first character of |
| 1414 | * the token text. |
| 1415 | */ |
| 1416 | return tokval->t_type = tline->text[0]; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1420 | * Compare a string to the name of an existing macro; this is a |
| 1421 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1422 | * depending on the value of the `casesense' parameter. |
| 1423 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1424 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1425 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1426 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1430 | * Compare a string to the name of an existing macro; this is a |
| 1431 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1432 | * depending on the value of the `casesense' parameter. |
| 1433 | */ |
| 1434 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1435 | { |
| 1436 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1437 | } |
| 1438 | |
| 1439 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1440 | * Return the Context structure associated with a %$ token. Return |
| 1441 | * NULL, having _already_ reported an error condition, if the |
| 1442 | * context stack isn't deep enough for the supplied number of $ |
| 1443 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1444 | * |
| 1445 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1446 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1447 | */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1448 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1449 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1450 | Context *ctx; |
| 1451 | int i; |
| 1452 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1453 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1454 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1455 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1456 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1457 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1458 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1459 | if (!cstk) { |
| 1460 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
| 1461 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1464 | name += 2; |
| 1465 | ctx = cstk; |
| 1466 | i = 0; |
| 1467 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1468 | name++; |
| 1469 | i++; |
| 1470 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1471 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1472 | if (!ctx) { |
| 1473 | error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1474 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1475 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1476 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1477 | |
| 1478 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1479 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1480 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1481 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1485 | * Check to see if a file is already in a string list |
| 1486 | */ |
| 1487 | static bool in_list(const StrList *list, const char *str) |
| 1488 | { |
| 1489 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1490 | if (!strcmp(list->str, str)) |
| 1491 | return true; |
| 1492 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1493 | } |
| 1494 | return false; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1498 | * Open an include file. This routine must always return a valid |
| 1499 | * file pointer if it returns - it's responsible for throwing an |
| 1500 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1501 | * the include path one by one until it finds the file or reaches |
| 1502 | * the end of the path. |
| 1503 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1504 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1505 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1506 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1507 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1508 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1509 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1510 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1511 | size_t prefix_len = 0; |
| 1512 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1513 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1514 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1515 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1516 | memcpy(sl->str, prefix, prefix_len); |
| 1517 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1518 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1519 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1520 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1521 | **dtail = sl; |
| 1522 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1523 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1524 | nasm_free(sl); |
| 1525 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1526 | if (fp) |
| 1527 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1528 | if (!ip) { |
| 1529 | if (!missing_ok) |
| 1530 | break; |
| 1531 | prefix = NULL; |
| 1532 | } else { |
| 1533 | prefix = ip->path; |
| 1534 | ip = ip->next; |
| 1535 | } |
| 1536 | if (prefix) { |
| 1537 | prefix_len = strlen(prefix); |
| 1538 | } else { |
| 1539 | /* -MG given and file not found */ |
| 1540 | if (dhead && !in_list(*dhead, file)) { |
| 1541 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1542 | sl->next = NULL; |
| 1543 | strcpy(sl->str, file); |
| 1544 | **dtail = sl; |
| 1545 | *dtail = &sl->next; |
| 1546 | } |
| 1547 | return NULL; |
| 1548 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1549 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1550 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1551 | error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1552 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1556 | * 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] | 1557 | * 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] | 1558 | * return true if _any_ single-line macro of that name is defined. |
| 1559 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1560 | * `nparam' or no parameters is defined. |
| 1561 | * |
| 1562 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1563 | * defined, or nparam is -1, the address of the definition structure |
| 1564 | * 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] | 1565 | * is NULL, no action will be taken regarding its contents, and no |
| 1566 | * error will occur. |
| 1567 | * |
| 1568 | * Note that this is also called with nparam zero to resolve |
| 1569 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1570 | * |
| 1571 | * If you already know which context macro belongs to, you can pass |
| 1572 | * the context pointer as first parameter; if you won't but name begins |
| 1573 | * with %$ the context will be automatically computed. If all_contexts |
| 1574 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1575 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1576 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1577 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1578 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1579 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1580 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1581 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1582 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1583 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1584 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1585 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1586 | if (cstk) |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 1587 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1588 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1589 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1590 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1591 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1592 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1593 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1594 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1595 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1596 | while (m) { |
| 1597 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1598 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1599 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1600 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1601 | *defn = m; |
| 1602 | else |
| 1603 | *defn = NULL; |
| 1604 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1605 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1606 | } |
| 1607 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1608 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1609 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1610 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * Count and mark off the parameters in a multi-line macro call. |
| 1615 | * This is called both from within the multi-line macro expansion |
| 1616 | * code, and also to mark off the default parameters when provided |
| 1617 | * in a %macro definition line. |
| 1618 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1619 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1620 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1621 | int paramsize, brace; |
| 1622 | |
| 1623 | *nparam = paramsize = 0; |
| 1624 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1625 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1626 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1627 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1628 | paramsize += PARAM_DELTA; |
| 1629 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1630 | } |
| 1631 | skip_white_(t); |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1632 | brace = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1633 | if (tok_is_(t, "{")) |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1634 | brace++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1635 | (*params)[(*nparam)++] = t; |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1636 | if (brace) { |
| 1637 | while (brace && (t = t->next) != NULL) { |
| 1638 | if (tok_is_(t, "{")) |
| 1639 | brace++; |
| 1640 | else if (tok_is_(t, "}")) |
| 1641 | brace--; |
| 1642 | } |
| 1643 | |
| 1644 | if (t) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1645 | /* |
| 1646 | * Now we've found the closing brace, look further |
| 1647 | * for the comma. |
| 1648 | */ |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1649 | t = t->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1650 | skip_white_(t); |
| 1651 | if (tok_isnt_(t, ",")) { |
| 1652 | error(ERR_NONFATAL, |
| 1653 | "braces do not enclose all of macro parameter"); |
| 1654 | while (tok_isnt_(t, ",")) |
| 1655 | t = t->next; |
| 1656 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1657 | } |
Jin Kyu Song | 5eac14b | 2013-11-27 20:52:16 -0800 | [diff] [blame] | 1658 | } else { |
| 1659 | while (tok_isnt_(t, ",")) |
| 1660 | t = t->next; |
| 1661 | } |
| 1662 | if (t) { /* got a comma/brace */ |
| 1663 | t = t->next; /* eat the comma */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1664 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1669 | * Determine whether one of the various `if' conditions is true or |
| 1670 | * not. |
| 1671 | * |
| 1672 | * We must free the tline we get passed. |
| 1673 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1674 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1675 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1676 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1677 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1678 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1679 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1680 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1681 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1682 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1683 | |
| 1684 | origline = tline; |
| 1685 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1686 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1687 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1688 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1689 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1690 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1691 | if (!tline) |
| 1692 | break; |
| 1693 | if (tline->type != TOK_ID) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1694 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1695 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1696 | free_tlist(origline); |
| 1697 | return -1; |
| 1698 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1699 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1700 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1701 | tline = tline->next; |
| 1702 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1703 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1704 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1705 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1706 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1707 | while (tline) { |
| 1708 | skip_white_(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1709 | if (!tline || (tline->type != TOK_ID && |
| 1710 | (tline->type != TOK_PREPROC_ID || |
| 1711 | tline->text[1] != '$'))) { |
| 1712 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1713 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1714 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1715 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1716 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1717 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1718 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1719 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1720 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1721 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1722 | case PPC_IFENV: |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1723 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1724 | j = false; /* have we matched yet? */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1725 | while (tline) { |
| 1726 | skip_white_(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1727 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1728 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1729 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1730 | tline->text[1] != '!'))) { |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1731 | error(ERR_NONFATAL, |
| 1732 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1733 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1734 | goto fail; |
| 1735 | } |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 1736 | p = tline->text; |
| 1737 | if (tline->type == TOK_PREPROC_ID) |
| 1738 | p += 2; /* Skip leading %! */ |
| 1739 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1740 | nasm_unquote_cstr(p, ct); |
| 1741 | if (getenv(p)) |
| 1742 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1743 | tline = tline->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1744 | } |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1745 | break; |
| 1746 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1747 | case PPC_IFIDN: |
| 1748 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1749 | tline = expand_smacro(tline); |
| 1750 | t = tt = tline; |
| 1751 | while (tok_isnt_(tt, ",")) |
| 1752 | tt = tt->next; |
| 1753 | if (!tt) { |
| 1754 | error(ERR_NONFATAL, |
| 1755 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1756 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1757 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1758 | } |
| 1759 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1760 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1761 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1762 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1763 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1764 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1765 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1766 | } |
| 1767 | if (t->type == TOK_WHITESPACE) { |
| 1768 | t = t->next; |
| 1769 | continue; |
| 1770 | } |
| 1771 | if (tt->type == TOK_WHITESPACE) { |
| 1772 | tt = tt->next; |
| 1773 | continue; |
| 1774 | } |
| 1775 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1776 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1777 | break; |
| 1778 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1779 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1780 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1781 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1782 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1783 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1784 | if (l1 != l2) { |
| 1785 | j = false; |
| 1786 | break; |
| 1787 | } |
| 1788 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1789 | j = false; |
| 1790 | break; |
| 1791 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1792 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1793 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1794 | break; |
| 1795 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1796 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1797 | t = t->next; |
| 1798 | tt = tt->next; |
| 1799 | } |
| 1800 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1801 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1802 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1803 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1804 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1805 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1806 | bool found = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1807 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1808 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1809 | skip_white_(tline); |
| 1810 | tline = expand_id(tline); |
| 1811 | if (!tok_type_(tline, TOK_ID)) { |
| 1812 | error(ERR_NONFATAL, |
| 1813 | "`%s' expects a macro name", pp_directives[ct]); |
| 1814 | goto fail; |
| 1815 | } |
| 1816 | searching.name = nasm_strdup(tline->text); |
| 1817 | searching.casesense = true; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1818 | searching.plus = false; |
| 1819 | searching.nolist = false; |
| 1820 | searching.in_progress = 0; |
| 1821 | searching.max_depth = 0; |
| 1822 | searching.rep_nest = NULL; |
| 1823 | searching.nparam_min = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1824 | searching.nparam_max = INT_MAX; |
| 1825 | tline = expand_smacro(tline->next); |
| 1826 | skip_white_(tline); |
| 1827 | if (!tline) { |
| 1828 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 1829 | error(ERR_NONFATAL, |
| 1830 | "`%s' expects a parameter count or nothing", |
| 1831 | pp_directives[ct]); |
| 1832 | } else { |
| 1833 | searching.nparam_min = searching.nparam_max = |
| 1834 | readnum(tline->text, &j); |
| 1835 | if (j) |
| 1836 | error(ERR_NONFATAL, |
| 1837 | "unable to parse parameter count `%s'", |
| 1838 | tline->text); |
| 1839 | } |
| 1840 | if (tline && tok_is_(tline->next, "-")) { |
| 1841 | tline = tline->next->next; |
| 1842 | if (tok_is_(tline, "*")) |
| 1843 | searching.nparam_max = INT_MAX; |
| 1844 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 1845 | error(ERR_NONFATAL, |
| 1846 | "`%s' expects a parameter count after `-'", |
| 1847 | pp_directives[ct]); |
| 1848 | else { |
| 1849 | searching.nparam_max = readnum(tline->text, &j); |
| 1850 | if (j) |
| 1851 | error(ERR_NONFATAL, |
| 1852 | "unable to parse parameter count `%s'", |
| 1853 | tline->text); |
| 1854 | if (searching.nparam_min > searching.nparam_max) |
| 1855 | error(ERR_NONFATAL, |
| 1856 | "minimum parameter count exceeds maximum"); |
| 1857 | } |
| 1858 | } |
| 1859 | if (tline && tok_is_(tline->next, "+")) { |
| 1860 | tline = tline->next; |
| 1861 | searching.plus = true; |
| 1862 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1863 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
| 1864 | while (mmac) { |
| 1865 | if (!strcmp(mmac->name, searching.name) && |
| 1866 | (mmac->nparam_min <= searching.nparam_max |
| 1867 | || searching.plus) |
| 1868 | && (searching.nparam_min <= mmac->nparam_max |
| 1869 | || mmac->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1870 | found = true; |
| 1871 | break; |
| 1872 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1873 | mmac = mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1874 | } |
| 1875 | if (tline && tline->next) |
| 1876 | error(ERR_WARNING|ERR_PASS1, |
| 1877 | "trailing garbage after %%ifmacro ignored"); |
| 1878 | nasm_free(searching.name); |
| 1879 | j = found; |
| 1880 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1881 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1882 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1883 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1884 | needtype = TOK_ID; |
| 1885 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1886 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1887 | needtype = TOK_NUMBER; |
| 1888 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1889 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1890 | needtype = TOK_STRING; |
| 1891 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1892 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1893 | iftype: |
| 1894 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1895 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1896 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1897 | (needtype == TOK_NUMBER && |
| 1898 | tok_type_(t, TOK_OTHER) && |
| 1899 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1900 | !t->text[1])) |
| 1901 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1902 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1903 | j = tok_type_(t, needtype); |
| 1904 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1905 | |
| 1906 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1907 | t = tline = expand_smacro(tline); |
| 1908 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1909 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1910 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1911 | j = false; |
| 1912 | if (t) { |
| 1913 | t = t->next; /* Skip the actual token */ |
| 1914 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1915 | t = t->next; |
| 1916 | j = !t; /* Should be nothing left */ |
| 1917 | } |
| 1918 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1919 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1920 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1921 | t = tline = expand_smacro(tline); |
| 1922 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1923 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1924 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1925 | j = !t; /* Should be empty */ |
| 1926 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1927 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1928 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1929 | t = tline = expand_smacro(tline); |
| 1930 | tptr = &t; |
| 1931 | tokval.t_type = TOKEN_INVALID; |
| 1932 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 1933 | NULL, pass | CRITICAL, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1934 | if (!evalresult) |
| 1935 | return -1; |
| 1936 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1937 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1938 | "trailing garbage after expression ignored"); |
| 1939 | if (!is_simple(evalresult)) { |
| 1940 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1941 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1942 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1943 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1944 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1945 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 1946 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1947 | default: |
| 1948 | error(ERR_FATAL, |
| 1949 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1950 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1951 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1952 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1953 | |
| 1954 | free_tlist(origline); |
| 1955 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1956 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1957 | fail: |
| 1958 | free_tlist(origline); |
| 1959 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1963 | * Common code for defining an smacro |
| 1964 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1965 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1966 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1967 | { |
| 1968 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1969 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1970 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1971 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1972 | if (!smac) { |
| 1973 | error(ERR_WARNING|ERR_PASS1, |
| 1974 | "single-line macro `%s' defined both with and" |
| 1975 | " without parameters", mname); |
| 1976 | /* |
| 1977 | * Some instances of the old code considered this a failure, |
| 1978 | * some others didn't. What is the right thing to do here? |
| 1979 | */ |
| 1980 | free_tlist(expansion); |
| 1981 | return false; /* Failure */ |
| 1982 | } else { |
| 1983 | /* |
| 1984 | * We're redefining, so we have to take over an |
| 1985 | * existing SMacro structure. This means freeing |
| 1986 | * what was already in it. |
| 1987 | */ |
| 1988 | nasm_free(smac->name); |
| 1989 | free_tlist(smac->expansion); |
| 1990 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1991 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1992 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 1993 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 1994 | smac = nasm_malloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1995 | smac->next = *smhead; |
| 1996 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1997 | } |
| 1998 | smac->name = nasm_strdup(mname); |
| 1999 | smac->casesense = casesense; |
| 2000 | smac->nparam = nparam; |
| 2001 | smac->expansion = expansion; |
| 2002 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2003 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * Undefine an smacro |
| 2008 | */ |
| 2009 | static void undef_smacro(Context *ctx, const char *mname) |
| 2010 | { |
| 2011 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2012 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2013 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2014 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2015 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2016 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2017 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2018 | /* |
| 2019 | * We now have a macro name... go hunt for it. |
| 2020 | */ |
| 2021 | sp = smhead; |
| 2022 | while ((s = *sp) != NULL) { |
| 2023 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2024 | *sp = s->next; |
| 2025 | nasm_free(s->name); |
| 2026 | free_tlist(s->expansion); |
| 2027 | nasm_free(s); |
| 2028 | } else { |
| 2029 | sp = &s->next; |
| 2030 | } |
| 2031 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2032 | } |
| 2033 | } |
| 2034 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2035 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2036 | * Parse a mmacro specification. |
| 2037 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2038 | 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] | 2039 | { |
| 2040 | bool err; |
| 2041 | |
| 2042 | tline = tline->next; |
| 2043 | skip_white_(tline); |
| 2044 | tline = expand_id(tline); |
| 2045 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2046 | error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
| 2047 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2048 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2049 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2050 | def->prev = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2051 | def->name = nasm_strdup(tline->text); |
| 2052 | def->plus = false; |
| 2053 | def->nolist = false; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2054 | def->in_progress = 0; |
| 2055 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2056 | def->nparam_min = 0; |
| 2057 | def->nparam_max = 0; |
| 2058 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2059 | tline = expand_smacro(tline->next); |
| 2060 | skip_white_(tline); |
| 2061 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2062 | error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2063 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2064 | def->nparam_min = def->nparam_max = |
| 2065 | readnum(tline->text, &err); |
| 2066 | if (err) |
| 2067 | error(ERR_NONFATAL, |
| 2068 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2069 | } |
| 2070 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2071 | tline = tline->next->next; |
| 2072 | if (tok_is_(tline, "*")) { |
| 2073 | def->nparam_max = INT_MAX; |
| 2074 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2075 | error(ERR_NONFATAL, |
| 2076 | "`%s' expects a parameter count after `-'", directive); |
| 2077 | } else { |
| 2078 | def->nparam_max = readnum(tline->text, &err); |
| 2079 | if (err) { |
| 2080 | error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
| 2081 | tline->text); |
| 2082 | } |
| 2083 | if (def->nparam_min > def->nparam_max) { |
| 2084 | error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
| 2085 | } |
| 2086 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2087 | } |
| 2088 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2089 | tline = tline->next; |
| 2090 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2091 | } |
| 2092 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2093 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2094 | tline = tline->next; |
| 2095 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2096 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2097 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2098 | /* |
| 2099 | * Handle default parameters. |
| 2100 | */ |
| 2101 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2102 | def->dlist = tline->next; |
| 2103 | tline->next = NULL; |
| 2104 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2105 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2106 | def->dlist = NULL; |
| 2107 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2108 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2109 | def->expansion = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2110 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2111 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2112 | !def->plus) |
| 2113 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
| 2114 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2115 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2116 | return true; |
| 2117 | } |
| 2118 | |
| 2119 | |
| 2120 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2121 | * Decode a size directive |
| 2122 | */ |
| 2123 | static int parse_size(const char *str) { |
| 2124 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2125 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2126 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2128 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2129 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2130 | } |
| 2131 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2132 | /** |
| 2133 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2134 | * Find out if a line contains a preprocessor directive, and deal |
| 2135 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2136 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2137 | * If a directive _is_ found, it is the responsibility of this routine |
| 2138 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2139 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2140 | * @param tline a pointer to the current tokeninzed line linked list |
| 2141 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2142 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2143 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2144 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2145 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2146 | enum preproc_token i; |
| 2147 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2148 | bool err; |
| 2149 | int nparam; |
| 2150 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2151 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2152 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2153 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2154 | char *p, *pp; |
| 2155 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2156 | Include *inc; |
| 2157 | Context *ctx; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2158 | Cond *cond; |
| 2159 | MMacro *mmac, **mmhead; |
Jin Kyu Song | c9486b9 | 2013-10-28 17:07:57 -0700 | [diff] [blame] | 2160 | Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2161 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2162 | struct tokenval tokval; |
| 2163 | expr *evalresult; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2164 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2165 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2166 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2167 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2168 | |
| 2169 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2170 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2171 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2172 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2173 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2174 | || tline->text[1] == '!')) |
| 2175 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2176 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2177 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2178 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2179 | /* |
| 2180 | * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO |
| 2181 | * since they are known to be buggy at moment, we need to fix them |
| 2182 | * in future release (2.09-2.10) |
| 2183 | */ |
Philipp Kloke | b432f57 | 2013-03-31 12:01:23 +0200 | [diff] [blame] | 2184 | if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2185 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2186 | tline->text); |
| 2187 | return NO_DIRECTIVE_FOUND; |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | * If we're in a non-emitting branch of a condition construct, |
| 2192 | * or walking to the end of an already terminated %rep block, |
| 2193 | * we should ignore all directives except for condition |
| 2194 | * directives. |
| 2195 | */ |
| 2196 | if (((istk->conds && !emitting(istk->conds->state)) || |
| 2197 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2198 | return NO_DIRECTIVE_FOUND; |
| 2199 | } |
| 2200 | |
| 2201 | /* |
| 2202 | * If we're defining a macro or reading a %rep block, we should |
| 2203 | * ignore all directives except for %macro/%imacro (which nest), |
| 2204 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2205 | * If we're in a %rep block, another %rep nests, so should be let through. |
| 2206 | */ |
| 2207 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
| 2208 | i != PP_RMACRO && i != PP_IRMACRO && |
| 2209 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2210 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2211 | return NO_DIRECTIVE_FOUND; |
| 2212 | } |
| 2213 | |
| 2214 | if (defining) { |
| 2215 | if (i == PP_MACRO || i == PP_IMACRO || |
| 2216 | i == PP_RMACRO || i == PP_IRMACRO) { |
| 2217 | nested_mac_count++; |
| 2218 | return NO_DIRECTIVE_FOUND; |
| 2219 | } else if (nested_mac_count > 0) { |
| 2220 | if (i == PP_ENDMACRO) { |
| 2221 | nested_mac_count--; |
| 2222 | return NO_DIRECTIVE_FOUND; |
| 2223 | } |
| 2224 | } |
| 2225 | if (!defining->name) { |
| 2226 | if (i == PP_REP) { |
| 2227 | nested_rep_count++; |
| 2228 | return NO_DIRECTIVE_FOUND; |
| 2229 | } else if (nested_rep_count > 0) { |
| 2230 | if (i == PP_ENDREP) { |
| 2231 | nested_rep_count--; |
| 2232 | return NO_DIRECTIVE_FOUND; |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2238 | switch (i) { |
| 2239 | case PP_INVALID: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2240 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2241 | tline->text); |
| 2242 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2243 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2244 | case PP_STACKSIZE: |
| 2245 | /* Directive to tell NASM what the default stack size is. The |
| 2246 | * default is for a 16-bit stack, and this can be overriden with |
| 2247 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2248 | */ |
| 2249 | tline = tline->next; |
| 2250 | if (tline && tline->type == TOK_WHITESPACE) |
| 2251 | tline = tline->next; |
| 2252 | if (!tline || tline->type != TOK_ID) { |
| 2253 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 2254 | free_tlist(origline); |
| 2255 | return DIRECTIVE_FOUND; |
| 2256 | } |
| 2257 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2258 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2259 | StackSize = 4; |
| 2260 | StackPointer = "ebp"; |
| 2261 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2262 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2263 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2264 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2265 | StackSize = 8; |
| 2266 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2267 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2268 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2269 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2270 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2271 | * far function call. |
| 2272 | */ |
| 2273 | StackSize = 2; |
| 2274 | StackPointer = "bp"; |
| 2275 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2276 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2277 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2278 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2279 | * far function call. We don't support near functions. |
| 2280 | */ |
| 2281 | StackSize = 2; |
| 2282 | StackPointer = "bp"; |
| 2283 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2284 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2285 | } else { |
| 2286 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 2287 | free_tlist(origline); |
| 2288 | return DIRECTIVE_FOUND; |
| 2289 | } |
| 2290 | free_tlist(origline); |
| 2291 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2292 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2293 | case PP_ARG: |
| 2294 | /* TASM like ARG directive to define arguments to functions, in |
| 2295 | * the following form: |
| 2296 | * |
| 2297 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2298 | */ |
| 2299 | offset = ArgOffset; |
| 2300 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2301 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2302 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2303 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2304 | /* Find the argument name */ |
| 2305 | tline = tline->next; |
| 2306 | if (tline && tline->type == TOK_WHITESPACE) |
| 2307 | tline = tline->next; |
| 2308 | if (!tline || tline->type != TOK_ID) { |
| 2309 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 2310 | free_tlist(origline); |
| 2311 | return DIRECTIVE_FOUND; |
| 2312 | } |
| 2313 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2314 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2315 | /* Find the argument size type */ |
| 2316 | tline = tline->next; |
| 2317 | if (!tline || tline->type != TOK_OTHER |
| 2318 | || tline->text[0] != ':') { |
| 2319 | error(ERR_NONFATAL, |
| 2320 | "Syntax error processing `%%arg' directive"); |
| 2321 | free_tlist(origline); |
| 2322 | return DIRECTIVE_FOUND; |
| 2323 | } |
| 2324 | tline = tline->next; |
| 2325 | if (!tline || tline->type != TOK_ID) { |
| 2326 | error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
| 2327 | free_tlist(origline); |
| 2328 | return DIRECTIVE_FOUND; |
| 2329 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2330 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2331 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2332 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2333 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2334 | size = parse_size(tt->text); |
| 2335 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2336 | error(ERR_NONFATAL, |
| 2337 | "Invalid size type for `%%arg' missing directive"); |
| 2338 | free_tlist(tt); |
| 2339 | free_tlist(origline); |
| 2340 | return DIRECTIVE_FOUND; |
| 2341 | } |
| 2342 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2343 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2344 | /* Round up to even stack slots */ |
| 2345 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2346 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2347 | /* Now define the macro for the argument */ |
| 2348 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2349 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2350 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2351 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2352 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2353 | /* Move to the next argument in the list */ |
| 2354 | tline = tline->next; |
| 2355 | if (tline && tline->type == TOK_WHITESPACE) |
| 2356 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2357 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2358 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2359 | free_tlist(origline); |
| 2360 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2361 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2362 | case PP_LOCAL: |
| 2363 | /* TASM like LOCAL directive to define local variables for a |
| 2364 | * function, in the following form: |
| 2365 | * |
| 2366 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2367 | * |
| 2368 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2369 | * required by TASM to define the local parameter size (and used |
| 2370 | * by the TASM macro package). |
| 2371 | */ |
| 2372 | offset = LocalOffset; |
| 2373 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2374 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2375 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2376 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2377 | /* Find the argument name */ |
| 2378 | tline = tline->next; |
| 2379 | if (tline && tline->type == TOK_WHITESPACE) |
| 2380 | tline = tline->next; |
| 2381 | if (!tline || tline->type != TOK_ID) { |
| 2382 | error(ERR_NONFATAL, |
| 2383 | "`%%local' missing argument parameter"); |
| 2384 | free_tlist(origline); |
| 2385 | return DIRECTIVE_FOUND; |
| 2386 | } |
| 2387 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2388 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2389 | /* Find the argument size type */ |
| 2390 | tline = tline->next; |
| 2391 | if (!tline || tline->type != TOK_OTHER |
| 2392 | || tline->text[0] != ':') { |
| 2393 | error(ERR_NONFATAL, |
| 2394 | "Syntax error processing `%%local' directive"); |
| 2395 | free_tlist(origline); |
| 2396 | return DIRECTIVE_FOUND; |
| 2397 | } |
| 2398 | tline = tline->next; |
| 2399 | if (!tline || tline->type != TOK_ID) { |
| 2400 | error(ERR_NONFATAL, |
| 2401 | "`%%local' missing size type parameter"); |
| 2402 | free_tlist(origline); |
| 2403 | return DIRECTIVE_FOUND; |
| 2404 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2405 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2406 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2407 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2408 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2409 | size = parse_size(tt->text); |
| 2410 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2411 | error(ERR_NONFATAL, |
| 2412 | "Invalid size type for `%%local' missing directive"); |
| 2413 | free_tlist(tt); |
| 2414 | free_tlist(origline); |
| 2415 | return DIRECTIVE_FOUND; |
| 2416 | } |
| 2417 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2418 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2419 | /* Round up to even stack slots */ |
| 2420 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2421 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2422 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2423 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2424 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2425 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2426 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2427 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2428 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2429 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2430 | snprintf(directive, sizeof(directive), |
| 2431 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2432 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2433 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | /* Move to the next argument in the list */ |
| 2435 | tline = tline->next; |
| 2436 | if (tline && tline->type == TOK_WHITESPACE) |
| 2437 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2438 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2439 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2440 | free_tlist(origline); |
| 2441 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2442 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2443 | case PP_CLEAR: |
| 2444 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2445 | error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2446 | "trailing garbage after `%%clear' ignored"); |
| 2447 | free_macros(); |
| 2448 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2449 | free_tlist(origline); |
| 2450 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2451 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2452 | case PP_DEPEND: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2453 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2454 | skip_white_(t); |
| 2455 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2456 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2457 | error(ERR_NONFATAL, "`%%depend' expects a file name"); |
| 2458 | free_tlist(origline); |
| 2459 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2460 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2461 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2462 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2463 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2464 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2465 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2466 | nasm_unquote_cstr(p, i); |
| 2467 | if (dephead && !in_list(*dephead, p)) { |
| 2468 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2469 | sl->next = NULL; |
| 2470 | strcpy(sl->str, p); |
| 2471 | *deptail = sl; |
| 2472 | deptail = &sl->next; |
| 2473 | } |
| 2474 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2475 | return DIRECTIVE_FOUND; |
| 2476 | |
| 2477 | case PP_INCLUDE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2478 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2479 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2480 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2481 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2482 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2483 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2484 | free_tlist(origline); |
| 2485 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2486 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2487 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2488 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2489 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2490 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2491 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2492 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2493 | inc = nasm_malloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2494 | inc->next = istk; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2495 | inc->conds = NULL; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2496 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2497 | if (!inc->fp) { |
| 2498 | /* -MG given but file not found */ |
| 2499 | nasm_free(inc); |
| 2500 | } else { |
| 2501 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2502 | inc->lineno = src_set_linnum(0); |
| 2503 | inc->lineinc = 1; |
| 2504 | inc->expansion = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2505 | inc->mstk = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2506 | istk = inc; |
| 2507 | list->uplevel(LIST_INCLUDE); |
| 2508 | } |
| 2509 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2510 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2511 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2512 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2513 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2514 | static macros_t *use_pkg; |
| 2515 | const char *pkg_macro = NULL; |
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 | tline = tline->next; |
| 2518 | skip_white_(tline); |
| 2519 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2520 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2521 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2522 | tline->type != TOK_INTERNAL_STRING && |
| 2523 | tline->type != TOK_ID)) { |
H. Peter Anvin | 926fc40 | 2008-06-19 16:26:12 -0700 | [diff] [blame] | 2524 | error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2525 | free_tlist(origline); |
| 2526 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2527 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2528 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2529 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2530 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2531 | if (tline->type == TOK_STRING) |
| 2532 | nasm_unquote_cstr(tline->text, i); |
| 2533 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2534 | if (!use_pkg) |
| 2535 | error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
| 2536 | else |
| 2537 | 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] | 2538 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2539 | /* Not already included, go ahead and include it */ |
| 2540 | stdmacpos = use_pkg; |
| 2541 | } |
| 2542 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2543 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2544 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2545 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2546 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2547 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2548 | tline = tline->next; |
| 2549 | skip_white_(tline); |
| 2550 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2551 | if (tline) { |
| 2552 | if (!tok_type_(tline, TOK_ID)) { |
| 2553 | error(ERR_NONFATAL, "`%s' expects a context identifier", |
| 2554 | pp_directives[i]); |
| 2555 | free_tlist(origline); |
| 2556 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2557 | } |
| 2558 | if (tline->next) |
| 2559 | error(ERR_WARNING|ERR_PASS1, |
| 2560 | "trailing garbage after `%s' ignored", |
| 2561 | pp_directives[i]); |
| 2562 | p = nasm_strdup(tline->text); |
| 2563 | } else { |
| 2564 | p = NULL; /* Anonymous */ |
| 2565 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2566 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2567 | if (i == PP_PUSH) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2568 | ctx = nasm_malloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2569 | ctx->next = cstk; |
| 2570 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2571 | ctx->name = p; |
| 2572 | ctx->number = unique++; |
| 2573 | cstk = ctx; |
| 2574 | } else { |
| 2575 | /* %pop or %repl */ |
| 2576 | if (!cstk) { |
| 2577 | error(ERR_NONFATAL, "`%s': context stack is empty", |
| 2578 | pp_directives[i]); |
| 2579 | } else if (i == PP_POP) { |
| 2580 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
| 2581 | error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
| 2582 | "expected %s", |
| 2583 | cstk->name ? cstk->name : "anonymous", p); |
| 2584 | else |
| 2585 | ctx_pop(); |
| 2586 | } else { |
| 2587 | /* i == PP_REPL */ |
| 2588 | nasm_free(cstk->name); |
| 2589 | cstk->name = p; |
| 2590 | p = NULL; |
| 2591 | } |
| 2592 | nasm_free(p); |
| 2593 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2594 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2595 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2596 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2597 | severity = ERR_FATAL; |
| 2598 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2599 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2600 | severity = ERR_NONFATAL; |
| 2601 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2602 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2603 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2604 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2605 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2606 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2607 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | /* Only error out if this is the final pass */ |
| 2609 | if (pass != 2 && i != PP_FATAL) |
| 2610 | return DIRECTIVE_FOUND; |
| 2611 | |
| 2612 | tline->next = expand_smacro(tline->next); |
| 2613 | tline = tline->next; |
| 2614 | skip_white_(tline); |
| 2615 | t = tline ? tline->next : NULL; |
| 2616 | skip_white_(t); |
| 2617 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2618 | /* The line contains only a quoted string */ |
| 2619 | p = tline->text; |
| 2620 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
| 2621 | error(severity, "%s", p); |
| 2622 | } else { |
| 2623 | /* Not a quoted string, or more than a quoted string */ |
| 2624 | p = detoken(tline, false); |
| 2625 | error(severity, "%s", p); |
| 2626 | nasm_free(p); |
| 2627 | } |
| 2628 | free_tlist(origline); |
| 2629 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2630 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2631 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2632 | CASE_PP_IF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2633 | if (istk->conds && !emitting(istk->conds->state)) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2634 | j = COND_NEVER; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2635 | else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2636 | j = if_condition(tline->next, i); |
| 2637 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2638 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2639 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2640 | cond = nasm_malloc(sizeof(Cond)); |
| 2641 | cond->next = istk->conds; |
| 2642 | cond->state = j; |
| 2643 | istk->conds = cond; |
| 2644 | if(istk->mstk) |
| 2645 | istk->mstk->condcnt ++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2646 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2647 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2648 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2649 | CASE_PP_ELIF: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2650 | if (!istk->conds) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2651 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2652 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2653 | case COND_IF_TRUE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2654 | istk->conds->state = COND_DONE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2655 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2656 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2657 | case COND_DONE: |
| 2658 | case COND_NEVER: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2659 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2660 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2661 | case COND_ELSE_TRUE: |
| 2662 | case COND_ELSE_FALSE: |
| 2663 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2664 | "`%%elif' after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2665 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2666 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2667 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2668 | case COND_IF_FALSE: |
| 2669 | /* |
| 2670 | * IMPORTANT: In the case of %if, we will already have |
| 2671 | * called expand_mmac_params(); however, if we're |
| 2672 | * processing an %elif we must have been in a |
| 2673 | * non-emitting mode, which would have inhibited |
| 2674 | * the normal invocation of expand_mmac_params(). |
| 2675 | * Therefore, we have to do it explicitly here. |
| 2676 | */ |
| 2677 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2678 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2679 | istk->conds->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2680 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2681 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2682 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2683 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2684 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2685 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2686 | case PP_ELSE: |
| 2687 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2688 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2689 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2690 | if (!istk->conds) |
| 2691 | error(ERR_FATAL, "`%%else': no matching `%%if'"); |
| 2692 | switch(istk->conds->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2693 | case COND_IF_TRUE: |
| 2694 | case COND_DONE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2695 | istk->conds->state = COND_ELSE_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2696 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2697 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2698 | case COND_NEVER: |
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_IF_FALSE: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2702 | istk->conds->state = COND_ELSE_TRUE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2703 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2704 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2705 | case COND_ELSE_TRUE: |
| 2706 | case COND_ELSE_FALSE: |
| 2707 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2708 | "`%%else' after `%%else' ignored."); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2709 | istk->conds->state = COND_NEVER; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2710 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2711 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2712 | free_tlist(origline); |
| 2713 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2714 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2715 | case PP_ENDIF: |
| 2716 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2717 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2718 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2719 | if (!istk->conds) |
| 2720 | error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
| 2721 | cond = istk->conds; |
| 2722 | istk->conds = cond->next; |
| 2723 | nasm_free(cond); |
| 2724 | if(istk->mstk) |
| 2725 | istk->mstk->condcnt --; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2726 | free_tlist(origline); |
| 2727 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2728 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2729 | case PP_RMACRO: |
| 2730 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2731 | case PP_MACRO: |
| 2732 | case PP_IMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2733 | if (defining) { |
| 2734 | error(ERR_FATAL, "`%s': already defining a macro", |
| 2735 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2736 | return DIRECTIVE_FOUND; |
| 2737 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2738 | defining = nasm_malloc(sizeof(MMacro)); |
| 2739 | defining->max_depth = |
| 2740 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
| 2741 | defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2742 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2743 | nasm_free(defining); |
| 2744 | defining = NULL; |
| 2745 | return DIRECTIVE_FOUND; |
| 2746 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2747 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2748 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
| 2749 | while (mmac) { |
| 2750 | if (!strcmp(mmac->name, defining->name) && |
| 2751 | (mmac->nparam_min <= defining->nparam_max |
| 2752 | || defining->plus) |
| 2753 | && (defining->nparam_min <= mmac->nparam_max |
| 2754 | || mmac->plus)) { |
| 2755 | error(ERR_WARNING|ERR_PASS1, |
| 2756 | "redefining multi-line macro `%s'", defining->name); |
| 2757 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2758 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2759 | mmac = mmac->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2760 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2761 | free_tlist(origline); |
| 2762 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2763 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2764 | case PP_ENDM: |
| 2765 | case PP_ENDMACRO: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2766 | if (! (defining && defining->name)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2767 | error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
| 2768 | return DIRECTIVE_FOUND; |
| 2769 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2770 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
| 2771 | defining->next = *mmhead; |
| 2772 | *mmhead = defining; |
| 2773 | defining = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2774 | free_tlist(origline); |
| 2775 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2776 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2777 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2778 | /* |
| 2779 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2780 | * macro-end marker for a macro with a name. Then we |
| 2781 | * bypass all lines between exitmacro and endmacro. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2782 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2783 | list_for_each(l, istk->expansion) |
| 2784 | if (l->finishes && l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2785 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2786 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2787 | if (l) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2788 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2789 | * Remove all conditional entries relative to this |
| 2790 | * macro invocation. (safe to do in this context) |
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 | for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) { |
| 2793 | cond = istk->conds; |
| 2794 | istk->conds = cond->next; |
| 2795 | nasm_free(cond); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2796 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2797 | istk->expansion = l; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2798 | } else { |
| 2799 | error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
| 2800 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2801 | free_tlist(origline); |
| 2802 | return DIRECTIVE_FOUND; |
| 2803 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2804 | case PP_UNMACRO: |
| 2805 | case PP_UNIMACRO: |
| 2806 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2807 | MMacro **mmac_p; |
| 2808 | MMacro spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2809 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2810 | spec.casesense = (i == PP_UNMACRO); |
| 2811 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2812 | return DIRECTIVE_FOUND; |
| 2813 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2814 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2815 | while (mmac_p && *mmac_p) { |
| 2816 | mmac = *mmac_p; |
| 2817 | if (mmac->casesense == spec.casesense && |
| 2818 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2819 | mmac->nparam_min == spec.nparam_min && |
| 2820 | mmac->nparam_max == spec.nparam_max && |
| 2821 | mmac->plus == spec.plus) { |
| 2822 | *mmac_p = mmac->next; |
| 2823 | free_mmacro(mmac); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2824 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2825 | mmac_p = &mmac->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2826 | } |
| 2827 | } |
| 2828 | free_tlist(origline); |
| 2829 | free_tlist(spec.dlist); |
| 2830 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2831 | } |
| 2832 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2833 | case PP_ROTATE: |
| 2834 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2835 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2836 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2837 | free_tlist(origline); |
| 2838 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 2839 | return DIRECTIVE_FOUND; |
| 2840 | } |
| 2841 | t = expand_smacro(tline->next); |
| 2842 | tline->next = NULL; |
| 2843 | free_tlist(origline); |
| 2844 | tline = t; |
| 2845 | tptr = &t; |
| 2846 | tokval.t_type = TOKEN_INVALID; |
| 2847 | evalresult = |
| 2848 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2849 | free_tlist(tline); |
| 2850 | if (!evalresult) |
| 2851 | return DIRECTIVE_FOUND; |
| 2852 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2853 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2854 | "trailing garbage after expression ignored"); |
| 2855 | if (!is_simple(evalresult)) { |
| 2856 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
| 2857 | return DIRECTIVE_FOUND; |
| 2858 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2859 | mmac = istk->mstk; |
| 2860 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2861 | mmac = mmac->next_active; |
| 2862 | if (!mmac) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2863 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2864 | } else if (mmac->nparam == 0) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2865 | error(ERR_NONFATAL, |
| 2866 | "`%%rotate' invoked within macro without parameters"); |
| 2867 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2868 | int rotate = mmac->rotate + reloc_value(evalresult); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2869 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2870 | rotate %= (int)mmac->nparam; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2871 | if (rotate < 0) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2872 | rotate += mmac->nparam; |
| 2873 | |
| 2874 | mmac->rotate = rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2875 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2876 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2877 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2878 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2879 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2880 | do { |
| 2881 | tline = tline->next; |
| 2882 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2883 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2884 | if (tok_type_(tline, TOK_ID) && |
| 2885 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2886 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2887 | do { |
| 2888 | tline = tline->next; |
| 2889 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2890 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2891 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2892 | if (tline) { |
| 2893 | t = expand_smacro(tline); |
| 2894 | tptr = &t; |
| 2895 | tokval.t_type = TOKEN_INVALID; |
| 2896 | evalresult = |
| 2897 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2898 | if (!evalresult) { |
| 2899 | free_tlist(origline); |
| 2900 | return DIRECTIVE_FOUND; |
| 2901 | } |
| 2902 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2903 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2904 | "trailing garbage after expression ignored"); |
| 2905 | if (!is_simple(evalresult)) { |
| 2906 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 2907 | return DIRECTIVE_FOUND; |
| 2908 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2909 | count = reloc_value(evalresult); |
| 2910 | if (count >= REP_LIMIT) { |
Cyrill Gorcunov | 71f4f84 | 2010-08-09 20:17:17 +0400 | [diff] [blame] | 2911 | error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 2912 | count = 0; |
| 2913 | } else |
| 2914 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2915 | } else { |
| 2916 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2917 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2918 | } |
| 2919 | free_tlist(origline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2920 | |
| 2921 | tmp_defining = defining; |
| 2922 | defining = nasm_malloc(sizeof(MMacro)); |
| 2923 | defining->prev = NULL; |
| 2924 | defining->name = NULL; /* flags this macro as a %rep block */ |
| 2925 | defining->casesense = false; |
| 2926 | defining->plus = false; |
| 2927 | defining->nolist = nolist; |
| 2928 | defining->in_progress = count; |
| 2929 | defining->max_depth = 0; |
| 2930 | defining->nparam_min = defining->nparam_max = 0; |
| 2931 | defining->defaults = NULL; |
| 2932 | defining->dlist = NULL; |
| 2933 | defining->expansion = NULL; |
| 2934 | defining->next_active = istk->mstk; |
| 2935 | defining->rep_nest = tmp_defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2936 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2937 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2938 | case PP_ENDREP: |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2939 | if (!defining || defining->name) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2940 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
| 2941 | return DIRECTIVE_FOUND; |
| 2942 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2943 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2944 | /* |
| 2945 | * Now we have a "macro" defined - although it has no name |
| 2946 | * and we won't be entering it in the hash tables - we must |
| 2947 | * push a macro-end marker for it on to istk->expansion. |
| 2948 | * After that, it will take care of propagating itself (a |
| 2949 | * macro-end marker line for a macro which is really a %rep |
| 2950 | * block will cause the macro to be re-expanded, complete |
| 2951 | * with another macro-end marker to ensure the process |
| 2952 | * continues) until the whole expansion is forcibly removed |
| 2953 | * from istk->expansion by a %exitrep. |
| 2954 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2955 | l = nasm_malloc(sizeof(Line)); |
| 2956 | l->next = istk->expansion; |
| 2957 | l->finishes = defining; |
| 2958 | l->first = NULL; |
| 2959 | istk->expansion = l; |
| 2960 | |
| 2961 | istk->mstk = defining; |
| 2962 | |
| 2963 | list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 2964 | tmp_defining = defining; |
| 2965 | defining = defining->rep_nest; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2966 | free_tlist(origline); |
| 2967 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2968 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2969 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2970 | /* |
| 2971 | * We must search along istk->expansion until we hit a |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2972 | * macro-end marker for a macro with no name. Then we set |
| 2973 | * its `in_progress' flag to 0. |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2974 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2975 | list_for_each(l, istk->expansion) |
| 2976 | if (l->finishes && !l->finishes->name) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2977 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2978 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 2979 | if (l) |
| 2980 | l->finishes->in_progress = 1; |
| 2981 | else |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2982 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2983 | free_tlist(origline); |
| 2984 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2985 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2986 | case PP_XDEFINE: |
| 2987 | case PP_IXDEFINE: |
| 2988 | case PP_DEFINE: |
| 2989 | case PP_IDEFINE: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2990 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2991 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2992 | tline = tline->next; |
| 2993 | skip_white_(tline); |
| 2994 | tline = expand_id(tline); |
| 2995 | if (!tline || (tline->type != TOK_ID && |
| 2996 | (tline->type != TOK_PREPROC_ID || |
| 2997 | tline->text[1] != '$'))) { |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2998 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2999 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3000 | free_tlist(origline); |
| 3001 | return DIRECTIVE_FOUND; |
| 3002 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3003 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3004 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3005 | last = tline; |
| 3006 | param_start = tline = tline->next; |
| 3007 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3008 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3009 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3010 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3011 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3012 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3013 | if (tok_is_(tline, "(")) { |
| 3014 | /* |
| 3015 | * This macro has parameters. |
| 3016 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3017 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3018 | tline = tline->next; |
| 3019 | while (1) { |
| 3020 | skip_white_(tline); |
| 3021 | if (!tline) { |
| 3022 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 3023 | free_tlist(origline); |
| 3024 | return DIRECTIVE_FOUND; |
| 3025 | } |
| 3026 | if (tline->type != TOK_ID) { |
| 3027 | error(ERR_NONFATAL, |
| 3028 | "`%s': parameter identifier expected", |
| 3029 | tline->text); |
| 3030 | free_tlist(origline); |
| 3031 | return DIRECTIVE_FOUND; |
| 3032 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3033 | tline->type = TOK_SMAC_PARAM + nparam++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3034 | tline = tline->next; |
| 3035 | skip_white_(tline); |
| 3036 | if (tok_is_(tline, ",")) { |
| 3037 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3038 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3039 | if (!tok_is_(tline, ")")) { |
| 3040 | error(ERR_NONFATAL, |
| 3041 | "`)' expected to terminate macro template"); |
| 3042 | free_tlist(origline); |
| 3043 | return DIRECTIVE_FOUND; |
| 3044 | } |
| 3045 | break; |
| 3046 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3047 | } |
| 3048 | last = tline; |
| 3049 | tline = tline->next; |
| 3050 | } |
| 3051 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3052 | last = tline, tline = tline->next; |
| 3053 | macro_start = NULL; |
| 3054 | last->next = NULL; |
| 3055 | t = tline; |
| 3056 | while (t) { |
| 3057 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3058 | list_for_each(tt, param_start) |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3059 | if (tt->type >= TOK_SMAC_PARAM && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3060 | !strcmp(tt->text, t->text)) |
| 3061 | t->type = tt->type; |
| 3062 | } |
| 3063 | tt = t->next; |
| 3064 | t->next = macro_start; |
| 3065 | macro_start = t; |
| 3066 | t = tt; |
| 3067 | } |
| 3068 | /* |
| 3069 | * Good. We now have a macro name, a parameter count, and a |
| 3070 | * token list (in reverse order) for an expansion. We ought |
| 3071 | * to be OK just to create an SMacro, store it, and let |
| 3072 | * free_tlist have the rest of the line (which we have |
| 3073 | * carefully re-terminated after chopping off the expansion |
| 3074 | * from the end). |
| 3075 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3076 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3077 | free_tlist(origline); |
| 3078 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3079 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3080 | case PP_UNDEF: |
| 3081 | tline = tline->next; |
| 3082 | skip_white_(tline); |
| 3083 | tline = expand_id(tline); |
| 3084 | if (!tline || (tline->type != TOK_ID && |
| 3085 | (tline->type != TOK_PREPROC_ID || |
| 3086 | tline->text[1] != '$'))) { |
| 3087 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 3088 | free_tlist(origline); |
| 3089 | return DIRECTIVE_FOUND; |
| 3090 | } |
| 3091 | if (tline->next) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3092 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3093 | "trailing garbage after macro name ignored"); |
| 3094 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3095 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3096 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3097 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3098 | undef_smacro(ctx, mname); |
| 3099 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3100 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3101 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3102 | case PP_DEFSTR: |
| 3103 | case PP_IDEFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3104 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3105 | |
| 3106 | tline = tline->next; |
| 3107 | skip_white_(tline); |
| 3108 | tline = expand_id(tline); |
| 3109 | if (!tline || (tline->type != TOK_ID && |
| 3110 | (tline->type != TOK_PREPROC_ID || |
| 3111 | tline->text[1] != '$'))) { |
| 3112 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3113 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3114 | free_tlist(origline); |
| 3115 | return DIRECTIVE_FOUND; |
| 3116 | } |
| 3117 | |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3118 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3119 | last = tline; |
| 3120 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3121 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3122 | |
| 3123 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3124 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3125 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3126 | p = detoken(tline, false); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3127 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3128 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3129 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3130 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3131 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3132 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3133 | |
| 3134 | /* |
| 3135 | * We now have a macro name, an implicit parameter count of |
| 3136 | * zero, and a string token to use as an expansion. Create |
| 3137 | * and store an SMacro. |
| 3138 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3139 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3140 | free_tlist(origline); |
| 3141 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3142 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3143 | case PP_DEFTOK: |
| 3144 | case PP_IDEFTOK: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3145 | casesense = (i == PP_DEFTOK); |
| 3146 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3147 | tline = tline->next; |
| 3148 | skip_white_(tline); |
| 3149 | tline = expand_id(tline); |
| 3150 | if (!tline || (tline->type != TOK_ID && |
| 3151 | (tline->type != TOK_PREPROC_ID || |
| 3152 | tline->text[1] != '$'))) { |
| 3153 | error(ERR_NONFATAL, |
| 3154 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3155 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3156 | free_tlist(origline); |
| 3157 | return DIRECTIVE_FOUND; |
| 3158 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3159 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3160 | last = tline; |
| 3161 | tline = expand_smacro(tline->next); |
| 3162 | last->next = NULL; |
| 3163 | |
| 3164 | t = tline; |
| 3165 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3166 | t = t->next; |
| 3167 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3168 | if (!tok_type_(t, TOK_STRING)) { |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3169 | error(ERR_NONFATAL, |
| 3170 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3171 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3172 | free_tlist(tline); |
| 3173 | free_tlist(origline); |
| 3174 | return DIRECTIVE_FOUND; |
| 3175 | } |
| 3176 | |
Cyrill Gorcunov | 4d8dbd9 | 2014-06-28 10:15:18 +0400 | [diff] [blame] | 3177 | /* |
| 3178 | * Convert the string to a token stream. Note that smacros |
| 3179 | * are stored with the token stream reversed, so we have to |
| 3180 | * reverse the output of tokenize(). |
| 3181 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3182 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3183 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3184 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3185 | /* |
| 3186 | * We now have a macro name, an implicit parameter count of |
| 3187 | * zero, and a numeric token to use as an expansion. Create |
| 3188 | * and store an SMacro. |
| 3189 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3190 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3191 | free_tlist(tline); |
| 3192 | free_tlist(origline); |
| 3193 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3194 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3195 | case PP_PATHSEARCH: |
| 3196 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3197 | FILE *fp; |
| 3198 | StrList *xsl = NULL; |
| 3199 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3200 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3201 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3202 | |
| 3203 | tline = tline->next; |
| 3204 | skip_white_(tline); |
| 3205 | tline = expand_id(tline); |
| 3206 | if (!tline || (tline->type != TOK_ID && |
| 3207 | (tline->type != TOK_PREPROC_ID || |
| 3208 | tline->text[1] != '$'))) { |
| 3209 | error(ERR_NONFATAL, |
| 3210 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3211 | free_tlist(origline); |
| 3212 | return DIRECTIVE_FOUND; |
| 3213 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3214 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3215 | last = tline; |
| 3216 | tline = expand_smacro(tline->next); |
| 3217 | last->next = NULL; |
| 3218 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3219 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3220 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3221 | t = t->next; |
| 3222 | |
| 3223 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3224 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3225 | error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3226 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3227 | free_tlist(origline); |
| 3228 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3229 | } |
| 3230 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3231 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3232 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3233 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3234 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3235 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3236 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3237 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3238 | if (fp) { |
| 3239 | p = xsl->str; |
| 3240 | fclose(fp); /* Don't actually care about the file */ |
| 3241 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3242 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3243 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3244 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3245 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3246 | macro_start->a.mac = NULL; |
| 3247 | if (xsl) |
| 3248 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3249 | |
| 3250 | /* |
| 3251 | * We now have a macro name, an implicit parameter count of |
| 3252 | * zero, and a string token to use as an expansion. Create |
| 3253 | * and store an SMacro. |
| 3254 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3255 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3256 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3257 | free_tlist(origline); |
| 3258 | return DIRECTIVE_FOUND; |
| 3259 | } |
| 3260 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3261 | case PP_STRLEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3262 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3263 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3264 | tline = tline->next; |
| 3265 | skip_white_(tline); |
| 3266 | tline = expand_id(tline); |
| 3267 | if (!tline || (tline->type != TOK_ID && |
| 3268 | (tline->type != TOK_PREPROC_ID || |
| 3269 | tline->text[1] != '$'))) { |
| 3270 | error(ERR_NONFATAL, |
| 3271 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3272 | free_tlist(origline); |
| 3273 | return DIRECTIVE_FOUND; |
| 3274 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3275 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3276 | last = tline; |
| 3277 | tline = expand_smacro(tline->next); |
| 3278 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3279 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3280 | t = tline; |
| 3281 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3282 | t = t->next; |
| 3283 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3284 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3285 | error(ERR_NONFATAL, |
| 3286 | "`%%strlen` requires string as second parameter"); |
| 3287 | free_tlist(tline); |
| 3288 | free_tlist(origline); |
| 3289 | return DIRECTIVE_FOUND; |
| 3290 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3291 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3292 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3293 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3294 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3295 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3296 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3297 | /* |
| 3298 | * We now have a macro name, an implicit parameter count of |
| 3299 | * zero, and a numeric token to use as an expansion. Create |
| 3300 | * and store an SMacro. |
| 3301 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3302 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3303 | free_tlist(tline); |
| 3304 | free_tlist(origline); |
| 3305 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3306 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3307 | case PP_STRCAT: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3308 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3309 | |
| 3310 | tline = tline->next; |
| 3311 | skip_white_(tline); |
| 3312 | tline = expand_id(tline); |
| 3313 | if (!tline || (tline->type != TOK_ID && |
| 3314 | (tline->type != TOK_PREPROC_ID || |
| 3315 | tline->text[1] != '$'))) { |
| 3316 | error(ERR_NONFATAL, |
| 3317 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3318 | free_tlist(origline); |
| 3319 | return DIRECTIVE_FOUND; |
| 3320 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3321 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3322 | last = tline; |
| 3323 | tline = expand_smacro(tline->next); |
| 3324 | last->next = NULL; |
| 3325 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3326 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3327 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3328 | switch (t->type) { |
| 3329 | case TOK_WHITESPACE: |
| 3330 | break; |
| 3331 | case TOK_STRING: |
| 3332 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3333 | break; |
| 3334 | case TOK_OTHER: |
| 3335 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3336 | break; |
| 3337 | /* else fall through */ |
| 3338 | default: |
| 3339 | error(ERR_NONFATAL, |
| 3340 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3341 | free_tlist(tline); |
| 3342 | free_tlist(origline); |
| 3343 | return DIRECTIVE_FOUND; |
| 3344 | } |
| 3345 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3346 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3347 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3348 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3349 | if (t->type == TOK_STRING) { |
| 3350 | memcpy(p, t->text, t->a.len); |
| 3351 | p += t->a.len; |
| 3352 | } |
| 3353 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3354 | |
| 3355 | /* |
| 3356 | * We now have a macro name, an implicit parameter count of |
| 3357 | * zero, and a numeric token to use as an expansion. Create |
| 3358 | * and store an SMacro. |
| 3359 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3360 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3361 | macro_start->text = nasm_quote(pp, len); |
| 3362 | nasm_free(pp); |
| 3363 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3364 | free_tlist(tline); |
| 3365 | free_tlist(origline); |
| 3366 | return DIRECTIVE_FOUND; |
| 3367 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3368 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3369 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3370 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3371 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3372 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3373 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3374 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3375 | tline = tline->next; |
| 3376 | skip_white_(tline); |
| 3377 | tline = expand_id(tline); |
| 3378 | if (!tline || (tline->type != TOK_ID && |
| 3379 | (tline->type != TOK_PREPROC_ID || |
| 3380 | tline->text[1] != '$'))) { |
| 3381 | error(ERR_NONFATAL, |
| 3382 | "`%%substr' expects a macro identifier as first parameter"); |
| 3383 | free_tlist(origline); |
| 3384 | return DIRECTIVE_FOUND; |
| 3385 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3386 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3387 | last = tline; |
| 3388 | tline = expand_smacro(tline->next); |
| 3389 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3390 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3391 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3392 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3393 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3394 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3395 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3396 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3397 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3398 | error(ERR_NONFATAL, |
| 3399 | "`%%substr` requires string as second parameter"); |
| 3400 | free_tlist(tline); |
| 3401 | free_tlist(origline); |
| 3402 | return DIRECTIVE_FOUND; |
| 3403 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3404 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3405 | tt = t->next; |
| 3406 | tptr = &tt; |
| 3407 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3408 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3409 | pass, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3410 | if (!evalresult) { |
| 3411 | free_tlist(tline); |
| 3412 | free_tlist(origline); |
| 3413 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3414 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3415 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3416 | free_tlist(tline); |
| 3417 | free_tlist(origline); |
| 3418 | return DIRECTIVE_FOUND; |
| 3419 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3420 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3421 | |
| 3422 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3423 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3424 | if (!tt) { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3425 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3426 | } else { |
| 3427 | tokval.t_type = TOKEN_INVALID; |
| 3428 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3429 | pass, error, NULL); |
| 3430 | if (!evalresult) { |
| 3431 | free_tlist(tline); |
| 3432 | free_tlist(origline); |
| 3433 | return DIRECTIVE_FOUND; |
| 3434 | } else if (!is_simple(evalresult)) { |
| 3435 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3436 | free_tlist(tline); |
| 3437 | free_tlist(origline); |
| 3438 | return DIRECTIVE_FOUND; |
| 3439 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3440 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3441 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3442 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3443 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3444 | |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3445 | /* make start and count being in range */ |
| 3446 | if (start < 0) |
| 3447 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3448 | if (count < 0) |
| 3449 | count = len + count + 1 - start; |
| 3450 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3451 | count = len - start; |
| 3452 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3453 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3454 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3455 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3456 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3457 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3458 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 3459 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3460 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3461 | /* |
| 3462 | * We now have a macro name, an implicit parameter count of |
| 3463 | * zero, and a numeric token to use as an expansion. Create |
| 3464 | * and store an SMacro. |
| 3465 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3466 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3467 | free_tlist(tline); |
| 3468 | free_tlist(origline); |
| 3469 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3470 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3471 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3472 | case PP_ASSIGN: |
| 3473 | case PP_IASSIGN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3474 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3475 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3476 | tline = tline->next; |
| 3477 | skip_white_(tline); |
| 3478 | tline = expand_id(tline); |
| 3479 | if (!tline || (tline->type != TOK_ID && |
| 3480 | (tline->type != TOK_PREPROC_ID || |
| 3481 | tline->text[1] != '$'))) { |
| 3482 | error(ERR_NONFATAL, |
| 3483 | "`%%%sassign' expects a macro identifier", |
| 3484 | (i == PP_IASSIGN ? "i" : "")); |
| 3485 | free_tlist(origline); |
| 3486 | return DIRECTIVE_FOUND; |
| 3487 | } |
Cyrill Gorcunov | 1a42fb2 | 2012-03-11 11:38:47 +0400 | [diff] [blame] | 3488 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3489 | last = tline; |
| 3490 | tline = expand_smacro(tline->next); |
| 3491 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3492 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3493 | t = tline; |
| 3494 | tptr = &t; |
| 3495 | tokval.t_type = TOKEN_INVALID; |
| 3496 | evalresult = |
| 3497 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 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 | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3505 | 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)) { |
| 3509 | error(ERR_NONFATAL, |
| 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)) { |
| 3537 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 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)) { |
| 3547 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 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: |
| 3564 | error(ERR_FATAL, |
| 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) |
| 3651 | error(ERR_FATAL, "No lvalue found on pasting"); |
| 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: |
| 3843 | error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
| 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 | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3881 | 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) { |
| 3912 | error(ERR_NONFATAL, |
| 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) { |
| 3919 | error(ERR_NONFATAL, |
| 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) { |
| 3938 | error(ERR_NONFATAL, |
| 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) { |
| 4080 | 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) { |
| 4187 | error(ERR_NONFATAL, |
| 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; |
| 4237 | error(ERR_NONFATAL, "braces do not " |
| 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 | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4249 | 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) { |
| 4487 | error(ERR_WARNING, |
| 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 | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4539 | 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) |
| 4675 | 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 | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4773 | list->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 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4778 | /* The function that actually does the error reporting */ |
| 4779 | static void verror(int severity, const char *fmt, va_list arg) |
| 4780 | { |
| 4781 | char buff[1024]; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4782 | MMacro *mmac = NULL; |
| 4783 | int delta = 0; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4784 | |
| 4785 | vsnprintf(buff, sizeof(buff), fmt, arg); |
| 4786 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4787 | /* get %macro name */ |
| 4788 | if (istk && istk->mstk) { |
| 4789 | mmac = istk->mstk; |
| 4790 | /* but %rep blocks should be skipped */ |
| 4791 | while (mmac && !mmac->name) |
| 4792 | mmac = mmac->next_active, delta++; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4793 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4794 | |
| 4795 | if (mmac) |
| 4796 | nasm_error(severity, "(%s:%d) %s", |
| 4797 | mmac->name, mmac->lineno - delta, buff); |
| 4798 | else |
| 4799 | nasm_error(severity, "%s", buff); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4800 | } |
| 4801 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4802 | /* |
| 4803 | * Since preprocessor always operate only on the line that didn't |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4804 | * arrived yet, we should always use ERR_OFFBY1. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4805 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4806 | static void error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4807 | { |
| 4808 | va_list arg; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4809 | |
| 4810 | /* If we're in a dead branch of IF or something like it, ignore the error */ |
| 4811 | if (istk && istk->conds && !emitting(istk->conds->state)) |
| 4812 | return; |
| 4813 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4814 | va_start(arg, fmt); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4815 | verror(severity, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4816 | va_end(arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4817 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4818 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4819 | /* |
| 4820 | * Because %else etc are evaluated in the state context |
| 4821 | * of the previous branch, errors might get lost with error(): |
| 4822 | * %if 0 ... %else trailing garbage ... %endif |
| 4823 | * So %else etc should report errors with this function. |
| 4824 | */ |
| 4825 | static void error_precond(int severity, const char *fmt, ...) |
| 4826 | { |
| 4827 | va_list arg; |
| 4828 | |
| 4829 | /* Only ignore the error if it's really in a dead branch */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4830 | if (istk && istk->conds && istk->conds->state == COND_NEVER) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4831 | return; |
| 4832 | |
| 4833 | va_start(arg, fmt); |
| 4834 | verror(severity, fmt, arg); |
| 4835 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4838 | static void |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 4839 | pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4840 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4841 | Token *t; |
| 4842 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4843 | cstk = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4844 | istk = nasm_malloc(sizeof(Include)); |
| 4845 | istk->next = NULL; |
| 4846 | istk->conds = NULL; |
| 4847 | istk->expansion = NULL; |
| 4848 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4849 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4850 | istk->fname = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4851 | src_set_fname(nasm_strdup(file)); |
| 4852 | src_set_linnum(0); |
| 4853 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4854 | if (!istk->fp) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4855 | error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4856 | file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4857 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4858 | nested_mac_count = 0; |
| 4859 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4860 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4861 | unique = 0; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4862 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4863 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4864 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4865 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4866 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4867 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 4868 | do_predef = true; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4869 | list = listgen; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4870 | |
| 4871 | /* |
| 4872 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4873 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4874 | * we can set __PASS__ accordingly. |
| 4875 | */ |
| 4876 | pass = apass > 2 ? 2 : apass; |
| 4877 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4878 | dephead = deptail = deplist; |
| 4879 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4880 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 4881 | sl->next = NULL; |
| 4882 | strcpy(sl->str, file); |
| 4883 | *deptail = sl; |
| 4884 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4885 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4886 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4887 | /* |
| 4888 | * Define the __PASS__ macro. This is defined here unlike |
| 4889 | * all the other builtins, because it is special -- it varies between |
| 4890 | * passes. |
| 4891 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4892 | t = nasm_malloc(sizeof(*t)); |
| 4893 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4894 | make_tok_num(t, apass); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4895 | t->a.mac = NULL; |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4896 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4899 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4900 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4901 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4902 | Token *tline; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4903 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4904 | while (1) { |
| 4905 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4906 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4907 | * buffer or from the input file. |
| 4908 | */ |
| 4909 | tline = NULL; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4910 | while (istk->expansion && istk->expansion->finishes) { |
| 4911 | Line *l = istk->expansion; |
| 4912 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 4913 | Line *ll; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4914 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4915 | /* |
| 4916 | * This is a macro-end marker for a macro with no |
| 4917 | * name, which means it's not really a macro at all |
| 4918 | * but a %rep block, and the `in_progress' field is |
| 4919 | * more than 1, meaning that we still need to |
| 4920 | * repeat. (1 means the natural last repetition; 0 |
| 4921 | * means termination by %exitrep.) We have |
| 4922 | * therefore expanded up to the %endrep, and must |
| 4923 | * push the whole block on to the expansion buffer |
| 4924 | * again. We don't bother to remove the macro-end |
| 4925 | * marker: we'd only have to generate another one |
| 4926 | * if we did. |
| 4927 | */ |
| 4928 | l->finishes->in_progress--; |
| 4929 | list_for_each(l, l->finishes->expansion) { |
| 4930 | Token *t, *tt, **tail; |
| 4931 | |
| 4932 | ll = nasm_malloc(sizeof(Line)); |
| 4933 | ll->next = istk->expansion; |
| 4934 | ll->finishes = NULL; |
| 4935 | ll->first = NULL; |
| 4936 | tail = &ll->first; |
| 4937 | |
| 4938 | list_for_each(t, l->first) { |
| 4939 | if (t->text || t->type == TOK_WHITESPACE) { |
| 4940 | tt = *tail = new_Token(NULL, t->type, t->text, 0); |
| 4941 | tail = &tt->next; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4942 | } |
| 4943 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4944 | |
| 4945 | istk->expansion = ll; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4946 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4947 | } else { |
| 4948 | /* |
| 4949 | * Check whether a `%rep' was started and not ended |
| 4950 | * within this macro expansion. This can happen and |
| 4951 | * should be detected. It's a fatal error because |
| 4952 | * I'm too confused to work out how to recover |
| 4953 | * sensibly from it. |
| 4954 | */ |
| 4955 | if (defining) { |
| 4956 | if (defining->name) |
| 4957 | error(ERR_PANIC, |
| 4958 | "defining with name in expansion"); |
| 4959 | else if (istk->mstk->name) |
| 4960 | error(ERR_FATAL, |
| 4961 | "`%%rep' without `%%endrep' within" |
| 4962 | " expansion of macro `%s'", |
| 4963 | istk->mstk->name); |
| 4964 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4965 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 4966 | /* |
| 4967 | * FIXME: investigate the relationship at this point between |
| 4968 | * istk->mstk and l->finishes |
| 4969 | */ |
| 4970 | { |
| 4971 | MMacro *m = istk->mstk; |
| 4972 | istk->mstk = m->next_active; |
| 4973 | if (m->name) { |
| 4974 | /* |
| 4975 | * This was a real macro call, not a %rep, and |
| 4976 | * therefore the parameter information needs to |
| 4977 | * be freed. |
| 4978 | */ |
| 4979 | if (m->prev) { |
| 4980 | pop_mmacro(m); |
| 4981 | l->finishes->in_progress --; |
| 4982 | } else { |
| 4983 | nasm_free(m->params); |
| 4984 | free_tlist(m->iline); |
| 4985 | nasm_free(m->paramlen); |
| 4986 | l->finishes->in_progress = 0; |
| 4987 | } |
| 4988 | } else |
| 4989 | free_mmacro(m); |
| 4990 | } |
| 4991 | istk->expansion = l->next; |
| 4992 | nasm_free(l); |
| 4993 | list->downlevel(LIST_MACRO); |
| 4994 | } |
| 4995 | } |
| 4996 | while (1) { /* until we get a line we can use */ |
| 4997 | |
| 4998 | if (istk->expansion) { /* from a macro expansion */ |
| 4999 | char *p; |
| 5000 | Line *l = istk->expansion; |
| 5001 | if (istk->mstk) |
| 5002 | istk->mstk->lineno++; |
| 5003 | tline = l->first; |
| 5004 | istk->expansion = l->next; |
| 5005 | nasm_free(l); |
| 5006 | p = detoken(tline, false); |
| 5007 | list->line(LIST_MACRO, p); |
| 5008 | nasm_free(p); |
| 5009 | break; |
| 5010 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5011 | line = read_line(); |
| 5012 | if (line) { /* from the current input file */ |
| 5013 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5014 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5015 | nasm_free(line); |
| 5016 | break; |
| 5017 | } |
| 5018 | /* |
| 5019 | * The current file has ended; work down the istk |
| 5020 | */ |
| 5021 | { |
| 5022 | Include *i = istk; |
| 5023 | fclose(i->fp); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5024 | if (i->conds) { |
| 5025 | /* nasm_error can't be conditionally suppressed */ |
| 5026 | nasm_error(ERR_FATAL, |
| 5027 | "expected `%%endif' before end of file"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5028 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5029 | /* only set line and file name if there's a next node */ |
| 5030 | if (i->next) { |
| 5031 | src_set_linnum(i->lineno); |
Cyrill Gorcunov | d34a108 | 2011-03-07 11:23:08 +0300 | [diff] [blame] | 5032 | nasm_free(src_set_fname(nasm_strdup(i->fname))); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5033 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5034 | istk = i->next; |
| 5035 | list->downlevel(LIST_INCLUDE); |
| 5036 | nasm_free(i); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5037 | if (!istk) |
| 5038 | return NULL; |
| 5039 | if (istk->expansion && istk->expansion->finishes) |
| 5040 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5041 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5042 | } |
| 5043 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5044 | /* |
| 5045 | * We must expand MMacro parameters and MMacro-local labels |
| 5046 | * _before_ we plunge into directive processing, to cope |
| 5047 | * with things like `%define something %1' such as STRUC |
| 5048 | * uses. Unless we're _defining_ a MMacro, in which case |
| 5049 | * those tokens should be left alone to go into the |
| 5050 | * definition; and unless we're in a non-emitting |
| 5051 | * condition, in which case we don't want to meddle with |
| 5052 | * anything. |
| 5053 | */ |
| 5054 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
| 5055 | && !(istk->mstk && !istk->mstk->in_progress)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5056 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5057 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5058 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5059 | /* |
| 5060 | * Check the line to see if it's a preprocessor directive. |
| 5061 | */ |
| 5062 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5063 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5064 | } else if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5065 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5066 | * We're defining a multi-line macro. We emit nothing |
| 5067 | * at all, and just |
| 5068 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5069 | */ |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5070 | Line *l = nasm_malloc(sizeof(Line)); |
| 5071 | l->next = defining->expansion; |
| 5072 | l->first = tline; |
| 5073 | l->finishes = NULL; |
| 5074 | defining->expansion = l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5075 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5076 | } else if (istk->conds && !emitting(istk->conds->state)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5077 | /* |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5078 | * We're in a non-emitting branch of a condition block. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5079 | * Emit nothing at all, not even a blank line: when we |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5080 | * emerge from the condition we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5081 | * directive so we keep our place correctly. |
| 5082 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5083 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5084 | continue; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5085 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 5086 | /* |
| 5087 | * We're in a %rep block which has been terminated, so |
| 5088 | * we're walking through to the %endrep without |
| 5089 | * emitting anything. Emit nothing at all, not even a |
| 5090 | * blank line: when we emerge from the %rep block we'll |
| 5091 | * give a line-number directive so we keep our place |
| 5092 | * correctly. |
| 5093 | */ |
| 5094 | free_tlist(tline); |
| 5095 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5096 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5097 | tline = expand_smacro(tline); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5098 | if (!expand_mmacro(tline)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5099 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5100 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5101 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5102 | line = detoken(tline, true); |
| 5103 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5104 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5105 | } else { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5106 | continue; /* expand_mmacro calls free_tlist */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5107 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5108 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5109 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5110 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5111 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5114 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5115 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5116 | if (defining) { |
| 5117 | if (defining->name) { |
| 5118 | error(ERR_NONFATAL, |
| 5119 | "end of file while still defining macro `%s'", |
| 5120 | defining->name); |
| 5121 | } else { |
| 5122 | error(ERR_NONFATAL, "end of file while still in %%rep"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5123 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5124 | |
| 5125 | free_mmacro(defining); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5126 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5127 | } |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5128 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5129 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5130 | free_macros(); |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5131 | while (istk) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5132 | Include *i = istk; |
| 5133 | istk = istk->next; |
| 5134 | fclose(i->fp); |
| 5135 | nasm_free(i->fname); |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5136 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5137 | } |
| 5138 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5139 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5140 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5141 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5142 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5143 | free_llist(predef); |
| 5144 | delete_Blocks(); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5145 | while ((i = ipath)) { |
| 5146 | ipath = i->next; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5147 | if (i->path) |
| 5148 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5149 | nasm_free(i); |
| 5150 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5151 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5154 | static void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5155 | { |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5156 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5157 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5158 | i = nasm_malloc(sizeof(IncPath)); |
| 5159 | i->path = path ? nasm_strdup(path) : NULL; |
| 5160 | i->next = NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5161 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5162 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5163 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5164 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5165 | j = j->next; |
| 5166 | j->next = i; |
| 5167 | } else { |
| 5168 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5169 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5170 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5171 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5172 | static void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5173 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5174 | Token *inc, *space, *name; |
| 5175 | Line *l; |
| 5176 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5177 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5178 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5179 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5180 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5181 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5182 | l->next = predef; |
| 5183 | l->first = inc; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5184 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5185 | predef = l; |
| 5186 | } |
| 5187 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5188 | static void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5189 | { |
| 5190 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5191 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5192 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5193 | |
| 5194 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5195 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5196 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5197 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5198 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5199 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5200 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5201 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5202 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5203 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5204 | l->next = predef; |
| 5205 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5206 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5207 | predef = l; |
| 5208 | } |
| 5209 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5210 | static void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5211 | { |
| 5212 | Token *def, *space; |
| 5213 | Line *l; |
| 5214 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5215 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5216 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5217 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5218 | |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5219 | l = nasm_malloc(sizeof(Line)); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5220 | l->next = predef; |
| 5221 | l->first = def; |
H. Peter Anvin | 36206cd | 2012-03-03 16:14:51 -0800 | [diff] [blame] | 5222 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5223 | predef = l; |
| 5224 | } |
| 5225 | |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5226 | static void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5227 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5228 | extrastdmac = macros; |
| 5229 | } |
| 5230 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5231 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5232 | { |
Cyrill Gorcunov | ce65274 | 2013-05-06 23:43:43 +0400 | [diff] [blame] | 5233 | char numbuf[32]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5234 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5235 | tok->text = nasm_strdup(numbuf); |
| 5236 | tok->type = TOK_NUMBER; |
| 5237 | } |
| 5238 | |
Cyrill Gorcunov | 86b2ad0 | 2011-07-01 10:38:25 +0400 | [diff] [blame] | 5239 | struct preproc_ops nasmpp = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5240 | pp_reset, |
| 5241 | pp_getline, |
Cyrill Gorcunov | 0b78bff | 2012-05-07 01:57:55 +0400 | [diff] [blame] | 5242 | pp_cleanup, |
| 5243 | pp_extra_stdmac, |
| 5244 | pp_pre_define, |
| 5245 | pp_pre_undefine, |
| 5246 | pp_pre_include, |
| 5247 | pp_include_path |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5248 | }; |