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 | * |
| 3 | * Copyright 1996-2010 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; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 85 | typedef struct ExpDef ExpDef; |
| 86 | typedef struct ExpInv ExpInv; |
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; |
| 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 | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 108 | SMacro *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 109 | char *name; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 110 | bool casesense; |
H. Peter Anvin | 16ed438 | 2007-10-11 10:06:19 -0700 | [diff] [blame] | 111 | bool in_progress; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 112 | unsigned int nparam; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 113 | Token *expansion; |
| 114 | }; |
| 115 | |
| 116 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 117 | * The context stack is composed of a linked list of these. |
| 118 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 119 | struct Context { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 120 | Context *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 121 | char *name; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 122 | struct hash_table localmac; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 123 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * This is the internal form which we break input lines up into. |
| 128 | * Typically stored in linked lists. |
| 129 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 130 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 131 | * necessarily used as-is, but is intended to denote the number of |
| 132 | * the substituted parameter. So in the definition |
| 133 | * |
| 134 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 135 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 136 | * the token representing `x' will have its type changed to |
| 137 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 138 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 139 | * |
| 140 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 141 | * which doesn't need quotes around it. Used in the pre-include |
| 142 | * mechanism as an alternative to trying to find a sensible type of |
| 143 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 144 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 145 | enum pp_token_type { |
| 146 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 147 | TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 148 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
| 149 | TOK_INTERNAL_STRING, |
| 150 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 151 | TOK_PASTE, /* %+ */ |
| 152 | TOK_INDIRECT, /* %[...] */ |
| 153 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 154 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 157 | #define PP_CONCAT_MASK(x) (1 << (x)) |
| 158 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 159 | struct tokseq_match { |
| 160 | int mask_head; |
| 161 | int mask_tail; |
| 162 | }; |
| 163 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 164 | struct Token { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 165 | Token *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 166 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 167 | union { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 168 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 169 | size_t len; /* scratch length field */ |
| 170 | } a; /* Auxiliary data */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 171 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 175 | * Expansion definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 176 | * these, which is essentially a container to allow several linked |
| 177 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 178 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 179 | * Note that in this module, linked lists are treated as stacks |
| 180 | * wherever possible. For this reason, Lines are _pushed_ on to the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 181 | * `last' field in ExpDef structures, so that the linked list, |
| 182 | * if walked, would emit the expansion lines in the proper order. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 183 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 184 | struct Line { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 185 | Line *next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 186 | Token *first; |
| 187 | }; |
| 188 | |
| 189 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 190 | * Expansion Types |
| 191 | */ |
| 192 | enum pp_exp_type { |
| 193 | EXP_NONE = 0, EXP_PREDEF, |
| 194 | EXP_MMACRO, EXP_REP, |
| 195 | EXP_IF, EXP_WHILE, |
| 196 | EXP_COMMENT, EXP_FINAL, |
| 197 | EXP_MAX = INT_MAX /* Keep compiler from reducing the range */ |
| 198 | }; |
| 199 | |
| 200 | /* |
| 201 | * Store the definition of an expansion, in which is any |
| 202 | * preprocessor directive that has an ending pair. |
| 203 | * |
| 204 | * This design allows for arbitrary expansion/recursion depth, |
| 205 | * upto the DEADMAN_LIMIT. |
| 206 | * |
| 207 | * The `next' field is used for storing ExpDef in hash tables; the |
| 208 | * `prev' field is for the global `expansions` linked-list. |
| 209 | */ |
| 210 | struct ExpDef { |
| 211 | ExpDef *prev; /* previous definition */ |
| 212 | ExpDef *next; /* next in hash table */ |
| 213 | enum pp_exp_type type; /* expansion type */ |
| 214 | char *name; /* definition name */ |
| 215 | int nparam_min, nparam_max; |
| 216 | bool casesense; |
| 217 | bool plus; /* is the last parameter greedy? */ |
| 218 | bool nolist; /* is this expansion listing-inhibited? */ |
| 219 | Token *dlist; /* all defaults as one list */ |
| 220 | Token **defaults; /* parameter default pointers */ |
| 221 | int ndefs; /* number of default parameters */ |
| 222 | |
| 223 | int prepend; /* label prepend state */ |
| 224 | Line *label; |
| 225 | Line *line; |
| 226 | Line *last; |
| 227 | int linecount; /* number of lines within expansion */ |
| 228 | |
| 229 | int64_t def_depth; /* current number of definition pairs deep */ |
| 230 | int64_t cur_depth; /* current number of expansions */ |
| 231 | int64_t max_depth; /* maximum number of expansions allowed */ |
| 232 | |
| 233 | int state; /* condition state */ |
| 234 | bool ignoring; /* ignoring definition lines */ |
| 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * Store the invocation of an expansion. |
| 239 | * |
| 240 | * The `prev' field is for the `istk->expansion` linked-list. |
| 241 | * |
| 242 | * When an expansion is being expanded, `params', `iline', `nparam', |
| 243 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 244 | */ |
| 245 | struct ExpInv { |
| 246 | ExpInv *prev; /* previous invocation */ |
| 247 | enum pp_exp_type type; /* expansion type */ |
| 248 | ExpDef *def; /* pointer to expansion definition */ |
| 249 | char *name; /* invocation name */ |
| 250 | Line *label; /* pointer to label */ |
| 251 | char *label_text; /* pointer to label text */ |
| 252 | Line *current; /* pointer to current line in invocation */ |
| 253 | |
| 254 | Token **params; /* actual parameters */ |
| 255 | Token *iline; /* invocation line */ |
| 256 | unsigned int nparam, rotate; |
| 257 | int *paramlen; |
| 258 | |
| 259 | uint64_t unique; |
| 260 | bool emitting; |
| 261 | int lineno; /* current line number in expansion */ |
| 262 | int linnum; /* line number at invocation */ |
| 263 | int relno; /* relative line number at invocation */ |
| 264 | }; |
| 265 | |
| 266 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 267 | * To handle an arbitrary level of file inclusion, we maintain a |
| 268 | * stack (ie linked list) of these things. |
| 269 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 270 | struct Include { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 271 | Include *next; |
| 272 | FILE *fp; |
| 273 | Cond *conds; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 274 | ExpInv *expansion; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 275 | char *fname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 276 | int lineno, lineinc; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 277 | int mmac_depth; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 281 | * Include search path. This is simply a list of strings which get |
| 282 | * prepended, in turn, to the name of an include file, in an |
| 283 | * attempt to find the file if it's not in the current directory. |
| 284 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 285 | struct IncPath { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 286 | IncPath *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 287 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 291 | * Conditional assembly: we maintain a separate stack of these for |
| 292 | * each level of file inclusion. (The only reason we keep the |
| 293 | * stacks separate is to ensure that a stray `%endif' in a file |
| 294 | * included from within the true branch of a `%if' won't terminate |
| 295 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 296 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 297 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 298 | /* |
| 299 | * These states are for use just after %if or %elif: IF_TRUE |
| 300 | * means the condition has evaluated to truth so we are |
| 301 | * currently emitting, whereas IF_FALSE means we are not |
| 302 | * currently emitting but will start doing so if a %else comes |
| 303 | * up. In these states, all directives are admissible: %elif, |
| 304 | * %else and %endif. (And of course %if.) |
| 305 | */ |
| 306 | COND_IF_TRUE, COND_IF_FALSE, |
| 307 | /* |
| 308 | * These states come up after a %else: ELSE_TRUE means we're |
| 309 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 310 | * any %elif or %else will cause an error. |
| 311 | */ |
| 312 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
| 313 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 314 | * These states mean that we're not emitting now, and also that |
| 315 | * nothing until %endif will be emitted at all. COND_DONE is |
| 316 | * used when we've had our moment of emission |
| 317 | * and have now started seeing %elifs. COND_NEVER is used when |
| 318 | * the condition construct in question is contained within a |
| 319 | * non-emitting branch of a larger condition construct, |
| 320 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 321 | */ |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 322 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 323 | }; |
| 324 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
| 325 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 326 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 327 | * These defines are used as the possible return values for do_directive |
| 328 | */ |
| 329 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 330 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 331 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 332 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 333 | * This define sets the upper limit for smacro and 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 | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 363 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 364 | * conditional compilation directives. Using the NASM pre-processor does |
| 365 | * not work, so we look for them specifically from the following list and |
| 366 | * then jam in the equivalent NASM directive into the input stream. |
| 367 | */ |
| 368 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 369 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 370 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 371 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 372 | }; |
| 373 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 374 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 375 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 376 | "ifndef", "include", "local" |
| 377 | }; |
| 378 | |
| 379 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 380 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 381 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 382 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 383 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 384 | static Context *cstk; |
| 385 | static Include *istk; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 386 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 387 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 388 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 389 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 390 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 391 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 392 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 393 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 394 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 395 | |
| 396 | static ListGen *list; |
| 397 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 398 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 399 | * The current set of expansion definitions we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 400 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 401 | static struct hash_table expdefs; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * The current set of single-line macros we have defined. |
| 405 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 406 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 409 | * Linked List of all active expansion definitions |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 410 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 411 | struct ExpDef *expansions = NULL; |
| 412 | |
| 413 | /* |
| 414 | * The expansion we are currently defining |
| 415 | */ |
| 416 | static ExpDef *defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 417 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 418 | static uint64_t nested_mac_count; |
| 419 | static uint64_t nested_rep_count; |
| 420 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 421 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 422 | * Linked-list of lines to preprocess, prior to cleanup |
| 423 | */ |
| 424 | static Line *finals = NULL; |
| 425 | static bool in_final = false; |
| 426 | |
| 427 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 428 | * The number of macro parameters to allocate space for at a time. |
| 429 | */ |
| 430 | #define PARAM_DELTA 16 |
| 431 | |
| 432 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 433 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 434 | * 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] | 435 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 436 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 437 | |
| 438 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 439 | * The extra standard macros that come from the object format, if |
| 440 | * any. |
| 441 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 442 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 443 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 444 | |
| 445 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 446 | * Tokens are allocated in blocks to improve speed |
| 447 | */ |
| 448 | #define TOKEN_BLOCKSIZE 4096 |
| 449 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 450 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 451 | Blocks *next; |
| 452 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 456 | |
| 457 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 458 | * Forward declarations. |
| 459 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 460 | static Token *expand_mmac_params(Token * tline); |
| 461 | static Token *expand_smacro(Token * tline); |
| 462 | static Token *expand_id(Token * tline); |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 463 | static Context *get_ctx(const char *name, const char **namep, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 464 | bool all_contexts); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 465 | static void make_tok_num(Token * tok, int64_t val); |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 466 | static void error(int severity, const char *fmt, ...); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 467 | static void error_precond(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 468 | static void *new_Block(size_t size); |
| 469 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 470 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 471 | const char *text, int txtlen); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 472 | static Token *copy_Token(Token * tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 473 | static Token *delete_Token(Token * t); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 474 | static Line *new_Line(void); |
| 475 | static ExpDef *new_ExpDef(int exp_type); |
| 476 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 480 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 481 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 482 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 483 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 484 | #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] | 485 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 486 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 487 | * nasm_unquote with error if the string contains NUL characters. |
| 488 | * If the string contains NUL characters, issue an error and return |
| 489 | * the C len, i.e. truncate at the NUL. |
| 490 | */ |
| 491 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 492 | { |
| 493 | size_t len = nasm_unquote(qstr, NULL); |
| 494 | size_t clen = strlen(qstr); |
| 495 | |
| 496 | if (len != clen) |
| 497 | error(ERR_NONFATAL, "NUL character in `%s' directive", |
| 498 | pp_directives[directive]); |
| 499 | |
| 500 | return clen; |
| 501 | } |
| 502 | |
| 503 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 504 | * In-place reverse a list of tokens. |
| 505 | */ |
| 506 | static Token *reverse_tokens(Token *t) |
| 507 | { |
| 508 | Token *prev = NULL; |
| 509 | Token *next; |
| 510 | |
| 511 | while (t) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 512 | next = t->next; |
| 513 | t->next = prev; |
| 514 | prev = t; |
| 515 | t = next; |
| 516 | } |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 517 | |
| 518 | return prev; |
| 519 | } |
| 520 | |
| 521 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 522 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 523 | * front of them. We do it here because I could not find any other |
| 524 | * place to do it for the moment, and it is a hack (ideally it would |
| 525 | * be nice to be able to use the NASM pre-processor to do it). |
| 526 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 527 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 528 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 529 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 530 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 531 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 532 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 533 | |
| 534 | /* Binary search for the directive name */ |
| 535 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 536 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 537 | q = nasm_skip_word(p); |
| 538 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 539 | if (len) { |
| 540 | oldchar = p[len]; |
| 541 | p[len] = 0; |
| 542 | while (j - i > 1) { |
| 543 | k = (j + i) / 2; |
| 544 | m = nasm_stricmp(p, tasm_directives[k]); |
| 545 | if (m == 0) { |
| 546 | /* We have found a directive, so jam a % in front of it |
| 547 | * so that NASM will then recognise it as one if it's own. |
| 548 | */ |
| 549 | p[len] = oldchar; |
| 550 | len = strlen(p); |
| 551 | oldline = line; |
| 552 | line = nasm_malloc(len + 2); |
| 553 | line[0] = '%'; |
| 554 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 555 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 556 | * NASM does not recognise IFDIFI, so we convert |
| 557 | * it to %if 0. This is not used in NASM |
| 558 | * compatible code, but does need to parse for the |
| 559 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 560 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 561 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 562 | } else { |
| 563 | memcpy(line + 1, p, len + 1); |
| 564 | } |
| 565 | nasm_free(oldline); |
| 566 | return line; |
| 567 | } else if (m < 0) { |
| 568 | j = k; |
| 569 | } else |
| 570 | i = k; |
| 571 | } |
| 572 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 573 | } |
| 574 | return line; |
| 575 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 576 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 577 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 578 | * The pre-preprocessing stage... This function translates line |
| 579 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 580 | * flags') into NASM preprocessor line number indications (`%line |
| 581 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 582 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 583 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 584 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 585 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 586 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 587 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 588 | if (line[0] == '#' && line[1] == ' ') { |
| 589 | oldline = line; |
| 590 | fname = oldline + 2; |
| 591 | lineno = atoi(fname); |
| 592 | fname += strspn(fname, "0123456789 "); |
| 593 | if (*fname == '"') |
| 594 | fname++; |
| 595 | fnlen = strcspn(fname, "\""); |
| 596 | line = nasm_malloc(20 + fnlen); |
| 597 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 598 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 599 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 600 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 601 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 602 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 606 | * Free a linked list of tokens. |
| 607 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 608 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 609 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 610 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 611 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Free a linked list of lines. |
| 616 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 617 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 618 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 619 | Line *l, *tmp; |
| 620 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 621 | free_tlist(l->first); |
| 622 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 627 | * Free an ExpDef |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 628 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 629 | static void free_expdef(ExpDef * ed) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 630 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 631 | nasm_free(ed->name); |
| 632 | free_tlist(ed->dlist); |
| 633 | nasm_free(ed->defaults); |
| 634 | free_llist(ed->line); |
| 635 | nasm_free(ed); |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Free an ExpInv |
| 640 | */ |
| 641 | static void free_expinv(ExpInv * ei) |
| 642 | { |
| 643 | if (ei->name != NULL) |
| 644 | nasm_free(ei->name); |
| 645 | if (ei->label_text != NULL) |
| 646 | nasm_free(ei->label_text); |
| 647 | nasm_free(ei); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 651 | * Free all currently defined macros, and free the hash tables |
| 652 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 653 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 654 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 655 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 656 | const char *key; |
| 657 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 658 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 659 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 660 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 661 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 662 | nasm_free(s->name); |
| 663 | free_tlist(s->expansion); |
| 664 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 665 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 666 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 667 | hash_free(smt); |
| 668 | } |
| 669 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 670 | static void free_expdef_table(struct hash_table *edt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 671 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 672 | ExpDef *ed, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 673 | const char *key; |
| 674 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 675 | |
| 676 | it = NULL; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 677 | while ((ed = hash_iterate(edt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 678 | nasm_free((void *)key); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 679 | list_for_each_safe(ed ,tmp, ed) |
| 680 | free_expdef(ed); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 681 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 682 | hash_free(edt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void free_macros(void) |
| 686 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 687 | free_smacro_table(&smacros); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 688 | free_expdef_table(&expdefs); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Initialize the hash tables |
| 693 | */ |
| 694 | static void init_macros(void) |
| 695 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 696 | hash_init(&smacros, HASH_LARGE); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 697 | hash_init(&expdefs, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 701 | * Pop the context stack. |
| 702 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 703 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 704 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 705 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 706 | |
| 707 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 708 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 709 | nasm_free(c->name); |
| 710 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 711 | } |
| 712 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 713 | /* |
| 714 | * Search for a key in the hash index; adding it if necessary |
| 715 | * (in which case we initialize the data pointer to NULL.) |
| 716 | */ |
| 717 | static void ** |
| 718 | hash_findi_add(struct hash_table *hash, const char *str) |
| 719 | { |
| 720 | struct hash_insert hi; |
| 721 | void **r; |
| 722 | char *strx; |
| 723 | |
| 724 | r = hash_findi(hash, str, &hi); |
| 725 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 726 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 727 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 728 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 729 | return hash_add(&hi, strx, NULL); |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Like hash_findi, but returns the data element rather than a pointer |
| 734 | * to it. Used only when not adding a new element, hence no third |
| 735 | * argument. |
| 736 | */ |
| 737 | static void * |
| 738 | hash_findix(struct hash_table *hash, const char *str) |
| 739 | { |
| 740 | void **p; |
| 741 | |
| 742 | p = hash_findi(hash, str, NULL); |
| 743 | return p ? *p : NULL; |
| 744 | } |
| 745 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 746 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 747 | * read line from standard macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 748 | * if there no more left -- return NULL |
| 749 | */ |
| 750 | static char *line_from_stdmac(void) |
| 751 | { |
| 752 | unsigned char c; |
| 753 | const unsigned char *p = stdmacpos; |
| 754 | char *line, *q; |
| 755 | size_t len = 0; |
| 756 | |
| 757 | if (!stdmacpos) |
| 758 | return NULL; |
| 759 | |
| 760 | while ((c = *p++)) { |
| 761 | if (c >= 0x80) |
| 762 | len += pp_directives_len[c - 0x80] + 1; |
| 763 | else |
| 764 | len++; |
| 765 | } |
| 766 | |
| 767 | line = nasm_malloc(len + 1); |
| 768 | q = line; |
| 769 | while ((c = *stdmacpos++)) { |
| 770 | if (c >= 0x80) { |
| 771 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 772 | q += pp_directives_len[c - 0x80]; |
| 773 | *q++ = ' '; |
| 774 | } else { |
| 775 | *q++ = c; |
| 776 | } |
| 777 | } |
| 778 | stdmacpos = p; |
| 779 | *q = '\0'; |
| 780 | |
| 781 | if (!*stdmacpos) { |
| 782 | /* This was the last of the standard macro chain... */ |
| 783 | stdmacpos = NULL; |
| 784 | if (any_extrastdmac) { |
| 785 | stdmacpos = extrastdmac; |
| 786 | any_extrastdmac = false; |
| 787 | } else if (do_predef) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 788 | ExpInv *ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 789 | Line *pd, *l; |
| 790 | Token *head, **tail, *t; |
| 791 | |
| 792 | /* |
| 793 | * Nasty hack: here we push the contents of |
| 794 | * `predef' on to the top-level expansion stack, |
| 795 | * since this is the most convenient way to |
| 796 | * implement the pre-include and pre-define |
| 797 | * features. |
| 798 | */ |
| 799 | list_for_each(pd, predef) { |
| 800 | head = NULL; |
| 801 | tail = &head; |
| 802 | list_for_each(t, pd->first) { |
| 803 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 804 | tail = &(*tail)->next; |
| 805 | } |
| 806 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 807 | l = new_Line(); |
| 808 | l->first = head; |
| 809 | ei = new_ExpInv(EXP_PREDEF, NULL); |
| 810 | ei->current = l; |
| 811 | ei->emitting = true; |
| 812 | ei->prev = istk->expansion; |
| 813 | istk->expansion = ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 814 | } |
| 815 | do_predef = false; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | return line; |
| 820 | } |
| 821 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 822 | #define BUF_DELTA 512 |
| 823 | /* |
| 824 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 825 | * at the end of the line read, and handling spurious ^Zs. Will |
| 826 | * return lines from the standard macro set if this has not already |
| 827 | * been done. |
| 828 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 829 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 830 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 831 | char *buffer, *p, *q; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 832 | int bufsize, continued_count; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 833 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 834 | /* |
| 835 | * standart macros set (predefined) goes first |
| 836 | */ |
| 837 | p = line_from_stdmac(); |
| 838 | if (p) |
| 839 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 840 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 841 | /* |
| 842 | * regular read from a file |
| 843 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 844 | bufsize = BUF_DELTA; |
| 845 | buffer = nasm_malloc(BUF_DELTA); |
| 846 | p = buffer; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 847 | continued_count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 848 | while (1) { |
| 849 | q = fgets(p, bufsize - (p - buffer), istk->fp); |
| 850 | if (!q) |
| 851 | break; |
| 852 | p += strlen(p); |
| 853 | if (p > buffer && p[-1] == '\n') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 854 | /* |
| 855 | * Convert backslash-CRLF line continuation sequences into |
| 856 | * nothing at all (for DOS and Windows) |
| 857 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 858 | if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { |
| 859 | p -= 3; |
| 860 | *p = 0; |
| 861 | continued_count++; |
| 862 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 863 | /* |
| 864 | * Also convert backslash-LF line continuation sequences into |
| 865 | * nothing at all (for Unix) |
| 866 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 867 | else if (((p - 1) > buffer) && (p[-2] == '\\')) { |
| 868 | p -= 2; |
| 869 | *p = 0; |
| 870 | continued_count++; |
| 871 | } else { |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | if (p - buffer > bufsize - 10) { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 876 | int32_t offset = p - buffer; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 877 | bufsize += BUF_DELTA; |
| 878 | buffer = nasm_realloc(buffer, bufsize); |
| 879 | p = buffer + offset; /* prevent stale-pointer problems */ |
| 880 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 881 | } |
| 882 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 883 | if (!q && p == buffer) { |
| 884 | nasm_free(buffer); |
| 885 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 888 | src_set_linnum(src_get_linnum() + istk->lineinc + |
| 889 | (continued_count * istk->lineinc)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 890 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 891 | /* |
| 892 | * Play safe: remove CRs as well as LFs, if any of either are |
| 893 | * present at the end of the line. |
| 894 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 895 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 896 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * Handle spurious ^Z, which may be inserted into source files |
| 900 | * by some file transfer utilities. |
| 901 | */ |
| 902 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 903 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 904 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 905 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 906 | return buffer; |
| 907 | } |
| 908 | |
| 909 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 910 | * 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] | 911 | * don't need to parse the value out of e.g. numeric tokens: we |
| 912 | * simply split one string into many. |
| 913 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 914 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 915 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 916 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 917 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 918 | Token *list = NULL; |
| 919 | Token *t, **tail = &list; |
| 920 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 921 | while (*line) { |
| 922 | p = line; |
| 923 | if (*p == '%') { |
| 924 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 925 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 926 | p++; |
| 927 | type = TOK_PASTE; |
| 928 | } else if (nasm_isdigit(*p) || |
| 929 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 930 | do { |
| 931 | p++; |
| 932 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 933 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 934 | type = TOK_PREPROC_ID; |
| 935 | } else if (*p == '{') { |
| 936 | p++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 937 | while (*p && *p != '}') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 938 | p[-1] = *p; |
| 939 | p++; |
| 940 | } |
| 941 | p[-1] = '\0'; |
| 942 | if (*p) |
| 943 | p++; |
| 944 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 945 | } else if (*p == '[') { |
| 946 | int lvl = 1; |
| 947 | line += 2; /* Skip the leading %[ */ |
| 948 | p++; |
| 949 | while (lvl && (c = *p++)) { |
| 950 | switch (c) { |
| 951 | case ']': |
| 952 | lvl--; |
| 953 | break; |
| 954 | case '%': |
| 955 | if (*p == '[') |
| 956 | lvl++; |
| 957 | break; |
| 958 | case '\'': |
| 959 | case '\"': |
| 960 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 961 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 962 | break; |
| 963 | default: |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | p--; |
| 968 | if (*p) |
| 969 | *p++ = '\0'; |
| 970 | if (lvl) |
| 971 | error(ERR_NONFATAL, "unterminated %[ construct"); |
| 972 | type = TOK_INDIRECT; |
| 973 | } else if (*p == '?') { |
| 974 | type = TOK_PREPROC_Q; /* %? */ |
| 975 | p++; |
| 976 | if (*p == '?') { |
| 977 | type = TOK_PREPROC_QQ; /* %?? */ |
| 978 | p++; |
| 979 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 980 | } else if (*p == '!') { |
| 981 | type = TOK_PREPROC_ID; |
| 982 | p++; |
| 983 | if (isidchar(*p)) { |
| 984 | do { |
| 985 | p++; |
| 986 | } while (isidchar(*p)); |
| 987 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 988 | p = nasm_skip_string(p); |
| 989 | if (*p) |
| 990 | p++; |
| 991 | else |
| 992 | error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string"); |
| 993 | } else { |
| 994 | /* %! without string or identifier */ |
| 995 | type = TOK_OTHER; /* Legacy behavior... */ |
| 996 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 997 | } else if (isidchar(*p) || |
| 998 | ((*p == '!' || *p == '%' || *p == '$') && |
| 999 | isidchar(p[1]))) { |
| 1000 | do { |
| 1001 | p++; |
| 1002 | } |
| 1003 | while (isidchar(*p)); |
| 1004 | type = TOK_PREPROC_ID; |
| 1005 | } else { |
| 1006 | type = TOK_OTHER; |
| 1007 | if (*p == '%') |
| 1008 | p++; |
| 1009 | } |
| 1010 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1011 | type = TOK_ID; |
| 1012 | p++; |
| 1013 | while (*p && isidchar(*p)) |
| 1014 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1015 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1016 | /* |
| 1017 | * A string token. |
| 1018 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1019 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1020 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1021 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1022 | if (*p) { |
| 1023 | p++; |
| 1024 | } else { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1025 | error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1026 | /* Handling unterminated strings by UNV */ |
| 1027 | /* type = -1; */ |
| 1028 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1029 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1030 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1031 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1032 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1033 | bool is_hex = false; |
| 1034 | bool is_float = false; |
| 1035 | bool has_e = false; |
| 1036 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1037 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1038 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1039 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1040 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1041 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1042 | if (*p == '$') { |
| 1043 | p++; |
| 1044 | is_hex = true; |
| 1045 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1046 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1047 | for (;;) { |
| 1048 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1049 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1050 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1051 | has_e = true; |
| 1052 | if (*p == '+' || *p == '-') { |
| 1053 | /* |
| 1054 | * e can only be followed by +/- if it is either a |
| 1055 | * prefixed hex number or a floating-point number |
| 1056 | */ |
| 1057 | p++; |
| 1058 | is_float = true; |
| 1059 | } |
| 1060 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1061 | is_hex = true; |
| 1062 | } else if (c == 'P' || c == 'p') { |
| 1063 | is_float = true; |
| 1064 | if (*p == '+' || *p == '-') |
| 1065 | p++; |
| 1066 | } else if (isnumchar(c) || c == '_') |
| 1067 | ; /* just advance */ |
| 1068 | else if (c == '.') { |
| 1069 | /* |
| 1070 | * we need to deal with consequences of the legacy |
| 1071 | * parser, like "1.nolist" being two tokens |
| 1072 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1073 | * a shot for now. In the future, we probably need |
| 1074 | * a flex-based scanner with proper pattern matching |
| 1075 | * to do it as well as it can be done. Nothing in |
| 1076 | * the world is going to help the person who wants |
| 1077 | * 0x123.p16 interpreted as two tokens, though. |
| 1078 | */ |
| 1079 | r = p; |
| 1080 | while (*r == '_') |
| 1081 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1082 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1083 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1084 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1085 | (*r == 'p' || *r == 'P')) { |
| 1086 | p = r; |
| 1087 | is_float = true; |
| 1088 | } else |
| 1089 | break; /* Terminate the token */ |
| 1090 | } else |
| 1091 | break; |
| 1092 | } |
| 1093 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1094 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1095 | if (p == line+1 && *line == '$') { |
| 1096 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1097 | } else { |
| 1098 | if (has_e && !is_hex) { |
| 1099 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1100 | is_float = true; |
| 1101 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1102 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1103 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1104 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1105 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1106 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1107 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1108 | /* |
| 1109 | * Whitespace just before end-of-line is discarded by |
| 1110 | * pretending it's a comment; whitespace just before a |
| 1111 | * comment gets lumped into the comment. |
| 1112 | */ |
| 1113 | if (!*p || *p == ';') { |
| 1114 | type = TOK_COMMENT; |
| 1115 | while (*p) |
| 1116 | p++; |
| 1117 | } |
| 1118 | } else if (*p == ';') { |
| 1119 | type = TOK_COMMENT; |
| 1120 | while (*p) |
| 1121 | p++; |
| 1122 | } else { |
| 1123 | /* |
| 1124 | * Anything else is an operator of some kind. We check |
| 1125 | * for all the double-character operators (>>, <<, //, |
| 1126 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1127 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1128 | */ |
| 1129 | type = TOK_OTHER; |
| 1130 | if ((p[0] == '>' && p[1] == '>') || |
| 1131 | (p[0] == '<' && p[1] == '<') || |
| 1132 | (p[0] == '/' && p[1] == '/') || |
| 1133 | (p[0] == '<' && p[1] == '=') || |
| 1134 | (p[0] == '>' && p[1] == '=') || |
| 1135 | (p[0] == '=' && p[1] == '=') || |
| 1136 | (p[0] == '!' && p[1] == '=') || |
| 1137 | (p[0] == '<' && p[1] == '>') || |
| 1138 | (p[0] == '&' && p[1] == '&') || |
| 1139 | (p[0] == '|' && p[1] == '|') || |
| 1140 | (p[0] == '^' && p[1] == '^')) { |
| 1141 | p++; |
| 1142 | } |
| 1143 | p++; |
| 1144 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1145 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1146 | /* Handling unterminated string by UNV */ |
| 1147 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1148 | { |
| 1149 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1150 | t->text[p-line] = *line; |
| 1151 | tail = &t->next; |
| 1152 | } |
| 1153 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1154 | if (type != TOK_COMMENT) { |
| 1155 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1156 | tail = &t->next; |
| 1157 | } |
| 1158 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1159 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1160 | return list; |
| 1161 | } |
| 1162 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1163 | /* |
| 1164 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1165 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1166 | * deleted only all at once by the delete_Blocks function. |
| 1167 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1168 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1169 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1170 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1171 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1172 | /* first, get to the end of the linked list */ |
| 1173 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1174 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1175 | /* now allocate the requested chunk */ |
| 1176 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1177 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1178 | /* now allocate a new block for the next request */ |
| 1179 | b->next = nasm_malloc(sizeof(Blocks)); |
| 1180 | /* and initialize the contents of the new block */ |
| 1181 | b->next->next = NULL; |
| 1182 | b->next->chunk = NULL; |
| 1183 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * this function deletes all managed blocks of memory |
| 1188 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1189 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1190 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1191 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1192 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1193 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1194 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1195 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1196 | * free it. |
| 1197 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1198 | while (b) { |
| 1199 | if (b->chunk) |
| 1200 | nasm_free(b->chunk); |
| 1201 | a = b; |
| 1202 | b = b->next; |
| 1203 | if (a != &blocks) |
| 1204 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1205 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1206 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1209 | * 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] | 1210 | * 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] | 1211 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1212 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1213 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1214 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1215 | { |
| 1216 | Token *t; |
| 1217 | int i; |
| 1218 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1219 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1220 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1221 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1222 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1223 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1224 | } |
| 1225 | t = freeTokens; |
| 1226 | freeTokens = t->next; |
| 1227 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1228 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1229 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1230 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1231 | t->text = NULL; |
| 1232 | } else { |
| 1233 | if (txtlen == 0) |
| 1234 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1235 | t->text = nasm_malloc(txtlen+1); |
| 1236 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1237 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1238 | } |
| 1239 | return t; |
| 1240 | } |
| 1241 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1242 | static Token *copy_Token(Token * tline) |
| 1243 | { |
| 1244 | Token *t, *tt, *first = NULL, *prev = NULL; |
| 1245 | int i; |
| 1246 | for (tt = tline; tt != NULL; tt = tt->next) { |
| 1247 | if (!freeTokens) { |
| 1248 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1249 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1250 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1251 | freeTokens[i].next = NULL; |
| 1252 | } |
| 1253 | t = freeTokens; |
| 1254 | freeTokens = t->next; |
| 1255 | t->next = NULL; |
| 1256 | t->text = ((tt->text != NULL) ? strdup(tt->text) : NULL); |
| 1257 | t->a.mac = tt->a.mac; |
| 1258 | t->a.len = tt->a.len; |
| 1259 | t->type = tt->type; |
| 1260 | if (prev != NULL) { |
| 1261 | prev->next = t; |
| 1262 | } else { |
| 1263 | first = t; |
| 1264 | } |
| 1265 | prev = t; |
| 1266 | } |
| 1267 | return first; |
| 1268 | } |
| 1269 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1270 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1271 | { |
| 1272 | Token *next = t->next; |
| 1273 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1274 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1275 | freeTokens = t; |
| 1276 | return next; |
| 1277 | } |
| 1278 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1279 | /* |
| 1280 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1281 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1282 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1283 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1284 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1285 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1286 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1287 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1288 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1289 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1290 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1291 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1292 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1293 | char *v; |
| 1294 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1295 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1296 | v = t->text + 2; |
| 1297 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1298 | size_t len = nasm_unquote(v, NULL); |
| 1299 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1300 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1301 | if (len != clen) { |
| 1302 | error(ERR_NONFATAL | ERR_PASS1, |
| 1303 | "NUL character in %! string"); |
| 1304 | v = NULL; |
| 1305 | } |
| 1306 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1307 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1308 | if (v) { |
| 1309 | char *p = getenv(v); |
| 1310 | if (!p) { |
| 1311 | error(ERR_NONFATAL | ERR_PASS1, |
| 1312 | "nonexistent environment variable `%s'", v); |
| 1313 | p = ""; |
| 1314 | } |
| 1315 | t->text = nasm_strdup(p); |
| 1316 | } |
| 1317 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1318 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1319 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1320 | /* Expand local macros here and not during preprocessing */ |
| 1321 | if (expand_locals && |
| 1322 | t->type == TOK_PREPROC_ID && t->text && |
| 1323 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1324 | const char *q; |
| 1325 | char *p; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1326 | Context *ctx = get_ctx(t->text, &q, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1327 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1328 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1329 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1330 | p = nasm_strcat(buffer, q); |
| 1331 | nasm_free(t->text); |
| 1332 | t->text = p; |
| 1333 | } |
| 1334 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1335 | |
| 1336 | /* Expand %? and %?? directives */ |
| 1337 | if (expand_locals && (istk->expansion != NULL) && |
| 1338 | ((t->type == TOK_PREPROC_Q) || |
| 1339 | (t->type == TOK_PREPROC_QQ))) { |
| 1340 | ExpInv *ei; |
| 1341 | for (ei = istk->expansion; ei != NULL; ei = ei->prev){ |
| 1342 | if (ei->type == EXP_MMACRO) { |
| 1343 | nasm_free(t->text); |
| 1344 | if (t->type == TOK_PREPROC_Q) { |
| 1345 | t->text = nasm_strdup(ei->name); |
| 1346 | } else { |
| 1347 | t->text = nasm_strdup(ei->def->name); |
| 1348 | } |
| 1349 | break; |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1354 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1355 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1356 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1357 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1358 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1359 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1360 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1361 | |
| 1362 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1363 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1364 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1365 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1366 | q = t->text; |
| 1367 | while (*q) |
| 1368 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1369 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1370 | } |
| 1371 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1372 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1373 | return line; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1377 | * Initialize a new Line |
| 1378 | */ |
| 1379 | static Line *new_Line(void) |
| 1380 | { |
| 1381 | Line *l = nasm_malloc(sizeof(Line)); |
| 1382 | l->next = NULL; |
| 1383 | l->first = NULL; |
| 1384 | return l; |
| 1385 | } |
| 1386 | |
| 1387 | |
| 1388 | /* |
| 1389 | * Initialize a new Expansion Definition |
| 1390 | */ |
| 1391 | static ExpDef *new_ExpDef(int exp_type) |
| 1392 | { |
Keith Kanios | 0bae3e5 | 2010-11-07 16:21:00 -0600 | [diff] [blame] | 1393 | ExpDef *ed = (ExpDef*)nasm_malloc(sizeof(ExpDef)); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1394 | ed->prev = NULL; |
| 1395 | ed->next = NULL; |
| 1396 | ed->type = exp_type; |
| 1397 | ed->name = NULL; |
| 1398 | ed->nparam_min = 0; |
| 1399 | ed->nparam_max = 0; |
| 1400 | ed->casesense = true; |
| 1401 | ed->plus = false; |
| 1402 | ed->prepend = 0; |
| 1403 | ed->label = NULL; |
| 1404 | ed->line = NULL; |
| 1405 | ed->last = NULL; |
| 1406 | ed->linecount = 0; |
| 1407 | ed->dlist = NULL; |
| 1408 | ed->defaults = NULL; |
| 1409 | ed->ndefs = 0; |
| 1410 | ed->state = COND_NEVER; |
| 1411 | ed->nolist = false; |
| 1412 | ed->def_depth = 0; |
| 1413 | ed->cur_depth = 0; |
| 1414 | ed->max_depth = 0; |
| 1415 | ed->ignoring = false; |
| 1416 | return ed; |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | /* |
| 1421 | * Initialize a new Expansion Instance |
| 1422 | */ |
| 1423 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed) |
| 1424 | { |
Keith Kanios | 0bae3e5 | 2010-11-07 16:21:00 -0600 | [diff] [blame] | 1425 | ExpInv *ei = (ExpInv*)nasm_malloc(sizeof(ExpInv)); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1426 | ei->prev = NULL; |
| 1427 | ei->type = exp_type; |
| 1428 | ei->def = ed; |
| 1429 | ei->name = NULL; |
| 1430 | ei->label = NULL; |
| 1431 | ei->label_text = NULL; |
| 1432 | ei->current = NULL; |
| 1433 | ei->params = NULL; |
| 1434 | ei->iline = NULL; |
| 1435 | ei->nparam = 0; |
| 1436 | ei->rotate = 0; |
| 1437 | ei->paramlen = NULL; |
Cyrill Gorcunov | 329e966 | 2010-11-08 00:47:10 +0300 | [diff] [blame] | 1438 | ei->unique = ++unique; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1439 | ei->emitting = false; |
| 1440 | ei->lineno = 0; |
| 1441 | if ((istk->mmac_depth < 1) && |
| 1442 | (istk->expansion == NULL) && |
| 1443 | (ed != NULL) && |
| 1444 | (ed->type != EXP_MMACRO) && |
| 1445 | (ed->type != EXP_REP) && |
| 1446 | (ed->type != EXP_WHILE)) { |
| 1447 | ei->linnum = src_get_linnum(); |
| 1448 | src_set_linnum(ei->linnum - ed->linecount - 1); |
| 1449 | } else { |
| 1450 | ei->linnum = -1; |
| 1451 | } |
| 1452 | if ((istk->expansion == NULL) || |
| 1453 | (ei->type == EXP_MMACRO)) { |
| 1454 | ei->relno = 0; |
| 1455 | } else { |
| 1456 | ei->relno = istk->expansion->lineno; |
| 1457 | if (ed != NULL) { |
| 1458 | ei->relno -= (ed->linecount + 1); |
| 1459 | } |
| 1460 | } |
| 1461 | return ei; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1465 | * A scanner, suitable for use by the expression evaluator, which |
| 1466 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1467 | * the first token in the line to be passed in as its private_data |
| 1468 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1469 | * |
| 1470 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1471 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1472 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1473 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1474 | Token **tlineptr = private_data; |
| 1475 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1476 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1477 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1478 | do { |
| 1479 | tline = *tlineptr; |
| 1480 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1481 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1482 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1485 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1486 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1487 | tokval->t_charptr = tline->text; |
| 1488 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1489 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1490 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1491 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1492 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1493 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1494 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1495 | p = tokval->t_charptr = tline->text; |
| 1496 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1497 | tokval->t_charptr++; |
| 1498 | return tokval->t_type = TOKEN_ID; |
| 1499 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1500 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1501 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1502 | if (r >= p+MAX_KEYWORD) |
| 1503 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1504 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1505 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1506 | *s = '\0'; |
| 1507 | /* right, so we have an identifier sitting in temp storage. now, |
| 1508 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1509 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1512 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1513 | bool rn_error; |
| 1514 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1515 | tokval->t_charptr = tline->text; |
| 1516 | if (rn_error) |
| 1517 | return tokval->t_type = TOKEN_ERRNUM; |
| 1518 | else |
| 1519 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1520 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1521 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1522 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1523 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1526 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1527 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1528 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1529 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1530 | tokval->t_charptr = tline->text; |
| 1531 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1532 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1533 | if (ep[0] != bq || ep[1] != '\0') |
| 1534 | return tokval->t_type = TOKEN_ERRSTR; |
| 1535 | else |
| 1536 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1539 | if (tline->type == TOK_OTHER) { |
| 1540 | if (!strcmp(tline->text, "<<")) |
| 1541 | return tokval->t_type = TOKEN_SHL; |
| 1542 | if (!strcmp(tline->text, ">>")) |
| 1543 | return tokval->t_type = TOKEN_SHR; |
| 1544 | if (!strcmp(tline->text, "//")) |
| 1545 | return tokval->t_type = TOKEN_SDIV; |
| 1546 | if (!strcmp(tline->text, "%%")) |
| 1547 | return tokval->t_type = TOKEN_SMOD; |
| 1548 | if (!strcmp(tline->text, "==")) |
| 1549 | return tokval->t_type = TOKEN_EQ; |
| 1550 | if (!strcmp(tline->text, "<>")) |
| 1551 | return tokval->t_type = TOKEN_NE; |
| 1552 | if (!strcmp(tline->text, "!=")) |
| 1553 | return tokval->t_type = TOKEN_NE; |
| 1554 | if (!strcmp(tline->text, "<=")) |
| 1555 | return tokval->t_type = TOKEN_LE; |
| 1556 | if (!strcmp(tline->text, ">=")) |
| 1557 | return tokval->t_type = TOKEN_GE; |
| 1558 | if (!strcmp(tline->text, "&&")) |
| 1559 | return tokval->t_type = TOKEN_DBL_AND; |
| 1560 | if (!strcmp(tline->text, "^^")) |
| 1561 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1562 | if (!strcmp(tline->text, "||")) |
| 1563 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * We have no other options: just return the first character of |
| 1568 | * the token text. |
| 1569 | */ |
| 1570 | return tokval->t_type = tline->text[0]; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1574 | * Compare a string to the name of an existing macro; this is a |
| 1575 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1576 | * depending on the value of the `casesense' parameter. |
| 1577 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1578 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1579 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1580 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1584 | * Compare a string to the name of an existing macro; this is a |
| 1585 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1586 | * depending on the value of the `casesense' parameter. |
| 1587 | */ |
| 1588 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1589 | { |
| 1590 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1591 | } |
| 1592 | |
| 1593 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1594 | * Return the Context structure associated with a %$ token. Return |
| 1595 | * NULL, having _already_ reported an error condition, if the |
| 1596 | * context stack isn't deep enough for the supplied number of $ |
| 1597 | * signs. |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1598 | * If all_contexts == true, contexts that enclose current are |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1599 | * also scanned for such smacro, until it is found; if not - |
| 1600 | * only the context that directly results from the number of $'s |
| 1601 | * in variable's name. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1602 | * |
| 1603 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1604 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1605 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1606 | static Context *get_ctx(const char *name, const char **namep, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1607 | bool all_contexts) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1608 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1609 | Context *ctx; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1610 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1611 | int i; |
| 1612 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1613 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1614 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1615 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1616 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1617 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1618 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1619 | if (!cstk) { |
| 1620 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
| 1621 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1624 | name += 2; |
| 1625 | ctx = cstk; |
| 1626 | i = 0; |
| 1627 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1628 | name++; |
| 1629 | i++; |
| 1630 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1631 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1632 | if (!ctx) { |
| 1633 | error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1634 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1635 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1636 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1637 | |
| 1638 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1639 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1640 | |
Keith Kanios | 404589e | 2010-08-10 20:12:57 -0500 | [diff] [blame] | 1641 | if (!all_contexts) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1642 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1643 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1644 | do { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1645 | /* Search for this smacro in found context */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1646 | m = hash_findix(&ctx->localmac, name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1647 | while (m) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1648 | if (!mstrcmp(m->name, name, m->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1649 | return ctx; |
| 1650 | m = m->next; |
| 1651 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1652 | ctx = ctx->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1653 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1654 | while (ctx); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1655 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1659 | * Check to see if a file is already in a string list |
| 1660 | */ |
| 1661 | static bool in_list(const StrList *list, const char *str) |
| 1662 | { |
| 1663 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1664 | if (!strcmp(list->str, str)) |
| 1665 | return true; |
| 1666 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1667 | } |
| 1668 | return false; |
| 1669 | } |
| 1670 | |
| 1671 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1672 | * Open an include file. This routine must always return a valid |
| 1673 | * file pointer if it returns - it's responsible for throwing an |
| 1674 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1675 | * the include path one by one until it finds the file or reaches |
| 1676 | * the end of the path. |
| 1677 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1678 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1679 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1680 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1681 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1682 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1683 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1684 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1685 | size_t prefix_len = 0; |
| 1686 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1687 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1689 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1690 | memcpy(sl->str, prefix, prefix_len); |
| 1691 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1692 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1693 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
| 1694 | sl->next = NULL; |
| 1695 | **dtail = sl; |
| 1696 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1697 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1698 | nasm_free(sl); |
| 1699 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1700 | if (fp) |
| 1701 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1702 | if (!ip) { |
| 1703 | if (!missing_ok) |
| 1704 | break; |
| 1705 | prefix = NULL; |
| 1706 | } else { |
| 1707 | prefix = ip->path; |
| 1708 | ip = ip->next; |
| 1709 | } |
| 1710 | if (prefix) { |
| 1711 | prefix_len = strlen(prefix); |
| 1712 | } else { |
| 1713 | /* -MG given and file not found */ |
| 1714 | if (dhead && !in_list(*dhead, file)) { |
| 1715 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1716 | sl->next = NULL; |
| 1717 | strcpy(sl->str, file); |
| 1718 | **dtail = sl; |
| 1719 | *dtail = &sl->next; |
| 1720 | } |
| 1721 | return NULL; |
| 1722 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1723 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1724 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1725 | error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1726 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1730 | * 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] | 1731 | * 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] | 1732 | * return true if _any_ single-line macro of that name is defined. |
| 1733 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1734 | * `nparam' or no parameters is defined. |
| 1735 | * |
| 1736 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1737 | * defined, or nparam is -1, the address of the definition structure |
| 1738 | * 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] | 1739 | * is NULL, no action will be taken regarding its contents, and no |
| 1740 | * error will occur. |
| 1741 | * |
| 1742 | * Note that this is also called with nparam zero to resolve |
| 1743 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1744 | * |
| 1745 | * If you already know which context macro belongs to, you can pass |
| 1746 | * the context pointer as first parameter; if you won't but name begins |
| 1747 | * with %$ the context will be automatically computed. If all_contexts |
| 1748 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1749 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1750 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1751 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1752 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1753 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1754 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1755 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1756 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1757 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1758 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1759 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1760 | if (cstk) |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1761 | ctx = get_ctx(name, &name, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1762 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1763 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1764 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1765 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1766 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1767 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1768 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1769 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1770 | while (m) { |
| 1771 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1772 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1773 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1774 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1775 | *defn = m; |
| 1776 | else |
| 1777 | *defn = NULL; |
| 1778 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1779 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1780 | } |
| 1781 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1782 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1783 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1784 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * Count and mark off the parameters in a multi-line macro call. |
| 1789 | * This is called both from within the multi-line macro expansion |
| 1790 | * code, and also to mark off the default parameters when provided |
| 1791 | * in a %macro definition line. |
| 1792 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1793 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1794 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1795 | int paramsize, brace; |
| 1796 | |
| 1797 | *nparam = paramsize = 0; |
| 1798 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1799 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1800 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1801 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1802 | paramsize += PARAM_DELTA; |
| 1803 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1804 | } |
| 1805 | skip_white_(t); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1806 | brace = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1807 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1808 | brace = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1809 | (*params)[(*nparam)++] = t; |
| 1810 | while (tok_isnt_(t, brace ? "}" : ",")) |
| 1811 | t = t->next; |
| 1812 | if (t) { /* got a comma/brace */ |
| 1813 | t = t->next; |
| 1814 | if (brace) { |
| 1815 | /* |
| 1816 | * Now we've found the closing brace, look further |
| 1817 | * for the comma. |
| 1818 | */ |
| 1819 | skip_white_(t); |
| 1820 | if (tok_isnt_(t, ",")) { |
| 1821 | error(ERR_NONFATAL, |
| 1822 | "braces do not enclose all of macro parameter"); |
| 1823 | while (tok_isnt_(t, ",")) |
| 1824 | t = t->next; |
| 1825 | } |
| 1826 | if (t) |
| 1827 | t = t->next; /* eat the comma */ |
| 1828 | } |
| 1829 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1834 | * Determine whether one of the various `if' conditions is true or |
| 1835 | * not. |
| 1836 | * |
| 1837 | * We must free the tline we get passed. |
| 1838 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1839 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1840 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1841 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1842 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1843 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1844 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1845 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1846 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1847 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1848 | |
| 1849 | origline = tline; |
| 1850 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1851 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1852 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1853 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1854 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1855 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1856 | if (!tline) |
| 1857 | break; |
| 1858 | if (tline->type != TOK_ID) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1859 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1860 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1861 | free_tlist(origline); |
| 1862 | return -1; |
| 1863 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1864 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1865 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1866 | tline = tline->next; |
| 1867 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1868 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1869 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1870 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1871 | j = false; /* have we matched yet? */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1872 | while (tline) { |
| 1873 | skip_white_(tline); |
| 1874 | if (!tline || (tline->type != TOK_ID && |
| 1875 | (tline->type != TOK_PREPROC_ID || |
| 1876 | tline->text[1] != '$'))) { |
| 1877 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1878 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1879 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1880 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1881 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1882 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1883 | tline = tline->next; |
| 1884 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1885 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1886 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1887 | case PPC_IFENV: |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1888 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1889 | j = false; /* have we matched yet? */ |
| 1890 | while (tline) { |
| 1891 | skip_white_(tline); |
| 1892 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1893 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1894 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1895 | tline->text[1] != '!'))) { |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1896 | error(ERR_NONFATAL, |
| 1897 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1898 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1899 | goto fail; |
| 1900 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1901 | p = tline->text; |
| 1902 | if (tline->type == TOK_PREPROC_ID) |
| 1903 | p += 2; /* Skip leading %! */ |
| 1904 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1905 | nasm_unquote_cstr(p, ct); |
| 1906 | if (getenv(p)) |
| 1907 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1908 | tline = tline->next; |
| 1909 | } |
| 1910 | break; |
| 1911 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1912 | case PPC_IFIDN: |
| 1913 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1914 | tline = expand_smacro(tline); |
| 1915 | t = tt = tline; |
| 1916 | while (tok_isnt_(tt, ",")) |
| 1917 | tt = tt->next; |
| 1918 | if (!tt) { |
| 1919 | error(ERR_NONFATAL, |
| 1920 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1921 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1922 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1923 | } |
| 1924 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1925 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1926 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1927 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1928 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1929 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1930 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1931 | } |
| 1932 | if (t->type == TOK_WHITESPACE) { |
| 1933 | t = t->next; |
| 1934 | continue; |
| 1935 | } |
| 1936 | if (tt->type == TOK_WHITESPACE) { |
| 1937 | tt = tt->next; |
| 1938 | continue; |
| 1939 | } |
| 1940 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1941 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1944 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1945 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1946 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1947 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1948 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1949 | if (l1 != l2) { |
| 1950 | j = false; |
| 1951 | break; |
| 1952 | } |
| 1953 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1954 | j = false; |
| 1955 | break; |
| 1956 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1957 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1958 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1961 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1962 | t = t->next; |
| 1963 | tt = tt->next; |
| 1964 | } |
| 1965 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1966 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1967 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1968 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1969 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1970 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1971 | bool found = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1972 | ExpDef searching, *ed; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1973 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1974 | skip_white_(tline); |
| 1975 | tline = expand_id(tline); |
| 1976 | if (!tok_type_(tline, TOK_ID)) { |
| 1977 | error(ERR_NONFATAL, |
| 1978 | "`%s' expects a macro name", pp_directives[ct]); |
| 1979 | goto fail; |
| 1980 | } |
| 1981 | searching.name = nasm_strdup(tline->text); |
| 1982 | searching.casesense = true; |
| 1983 | searching.plus = false; |
| 1984 | searching.nolist = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1985 | //searching.in_progress = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1986 | searching.max_depth = 0; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1987 | //searching.rep_nest = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1988 | searching.nparam_min = 0; |
| 1989 | searching.nparam_max = INT_MAX; |
| 1990 | tline = expand_smacro(tline->next); |
| 1991 | skip_white_(tline); |
| 1992 | if (!tline) { |
| 1993 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 1994 | error(ERR_NONFATAL, |
| 1995 | "`%s' expects a parameter count or nothing", |
| 1996 | pp_directives[ct]); |
| 1997 | } else { |
| 1998 | searching.nparam_min = searching.nparam_max = |
| 1999 | readnum(tline->text, &j); |
| 2000 | if (j) |
| 2001 | error(ERR_NONFATAL, |
| 2002 | "unable to parse parameter count `%s'", |
| 2003 | tline->text); |
| 2004 | } |
| 2005 | if (tline && tok_is_(tline->next, "-")) { |
| 2006 | tline = tline->next->next; |
| 2007 | if (tok_is_(tline, "*")) |
| 2008 | searching.nparam_max = INT_MAX; |
| 2009 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 2010 | error(ERR_NONFATAL, |
| 2011 | "`%s' expects a parameter count after `-'", |
| 2012 | pp_directives[ct]); |
| 2013 | else { |
| 2014 | searching.nparam_max = readnum(tline->text, &j); |
| 2015 | if (j) |
| 2016 | error(ERR_NONFATAL, |
| 2017 | "unable to parse parameter count `%s'", |
| 2018 | tline->text); |
| 2019 | if (searching.nparam_min > searching.nparam_max) |
| 2020 | error(ERR_NONFATAL, |
| 2021 | "minimum parameter count exceeds maximum"); |
| 2022 | } |
| 2023 | } |
| 2024 | if (tline && tok_is_(tline->next, "+")) { |
| 2025 | tline = tline->next; |
| 2026 | searching.plus = true; |
| 2027 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2028 | ed = (ExpDef *) hash_findix(&expdefs, searching.name); |
| 2029 | while (ed != NULL) { |
| 2030 | if (!strcmp(ed->name, searching.name) && |
| 2031 | (ed->nparam_min <= searching.nparam_max |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2032 | || searching.plus) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2033 | && (searching.nparam_min <= ed->nparam_max |
| 2034 | || ed->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2035 | found = true; |
| 2036 | break; |
| 2037 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2038 | ed = ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2039 | } |
| 2040 | if (tline && tline->next) |
| 2041 | error(ERR_WARNING|ERR_PASS1, |
| 2042 | "trailing garbage after %%ifmacro ignored"); |
| 2043 | nasm_free(searching.name); |
| 2044 | j = found; |
| 2045 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2046 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2047 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2048 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2049 | needtype = TOK_ID; |
| 2050 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2051 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2052 | needtype = TOK_NUMBER; |
| 2053 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2054 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2055 | needtype = TOK_STRING; |
| 2056 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2057 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2058 | iftype: |
| 2059 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2060 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2061 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2062 | (needtype == TOK_NUMBER && |
| 2063 | tok_type_(t, TOK_OTHER) && |
| 2064 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2065 | !t->text[1])) |
| 2066 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2067 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2068 | j = tok_type_(t, needtype); |
| 2069 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2070 | |
| 2071 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2072 | t = tline = expand_smacro(tline); |
| 2073 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2074 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2075 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2076 | j = false; |
| 2077 | if (t) { |
| 2078 | t = t->next; /* Skip the actual token */ |
| 2079 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2080 | t = t->next; |
| 2081 | j = !t; /* Should be nothing left */ |
| 2082 | } |
| 2083 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2084 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2085 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2086 | t = tline = expand_smacro(tline); |
| 2087 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2088 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2089 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2090 | j = !t; /* Should be empty */ |
| 2091 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2092 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2093 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2094 | t = tline = expand_smacro(tline); |
| 2095 | tptr = &t; |
| 2096 | tokval.t_type = TOKEN_INVALID; |
| 2097 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 2098 | NULL, pass | CRITICAL, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2099 | if (!evalresult) |
| 2100 | return -1; |
| 2101 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2102 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2103 | "trailing garbage after expression ignored"); |
| 2104 | if (!is_simple(evalresult)) { |
| 2105 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2106 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2107 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2108 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2109 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2110 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2111 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2112 | default: |
| 2113 | error(ERR_FATAL, |
| 2114 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2115 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2116 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2117 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2118 | |
| 2119 | free_tlist(origline); |
| 2120 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2121 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2122 | fail: |
| 2123 | free_tlist(origline); |
| 2124 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2128 | * Common code for defining an smacro |
| 2129 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2130 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2131 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2132 | { |
| 2133 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2134 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2135 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2136 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2137 | if (!smac) { |
| 2138 | error(ERR_WARNING|ERR_PASS1, |
| 2139 | "single-line macro `%s' defined both with and" |
| 2140 | " without parameters", mname); |
| 2141 | /* |
| 2142 | * Some instances of the old code considered this a failure, |
| 2143 | * some others didn't. What is the right thing to do here? |
| 2144 | */ |
| 2145 | free_tlist(expansion); |
| 2146 | return false; /* Failure */ |
| 2147 | } else { |
| 2148 | /* |
| 2149 | * We're redefining, so we have to take over an |
| 2150 | * existing SMacro structure. This means freeing |
| 2151 | * what was already in it. |
| 2152 | */ |
| 2153 | nasm_free(smac->name); |
| 2154 | free_tlist(smac->expansion); |
| 2155 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2156 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2157 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2158 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
| 2159 | smac = nasm_malloc(sizeof(SMacro)); |
| 2160 | smac->next = *smhead; |
| 2161 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2162 | } |
| 2163 | smac->name = nasm_strdup(mname); |
| 2164 | smac->casesense = casesense; |
| 2165 | smac->nparam = nparam; |
| 2166 | smac->expansion = expansion; |
| 2167 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2168 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | /* |
| 2172 | * Undefine an smacro |
| 2173 | */ |
| 2174 | static void undef_smacro(Context *ctx, const char *mname) |
| 2175 | { |
| 2176 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2177 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2178 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2179 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2180 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2181 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2182 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2183 | /* |
| 2184 | * We now have a macro name... go hunt for it. |
| 2185 | */ |
| 2186 | sp = smhead; |
| 2187 | while ((s = *sp) != NULL) { |
| 2188 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2189 | *sp = s->next; |
| 2190 | nasm_free(s->name); |
| 2191 | free_tlist(s->expansion); |
| 2192 | nasm_free(s); |
| 2193 | } else { |
| 2194 | sp = &s->next; |
| 2195 | } |
| 2196 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2197 | } |
| 2198 | } |
| 2199 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2200 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2201 | * Parse a mmacro specification. |
| 2202 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2203 | static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive) |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2204 | { |
| 2205 | bool err; |
| 2206 | |
| 2207 | tline = tline->next; |
| 2208 | skip_white_(tline); |
| 2209 | tline = expand_id(tline); |
| 2210 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2211 | error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
| 2212 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2213 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2214 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2215 | def->name = nasm_strdup(tline->text); |
| 2216 | def->plus = false; |
| 2217 | def->nolist = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2218 | // def->in_progress = 0; |
| 2219 | // def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2220 | def->nparam_min = 0; |
| 2221 | def->nparam_max = 0; |
| 2222 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2223 | tline = expand_smacro(tline->next); |
| 2224 | skip_white_(tline); |
| 2225 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2226 | error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2227 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2228 | def->nparam_min = def->nparam_max = |
| 2229 | readnum(tline->text, &err); |
| 2230 | if (err) |
| 2231 | error(ERR_NONFATAL, |
| 2232 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2233 | } |
| 2234 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2235 | tline = tline->next->next; |
| 2236 | if (tok_is_(tline, "*")) { |
| 2237 | def->nparam_max = INT_MAX; |
| 2238 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2239 | error(ERR_NONFATAL, |
| 2240 | "`%s' expects a parameter count after `-'", directive); |
| 2241 | } else { |
| 2242 | def->nparam_max = readnum(tline->text, &err); |
| 2243 | if (err) { |
| 2244 | error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
| 2245 | tline->text); |
| 2246 | } |
| 2247 | if (def->nparam_min > def->nparam_max) { |
| 2248 | error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
| 2249 | } |
| 2250 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2251 | } |
| 2252 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2253 | tline = tline->next; |
| 2254 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2255 | } |
| 2256 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2257 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2258 | tline = tline->next; |
| 2259 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2260 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2261 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2262 | /* |
| 2263 | * Handle default parameters. |
| 2264 | */ |
| 2265 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2266 | def->dlist = tline->next; |
| 2267 | tline->next = NULL; |
| 2268 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2269 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2270 | def->dlist = NULL; |
| 2271 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2272 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2273 | def->line = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2274 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2275 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2276 | !def->plus) |
| 2277 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
| 2278 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2279 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2280 | return true; |
| 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2285 | * Decode a size directive |
| 2286 | */ |
| 2287 | static int parse_size(const char *str) { |
| 2288 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2289 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2290 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2291 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2292 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2293 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2294 | } |
| 2295 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2296 | /** |
| 2297 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2298 | * Find out if a line contains a preprocessor directive, and deal |
| 2299 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2300 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2301 | * If a directive _is_ found, it is the responsibility of this routine |
| 2302 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2303 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2304 | * @param tline a pointer to the current tokeninzed line linked list |
| 2305 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2306 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2307 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2308 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2309 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2310 | enum preproc_token i; |
| 2311 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2312 | bool err; |
| 2313 | int nparam; |
| 2314 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2315 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2316 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2317 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2318 | char *p, *pp; |
| 2319 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2320 | Include *inc; |
| 2321 | Context *ctx; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2322 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2323 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2324 | struct tokenval tokval; |
| 2325 | expr *evalresult; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2326 | ExpDef *ed, *eed, **edhead; |
| 2327 | ExpInv *ei, *eei; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2328 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2329 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2330 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2331 | |
| 2332 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2333 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2334 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2335 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2336 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2337 | || tline->text[1] == '!')) |
| 2338 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2339 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2340 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2341 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2342 | switch (i) { |
| 2343 | case PP_INVALID: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2344 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2345 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2346 | tline->text); |
| 2347 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2348 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2349 | case PP_STACKSIZE: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2350 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2351 | /* Directive to tell NASM what the default stack size is. The |
| 2352 | * default is for a 16-bit stack, and this can be overriden with |
| 2353 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2354 | */ |
| 2355 | tline = tline->next; |
| 2356 | if (tline && tline->type == TOK_WHITESPACE) |
| 2357 | tline = tline->next; |
| 2358 | if (!tline || tline->type != TOK_ID) { |
| 2359 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 2360 | free_tlist(origline); |
| 2361 | return DIRECTIVE_FOUND; |
| 2362 | } |
| 2363 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2364 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2365 | StackSize = 4; |
| 2366 | StackPointer = "ebp"; |
| 2367 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2368 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2369 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2370 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2371 | StackSize = 8; |
| 2372 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2373 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2374 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2375 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2376 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2377 | * far function call. |
| 2378 | */ |
| 2379 | StackSize = 2; |
| 2380 | StackPointer = "bp"; |
| 2381 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2382 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2383 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2384 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2385 | * far function call. We don't support near functions. |
| 2386 | */ |
| 2387 | StackSize = 2; |
| 2388 | StackPointer = "bp"; |
| 2389 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2390 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2391 | } else { |
| 2392 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 2393 | free_tlist(origline); |
| 2394 | return DIRECTIVE_FOUND; |
| 2395 | } |
| 2396 | free_tlist(origline); |
| 2397 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2398 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2399 | case PP_ARG: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2400 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2401 | /* TASM like ARG directive to define arguments to functions, in |
| 2402 | * the following form: |
| 2403 | * |
| 2404 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2405 | */ |
| 2406 | offset = ArgOffset; |
| 2407 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2408 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2409 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2410 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2411 | /* Find the argument name */ |
| 2412 | tline = tline->next; |
| 2413 | if (tline && tline->type == TOK_WHITESPACE) |
| 2414 | tline = tline->next; |
| 2415 | if (!tline || tline->type != TOK_ID) { |
| 2416 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 2417 | free_tlist(origline); |
| 2418 | return DIRECTIVE_FOUND; |
| 2419 | } |
| 2420 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2421 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2422 | /* Find the argument size type */ |
| 2423 | tline = tline->next; |
| 2424 | if (!tline || tline->type != TOK_OTHER |
| 2425 | || tline->text[0] != ':') { |
| 2426 | error(ERR_NONFATAL, |
| 2427 | "Syntax error processing `%%arg' directive"); |
| 2428 | free_tlist(origline); |
| 2429 | return DIRECTIVE_FOUND; |
| 2430 | } |
| 2431 | tline = tline->next; |
| 2432 | if (!tline || tline->type != TOK_ID) { |
| 2433 | error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
| 2434 | free_tlist(origline); |
| 2435 | return DIRECTIVE_FOUND; |
| 2436 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2437 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2438 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2439 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2440 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2441 | size = parse_size(tt->text); |
| 2442 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2443 | error(ERR_NONFATAL, |
| 2444 | "Invalid size type for `%%arg' missing directive"); |
| 2445 | free_tlist(tt); |
| 2446 | free_tlist(origline); |
| 2447 | return DIRECTIVE_FOUND; |
| 2448 | } |
| 2449 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2450 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2451 | /* Round up to even stack slots */ |
| 2452 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2453 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2454 | /* Now define the macro for the argument */ |
| 2455 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2456 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2457 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2458 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2459 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2460 | /* Move to the next argument in the list */ |
| 2461 | tline = tline->next; |
| 2462 | if (tline && tline->type == TOK_WHITESPACE) |
| 2463 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2464 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2465 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2466 | free_tlist(origline); |
| 2467 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2468 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2469 | case PP_LOCAL: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2470 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2471 | /* TASM like LOCAL directive to define local variables for a |
| 2472 | * function, in the following form: |
| 2473 | * |
| 2474 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2475 | * |
| 2476 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2477 | * required by TASM to define the local parameter size (and used |
| 2478 | * by the TASM macro package). |
| 2479 | */ |
| 2480 | offset = LocalOffset; |
| 2481 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2482 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2483 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2484 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2485 | /* Find the argument name */ |
| 2486 | tline = tline->next; |
| 2487 | if (tline && tline->type == TOK_WHITESPACE) |
| 2488 | tline = tline->next; |
| 2489 | if (!tline || tline->type != TOK_ID) { |
| 2490 | error(ERR_NONFATAL, |
| 2491 | "`%%local' missing argument parameter"); |
| 2492 | free_tlist(origline); |
| 2493 | return DIRECTIVE_FOUND; |
| 2494 | } |
| 2495 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2496 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2497 | /* Find the argument size type */ |
| 2498 | tline = tline->next; |
| 2499 | if (!tline || tline->type != TOK_OTHER |
| 2500 | || tline->text[0] != ':') { |
| 2501 | error(ERR_NONFATAL, |
| 2502 | "Syntax error processing `%%local' directive"); |
| 2503 | free_tlist(origline); |
| 2504 | return DIRECTIVE_FOUND; |
| 2505 | } |
| 2506 | tline = tline->next; |
| 2507 | if (!tline || tline->type != TOK_ID) { |
| 2508 | error(ERR_NONFATAL, |
| 2509 | "`%%local' missing size type parameter"); |
| 2510 | free_tlist(origline); |
| 2511 | return DIRECTIVE_FOUND; |
| 2512 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2513 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2514 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2515 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2516 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2517 | size = parse_size(tt->text); |
| 2518 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2519 | error(ERR_NONFATAL, |
| 2520 | "Invalid size type for `%%local' missing directive"); |
| 2521 | free_tlist(tt); |
| 2522 | free_tlist(origline); |
| 2523 | return DIRECTIVE_FOUND; |
| 2524 | } |
| 2525 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2526 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2527 | /* Round up to even stack slots */ |
| 2528 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2529 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2530 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2531 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2532 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2533 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2534 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2535 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2536 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2537 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2538 | snprintf(directive, sizeof(directive), |
| 2539 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2540 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2541 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2542 | /* Move to the next argument in the list */ |
| 2543 | tline = tline->next; |
| 2544 | if (tline && tline->type == TOK_WHITESPACE) |
| 2545 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2546 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2547 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2548 | free_tlist(origline); |
| 2549 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2550 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2551 | case PP_CLEAR: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2552 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2553 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2554 | error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2555 | "trailing garbage after `%%clear' ignored"); |
| 2556 | free_macros(); |
| 2557 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2558 | free_tlist(origline); |
| 2559 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2560 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2561 | case PP_DEPEND: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2562 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2563 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2564 | skip_white_(t); |
| 2565 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2566 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2567 | error(ERR_NONFATAL, "`%%depend' expects a file name"); |
| 2568 | free_tlist(origline); |
| 2569 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2570 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2571 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2572 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2573 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2574 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2575 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2576 | nasm_unquote_cstr(p, i); |
| 2577 | if (dephead && !in_list(*dephead, p)) { |
| 2578 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2579 | sl->next = NULL; |
| 2580 | strcpy(sl->str, p); |
| 2581 | *deptail = sl; |
| 2582 | deptail = &sl->next; |
| 2583 | } |
| 2584 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2585 | return DIRECTIVE_FOUND; |
| 2586 | |
| 2587 | case PP_INCLUDE: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2588 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2589 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2590 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2591 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2592 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2593 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2594 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2595 | free_tlist(origline); |
| 2596 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2597 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2598 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2599 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2600 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2601 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2602 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2603 | nasm_unquote_cstr(p, i); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2604 | inc = nasm_malloc(sizeof(Include)); |
| 2605 | inc->next = istk; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2606 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2607 | if (!inc->fp) { |
| 2608 | /* -MG given but file not found */ |
| 2609 | nasm_free(inc); |
| 2610 | } else { |
| 2611 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2612 | inc->lineno = src_set_linnum(0); |
| 2613 | inc->lineinc = 1; |
| 2614 | inc->expansion = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2615 | istk = inc; |
| 2616 | list->uplevel(LIST_INCLUDE); |
| 2617 | } |
| 2618 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2619 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2620 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2621 | case PP_USE: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2622 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2623 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2624 | static macros_t *use_pkg; |
| 2625 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2626 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2627 | tline = tline->next; |
| 2628 | skip_white_(tline); |
| 2629 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2630 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2631 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2632 | tline->type != TOK_INTERNAL_STRING && |
| 2633 | tline->type != TOK_ID)) { |
H. Peter Anvin | 926fc40 | 2008-06-19 16:26:12 -0700 | [diff] [blame] | 2634 | error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2635 | free_tlist(origline); |
| 2636 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2637 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2638 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2639 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2640 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2641 | if (tline->type == TOK_STRING) |
| 2642 | nasm_unquote_cstr(tline->text, i); |
| 2643 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2644 | if (!use_pkg) |
| 2645 | error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
| 2646 | else |
| 2647 | 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] | 2648 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2649 | /* Not already included, go ahead and include it */ |
| 2650 | stdmacpos = use_pkg; |
| 2651 | } |
| 2652 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2653 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2654 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2655 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2656 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2657 | case PP_POP: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2658 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2659 | tline = tline->next; |
| 2660 | skip_white_(tline); |
| 2661 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2662 | if (tline) { |
| 2663 | if (!tok_type_(tline, TOK_ID)) { |
| 2664 | error(ERR_NONFATAL, "`%s' expects a context identifier", |
| 2665 | pp_directives[i]); |
| 2666 | free_tlist(origline); |
| 2667 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2668 | } |
| 2669 | if (tline->next) |
| 2670 | error(ERR_WARNING|ERR_PASS1, |
| 2671 | "trailing garbage after `%s' ignored", |
| 2672 | pp_directives[i]); |
| 2673 | p = nasm_strdup(tline->text); |
| 2674 | } else { |
| 2675 | p = NULL; /* Anonymous */ |
| 2676 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2677 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2678 | if (i == PP_PUSH) { |
| 2679 | ctx = nasm_malloc(sizeof(Context)); |
| 2680 | ctx->next = cstk; |
| 2681 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2682 | ctx->name = p; |
| 2683 | ctx->number = unique++; |
| 2684 | cstk = ctx; |
| 2685 | } else { |
| 2686 | /* %pop or %repl */ |
| 2687 | if (!cstk) { |
| 2688 | error(ERR_NONFATAL, "`%s': context stack is empty", |
| 2689 | pp_directives[i]); |
| 2690 | } else if (i == PP_POP) { |
| 2691 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
| 2692 | error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
| 2693 | "expected %s", |
| 2694 | cstk->name ? cstk->name : "anonymous", p); |
| 2695 | else |
| 2696 | ctx_pop(); |
| 2697 | } else { |
| 2698 | /* i == PP_REPL */ |
| 2699 | nasm_free(cstk->name); |
| 2700 | cstk->name = p; |
| 2701 | p = NULL; |
| 2702 | } |
| 2703 | nasm_free(p); |
| 2704 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2705 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2706 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2707 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2708 | severity = ERR_FATAL; |
| 2709 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2710 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2711 | severity = ERR_NONFATAL; |
| 2712 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2713 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2714 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2715 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2716 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2717 | issue_error: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2718 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2719 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2720 | /* Only error out if this is the final pass */ |
| 2721 | if (pass != 2 && i != PP_FATAL) |
| 2722 | return DIRECTIVE_FOUND; |
| 2723 | |
| 2724 | tline->next = expand_smacro(tline->next); |
| 2725 | tline = tline->next; |
| 2726 | skip_white_(tline); |
| 2727 | t = tline ? tline->next : NULL; |
| 2728 | skip_white_(t); |
| 2729 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2730 | /* The line contains only a quoted string */ |
| 2731 | p = tline->text; |
| 2732 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
| 2733 | error(severity, "%s", p); |
| 2734 | } else { |
| 2735 | /* Not a quoted string, or more than a quoted string */ |
| 2736 | p = detoken(tline, false); |
| 2737 | error(severity, "%s", p); |
| 2738 | nasm_free(p); |
| 2739 | } |
| 2740 | free_tlist(origline); |
| 2741 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2742 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2743 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2744 | CASE_PP_IF: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2745 | if (defining != NULL) { |
| 2746 | if (defining->type == EXP_IF) { |
| 2747 | defining->def_depth ++; |
| 2748 | } |
| 2749 | return NO_DIRECTIVE_FOUND; |
| 2750 | } |
| 2751 | if ((istk->expansion != NULL) && |
| 2752 | (istk->expansion->emitting == false)) { |
| 2753 | j = COND_NEVER; |
| 2754 | } else { |
| 2755 | j = if_condition(tline->next, i); |
| 2756 | tline->next = NULL; /* it got freed */ |
| 2757 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 2758 | } |
| 2759 | ed = new_ExpDef(EXP_IF); |
| 2760 | ed->state = j; |
| 2761 | ed->nolist = NULL; |
| 2762 | ed->def_depth = 0; |
| 2763 | ed->cur_depth = 0; |
| 2764 | ed->max_depth = 0; |
| 2765 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 2766 | ed->prev = defining; |
| 2767 | defining = ed; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2768 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2769 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2770 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2771 | CASE_PP_ELIF: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2772 | if (defining != NULL) { |
| 2773 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2774 | return NO_DIRECTIVE_FOUND; |
| 2775 | } |
| 2776 | } |
| 2777 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2778 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2779 | } |
| 2780 | switch (defining->state) { |
| 2781 | case COND_IF_TRUE: |
| 2782 | defining->state = COND_DONE; |
| 2783 | defining->ignoring = true; |
| 2784 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2785 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2786 | case COND_DONE: |
| 2787 | case COND_NEVER: |
| 2788 | defining->ignoring = true; |
| 2789 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2790 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2791 | case COND_ELSE_TRUE: |
| 2792 | case COND_ELSE_FALSE: |
| 2793 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2794 | "`%%elif' after `%%else' ignored"); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2795 | defining->state = COND_NEVER; |
| 2796 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2797 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2798 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2799 | case COND_IF_FALSE: |
| 2800 | /* |
| 2801 | * IMPORTANT: In the case of %if, we will already have |
| 2802 | * called expand_mmac_params(); however, if we're |
| 2803 | * processing an %elif we must have been in a |
| 2804 | * non-emitting mode, which would have inhibited |
| 2805 | * the normal invocation of expand_mmac_params(). |
| 2806 | * Therefore, we have to do it explicitly here. |
| 2807 | */ |
| 2808 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2809 | tline->next = NULL; /* it got freed */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2810 | defining->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2811 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2812 | defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2813 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2814 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2815 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2816 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2817 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2818 | case PP_ELSE: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2819 | if (defining != NULL) { |
| 2820 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2821 | return NO_DIRECTIVE_FOUND; |
| 2822 | } |
| 2823 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2824 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2825 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2826 | "trailing garbage after `%%else' ignored"); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2827 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2828 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2829 | } |
| 2830 | switch (defining->state) { |
| 2831 | case COND_IF_TRUE: |
| 2832 | case COND_DONE: |
| 2833 | defining->state = COND_ELSE_FALSE; |
| 2834 | defining->ignoring = true; |
| 2835 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2836 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2837 | case COND_NEVER: |
| 2838 | defining->ignoring = true; |
| 2839 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2840 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2841 | case COND_IF_FALSE: |
| 2842 | defining->state = COND_ELSE_TRUE; |
| 2843 | defining->ignoring = false; |
| 2844 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2845 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2846 | case COND_ELSE_TRUE: |
| 2847 | case COND_ELSE_FALSE: |
| 2848 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2849 | "`%%else' after `%%else' ignored."); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2850 | defining->state = COND_NEVER; |
| 2851 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2852 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2853 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2854 | free_tlist(origline); |
| 2855 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2856 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2857 | case PP_ENDIF: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2858 | if (defining != NULL) { |
| 2859 | if (defining->type == EXP_IF) { |
| 2860 | if (defining->def_depth > 0) { |
| 2861 | defining->def_depth --; |
| 2862 | return NO_DIRECTIVE_FOUND; |
| 2863 | } |
| 2864 | } else { |
| 2865 | return NO_DIRECTIVE_FOUND; |
| 2866 | } |
| 2867 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2868 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2869 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2870 | "trailing garbage after `%%endif' ignored"); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2871 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2872 | error(ERR_NONFATAL, "`%%endif': no matching `%%if'"); |
| 2873 | return DIRECTIVE_FOUND; |
| 2874 | } |
| 2875 | ed = defining; |
| 2876 | defining = ed->prev; |
| 2877 | ed->prev = expansions; |
| 2878 | expansions = ed; |
| 2879 | ei = new_ExpInv(EXP_IF, ed); |
| 2880 | ei->current = ed->line; |
| 2881 | ei->emitting = true; |
| 2882 | ei->prev = istk->expansion; |
| 2883 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2884 | free_tlist(origline); |
| 2885 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2886 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2887 | case PP_RMACRO: |
| 2888 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2889 | case PP_MACRO: |
| 2890 | case PP_IMACRO: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2891 | if (defining != NULL) { |
| 2892 | if (defining->type == EXP_MMACRO) { |
| 2893 | defining->def_depth ++; |
| 2894 | } |
| 2895 | return NO_DIRECTIVE_FOUND; |
| 2896 | } |
| 2897 | ed = new_ExpDef(EXP_MMACRO); |
| 2898 | ed->max_depth = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2899 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2900 | ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
| 2901 | if (!parse_mmacro_spec(tline, ed, pp_directives[i])) { |
| 2902 | nasm_free(ed); |
| 2903 | ed = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2904 | return DIRECTIVE_FOUND; |
| 2905 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2906 | ed->def_depth = 0; |
| 2907 | ed->cur_depth = 0; |
| 2908 | ed->max_depth = (ed->max_depth + 1); |
| 2909 | ed->ignoring = false; |
| 2910 | ed->prev = defining; |
| 2911 | defining = ed; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2912 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2913 | eed = (ExpDef *) hash_findix(&expdefs, ed->name); |
| 2914 | while (eed) { |
| 2915 | if (!strcmp(eed->name, ed->name) && |
| 2916 | (eed->nparam_min <= ed->nparam_max |
| 2917 | || ed->plus) |
| 2918 | && (ed->nparam_min <= eed->nparam_max |
| 2919 | || eed->plus)) { |
| 2920 | error(ERR_WARNING|ERR_PASS1, |
| 2921 | "redefining multi-line macro `%s'", ed->name); |
| 2922 | return DIRECTIVE_FOUND; |
| 2923 | } |
| 2924 | eed = eed->next; |
| 2925 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2926 | free_tlist(origline); |
| 2927 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2928 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2929 | case PP_ENDM: |
| 2930 | case PP_ENDMACRO: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2931 | if (defining != NULL) { |
| 2932 | if (defining->type == EXP_MMACRO) { |
| 2933 | if (defining->def_depth > 0) { |
| 2934 | defining->def_depth --; |
| 2935 | return NO_DIRECTIVE_FOUND; |
| 2936 | } |
| 2937 | } else { |
| 2938 | return NO_DIRECTIVE_FOUND; |
| 2939 | } |
| 2940 | } |
| 2941 | if (!(defining) || (defining->type != EXP_MMACRO)) { |
| 2942 | error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
| 2943 | return DIRECTIVE_FOUND; |
| 2944 | } |
| 2945 | edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name); |
| 2946 | defining->next = *edhead; |
| 2947 | *edhead = defining; |
| 2948 | ed = defining; |
| 2949 | defining = ed->prev; |
| 2950 | ed->prev = expansions; |
| 2951 | expansions = ed; |
| 2952 | ed = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2953 | free_tlist(origline); |
| 2954 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2955 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2956 | case PP_EXITMACRO: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2957 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 2958 | /* |
| 2959 | * We must search along istk->expansion until we hit a |
| 2960 | * macro invocation. Then we disable the emitting state(s) |
| 2961 | * between exitmacro and endmacro. |
| 2962 | */ |
| 2963 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 2964 | if(ei->type == EXP_MMACRO) { |
| 2965 | break; |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | if (ei != NULL) { |
| 2970 | /* |
| 2971 | * Set all invocations leading back to the macro |
| 2972 | * invocation to a non-emitting state. |
| 2973 | */ |
| 2974 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 2975 | eei->emitting = false; |
| 2976 | } |
| 2977 | eei->emitting = false; |
| 2978 | } else { |
| 2979 | error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
| 2980 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 2981 | free_tlist(origline); |
| 2982 | return DIRECTIVE_FOUND; |
| 2983 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2984 | case PP_UNMACRO: |
| 2985 | case PP_UNIMACRO: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2986 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2987 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2988 | ExpDef **ed_p; |
| 2989 | ExpDef spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2990 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2991 | spec.casesense = (i == PP_UNMACRO); |
| 2992 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2993 | return DIRECTIVE_FOUND; |
| 2994 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2995 | ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL); |
| 2996 | while (ed_p && *ed_p) { |
| 2997 | ed = *ed_p; |
| 2998 | if (ed->casesense == spec.casesense && |
| 2999 | !mstrcmp(ed->name, spec.name, spec.casesense) && |
| 3000 | ed->nparam_min == spec.nparam_min && |
| 3001 | ed->nparam_max == spec.nparam_max && |
| 3002 | ed->plus == spec.plus) { |
| 3003 | *ed_p = ed->next; |
| 3004 | free_expdef(ed); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3005 | } else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3006 | ed_p = &ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | free_tlist(origline); |
| 3010 | free_tlist(spec.dlist); |
| 3011 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3012 | } |
| 3013 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3014 | case PP_ROTATE: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3015 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3016 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3017 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3018 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3019 | free_tlist(origline); |
| 3020 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 3021 | return DIRECTIVE_FOUND; |
| 3022 | } |
| 3023 | t = expand_smacro(tline->next); |
| 3024 | tline->next = NULL; |
| 3025 | free_tlist(origline); |
| 3026 | tline = t; |
| 3027 | tptr = &t; |
| 3028 | tokval.t_type = TOKEN_INVALID; |
| 3029 | evalresult = |
| 3030 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3031 | free_tlist(tline); |
| 3032 | if (!evalresult) |
| 3033 | return DIRECTIVE_FOUND; |
| 3034 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3035 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3036 | "trailing garbage after expression ignored"); |
| 3037 | if (!is_simple(evalresult)) { |
| 3038 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
| 3039 | return DIRECTIVE_FOUND; |
| 3040 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3041 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3042 | if (ei->type == EXP_MMACRO) { |
| 3043 | break; |
| 3044 | } |
| 3045 | } |
| 3046 | if (ei == NULL) { |
| 3047 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
| 3048 | } else if (ei->nparam == 0) { |
| 3049 | error(ERR_NONFATAL, |
| 3050 | "`%%rotate' invoked within macro without parameters"); |
| 3051 | } else { |
| 3052 | int rotate = ei->rotate + reloc_value(evalresult); |
| 3053 | |
| 3054 | rotate %= (int)ei->nparam; |
| 3055 | if (rotate < 0) |
| 3056 | rotate += ei->nparam; |
| 3057 | ei->rotate = rotate; |
| 3058 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3059 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3060 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3061 | case PP_REP: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3062 | if (defining != NULL) { |
| 3063 | if (defining->type == EXP_REP) { |
| 3064 | defining->def_depth ++; |
| 3065 | } |
| 3066 | return NO_DIRECTIVE_FOUND; |
| 3067 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3068 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3069 | do { |
| 3070 | tline = tline->next; |
| 3071 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3072 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3073 | if (tok_type_(tline, TOK_ID) && |
| 3074 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3075 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3076 | do { |
| 3077 | tline = tline->next; |
| 3078 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3079 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3080 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3081 | if (tline) { |
| 3082 | t = expand_smacro(tline); |
| 3083 | tptr = &t; |
| 3084 | tokval.t_type = TOKEN_INVALID; |
| 3085 | evalresult = |
| 3086 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3087 | if (!evalresult) { |
| 3088 | free_tlist(origline); |
| 3089 | return DIRECTIVE_FOUND; |
| 3090 | } |
| 3091 | if (tokval.t_type) |
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 expression ignored"); |
| 3094 | if (!is_simple(evalresult)) { |
| 3095 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 3096 | return DIRECTIVE_FOUND; |
| 3097 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3098 | count = reloc_value(evalresult); |
| 3099 | if (count >= REP_LIMIT) { |
Cyrill Gorcunov | 71f4f84 | 2010-08-09 20:17:17 +0400 | [diff] [blame] | 3100 | error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3101 | count = 0; |
| 3102 | } else |
| 3103 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3104 | } else { |
| 3105 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3106 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3107 | } |
| 3108 | free_tlist(origline); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3109 | ed = new_ExpDef(EXP_REP); |
| 3110 | ed->nolist = nolist; |
| 3111 | ed->def_depth = 0; |
| 3112 | ed->cur_depth = 1; |
| 3113 | ed->max_depth = (count - 1); |
| 3114 | ed->ignoring = false; |
| 3115 | ed->prev = defining; |
| 3116 | defining = ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3117 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3118 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3119 | case PP_ENDREP: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3120 | if (defining != NULL) { |
| 3121 | if (defining->type == EXP_REP) { |
| 3122 | if (defining->def_depth > 0) { |
| 3123 | defining->def_depth --; |
| 3124 | return NO_DIRECTIVE_FOUND; |
| 3125 | } |
| 3126 | } else { |
| 3127 | return NO_DIRECTIVE_FOUND; |
| 3128 | } |
| 3129 | } |
| 3130 | if ((defining == NULL) || (defining->type != EXP_REP)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3131 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
| 3132 | return DIRECTIVE_FOUND; |
| 3133 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3134 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3135 | /* |
| 3136 | * Now we have a "macro" defined - although it has no name |
| 3137 | * and we won't be entering it in the hash tables - we must |
| 3138 | * push a macro-end marker for it on to istk->expansion. |
| 3139 | * After that, it will take care of propagating itself (a |
| 3140 | * macro-end marker line for a macro which is really a %rep |
| 3141 | * block will cause the macro to be re-expanded, complete |
| 3142 | * with another macro-end marker to ensure the process |
| 3143 | * continues) until the whole expansion is forcibly removed |
| 3144 | * from istk->expansion by a %exitrep. |
| 3145 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3146 | ed = defining; |
| 3147 | defining = ed->prev; |
| 3148 | ed->prev = expansions; |
| 3149 | expansions = ed; |
| 3150 | ei = new_ExpInv(EXP_REP, ed); |
| 3151 | ei->current = ed->line; |
| 3152 | ei->emitting = ((ed->max_depth > 0) ? true : false); |
| 3153 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 3154 | ei->prev = istk->expansion; |
| 3155 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3156 | free_tlist(origline); |
| 3157 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3158 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3159 | case PP_EXITREP: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3160 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3161 | /* |
| 3162 | * We must search along istk->expansion until we hit a |
| 3163 | * rep invocation. Then we disable the emitting state(s) |
| 3164 | * between exitrep and endrep. |
| 3165 | */ |
| 3166 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3167 | if (ei->type == EXP_REP) { |
| 3168 | break; |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | if (ei != NULL) { |
| 3173 | /* |
| 3174 | * Set all invocations leading back to the rep |
| 3175 | * invocation to a non-emitting state. |
| 3176 | */ |
| 3177 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3178 | eei->emitting = false; |
| 3179 | } |
| 3180 | eei->emitting = false; |
| 3181 | eei->current = NULL; |
| 3182 | eei->def->cur_depth = eei->def->max_depth; |
| 3183 | } else { |
| 3184 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 3185 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3186 | free_tlist(origline); |
| 3187 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3188 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3189 | case PP_XDEFINE: |
| 3190 | case PP_IXDEFINE: |
| 3191 | case PP_DEFINE: |
| 3192 | case PP_IDEFINE: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3193 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3194 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3195 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3196 | tline = tline->next; |
| 3197 | skip_white_(tline); |
| 3198 | tline = expand_id(tline); |
| 3199 | if (!tline || (tline->type != TOK_ID && |
| 3200 | (tline->type != TOK_PREPROC_ID || |
| 3201 | tline->text[1] != '$'))) { |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3202 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3203 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3204 | free_tlist(origline); |
| 3205 | return DIRECTIVE_FOUND; |
| 3206 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3207 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3208 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3209 | last = tline; |
| 3210 | param_start = tline = tline->next; |
| 3211 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3212 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3213 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3214 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3215 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3216 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3217 | if (tok_is_(tline, "(")) { |
| 3218 | /* |
| 3219 | * This macro has parameters. |
| 3220 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3221 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3222 | tline = tline->next; |
| 3223 | while (1) { |
| 3224 | skip_white_(tline); |
| 3225 | if (!tline) { |
| 3226 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 3227 | free_tlist(origline); |
| 3228 | return DIRECTIVE_FOUND; |
| 3229 | } |
| 3230 | if (tline->type != TOK_ID) { |
| 3231 | error(ERR_NONFATAL, |
| 3232 | "`%s': parameter identifier expected", |
| 3233 | tline->text); |
| 3234 | free_tlist(origline); |
| 3235 | return DIRECTIVE_FOUND; |
| 3236 | } |
| 3237 | tline->type = TOK_SMAC_PARAM + nparam++; |
| 3238 | tline = tline->next; |
| 3239 | skip_white_(tline); |
| 3240 | if (tok_is_(tline, ",")) { |
| 3241 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3242 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3243 | if (!tok_is_(tline, ")")) { |
| 3244 | error(ERR_NONFATAL, |
| 3245 | "`)' expected to terminate macro template"); |
| 3246 | free_tlist(origline); |
| 3247 | return DIRECTIVE_FOUND; |
| 3248 | } |
| 3249 | break; |
| 3250 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3251 | } |
| 3252 | last = tline; |
| 3253 | tline = tline->next; |
| 3254 | } |
| 3255 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3256 | last = tline, tline = tline->next; |
| 3257 | macro_start = NULL; |
| 3258 | last->next = NULL; |
| 3259 | t = tline; |
| 3260 | while (t) { |
| 3261 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3262 | list_for_each(tt, param_start) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3263 | if (tt->type >= TOK_SMAC_PARAM && |
| 3264 | !strcmp(tt->text, t->text)) |
| 3265 | t->type = tt->type; |
| 3266 | } |
| 3267 | tt = t->next; |
| 3268 | t->next = macro_start; |
| 3269 | macro_start = t; |
| 3270 | t = tt; |
| 3271 | } |
| 3272 | /* |
| 3273 | * Good. We now have a macro name, a parameter count, and a |
| 3274 | * token list (in reverse order) for an expansion. We ought |
| 3275 | * to be OK just to create an SMacro, store it, and let |
| 3276 | * free_tlist have the rest of the line (which we have |
| 3277 | * carefully re-terminated after chopping off the expansion |
| 3278 | * from the end). |
| 3279 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3280 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3281 | free_tlist(origline); |
| 3282 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3283 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3284 | case PP_UNDEF: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3285 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3286 | tline = tline->next; |
| 3287 | skip_white_(tline); |
| 3288 | tline = expand_id(tline); |
| 3289 | if (!tline || (tline->type != TOK_ID && |
| 3290 | (tline->type != TOK_PREPROC_ID || |
| 3291 | tline->text[1] != '$'))) { |
| 3292 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 3293 | free_tlist(origline); |
| 3294 | return DIRECTIVE_FOUND; |
| 3295 | } |
| 3296 | if (tline->next) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3297 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3298 | "trailing garbage after macro name ignored"); |
| 3299 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3300 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3301 | /* Find the context that symbol belongs to */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3302 | ctx = get_ctx(tline->text, &mname, false); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3303 | undef_smacro(ctx, mname); |
| 3304 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3305 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3306 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3307 | case PP_DEFSTR: |
| 3308 | case PP_IDEFSTR: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3309 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3310 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3311 | |
| 3312 | tline = tline->next; |
| 3313 | skip_white_(tline); |
| 3314 | tline = expand_id(tline); |
| 3315 | if (!tline || (tline->type != TOK_ID && |
| 3316 | (tline->type != TOK_PREPROC_ID || |
| 3317 | tline->text[1] != '$'))) { |
| 3318 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3319 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3320 | free_tlist(origline); |
| 3321 | return DIRECTIVE_FOUND; |
| 3322 | } |
| 3323 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3324 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3325 | last = tline; |
| 3326 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3327 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3328 | |
| 3329 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3330 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3331 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3332 | p = detoken(tline, false); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3333 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3334 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3335 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3336 | macro_start->type = TOK_STRING; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3337 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3338 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3339 | |
| 3340 | /* |
| 3341 | * We now have a macro name, an implicit parameter count of |
| 3342 | * zero, and a string token to use as an expansion. Create |
| 3343 | * and store an SMacro. |
| 3344 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3345 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3346 | free_tlist(origline); |
| 3347 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3348 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3349 | case PP_DEFTOK: |
| 3350 | case PP_IDEFTOK: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3351 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3352 | casesense = (i == PP_DEFTOK); |
| 3353 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3354 | tline = tline->next; |
| 3355 | skip_white_(tline); |
| 3356 | tline = expand_id(tline); |
| 3357 | if (!tline || (tline->type != TOK_ID && |
| 3358 | (tline->type != TOK_PREPROC_ID || |
| 3359 | tline->text[1] != '$'))) { |
| 3360 | error(ERR_NONFATAL, |
| 3361 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3362 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3363 | free_tlist(origline); |
| 3364 | return DIRECTIVE_FOUND; |
| 3365 | } |
| 3366 | ctx = get_ctx(tline->text, &mname, false); |
| 3367 | last = tline; |
| 3368 | tline = expand_smacro(tline->next); |
| 3369 | last->next = NULL; |
| 3370 | |
| 3371 | t = tline; |
| 3372 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3373 | t = t->next; |
| 3374 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3375 | if (!tok_type_(t, TOK_STRING)) { |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3376 | error(ERR_NONFATAL, |
| 3377 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3378 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3379 | free_tlist(tline); |
| 3380 | free_tlist(origline); |
| 3381 | return DIRECTIVE_FOUND; |
| 3382 | } |
| 3383 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3384 | /* |
| 3385 | * Convert the string to a token stream. Note that smacros |
| 3386 | * are stored with the token stream reversed, so we have to |
| 3387 | * reverse the output of tokenize(). |
| 3388 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3389 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3390 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3391 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3392 | /* |
| 3393 | * We now have a macro name, an implicit parameter count of |
| 3394 | * zero, and a numeric token to use as an expansion. Create |
| 3395 | * and store an SMacro. |
| 3396 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3397 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3398 | free_tlist(tline); |
| 3399 | free_tlist(origline); |
| 3400 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3401 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3402 | case PP_PATHSEARCH: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3403 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3404 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3405 | FILE *fp; |
| 3406 | StrList *xsl = NULL; |
| 3407 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3408 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3409 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3410 | |
| 3411 | tline = tline->next; |
| 3412 | skip_white_(tline); |
| 3413 | tline = expand_id(tline); |
| 3414 | if (!tline || (tline->type != TOK_ID && |
| 3415 | (tline->type != TOK_PREPROC_ID || |
| 3416 | tline->text[1] != '$'))) { |
| 3417 | error(ERR_NONFATAL, |
| 3418 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3419 | free_tlist(origline); |
| 3420 | return DIRECTIVE_FOUND; |
| 3421 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3422 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3423 | last = tline; |
| 3424 | tline = expand_smacro(tline->next); |
| 3425 | last->next = NULL; |
| 3426 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3427 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3428 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3429 | t = t->next; |
| 3430 | |
| 3431 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3432 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3433 | error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3434 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3435 | free_tlist(origline); |
| 3436 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3437 | } |
| 3438 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3439 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3440 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3441 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3442 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3443 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3444 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3445 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3446 | if (fp) { |
| 3447 | p = xsl->str; |
| 3448 | fclose(fp); /* Don't actually care about the file */ |
| 3449 | } |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3450 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3451 | macro_start->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3452 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3453 | macro_start->type = TOK_STRING; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3454 | macro_start->a.mac = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3455 | if (xsl) |
| 3456 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3457 | |
| 3458 | /* |
| 3459 | * We now have a macro name, an implicit parameter count of |
| 3460 | * zero, and a string token to use as an expansion. Create |
| 3461 | * and store an SMacro. |
| 3462 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3463 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3464 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3465 | free_tlist(origline); |
| 3466 | return DIRECTIVE_FOUND; |
| 3467 | } |
| 3468 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3469 | case PP_STRLEN: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3470 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3471 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3472 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3473 | tline = tline->next; |
| 3474 | skip_white_(tline); |
| 3475 | tline = expand_id(tline); |
| 3476 | if (!tline || (tline->type != TOK_ID && |
| 3477 | (tline->type != TOK_PREPROC_ID || |
| 3478 | tline->text[1] != '$'))) { |
| 3479 | error(ERR_NONFATAL, |
| 3480 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3481 | free_tlist(origline); |
| 3482 | return DIRECTIVE_FOUND; |
| 3483 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3484 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3485 | last = tline; |
| 3486 | tline = expand_smacro(tline->next); |
| 3487 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3488 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3489 | t = tline; |
| 3490 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3491 | t = t->next; |
| 3492 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3493 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3494 | error(ERR_NONFATAL, |
| 3495 | "`%%strlen` requires string as second parameter"); |
| 3496 | free_tlist(tline); |
| 3497 | free_tlist(origline); |
| 3498 | return DIRECTIVE_FOUND; |
| 3499 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3500 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3501 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3502 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3503 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3504 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3505 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3506 | /* |
| 3507 | * We now have a macro name, an implicit parameter count of |
| 3508 | * zero, and a numeric token to use as an expansion. Create |
| 3509 | * and store an SMacro. |
| 3510 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3511 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3512 | free_tlist(tline); |
| 3513 | free_tlist(origline); |
| 3514 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3515 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3516 | case PP_STRCAT: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3517 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3518 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3519 | |
| 3520 | tline = tline->next; |
| 3521 | skip_white_(tline); |
| 3522 | tline = expand_id(tline); |
| 3523 | if (!tline || (tline->type != TOK_ID && |
| 3524 | (tline->type != TOK_PREPROC_ID || |
| 3525 | tline->text[1] != '$'))) { |
| 3526 | error(ERR_NONFATAL, |
| 3527 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3528 | free_tlist(origline); |
| 3529 | return DIRECTIVE_FOUND; |
| 3530 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3531 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3532 | last = tline; |
| 3533 | tline = expand_smacro(tline->next); |
| 3534 | last->next = NULL; |
| 3535 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3536 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3537 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3538 | switch (t->type) { |
| 3539 | case TOK_WHITESPACE: |
| 3540 | break; |
| 3541 | case TOK_STRING: |
| 3542 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3543 | break; |
| 3544 | case TOK_OTHER: |
| 3545 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3546 | break; |
| 3547 | /* else fall through */ |
| 3548 | default: |
| 3549 | error(ERR_NONFATAL, |
| 3550 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3551 | free_tlist(tline); |
| 3552 | free_tlist(origline); |
| 3553 | return DIRECTIVE_FOUND; |
| 3554 | } |
| 3555 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3556 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3557 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3558 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3559 | if (t->type == TOK_STRING) { |
| 3560 | memcpy(p, t->text, t->a.len); |
| 3561 | p += t->a.len; |
| 3562 | } |
| 3563 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3564 | |
| 3565 | /* |
| 3566 | * We now have a macro name, an implicit parameter count of |
| 3567 | * zero, and a numeric token to use as an expansion. Create |
| 3568 | * and store an SMacro. |
| 3569 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3570 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3571 | macro_start->text = nasm_quote(pp, len); |
| 3572 | nasm_free(pp); |
| 3573 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3574 | free_tlist(tline); |
| 3575 | free_tlist(origline); |
| 3576 | return DIRECTIVE_FOUND; |
| 3577 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3578 | case PP_SUBSTR: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3579 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3580 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3581 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3582 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3583 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3584 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3585 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3586 | tline = tline->next; |
| 3587 | skip_white_(tline); |
| 3588 | tline = expand_id(tline); |
| 3589 | if (!tline || (tline->type != TOK_ID && |
| 3590 | (tline->type != TOK_PREPROC_ID || |
| 3591 | tline->text[1] != '$'))) { |
| 3592 | error(ERR_NONFATAL, |
| 3593 | "`%%substr' expects a macro identifier as first parameter"); |
| 3594 | free_tlist(origline); |
| 3595 | return DIRECTIVE_FOUND; |
| 3596 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3597 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3598 | last = tline; |
| 3599 | tline = expand_smacro(tline->next); |
| 3600 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3601 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3602 | if (tline) /* skip expanded id */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3603 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3604 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3605 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3606 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3607 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3608 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3609 | error(ERR_NONFATAL, |
| 3610 | "`%%substr` requires string as second parameter"); |
| 3611 | free_tlist(tline); |
| 3612 | free_tlist(origline); |
| 3613 | return DIRECTIVE_FOUND; |
| 3614 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3615 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3616 | tt = t->next; |
| 3617 | tptr = &tt; |
| 3618 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3619 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3620 | pass, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3621 | if (!evalresult) { |
| 3622 | free_tlist(tline); |
| 3623 | free_tlist(origline); |
| 3624 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3625 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3626 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3627 | free_tlist(tline); |
| 3628 | free_tlist(origline); |
| 3629 | return DIRECTIVE_FOUND; |
| 3630 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3631 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3632 | |
| 3633 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3634 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3635 | if (!tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3636 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3637 | } else { |
| 3638 | tokval.t_type = TOKEN_INVALID; |
| 3639 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3640 | pass, error, NULL); |
| 3641 | if (!evalresult) { |
| 3642 | free_tlist(tline); |
| 3643 | free_tlist(origline); |
| 3644 | return DIRECTIVE_FOUND; |
| 3645 | } else if (!is_simple(evalresult)) { |
| 3646 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3647 | free_tlist(tline); |
| 3648 | free_tlist(origline); |
| 3649 | return DIRECTIVE_FOUND; |
| 3650 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3651 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3652 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3653 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3654 | len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3655 | /* make start and count being in range */ |
| 3656 | if (start < 0) |
| 3657 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3658 | if (count < 0) |
| 3659 | count = len + count + 1 - start; |
| 3660 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3661 | count = len - start; |
| 3662 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3663 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3664 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3665 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3666 | macro_start->next = NULL; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3667 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3668 | macro_start->type = TOK_STRING; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3669 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3670 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3671 | /* |
| 3672 | * We now have a macro name, an implicit parameter count of |
| 3673 | * zero, and a numeric token to use as an expansion. Create |
| 3674 | * and store an SMacro. |
| 3675 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3676 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3677 | free_tlist(tline); |
| 3678 | free_tlist(origline); |
| 3679 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3680 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3681 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3682 | case PP_ASSIGN: |
| 3683 | case PP_IASSIGN: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3684 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3685 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3686 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3687 | tline = tline->next; |
| 3688 | skip_white_(tline); |
| 3689 | tline = expand_id(tline); |
| 3690 | if (!tline || (tline->type != TOK_ID && |
| 3691 | (tline->type != TOK_PREPROC_ID || |
| 3692 | tline->text[1] != '$'))) { |
| 3693 | error(ERR_NONFATAL, |
| 3694 | "`%%%sassign' expects a macro identifier", |
| 3695 | (i == PP_IASSIGN ? "i" : "")); |
| 3696 | free_tlist(origline); |
| 3697 | return DIRECTIVE_FOUND; |
| 3698 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3699 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3700 | last = tline; |
| 3701 | tline = expand_smacro(tline->next); |
| 3702 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3703 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3704 | t = tline; |
| 3705 | tptr = &t; |
| 3706 | tokval.t_type = TOKEN_INVALID; |
| 3707 | evalresult = |
| 3708 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3709 | free_tlist(tline); |
| 3710 | if (!evalresult) { |
| 3711 | free_tlist(origline); |
| 3712 | return DIRECTIVE_FOUND; |
| 3713 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3714 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3715 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3716 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3717 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3718 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3719 | if (!is_simple(evalresult)) { |
| 3720 | error(ERR_NONFATAL, |
| 3721 | "non-constant value given to `%%%sassign'", |
| 3722 | (i == PP_IASSIGN ? "i" : "")); |
| 3723 | free_tlist(origline); |
| 3724 | return DIRECTIVE_FOUND; |
| 3725 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3726 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3727 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3728 | macro_start->next = NULL; |
| 3729 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3730 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3731 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3732 | /* |
| 3733 | * We now have a macro name, an implicit parameter count of |
| 3734 | * zero, and a numeric token to use as an expansion. Create |
| 3735 | * and store an SMacro. |
| 3736 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3737 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3738 | free_tlist(origline); |
| 3739 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3740 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3741 | case PP_LINE: |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3742 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3743 | /* |
| 3744 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3745 | */ |
| 3746 | tline = tline->next; |
| 3747 | skip_white_(tline); |
| 3748 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3749 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3750 | free_tlist(origline); |
| 3751 | return DIRECTIVE_FOUND; |
| 3752 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3753 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3754 | m = 1; |
| 3755 | tline = tline->next; |
| 3756 | if (tok_is_(tline, "+")) { |
| 3757 | tline = tline->next; |
| 3758 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3759 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3760 | free_tlist(origline); |
| 3761 | return DIRECTIVE_FOUND; |
| 3762 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3763 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3764 | tline = tline->next; |
| 3765 | } |
| 3766 | skip_white_(tline); |
| 3767 | src_set_linnum(k); |
| 3768 | istk->lineinc = m; |
| 3769 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3770 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3771 | } |
| 3772 | free_tlist(origline); |
| 3773 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3774 | |
| 3775 | case PP_WHILE: |
| 3776 | if (defining != NULL) { |
| 3777 | if (defining->type == EXP_WHILE) { |
| 3778 | defining->def_depth ++; |
| 3779 | } |
| 3780 | return NO_DIRECTIVE_FOUND; |
| 3781 | } |
| 3782 | l = NULL; |
| 3783 | if ((istk->expansion != NULL) && |
| 3784 | (istk->expansion->emitting == false)) { |
| 3785 | j = COND_NEVER; |
| 3786 | } else { |
| 3787 | l = new_Line(); |
| 3788 | l->first = copy_Token(tline->next); |
| 3789 | j = if_condition(tline->next, i); |
| 3790 | tline->next = NULL; /* it got freed */ |
| 3791 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 3792 | } |
| 3793 | ed = new_ExpDef(EXP_WHILE); |
| 3794 | ed->state = j; |
| 3795 | ed->cur_depth = 1; |
| 3796 | ed->max_depth = DEADMAN_LIMIT; |
| 3797 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 3798 | if (ed->ignoring == false) { |
| 3799 | ed->line = l; |
| 3800 | ed->last = l; |
| 3801 | } else if (l != NULL) { |
| 3802 | delete_Token(l->first); |
| 3803 | nasm_free(l); |
| 3804 | l = NULL; |
| 3805 | } |
| 3806 | ed->prev = defining; |
| 3807 | defining = ed; |
| 3808 | free_tlist(origline); |
| 3809 | return DIRECTIVE_FOUND; |
| 3810 | |
| 3811 | case PP_ENDWHILE: |
| 3812 | if (defining != NULL) { |
| 3813 | if (defining->type == EXP_WHILE) { |
| 3814 | if (defining->def_depth > 0) { |
| 3815 | defining->def_depth --; |
| 3816 | return NO_DIRECTIVE_FOUND; |
| 3817 | } |
| 3818 | } else { |
| 3819 | return NO_DIRECTIVE_FOUND; |
| 3820 | } |
| 3821 | } |
| 3822 | if (tline->next != NULL) { |
| 3823 | error_precond(ERR_WARNING|ERR_PASS1, |
| 3824 | "trailing garbage after `%%endwhile' ignored"); |
| 3825 | } |
| 3826 | if ((defining == NULL) || (defining->type != EXP_WHILE)) { |
| 3827 | error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'"); |
| 3828 | return DIRECTIVE_FOUND; |
| 3829 | } |
| 3830 | ed = defining; |
| 3831 | defining = ed->prev; |
| 3832 | if (ed->ignoring == false) { |
| 3833 | ed->prev = expansions; |
| 3834 | expansions = ed; |
| 3835 | ei = new_ExpInv(EXP_WHILE, ed); |
| 3836 | ei->current = ed->line->next; |
| 3837 | ei->emitting = true; |
| 3838 | ei->prev = istk->expansion; |
| 3839 | istk->expansion = ei; |
| 3840 | } else { |
| 3841 | nasm_free(ed); |
| 3842 | } |
| 3843 | free_tlist(origline); |
| 3844 | return DIRECTIVE_FOUND; |
| 3845 | |
| 3846 | case PP_EXITWHILE: |
| 3847 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3848 | /* |
| 3849 | * We must search along istk->expansion until we hit a |
| 3850 | * while invocation. Then we disable the emitting state(s) |
| 3851 | * between exitwhile and endwhile. |
| 3852 | */ |
| 3853 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3854 | if (ei->type == EXP_WHILE) { |
| 3855 | break; |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | if (ei != NULL) { |
| 3860 | /* |
| 3861 | * Set all invocations leading back to the while |
| 3862 | * invocation to a non-emitting state. |
| 3863 | */ |
| 3864 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3865 | eei->emitting = false; |
| 3866 | } |
| 3867 | eei->emitting = false; |
| 3868 | eei->current = NULL; |
| 3869 | eei->def->cur_depth = eei->def->max_depth; |
| 3870 | } else { |
| 3871 | error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block"); |
| 3872 | } |
| 3873 | free_tlist(origline); |
| 3874 | return DIRECTIVE_FOUND; |
| 3875 | |
| 3876 | case PP_COMMENT: |
| 3877 | if (defining != NULL) { |
| 3878 | if (defining->type == EXP_COMMENT) { |
| 3879 | defining->def_depth ++; |
| 3880 | } |
| 3881 | return NO_DIRECTIVE_FOUND; |
| 3882 | } |
| 3883 | ed = new_ExpDef(EXP_COMMENT); |
| 3884 | ed->ignoring = true; |
| 3885 | ed->prev = defining; |
| 3886 | defining = ed; |
| 3887 | free_tlist(origline); |
| 3888 | return DIRECTIVE_FOUND; |
| 3889 | |
| 3890 | case PP_ENDCOMMENT: |
| 3891 | if (defining != NULL) { |
| 3892 | if (defining->type == EXP_COMMENT) { |
| 3893 | if (defining->def_depth > 0) { |
| 3894 | defining->def_depth --; |
| 3895 | return NO_DIRECTIVE_FOUND; |
| 3896 | } |
| 3897 | } else { |
| 3898 | return NO_DIRECTIVE_FOUND; |
| 3899 | } |
| 3900 | } |
| 3901 | if ((defining == NULL) || (defining->type != EXP_COMMENT)) { |
| 3902 | error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'"); |
| 3903 | return DIRECTIVE_FOUND; |
| 3904 | } |
| 3905 | ed = defining; |
| 3906 | defining = ed->prev; |
| 3907 | nasm_free(ed); |
| 3908 | free_tlist(origline); |
| 3909 | return DIRECTIVE_FOUND; |
| 3910 | |
| 3911 | case PP_FINAL: |
| 3912 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3913 | if (in_final != false) { |
| 3914 | error(ERR_FATAL, "`%%final' cannot be used recursively"); |
| 3915 | } |
| 3916 | tline = tline->next; |
| 3917 | skip_white_(tline); |
| 3918 | if (tline == NULL) { |
| 3919 | error(ERR_NONFATAL, "`%%final' expects at least one parameter"); |
| 3920 | } else { |
| 3921 | l = new_Line(); |
| 3922 | l->first = copy_Token(tline); |
| 3923 | l->next = finals; |
| 3924 | finals = l; |
| 3925 | } |
| 3926 | free_tlist(origline); |
| 3927 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3928 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3929 | default: |
| 3930 | error(ERR_FATAL, |
| 3931 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3932 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3933 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3934 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3938 | * Ensure that a macro parameter contains a condition code and |
| 3939 | * nothing else. Return the condition code index if so, or -1 |
| 3940 | * otherwise. |
| 3941 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3942 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3943 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3944 | Token *tt; |
| 3945 | int i, j, k, m; |
| 3946 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3947 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3948 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3949 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3950 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3951 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3952 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3953 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3954 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3955 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3956 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3957 | |
| 3958 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 3959 | j = ARRAY_SIZE(conditions); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3960 | while (j - i > 1) { |
| 3961 | k = (j + i) / 2; |
| 3962 | m = nasm_stricmp(t->text, conditions[k]); |
| 3963 | if (m == 0) { |
| 3964 | i = k; |
| 3965 | j = -2; |
| 3966 | break; |
| 3967 | } else if (m < 0) { |
| 3968 | j = k; |
| 3969 | } else |
| 3970 | i = k; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3971 | } |
| 3972 | if (j != -2) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3973 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3974 | return i; |
| 3975 | } |
| 3976 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3977 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
| 3978 | int mnum, bool handle_paste_tokens) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3979 | { |
| 3980 | Token **tail, *t, *tt; |
| 3981 | Token **paste_head; |
| 3982 | bool did_paste = false; |
| 3983 | char *tmp; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 3984 | int i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3985 | |
| 3986 | /* Now handle token pasting... */ |
| 3987 | paste_head = NULL; |
| 3988 | tail = head; |
| 3989 | while ((t = *tail) && (tt = t->next)) { |
| 3990 | switch (t->type) { |
| 3991 | case TOK_WHITESPACE: |
| 3992 | if (tt->type == TOK_WHITESPACE) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3993 | /* Zap adjacent whitespace tokens */ |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3994 | t->next = delete_Token(tt); |
| 3995 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3996 | /* Do not advance paste_head here */ |
| 3997 | tail = &t->next; |
| 3998 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3999 | break; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4000 | case TOK_PASTE: /* %+ */ |
| 4001 | if (handle_paste_tokens) { |
| 4002 | /* Zap %+ and whitespace tokens to the right */ |
| 4003 | while (t && (t->type == TOK_WHITESPACE || |
| 4004 | t->type == TOK_PASTE)) |
| 4005 | t = *tail = delete_Token(t); |
| 4006 | if (!paste_head || !t) |
| 4007 | break; /* Nothing to paste with */ |
| 4008 | tail = paste_head; |
| 4009 | t = *tail; |
| 4010 | tt = t->next; |
| 4011 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 4012 | tt = t->next = delete_Token(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4013 | if (tt) { |
| 4014 | tmp = nasm_strcat(t->text, tt->text); |
| 4015 | delete_Token(t); |
| 4016 | tt = delete_Token(tt); |
| 4017 | t = *tail = tokenize(tmp); |
| 4018 | nasm_free(tmp); |
| 4019 | while (t->next) { |
| 4020 | tail = &t->next; |
| 4021 | t = t->next; |
| 4022 | } |
| 4023 | t->next = tt; /* Attach the remaining token chain */ |
| 4024 | did_paste = true; |
| 4025 | } |
| 4026 | paste_head = tail; |
| 4027 | tail = &t->next; |
| 4028 | break; |
| 4029 | } |
| 4030 | /* else fall through */ |
| 4031 | default: |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4032 | /* |
| 4033 | * Concatenation of tokens might look nontrivial |
| 4034 | * but in real it's pretty simple -- the caller |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4035 | * prepares the masks of token types to be concatenated |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4036 | * and we simply find matched sequences and slip |
| 4037 | * them together |
| 4038 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4039 | for (i = 0; i < mnum; i++) { |
| 4040 | if (PP_CONCAT_MASK(t->type) & m[i].mask_head) { |
| 4041 | size_t len = 0; |
| 4042 | char *tmp, *p; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4043 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4044 | while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4045 | len += strlen(tt->text); |
| 4046 | tt = tt->next; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4047 | } |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4048 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4049 | /* |
| 4050 | * Now tt points to the first token after |
| 4051 | * the potential paste area... |
| 4052 | */ |
| 4053 | if (tt != t->next) { |
| 4054 | /* We have at least two tokens... */ |
| 4055 | len += strlen(t->text); |
| 4056 | p = tmp = nasm_malloc(len+1); |
| 4057 | while (t != tt) { |
| 4058 | strcpy(p, t->text); |
| 4059 | p = strchr(p, '\0'); |
| 4060 | t = delete_Token(t); |
| 4061 | } |
| 4062 | t = *tail = tokenize(tmp); |
| 4063 | nasm_free(tmp); |
| 4064 | while (t->next) { |
| 4065 | tail = &t->next; |
| 4066 | t = t->next; |
| 4067 | } |
| 4068 | t->next = tt; /* Attach the remaining token chain */ |
| 4069 | did_paste = true; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4070 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4071 | paste_head = tail; |
| 4072 | tail = &t->next; |
| 4073 | break; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4074 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4075 | } |
| 4076 | if (i >= mnum) { /* no match */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4077 | tail = &t->next; |
| 4078 | if (!tok_type_(t->next, TOK_WHITESPACE)) |
| 4079 | paste_head = tail; |
| 4080 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4081 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4082 | } |
| 4083 | } |
| 4084 | return did_paste; |
| 4085 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4086 | |
| 4087 | /* |
| 4088 | * expands to a list of tokens from %{x:y} |
| 4089 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4090 | static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4091 | { |
| 4092 | Token *t = tline, **tt, *tm, *head; |
| 4093 | char *pos; |
| 4094 | int fst, lst, j, i; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4095 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4096 | pos = strchr(tline->text, ':'); |
| 4097 | nasm_assert(pos); |
| 4098 | |
| 4099 | lst = atoi(pos + 1); |
| 4100 | fst = atoi(tline->text + 1); |
| 4101 | |
| 4102 | /* |
| 4103 | * only macros params are accounted so |
| 4104 | * if someone passes %0 -- we reject such |
| 4105 | * value(s) |
| 4106 | */ |
| 4107 | if (lst == 0 || fst == 0) |
| 4108 | goto err; |
| 4109 | |
| 4110 | /* the values should be sane */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4111 | if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) || |
| 4112 | (lst > (int)ei->nparam || lst < (-(int)ei->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4113 | goto err; |
| 4114 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4115 | fst = fst < 0 ? fst + (int)ei->nparam + 1: fst; |
| 4116 | lst = lst < 0 ? lst + (int)ei->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4117 | |
| 4118 | /* counted from zero */ |
| 4119 | fst--, lst--; |
| 4120 | |
| 4121 | /* |
| 4122 | * it will be at least one token |
| 4123 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4124 | tm = ei->params[(fst + ei->rotate) % ei->nparam]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4125 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4126 | head = t, tt = &t->next; |
| 4127 | if (fst < lst) { |
| 4128 | for (i = fst + 1; i <= lst; i++) { |
| 4129 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4130 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4131 | j = (i + ei->rotate) % ei->nparam; |
| 4132 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4133 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4134 | *tt = t, tt = &t->next; |
| 4135 | } |
| 4136 | } else { |
| 4137 | for (i = fst - 1; i >= lst; i--) { |
| 4138 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4139 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4140 | j = (i + ei->rotate) % ei->nparam; |
| 4141 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4142 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4143 | *tt = t, tt = &t->next; |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | *last = tt; |
| 4148 | return head; |
| 4149 | |
| 4150 | err: |
| 4151 | error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
| 4152 | &tline->text[1]); |
| 4153 | return tline; |
| 4154 | } |
| 4155 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4156 | /* |
| 4157 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4158 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4159 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4160 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4161 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4162 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4163 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4164 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4165 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4166 | |
| 4167 | tail = &thead; |
| 4168 | thead = NULL; |
| 4169 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4170 | while (tline) { |
| 4171 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4172 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4173 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4174 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4175 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4176 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4177 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4178 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4179 | int i; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4180 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4181 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4182 | t = tline; |
| 4183 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4184 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4185 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 4186 | if (ei->type == EXP_MMACRO) { |
| 4187 | break; |
| 4188 | } |
| 4189 | } |
| 4190 | if (ei == NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4191 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4192 | } else { |
| 4193 | pos = strchr(t->text, ':'); |
| 4194 | if (!pos) { |
| 4195 | switch (t->text[1]) { |
| 4196 | /* |
| 4197 | * We have to make a substitution of one of the |
| 4198 | * forms %1, %-1, %+1, %%foo, %0. |
| 4199 | */ |
| 4200 | case '0': |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4201 | if ((strlen(t->text) > 2) && (t->text[2] == '0')) { |
| 4202 | type = TOK_ID; |
| 4203 | text = nasm_strdup(ei->label_text); |
| 4204 | } else { |
| 4205 | type = TOK_NUMBER; |
| 4206 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam); |
| 4207 | text = nasm_strdup(tmpbuf); |
| 4208 | } |
| 4209 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4210 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4211 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4212 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4213 | ei->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4214 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4215 | break; |
| 4216 | case '-': |
| 4217 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4218 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4219 | tt = NULL; |
| 4220 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4221 | if (ei->nparam > 1) |
| 4222 | n = (n + ei->rotate) % ei->nparam; |
| 4223 | tt = ei->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4224 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4225 | cc = find_cc(tt); |
| 4226 | if (cc == -1) { |
| 4227 | error(ERR_NONFATAL, |
| 4228 | "macro parameter %d is not a condition code", |
| 4229 | n + 1); |
| 4230 | text = NULL; |
| 4231 | } else { |
| 4232 | type = TOK_ID; |
| 4233 | if (inverse_ccs[cc] == -1) { |
| 4234 | error(ERR_NONFATAL, |
| 4235 | "condition code `%s' is not invertible", |
| 4236 | conditions[cc]); |
| 4237 | text = NULL; |
| 4238 | } else |
| 4239 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4240 | } |
| 4241 | break; |
| 4242 | case '+': |
| 4243 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4244 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4245 | tt = NULL; |
| 4246 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4247 | if (ei->nparam > 1) |
| 4248 | n = (n + ei->rotate) % ei->nparam; |
| 4249 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4250 | } |
| 4251 | cc = find_cc(tt); |
| 4252 | if (cc == -1) { |
| 4253 | error(ERR_NONFATAL, |
| 4254 | "macro parameter %d is not a condition code", |
| 4255 | n + 1); |
| 4256 | text = NULL; |
| 4257 | } else { |
| 4258 | type = TOK_ID; |
| 4259 | text = nasm_strdup(conditions[cc]); |
| 4260 | } |
| 4261 | break; |
| 4262 | default: |
| 4263 | n = atoi(t->text + 1) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4264 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4265 | tt = NULL; |
| 4266 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4267 | if (ei->nparam > 1) |
| 4268 | n = (n + ei->rotate) % ei->nparam; |
| 4269 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4270 | } |
| 4271 | if (tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4272 | for (i = 0; i < ei->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4273 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4274 | tail = &(*tail)->next; |
| 4275 | tt = tt->next; |
| 4276 | } |
| 4277 | } |
| 4278 | text = NULL; /* we've done it here */ |
| 4279 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4280 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4281 | } else { |
| 4282 | /* |
| 4283 | * seems we have a parameters range here |
| 4284 | */ |
| 4285 | Token *head, **last; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4286 | head = expand_mmac_params_range(ei, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4287 | if (head != t) { |
| 4288 | *tail = head; |
| 4289 | *last = tline; |
| 4290 | tline = head; |
| 4291 | text = NULL; |
| 4292 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4293 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4294 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4295 | if (!text) { |
| 4296 | delete_Token(t); |
| 4297 | } else { |
| 4298 | *tail = t; |
| 4299 | tail = &t->next; |
| 4300 | t->type = type; |
| 4301 | nasm_free(t->text); |
| 4302 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4303 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4304 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4305 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4306 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4307 | } else if (tline->type == TOK_INDIRECT) { |
| 4308 | t = tline; |
| 4309 | tline = tline->next; |
| 4310 | tt = tokenize(t->text); |
| 4311 | tt = expand_mmac_params(tt); |
| 4312 | tt = expand_smacro(tt); |
| 4313 | *tail = tt; |
| 4314 | while (tt) { |
| 4315 | tt->a.mac = NULL; /* Necessary? */ |
| 4316 | tail = &tt->next; |
| 4317 | tt = tt->next; |
| 4318 | } |
| 4319 | delete_Token(t); |
| 4320 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4321 | } else { |
| 4322 | t = *tail = tline; |
| 4323 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4324 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4325 | tail = &t->next; |
| 4326 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4327 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4328 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4329 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4330 | if (changed) { |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4331 | const struct tokseq_match t[] = { |
| 4332 | { |
| 4333 | PP_CONCAT_MASK(TOK_ID) | |
| 4334 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4335 | PP_CONCAT_MASK(TOK_ID) | |
| 4336 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4337 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4338 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4339 | }, |
| 4340 | { |
| 4341 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4342 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4343 | } |
| 4344 | }; |
| 4345 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4346 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4347 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4348 | return thead; |
| 4349 | } |
| 4350 | |
| 4351 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4352 | * Expand all single-line macro calls made in the given line. |
| 4353 | * Return the expanded version of the line. The original is deemed |
| 4354 | * to be destroyed in the process. (In reality we'll just move |
| 4355 | * Tokens from input to output a lot of the time, rather than |
| 4356 | * actually bothering to destroy and replicate.) |
| 4357 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4358 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4359 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4360 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4361 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4362 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4363 | Token **params; |
| 4364 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4365 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4366 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4367 | Token *org_tline = tline; |
| 4368 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4369 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4370 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4371 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4372 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4373 | /* |
| 4374 | * Trick: we should avoid changing the start token pointer since it can |
| 4375 | * be contained in "next" field of other token. Because of this |
| 4376 | * we allocate a copy of first token and work with it; at the end of |
| 4377 | * routine we copy it back |
| 4378 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4379 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4380 | tline = new_Token(org_tline->next, org_tline->type, |
| 4381 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4382 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4383 | nasm_free(org_tline->text); |
| 4384 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4387 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4388 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4389 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4390 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4391 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4392 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4393 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4394 | if (!--deadman) { |
| 4395 | error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4396 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4397 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4398 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4399 | if ((mname = tline->text)) { |
| 4400 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4401 | if (tline->type == TOK_ID) { |
| 4402 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4403 | } else if (tline->type == TOK_PREPROC_ID) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4404 | ctx = get_ctx(mname, &mname, false); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4405 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4406 | } else |
| 4407 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4408 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4409 | /* |
| 4410 | * We've hit an identifier. As in is_mmacro below, we first |
| 4411 | * check whether the identifier is a single-line macro at |
| 4412 | * all, then think about checking for parameters if |
| 4413 | * necessary. |
| 4414 | */ |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4415 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4416 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4417 | break; |
| 4418 | if (m) { |
| 4419 | mstart = tline; |
| 4420 | params = NULL; |
| 4421 | paramsize = NULL; |
| 4422 | if (m->nparam == 0) { |
| 4423 | /* |
| 4424 | * Simple case: the macro is parameterless. Discard the |
| 4425 | * one token that the macro call took, and push the |
| 4426 | * expansion back on the to-do stack. |
| 4427 | */ |
| 4428 | if (!m->expansion) { |
| 4429 | if (!strcmp("__FILE__", m->name)) { |
| 4430 | int32_t num = 0; |
| 4431 | char *file = NULL; |
| 4432 | src_get(&num, &file); |
| 4433 | tline->text = nasm_quote(file, strlen(file)); |
| 4434 | tline->type = TOK_STRING; |
| 4435 | nasm_free(file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4436 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4437 | } |
| 4438 | if (!strcmp("__LINE__", m->name)) { |
| 4439 | nasm_free(tline->text); |
| 4440 | make_tok_num(tline, src_get_linnum()); |
| 4441 | continue; |
| 4442 | } |
| 4443 | if (!strcmp("__BITS__", m->name)) { |
| 4444 | nasm_free(tline->text); |
| 4445 | make_tok_num(tline, globalbits); |
| 4446 | continue; |
| 4447 | } |
| 4448 | tline = delete_Token(tline); |
| 4449 | continue; |
| 4450 | } |
| 4451 | } else { |
| 4452 | /* |
| 4453 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4454 | * exists and takes parameters. We must find the |
| 4455 | * parameters in the call, count them, find the SMacro |
| 4456 | * that corresponds to that form of the macro call, and |
| 4457 | * substitute for the parameters when we expand. What a |
| 4458 | * pain. |
| 4459 | */ |
| 4460 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4461 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4462 | do { |
| 4463 | t = tline->next; |
| 4464 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4465 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4466 | t->text = NULL; |
| 4467 | t = tline->next = delete_Token(t); |
| 4468 | } |
| 4469 | tline = t; |
| 4470 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4471 | if (!tok_is_(tline, "(")) { |
| 4472 | /* |
| 4473 | * This macro wasn't called with parameters: ignore |
| 4474 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4475 | */ |
| 4476 | tline = mstart; |
| 4477 | m = NULL; |
| 4478 | } else { |
| 4479 | int paren = 0; |
| 4480 | int white = 0; |
| 4481 | brackets = 0; |
| 4482 | nparam = 0; |
| 4483 | sparam = PARAM_DELTA; |
| 4484 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4485 | params[0] = tline->next; |
| 4486 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4487 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4488 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4489 | /* |
| 4490 | * For some unusual expansions |
| 4491 | * which concatenates function call |
| 4492 | */ |
| 4493 | t = tline->next; |
| 4494 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4495 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4496 | t->text = NULL; |
| 4497 | t = tline->next = delete_Token(t); |
| 4498 | } |
| 4499 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4500 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4501 | if (!tline) { |
| 4502 | error(ERR_NONFATAL, |
| 4503 | "macro call expects terminating `)'"); |
| 4504 | break; |
| 4505 | } |
| 4506 | if (tline->type == TOK_WHITESPACE |
| 4507 | && brackets <= 0) { |
| 4508 | if (paramsize[nparam]) |
| 4509 | white++; |
| 4510 | else |
| 4511 | params[nparam] = tline->next; |
| 4512 | continue; /* parameter loop */ |
| 4513 | } |
| 4514 | if (tline->type == TOK_OTHER |
| 4515 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4516 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4517 | if (ch == ',' && !paren && brackets <= 0) { |
| 4518 | if (++nparam >= sparam) { |
| 4519 | sparam += PARAM_DELTA; |
| 4520 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4521 | sparam * sizeof(Token *)); |
| 4522 | paramsize = nasm_realloc(paramsize, |
| 4523 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4524 | } |
| 4525 | params[nparam] = tline->next; |
| 4526 | paramsize[nparam] = 0; |
| 4527 | white = 0; |
| 4528 | continue; /* parameter loop */ |
| 4529 | } |
| 4530 | if (ch == '{' && |
| 4531 | (brackets > 0 || (brackets == 0 && |
| 4532 | !paramsize[nparam]))) |
| 4533 | { |
| 4534 | if (!(brackets++)) { |
| 4535 | params[nparam] = tline->next; |
| 4536 | continue; /* parameter loop */ |
| 4537 | } |
| 4538 | } |
| 4539 | if (ch == '}' && brackets > 0) |
| 4540 | if (--brackets == 0) { |
| 4541 | brackets = -1; |
| 4542 | continue; /* parameter loop */ |
| 4543 | } |
| 4544 | if (ch == '(' && !brackets) |
| 4545 | paren++; |
| 4546 | if (ch == ')' && brackets <= 0) |
| 4547 | if (--paren < 0) |
| 4548 | break; |
| 4549 | } |
| 4550 | if (brackets < 0) { |
| 4551 | brackets = 0; |
| 4552 | error(ERR_NONFATAL, "braces do not " |
| 4553 | "enclose all of macro parameter"); |
| 4554 | } |
| 4555 | paramsize[nparam] += white + 1; |
| 4556 | white = 0; |
| 4557 | } /* parameter loop */ |
| 4558 | nparam++; |
| 4559 | while (m && (m->nparam != nparam || |
| 4560 | mstrcmp(m->name, mname, |
| 4561 | m->casesense))) |
| 4562 | m = m->next; |
| 4563 | if (!m) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4564 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4565 | "macro `%s' exists, " |
| 4566 | "but not taking %d parameters", |
| 4567 | mstart->text, nparam); |
| 4568 | } |
| 4569 | } |
| 4570 | if (m && m->in_progress) |
| 4571 | m = NULL; |
| 4572 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4573 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4574 | * Design question: should we handle !tline, which |
| 4575 | * indicates missing ')' here, or expand those |
| 4576 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4577 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4578 | */ |
| 4579 | nasm_free(params); |
| 4580 | nasm_free(paramsize); |
| 4581 | tline = mstart; |
| 4582 | } else { |
| 4583 | /* |
| 4584 | * Expand the macro: we are placed on the last token of the |
| 4585 | * call, so that we can easily split the call from the |
| 4586 | * following tokens. We also start by pushing an SMAC_END |
| 4587 | * token for the cycle removal. |
| 4588 | */ |
| 4589 | t = tline; |
| 4590 | if (t) { |
| 4591 | tline = t->next; |
| 4592 | t->next = NULL; |
| 4593 | } |
| 4594 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4595 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4596 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4597 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4598 | list_for_each(t, m->expansion) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4599 | if (t->type >= TOK_SMAC_PARAM) { |
| 4600 | Token *pcopy = tline, **ptail = &pcopy; |
| 4601 | Token *ttt, *pt; |
| 4602 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4603 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4604 | ttt = params[t->type - TOK_SMAC_PARAM]; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4605 | i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 4606 | while (--i >= 0) { |
| 4607 | pt = *ptail = new_Token(tline, ttt->type, |
| 4608 | ttt->text, 0); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4609 | ptail = &pt->next; |
| 4610 | ttt = ttt->next; |
| 4611 | } |
| 4612 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4613 | } else if (t->type == TOK_PREPROC_Q) { |
| 4614 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4615 | tline = tt; |
| 4616 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4617 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4618 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4619 | } else { |
| 4620 | tt = new_Token(tline, t->type, t->text, 0); |
| 4621 | tline = tt; |
| 4622 | } |
| 4623 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4624 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4625 | /* |
| 4626 | * Having done that, get rid of the macro call, and clean |
| 4627 | * up the parameters. |
| 4628 | */ |
| 4629 | nasm_free(params); |
| 4630 | nasm_free(paramsize); |
| 4631 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4632 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4633 | continue; /* main token loop */ |
| 4634 | } |
| 4635 | } |
| 4636 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4637 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4638 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4639 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4640 | tline = delete_Token(tline); |
| 4641 | } else { |
| 4642 | t = *tail = tline; |
| 4643 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4644 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4645 | t->next = NULL; |
| 4646 | tail = &t->next; |
| 4647 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4650 | /* |
| 4651 | * 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] | 4652 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4653 | * TOK_IDs should be concatenated. |
| 4654 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4655 | * them (without white spaces in between). |
| 4656 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4657 | if (expanded) { |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4658 | const struct tokseq_match t[] = { |
| 4659 | { |
| 4660 | PP_CONCAT_MASK(TOK_ID) | |
| 4661 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4662 | PP_CONCAT_MASK(TOK_ID) | |
| 4663 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4664 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4665 | } |
| 4666 | }; |
| 4667 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4668 | /* |
| 4669 | * If we concatenated something, *and* we had previously expanded |
| 4670 | * an actual macro, scan the lines again for macros... |
| 4671 | */ |
| 4672 | tline = thead; |
| 4673 | expanded = false; |
| 4674 | goto again; |
| 4675 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4676 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4677 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4678 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4679 | if (org_tline) { |
| 4680 | if (thead) { |
| 4681 | *org_tline = *thead; |
| 4682 | /* since we just gave text to org_line, don't free it */ |
| 4683 | thead->text = NULL; |
| 4684 | delete_Token(thead); |
| 4685 | } else { |
| 4686 | /* the expression expanded to empty line; |
| 4687 | we can't return NULL for some reasons |
| 4688 | we just set the line to a single WHITESPACE token. */ |
| 4689 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4690 | org_tline->text = NULL; |
| 4691 | org_tline->type = TOK_WHITESPACE; |
| 4692 | } |
| 4693 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4696 | return thead; |
| 4697 | } |
| 4698 | |
| 4699 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4700 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4701 | * right before they are fetched in. The reason is that there can be |
| 4702 | * identifiers consisting of several subparts. We consider that if there |
| 4703 | * are more than one element forming the name, user wants a expansion, |
| 4704 | * otherwise it will be left as-is. Example: |
| 4705 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4706 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4707 | * |
| 4708 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4709 | * will suck it and define the corresponding value. Other case: |
| 4710 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4711 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4712 | * |
| 4713 | * In this case user wants name to be expanded *before* %define starts |
| 4714 | * working, so we'll expand %$abc into something (if it has a value; |
| 4715 | * otherwise it will be left as-is) then concatenate all successive |
| 4716 | * PP_IDs into one. |
| 4717 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4718 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4719 | { |
| 4720 | Token *cur, *oldnext = NULL; |
| 4721 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4722 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4723 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4724 | |
| 4725 | cur = tline; |
| 4726 | while (cur->next && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4727 | (cur->next->type == TOK_ID || |
| 4728 | cur->next->type == TOK_PREPROC_ID |
| 4729 | || cur->next->type == TOK_NUMBER)) |
| 4730 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4731 | |
| 4732 | /* If identifier consists of just one token, don't expand */ |
| 4733 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4734 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4735 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4736 | if (cur) { |
| 4737 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4738 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4741 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4742 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4743 | if (cur) { |
| 4744 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4745 | cur = tline; |
| 4746 | while (cur && cur->next) |
| 4747 | cur = cur->next; |
| 4748 | if (cur) |
| 4749 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | return tline; |
| 4753 | } |
| 4754 | |
| 4755 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4756 | * Determine whether the given line constitutes a multi-line macro |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4757 | * call, and return the ExpDef structure called if so. Doesn't have |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4758 | * to check for an initial label - that's taken care of in |
| 4759 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4760 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4761 | * name is easy to find. |
| 4762 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4763 | static ExpDef *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4764 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4765 | ExpDef *head, *ed; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4766 | Token **params; |
| 4767 | int nparam; |
| 4768 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4769 | head = (ExpDef *) hash_findix(&expdefs, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4770 | |
| 4771 | /* |
| 4772 | * Efficiency: first we see if any macro exists with the given |
| 4773 | * name. If not, we can return NULL immediately. _Then_ we |
| 4774 | * count the parameters, and then we look further along the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4775 | * list if necessary to find the proper ExpDef. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4776 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4777 | list_for_each(ed, head) |
| 4778 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4779 | break; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4780 | if (!ed) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4781 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4782 | |
| 4783 | /* |
| 4784 | * OK, we have a potential macro. Count and demarcate the |
| 4785 | * parameters. |
| 4786 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4787 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4788 | |
| 4789 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4790 | * So we know how many parameters we've got. Find the ExpDef |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4791 | * structure that handles this number. |
| 4792 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4793 | while (ed) { |
| 4794 | if (ed->nparam_min <= nparam |
| 4795 | && (ed->plus || nparam <= ed->nparam_max)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4796 | /* |
| 4797 | * It's right, and we can use it. Add its default |
| 4798 | * parameters to the end of our list if necessary. |
| 4799 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4800 | if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4801 | params = |
| 4802 | nasm_realloc(params, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4803 | ((ed->nparam_min + ed->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4804 | 1) * sizeof(*params))); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4805 | while (nparam < ed->nparam_min + ed->ndefs) { |
| 4806 | params[nparam] = ed->defaults[nparam - ed->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4807 | nparam++; |
| 4808 | } |
| 4809 | } |
| 4810 | /* |
| 4811 | * If we've gone over the maximum parameter count (and |
| 4812 | * we're in Plus mode), ignore parameters beyond |
| 4813 | * nparam_max. |
| 4814 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4815 | if (ed->plus && nparam > ed->nparam_max) |
| 4816 | nparam = ed->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4817 | /* |
| 4818 | * Then terminate the parameter list, and leave. |
| 4819 | */ |
| 4820 | if (!params) { /* need this special case */ |
| 4821 | params = nasm_malloc(sizeof(*params)); |
| 4822 | nparam = 0; |
| 4823 | } |
| 4824 | params[nparam] = NULL; |
| 4825 | *params_array = params; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4826 | return ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4827 | } |
| 4828 | /* |
| 4829 | * This one wasn't right: look for the next one with the |
| 4830 | * same name. |
| 4831 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4832 | list_for_each(ed, ed->next) |
| 4833 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4834 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | /* |
| 4838 | * After all that, we didn't find one with the right number of |
| 4839 | * parameters. Issue a warning, and fail to expand the macro. |
| 4840 | */ |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4841 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4842 | "macro `%s' exists, but not taking %d parameters", |
| 4843 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4844 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4845 | return NULL; |
| 4846 | } |
| 4847 | |
| 4848 | /* |
| 4849 | * Expand the multi-line macro call made by the given line, if |
| 4850 | * there is one to be expanded. If there is, push the expansion on |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4851 | * istk->expansion and return true. Otherwise return false. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4852 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4853 | static bool expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4854 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4855 | Token *label = NULL; |
| 4856 | int dont_prepend = 0; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4857 | Token **params, *t, *mtok; |
| 4858 | Line *l = NULL; |
| 4859 | ExpDef *ed; |
| 4860 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4861 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4862 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4863 | |
| 4864 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4865 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4866 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4867 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4868 | return false; |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4869 | mtok = t; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4870 | ed = is_mmacro(t, ¶ms); |
| 4871 | if (ed != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4872 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4873 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4874 | Token *last; |
| 4875 | /* |
| 4876 | * We have an id which isn't a macro call. We'll assume |
| 4877 | * it might be a label; we'll also check to see if a |
| 4878 | * colon follows it. Then, if there's another id after |
| 4879 | * that lot, we'll check it again for macro-hood. |
| 4880 | */ |
| 4881 | label = last = t; |
| 4882 | t = t->next; |
| 4883 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4884 | last = t, t = t->next; |
| 4885 | if (tok_is_(t, ":")) { |
| 4886 | dont_prepend = 1; |
| 4887 | last = t, t = t->next; |
| 4888 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4889 | last = t, t = t->next; |
| 4890 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4891 | if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, ¶ms))) |
| 4892 | return false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4893 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4894 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4895 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4896 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4897 | |
| 4898 | /* |
| 4899 | * Fix up the parameters: this involves stripping leading and |
| 4900 | * trailing whitespace, then stripping braces if they are |
| 4901 | * present. |
| 4902 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4903 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4904 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4905 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4906 | for (i = 0; params[i]; i++) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4907 | int brace = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4908 | int comma = (!ed->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4909 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4910 | t = params[i]; |
| 4911 | skip_white_(t); |
| 4912 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4913 | t = t->next, brace = true, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4914 | params[i] = t; |
| 4915 | paramlen[i] = 0; |
| 4916 | while (t) { |
| 4917 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4918 | break; /* ... because we have hit a comma */ |
| 4919 | if (comma && t->type == TOK_WHITESPACE |
| 4920 | && tok_is_(t->next, ",")) |
| 4921 | break; /* ... or a space then a comma */ |
| 4922 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
| 4923 | break; /* ... or a brace */ |
| 4924 | t = t->next; |
| 4925 | paramlen[i]++; |
| 4926 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4929 | if (ed->cur_depth >= ed->max_depth) { |
| 4930 | if (ed->max_depth > 1) { |
| 4931 | error(ERR_WARNING, |
| 4932 | "reached maximum macro recursion depth of %i for %s", |
| 4933 | ed->max_depth,ed->name); |
| 4934 | } |
| 4935 | return false; |
| 4936 | } else { |
| 4937 | ed->cur_depth ++; |
| 4938 | } |
| 4939 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4940 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4941 | * OK, we have found a ExpDef structure representing a |
| 4942 | * previously defined mmacro. Create an expansion invocation |
| 4943 | * and point it back to the expansion definition. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4944 | * parameter tokens and macro-local tokens doesn't get done |
| 4945 | * until the single-line macro substitution process; this is |
| 4946 | * because delaying them allows us to change the semantics |
| 4947 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4948 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4949 | ei = new_ExpInv(EXP_MMACRO, ed); |
| 4950 | ei->name = nasm_strdup(mname); |
| 4951 | // ei->label = label; |
| 4952 | // ei->label_text = detoken(label, false); |
| 4953 | ei->current = ed->line; |
| 4954 | ei->emitting = true; |
| 4955 | // ei->iline = tline; |
| 4956 | ei->params = params; |
| 4957 | ei->nparam = nparam; |
| 4958 | ei->rotate = 0; |
| 4959 | ei->paramlen = paramlen; |
| 4960 | ei->lineno = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4961 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4962 | ei->prev = istk->expansion; |
| 4963 | istk->expansion = ei; |
| 4964 | |
| 4965 | /* |
| 4966 | * Special case: detect %00 on first invocation; if found, |
| 4967 | * avoid emitting any labels that precede the mmacro call. |
| 4968 | * ed->prepend is set to -1 when %00 is detected, else 1. |
| 4969 | */ |
| 4970 | if (ed->prepend == 0) { |
| 4971 | for (l = ed->line; l != NULL; l = l->next) { |
| 4972 | for (t = l->first; t != NULL; t = t->next) { |
| 4973 | if ((t->type == TOK_PREPROC_ID) && |
| 4974 | (strlen(t->text) == 3) && |
| 4975 | (t->text[1] == '0') && (t->text[2] == '0')) { |
| 4976 | dont_prepend = -1; |
| 4977 | break; |
| 4978 | } |
| 4979 | } |
| 4980 | if (dont_prepend < 0) { |
| 4981 | break; |
| 4982 | } |
| 4983 | } |
| 4984 | ed->prepend = ((dont_prepend < 0) ? -1 : 1); |
| 4985 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4986 | |
| 4987 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4988 | * If we had a label, push it on as the first line of |
| 4989 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4990 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4991 | if (label != NULL) { |
| 4992 | if (ed->prepend < 0) { |
| 4993 | ei->label_text = detoken(label, false); |
| 4994 | } else { |
| 4995 | if (dont_prepend == 0) { |
| 4996 | t = label; |
| 4997 | while (t->next != NULL) { |
| 4998 | t = t->next; |
| 4999 | } |
| 5000 | t->next = new_Token(NULL, TOK_OTHER, ":", 0); |
| 5001 | } |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame^] | 5002 | l = new_Line(); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5003 | l->first = copy_Token(label); |
| 5004 | l->next = ei->current; |
| 5005 | ei->current = l; |
| 5006 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5007 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5008 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5009 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5010 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5011 | istk->mmac_depth++; |
| 5012 | return true; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5015 | /* The function that actually does the error reporting */ |
| 5016 | static void verror(int severity, const char *fmt, va_list arg) |
| 5017 | { |
| 5018 | char buff[1024]; |
| 5019 | |
| 5020 | vsnprintf(buff, sizeof(buff), fmt, arg); |
| 5021 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5022 | if ((istk != NULL) && (istk->mmac_depth > 0)) { |
| 5023 | ExpInv *ei = istk->expansion; |
| 5024 | int lineno = ei->lineno; |
| 5025 | while (ei != NULL) { |
| 5026 | if (ei->type == EXP_MMACRO) { |
| 5027 | break; |
| 5028 | } |
| 5029 | lineno += ei->relno; |
| 5030 | ei = ei->prev; |
| 5031 | } |
| 5032 | nasm_error(severity, "(%s:%d) %s", ei->def->name, |
| 5033 | lineno, buff); |
| 5034 | } else { |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5035 | nasm_error(severity, "%s", buff); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5036 | } |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5037 | } |
| 5038 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5039 | /* |
| 5040 | * Since preprocessor always operate only on the line that didn't |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5041 | * arrived yet, we should always use ERR_OFFBY1. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5042 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5043 | static void error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5044 | { |
| 5045 | va_list arg; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5046 | va_start(arg, fmt); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5047 | verror(severity, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5048 | va_end(arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5049 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5050 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5051 | /* |
| 5052 | * Because %else etc are evaluated in the state context |
| 5053 | * of the previous branch, errors might get lost with error(): |
| 5054 | * %if 0 ... %else trailing garbage ... %endif |
| 5055 | * So %else etc should report errors with this function. |
| 5056 | */ |
| 5057 | static void error_precond(int severity, const char *fmt, ...) |
| 5058 | { |
| 5059 | va_list arg; |
| 5060 | |
| 5061 | /* Only ignore the error if it's really in a dead branch */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5062 | if ((istk != NULL) && |
| 5063 | (istk->expansion != NULL) && |
| 5064 | (istk->expansion->type == EXP_IF) && |
| 5065 | (istk->expansion->def->state == COND_NEVER)) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5066 | return; |
| 5067 | |
| 5068 | va_start(arg, fmt); |
| 5069 | verror(severity, fmt, arg); |
| 5070 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5073 | static void |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5074 | pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5075 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5076 | Token *t; |
| 5077 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5078 | cstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5079 | istk = nasm_malloc(sizeof(Include)); |
| 5080 | istk->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5081 | istk->expansion = NULL; |
| 5082 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5083 | istk->fname = NULL; |
| 5084 | src_set_fname(nasm_strdup(file)); |
| 5085 | src_set_linnum(0); |
| 5086 | istk->lineinc = 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5087 | istk->mmac_depth = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5088 | if (!istk->fp) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5089 | error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5090 | file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5091 | defining = NULL; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5092 | finals = NULL; |
| 5093 | in_final = false; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5094 | nested_mac_count = 0; |
| 5095 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5096 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5097 | unique = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5098 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5099 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5100 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5101 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5102 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5103 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 5104 | do_predef = true; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5105 | list = listgen; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5106 | |
| 5107 | /* |
| 5108 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 5109 | * The caller, however, will also pass in 3 for preprocess-only so |
| 5110 | * we can set __PASS__ accordingly. |
| 5111 | */ |
| 5112 | pass = apass > 2 ? 2 : apass; |
| 5113 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5114 | dephead = deptail = deplist; |
| 5115 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5116 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 5117 | sl->next = NULL; |
| 5118 | strcpy(sl->str, file); |
| 5119 | *deptail = sl; |
| 5120 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5121 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5122 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5123 | /* |
| 5124 | * Define the __PASS__ macro. This is defined here unlike |
| 5125 | * all the other builtins, because it is special -- it varies between |
| 5126 | * passes. |
| 5127 | */ |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5128 | t = nasm_malloc(sizeof(*t)); |
| 5129 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5130 | make_tok_num(t, apass); |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5131 | t->a.mac = NULL; |
| 5132 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5135 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5136 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5137 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5138 | Token *tline; |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame^] | 5139 | ExpDef *ed; |
| 5140 | ExpInv *ei; |
| 5141 | Line *l; |
| 5142 | int j; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5143 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5144 | while (1) { |
| 5145 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5146 | * Fetch a tokenized line, either from the expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5147 | * buffer or from the input file. |
| 5148 | */ |
| 5149 | tline = NULL; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5150 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5151 | while (1) { /* until we get a line we can use */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5152 | /* |
| 5153 | * Fetch a tokenized line from the expansion buffer |
| 5154 | */ |
| 5155 | if (istk->expansion != NULL) { |
| 5156 | ei = istk->expansion; |
| 5157 | if (ei->current != NULL) { |
| 5158 | if (ei->emitting == false) { |
| 5159 | ei->current = NULL; |
| 5160 | continue; |
| 5161 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5162 | l = ei->current; |
| 5163 | ei->current = l->next; |
| 5164 | ei->lineno++; |
| 5165 | tline = copy_Token(l->first); |
| 5166 | if (((ei->type == EXP_REP) || |
| 5167 | (ei->type == EXP_MMACRO) || |
| 5168 | (ei->type == EXP_WHILE)) |
| 5169 | && (ei->def->nolist == false)) { |
| 5170 | char *p = detoken(tline, false); |
| 5171 | list->line(LIST_MACRO, p); |
| 5172 | nasm_free(p); |
| 5173 | } |
| 5174 | if (ei->linnum > -1) { |
| 5175 | src_set_linnum(src_get_linnum() + 1); |
| 5176 | } |
| 5177 | break; |
| 5178 | } else if ((ei->type == EXP_REP) && |
| 5179 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5180 | ei->def->cur_depth ++; |
| 5181 | ei->current = ei->def->line; |
| 5182 | ei->lineno = 0; |
| 5183 | continue; |
| 5184 | } else if ((ei->type == EXP_WHILE) && |
| 5185 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5186 | ei->current = ei->def->line; |
| 5187 | ei->lineno = 0; |
| 5188 | tline = copy_Token(ei->current->first); |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame^] | 5189 | j = if_condition(tline, PP_WHILE); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5190 | tline = NULL; |
| 5191 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 5192 | if (j == COND_IF_TRUE) { |
| 5193 | ei->current = ei->current->next; |
| 5194 | ei->def->cur_depth ++; |
| 5195 | } else { |
| 5196 | ei->emitting = false; |
| 5197 | ei->current = NULL; |
| 5198 | ei->def->cur_depth = ei->def->max_depth; |
| 5199 | } |
| 5200 | continue; |
| 5201 | } else { |
| 5202 | istk->expansion = ei->prev; |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame^] | 5203 | ed = ei->def; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5204 | if (ed != NULL) { |
| 5205 | if ((ei->emitting == true) && |
| 5206 | (ed->max_depth == DEADMAN_LIMIT) && |
| 5207 | (ed->cur_depth == DEADMAN_LIMIT) |
| 5208 | ) { |
| 5209 | error(ERR_FATAL, "runaway expansion detected, aborting"); |
| 5210 | } |
| 5211 | if (ed->cur_depth > 0) { |
| 5212 | ed->cur_depth --; |
| 5213 | } else if ((ed->type != EXP_MMACRO) && (ed->type != EXP_IF)) { |
| 5214 | /***** should this really be right here??? *****/ |
| 5215 | /* |
| 5216 | Line *l = NULL, *ll = NULL; |
| 5217 | for (l = ed->line; l != NULL;) { |
| 5218 | if (l->first != NULL) { |
| 5219 | free_tlist(l->first); |
| 5220 | l->first = NULL; |
| 5221 | } |
| 5222 | ll = l; |
| 5223 | l = l->next; |
| 5224 | nasm_free(ll); |
| 5225 | } |
| 5226 | expansions = ed->prev; |
| 5227 | nasm_free(ed); |
| 5228 | */ |
| 5229 | } |
| 5230 | if ((ei->type == EXP_REP) || |
| 5231 | (ei->type == EXP_MMACRO) || |
| 5232 | (ei->type == EXP_WHILE)) { |
| 5233 | list->downlevel(LIST_MACRO); |
| 5234 | if (ei->type == EXP_MMACRO) { |
| 5235 | istk->mmac_depth--; |
| 5236 | } |
| 5237 | } |
| 5238 | } |
| 5239 | if (ei->linnum > -1) { |
| 5240 | src_set_linnum(ei->linnum); |
| 5241 | } |
| 5242 | free_expinv(ei); |
| 5243 | continue; |
| 5244 | } |
| 5245 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5246 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5247 | /* |
| 5248 | * Read in line from input and tokenize |
| 5249 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5250 | line = read_line(); |
| 5251 | if (line) { /* from the current input file */ |
| 5252 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5253 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5254 | nasm_free(line); |
| 5255 | break; |
| 5256 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5257 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5258 | /* |
| 5259 | * The current file has ended; work down the istk |
| 5260 | */ |
| 5261 | { |
| 5262 | Include *i = istk; |
| 5263 | fclose(i->fp); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5264 | if (i->expansion != NULL) { |
| 5265 | error(ERR_FATAL, |
| 5266 | "end of file while still in an expansion"); |
| 5267 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5268 | /* only set line and file name if there's a next node */ |
| 5269 | if (i->next) { |
| 5270 | src_set_linnum(i->lineno); |
| 5271 | nasm_free(src_set_fname(i->fname)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5272 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5273 | if ((i->next == NULL) && (finals != NULL)) { |
| 5274 | in_final = true; |
| 5275 | ei = new_ExpInv(EXP_FINAL, NULL); |
| 5276 | ei->emitting = true; |
| 5277 | ei->current = finals; |
| 5278 | istk->expansion = ei; |
| 5279 | finals = NULL; |
| 5280 | continue; |
| 5281 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5282 | istk = i->next; |
| 5283 | list->downlevel(LIST_INCLUDE); |
| 5284 | nasm_free(i); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5285 | if (istk == NULL) { |
| 5286 | if (finals != NULL) { |
| 5287 | in_final = true; |
| 5288 | } else { |
| 5289 | return NULL; |
| 5290 | } |
| 5291 | } |
| 5292 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5293 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | if (defining == NULL) { |
| 5297 | tline = expand_mmac_params(tline); |
| 5298 | } |
| 5299 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5300 | /* |
| 5301 | * Check the line to see if it's a preprocessor directive. |
| 5302 | */ |
| 5303 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5304 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5305 | } else if (defining != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5306 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5307 | * We're defining an expansion. We emit nothing at all, |
| 5308 | * and just shove the tokenized line on to the definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5309 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5310 | if (defining->ignoring == false) { |
| 5311 | Line *l = new_Line(); |
| 5312 | l->first = tline; |
| 5313 | if (defining->line == NULL) { |
| 5314 | defining->line = l; |
| 5315 | defining->last = l; |
| 5316 | } else { |
| 5317 | defining->last->next = l; |
| 5318 | defining->last = l; |
| 5319 | } |
| 5320 | } else { |
| 5321 | //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/ |
| 5322 | } |
| 5323 | defining->linecount++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5324 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5325 | } else if ((istk->expansion != NULL) && |
| 5326 | (istk->expansion->emitting != true)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5327 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5328 | * We're in a non-emitting branch of an expansion. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5329 | * Emit nothing at all, not even a blank line: when we |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5330 | * emerge from the expansion we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5331 | * directive so we keep our place correctly. |
| 5332 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5333 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5334 | continue; |
| 5335 | } else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5336 | tline = expand_smacro(tline); |
| 5337 | if (expand_mmacro(tline) != true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5338 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5339 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5340 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5341 | line = detoken(tline, true); |
| 5342 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5343 | break; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5344 | } else { |
| 5345 | continue; |
| 5346 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5347 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5348 | } |
| 5349 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5352 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5353 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5354 | if (defining != NULL) { |
| 5355 | error(ERR_NONFATAL, "end of file while still defining an expansion"); |
| 5356 | while (defining != NULL) { |
| 5357 | ExpDef *ed = defining; |
| 5358 | defining = ed->prev; |
| 5359 | free_expdef(ed); |
| 5360 | } |
| 5361 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5362 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5363 | while (cstk != NULL) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5364 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5365 | free_macros(); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5366 | while (istk != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5367 | Include *i = istk; |
| 5368 | istk = istk->next; |
| 5369 | fclose(i->fp); |
| 5370 | nasm_free(i->fname); |
| 5371 | nasm_free(i); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5372 | while (i->expansion != NULL) { |
| 5373 | ExpInv *ei = i->expansion; |
| 5374 | i->expansion = ei->prev; |
| 5375 | free_expinv(ei); |
| 5376 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5377 | } |
| 5378 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5379 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5380 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5381 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5382 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5383 | free_llist(predef); |
| 5384 | delete_Blocks(); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5385 | while ((i = ipath)) { |
| 5386 | ipath = i->next; |
| 5387 | if (i->path) |
| 5388 | nasm_free(i->path); |
| 5389 | nasm_free(i); |
| 5390 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5391 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5394 | void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5395 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5396 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5397 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5398 | i = nasm_malloc(sizeof(IncPath)); |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5399 | i->path = path ? nasm_strdup(path) : NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5400 | i->next = NULL; |
| 5401 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5402 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5403 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5404 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5405 | j = j->next; |
| 5406 | j->next = i; |
| 5407 | } else { |
| 5408 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5409 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5410 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5411 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5412 | void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5413 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5414 | Token *inc, *space, *name; |
| 5415 | Line *l; |
| 5416 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5417 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5418 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5419 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5420 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5421 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5422 | l->next = predef; |
| 5423 | l->first = inc; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5424 | predef = l; |
| 5425 | } |
| 5426 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5427 | void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5428 | { |
| 5429 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5430 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5431 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5432 | |
| 5433 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5434 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5435 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5436 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5437 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5438 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5439 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5440 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5441 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5442 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5443 | l->next = predef; |
| 5444 | l->first = def; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5445 | predef = l; |
| 5446 | } |
| 5447 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5448 | void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5449 | { |
| 5450 | Token *def, *space; |
| 5451 | Line *l; |
| 5452 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5453 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5454 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5455 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5456 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5457 | l = new_Line(); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5458 | l->next = predef; |
| 5459 | l->first = def; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5460 | predef = l; |
| 5461 | } |
| 5462 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5463 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5464 | * This function is used to assist with "runtime" preprocessor |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5465 | * directives, e.g. pp_runtime("%define __BITS__ 64"); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5466 | * |
| 5467 | * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU |
| 5468 | * PASS A VALID STRING TO THIS FUNCTION!!!!! |
| 5469 | */ |
| 5470 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5471 | void pp_runtime(char *definition) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5472 | { |
| 5473 | Token *def; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5474 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5475 | def = tokenize(definition); |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5476 | if (do_directive(def) == NO_DIRECTIVE_FOUND) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5477 | free_tlist(def); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5478 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 5481 | void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5482 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5483 | extrastdmac = macros; |
| 5484 | } |
| 5485 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5486 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5487 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5488 | char numbuf[20]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5489 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5490 | tok->text = nasm_strdup(numbuf); |
| 5491 | tok->type = TOK_NUMBER; |
| 5492 | } |
| 5493 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5494 | Preproc nasmpp = { |
| 5495 | pp_reset, |
| 5496 | pp_getline, |
| 5497 | pp_cleanup |
| 5498 | }; |