H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2 | * |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 3 | * Copyright 1996-2011 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 { |
Cyrill Gorcunov | 10083ae | 2011-07-17 20:06:20 +0400 | [diff] [blame] | 108 | SMacro *next; |
| 109 | char *name; |
| 110 | Token *expansion; |
| 111 | unsigned int nparam; |
| 112 | bool casesense; |
| 113 | bool in_progress; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 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 { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 120 | Context *next; |
| 121 | char *name; |
| 122 | struct hash_table localmac; |
| 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 { |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 146 | TOK_NONE = 0, |
| 147 | TOK_WHITESPACE, |
| 148 | TOK_COMMENT, |
| 149 | TOK_ID, |
| 150 | TOK_PREPROC_ID, |
| 151 | TOK_STRING, |
| 152 | TOK_NUMBER, |
| 153 | TOK_FLOAT, |
| 154 | TOK_SMAC_END, |
| 155 | TOK_OTHER, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 156 | TOK_INTERNAL_STRING, |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 157 | TOK_PREPROC_Q, |
| 158 | TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 159 | TOK_PASTE, /* %+ */ |
| 160 | TOK_INDIRECT, /* %[...] */ |
| 161 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 162 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 165 | #define PP_CONCAT_MASK(x) (1 << (x)) |
| 166 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 167 | struct tokseq_match { |
| 168 | int mask_head; |
| 169 | int mask_tail; |
| 170 | }; |
| 171 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 172 | struct Token { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 173 | Token *next; |
| 174 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 175 | union { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 176 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 177 | size_t len; /* scratch length field */ |
| 178 | } a; /* Auxiliary data */ |
| 179 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 183 | * Expansion definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 184 | * these, which is essentially a container to allow several linked |
| 185 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 186 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 187 | * Note that in this module, linked lists are treated as stacks |
| 188 | * wherever possible. For this reason, Lines are _pushed_ on to the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 189 | * `last' field in ExpDef structures, so that the linked list, |
| 190 | * if walked, would emit the expansion lines in the proper order. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 191 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 192 | struct Line { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 193 | Line *next; |
| 194 | Token *first; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 198 | * Expansion Types |
| 199 | */ |
| 200 | enum pp_exp_type { |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 201 | EXP_NONE = 0, |
| 202 | EXP_PREDEF, |
| 203 | EXP_MMACRO, |
| 204 | EXP_REP, |
| 205 | EXP_IF, |
| 206 | EXP_WHILE, |
| 207 | EXP_COMMENT, |
| 208 | EXP_FINAL, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 209 | EXP_MAX = INT_MAX /* Keep compiler from reducing the range */ |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * Store the definition of an expansion, in which is any |
| 214 | * preprocessor directive that has an ending pair. |
| 215 | * |
| 216 | * This design allows for arbitrary expansion/recursion depth, |
| 217 | * upto the DEADMAN_LIMIT. |
| 218 | * |
| 219 | * The `next' field is used for storing ExpDef in hash tables; the |
| 220 | * `prev' field is for the global `expansions` linked-list. |
| 221 | */ |
| 222 | struct ExpDef { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 223 | ExpDef *prev; /* previous definition */ |
| 224 | ExpDef *next; /* next in hash table */ |
| 225 | enum pp_exp_type type; /* expansion type */ |
| 226 | char *name; /* definition name */ |
| 227 | int nparam_min; |
| 228 | int nparam_max; |
| 229 | bool casesense; |
| 230 | bool plus; /* is the last parameter greedy? */ |
| 231 | bool nolist; /* is this expansion listing-inhibited? */ |
| 232 | Token *dlist; /* all defaults as one list */ |
| 233 | Token **defaults; /* parameter default pointers */ |
| 234 | int ndefs; /* number of default parameters */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 235 | |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 236 | int prepend; /* label prepend state */ |
| 237 | Line *label; |
| 238 | Line *line; |
| 239 | Line *last; |
| 240 | int linecount; /* number of lines within expansion */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 241 | |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 242 | int64_t def_depth; /* current number of definition pairs deep */ |
| 243 | int64_t cur_depth; /* current number of expansions */ |
| 244 | int64_t max_depth; /* maximum number of expansions allowed */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 245 | |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 246 | int state; /* condition state */ |
| 247 | bool ignoring; /* ignoring definition lines */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | /* |
| 251 | * Store the invocation of an expansion. |
| 252 | * |
| 253 | * The `prev' field is for the `istk->expansion` linked-list. |
| 254 | * |
| 255 | * When an expansion is being expanded, `params', `iline', `nparam', |
| 256 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 257 | */ |
| 258 | struct ExpInv { |
Cyrill Gorcunov | d57a031 | 2011-07-17 20:11:08 +0400 | [diff] [blame] | 259 | ExpInv *prev; /* previous invocation */ |
| 260 | ExpDef *def; /* pointer to expansion definition */ |
| 261 | char *name; /* invocation name */ |
| 262 | Line *label; /* pointer to label */ |
| 263 | char *label_text; /* pointer to label text */ |
| 264 | Line *current; /* pointer to current line in invocation */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 265 | |
Cyrill Gorcunov | d57a031 | 2011-07-17 20:11:08 +0400 | [diff] [blame] | 266 | Token **params; /* actual parameters */ |
| 267 | Token *iline; /* invocation line */ |
| 268 | int *paramlen; |
| 269 | unsigned int nparam; |
| 270 | unsigned int rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 271 | |
Cyrill Gorcunov | d57a031 | 2011-07-17 20:11:08 +0400 | [diff] [blame] | 272 | uint64_t unique; |
| 273 | int lineno; /* current line number in expansion */ |
| 274 | int linnum; /* line number at invocation */ |
| 275 | int relno; /* relative line number at invocation */ |
| 276 | enum pp_exp_type type; /* expansion type */ |
| 277 | bool emitting; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 281 | * To handle an arbitrary level of file inclusion, we maintain a |
| 282 | * stack (ie linked list) of these things. |
| 283 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 284 | struct Include { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 285 | Include *next; |
| 286 | FILE *fp; |
| 287 | Cond *conds; |
| 288 | ExpInv *expansion; |
| 289 | char *fname; |
| 290 | int lineno; |
| 291 | int lineinc; |
| 292 | int mmac_depth; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 296 | * Include search path. This is simply a list of strings which get |
| 297 | * prepended, in turn, to the name of an include file, in an |
| 298 | * attempt to find the file if it's not in the current directory. |
| 299 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 300 | struct IncPath { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 301 | IncPath *next; |
| 302 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 306 | * Conditional assembly: we maintain a separate stack of these for |
| 307 | * each level of file inclusion. (The only reason we keep the |
| 308 | * stacks separate is to ensure that a stray `%endif' in a file |
| 309 | * included from within the true branch of a `%if' won't terminate |
| 310 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 311 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 312 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 313 | /* |
| 314 | * These states are for use just after %if or %elif: IF_TRUE |
| 315 | * means the condition has evaluated to truth so we are |
| 316 | * currently emitting, whereas IF_FALSE means we are not |
| 317 | * currently emitting but will start doing so if a %else comes |
| 318 | * up. In these states, all directives are admissible: %elif, |
| 319 | * %else and %endif. (And of course %if.) |
| 320 | */ |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 321 | COND_IF_TRUE, |
| 322 | COND_IF_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 323 | /* |
| 324 | * These states come up after a %else: ELSE_TRUE means we're |
| 325 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 326 | * any %elif or %else will cause an error. |
| 327 | */ |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 328 | COND_ELSE_TRUE, |
| 329 | COND_ELSE_FALSE, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 330 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 331 | * These states mean that we're not emitting now, and also that |
| 332 | * nothing until %endif will be emitted at all. COND_DONE is |
| 333 | * used when we've had our moment of emission |
| 334 | * and have now started seeing %elifs. COND_NEVER is used when |
| 335 | * the condition construct in question is contained within a |
| 336 | * non-emitting branch of a larger condition construct, |
| 337 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 338 | */ |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 339 | COND_DONE, |
| 340 | COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 341 | }; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 342 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 343 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 344 | * These defines are used as the possible return values for do_directive |
| 345 | */ |
| 346 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 347 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 348 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 349 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 350 | * This define sets the upper limit for smacro and expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 351 | */ |
| 352 | #define DEADMAN_LIMIT (1 << 20) |
| 353 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 354 | /* max reps */ |
| 355 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 356 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 357 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 358 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 359 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 360 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 361 | * ones, so we need a different enum... |
| 362 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 363 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 364 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 365 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 366 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 367 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 368 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 369 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 370 | 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] | 371 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 372 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 373 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 374 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 375 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 376 | 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] | 377 | 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] | 378 | }; |
| 379 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 380 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 381 | * conditional compilation directives. Using the NASM pre-processor does |
| 382 | * not work, so we look for them specifically from the following list and |
| 383 | * then jam in the equivalent NASM directive into the input stream. |
| 384 | */ |
| 385 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 386 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 387 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 388 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 389 | }; |
| 390 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 391 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 392 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 393 | "ifndef", "include", "local" |
| 394 | }; |
| 395 | |
| 396 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 397 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 398 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 399 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 400 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 401 | static Context *cstk; |
| 402 | static Include *istk; |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 403 | static IncPath *ipath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 404 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 405 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 406 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 407 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 408 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 409 | |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 410 | static Line *predef; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 411 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 412 | |
| 413 | static ListGen *list; |
| 414 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 415 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 416 | * The current set of expansion definitions we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 417 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 418 | static struct hash_table expdefs; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * The current set of single-line macros we have defined. |
| 422 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 423 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 426 | * Linked List of all active expansion definitions |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 427 | */ |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 428 | struct ExpDef *expansions; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * The expansion we are currently defining |
| 432 | */ |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 433 | static ExpDef *defining; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 434 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 435 | static uint64_t nested_mac_count; |
| 436 | static uint64_t nested_rep_count; |
| 437 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 438 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 439 | * Linked-list of lines to preprocess, prior to cleanup |
| 440 | */ |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 441 | static Line *finals; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 442 | static bool in_final = false; |
| 443 | |
| 444 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 445 | * The number of macro parameters to allocate space for at a time. |
| 446 | */ |
| 447 | #define PARAM_DELTA 16 |
| 448 | |
| 449 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 450 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 451 | * 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] | 452 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 453 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 454 | |
| 455 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 456 | * The extra standard macros that come from the object format, if |
| 457 | * any. |
| 458 | */ |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 459 | static macros_t *extrastdmac; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 460 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 461 | |
| 462 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 463 | * Tokens are allocated in blocks to improve speed |
| 464 | */ |
| 465 | #define TOKEN_BLOCKSIZE 4096 |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 466 | static Token *freeTokens; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 467 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 468 | Blocks *next; |
| 469 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 470 | }; |
| 471 | |
Cyrill Gorcunov | 5c60776 | 2011-10-09 19:04:14 +0400 | [diff] [blame] | 472 | static Blocks blocks; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 473 | |
| 474 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 475 | * Forward declarations. |
| 476 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 477 | static Token *expand_mmac_params(Token * tline); |
| 478 | static Token *expand_smacro(Token * tline); |
| 479 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 480 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 481 | static void make_tok_num(Token * tok, int64_t val); |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 482 | static void error(int severity, const char *fmt, ...); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 483 | static void error_precond(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 484 | static void *new_Block(size_t size); |
| 485 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 486 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 487 | const char *text, int txtlen); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 488 | static Token *copy_Token(Token * tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 489 | static Token *delete_Token(Token * t); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 490 | static Line *new_Line(void); |
| 491 | static ExpDef *new_ExpDef(int exp_type); |
| 492 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 496 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 497 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 498 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 499 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 500 | #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] | 501 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 502 | /* |
| 503 | * A few helpers for single macros |
| 504 | */ |
| 505 | |
| 506 | /* We might be not smacro parameter at all */ |
| 507 | static bool is_smacro_param(Token *t) |
| 508 | { |
| 509 | return t->type >= TOK_SMAC_PARAM; |
| 510 | } |
| 511 | |
| 512 | /* smacro parameters are counted in a special way */ |
| 513 | static int smacro_get_param_idx(Token *t) |
| 514 | { |
| 515 | return t->type - TOK_SMAC_PARAM; |
| 516 | } |
| 517 | |
| 518 | /* encode smacro parameter index */ |
| 519 | static int smacro_set_param_idx(Token *t, unsigned int index) |
| 520 | { |
| 521 | return t->type = TOK_SMAC_PARAM + index; |
| 522 | } |
| 523 | |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 524 | #ifdef NASM_TRACE |
| 525 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 526 | #define stringify(x) #x |
| 527 | |
Cyrill Gorcunov | 9d1141a | 2011-06-26 23:07:35 +0400 | [diff] [blame] | 528 | #define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__) |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 529 | #define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__); |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 530 | #define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__); |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 531 | |
| 532 | /* FIXME: we really need some compound type here instead of inplace code */ |
| 533 | static const char *nasm_get_tok_type_str(enum pp_token_type type) |
| 534 | { |
| 535 | #define SWITCH_TOK_NAME(type) \ |
| 536 | case (type): \ |
| 537 | return stringify(type) |
| 538 | |
| 539 | switch (type) { |
| 540 | SWITCH_TOK_NAME(TOK_NONE); |
| 541 | SWITCH_TOK_NAME(TOK_WHITESPACE); |
| 542 | SWITCH_TOK_NAME(TOK_COMMENT); |
| 543 | SWITCH_TOK_NAME(TOK_ID); |
| 544 | SWITCH_TOK_NAME(TOK_PREPROC_ID); |
| 545 | SWITCH_TOK_NAME(TOK_STRING); |
| 546 | SWITCH_TOK_NAME(TOK_NUMBER); |
| 547 | SWITCH_TOK_NAME(TOK_FLOAT); |
| 548 | SWITCH_TOK_NAME(TOK_SMAC_END); |
| 549 | SWITCH_TOK_NAME(TOK_OTHER); |
| 550 | SWITCH_TOK_NAME(TOK_INTERNAL_STRING); |
| 551 | SWITCH_TOK_NAME(TOK_PREPROC_Q); |
| 552 | SWITCH_TOK_NAME(TOK_PREPROC_QQ); |
| 553 | SWITCH_TOK_NAME(TOK_PASTE); |
| 554 | SWITCH_TOK_NAME(TOK_INDIRECT); |
| 555 | SWITCH_TOK_NAME(TOK_SMAC_PARAM); |
| 556 | SWITCH_TOK_NAME(TOK_MAX); |
| 557 | } |
| 558 | |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func) |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 563 | { |
| 564 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 565 | if (token) { |
| 566 | Token *t; |
| 567 | list_for_each(t, token) { |
| 568 | if (t->text) |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 569 | printf("'%s'(%s) ", t->text, |
| 570 | nasm_get_tok_type_str(t->type)); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 571 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 572 | printf("\n\n"); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 576 | static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func) |
| 577 | { |
| 578 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 579 | if (token) { |
| 580 | Token *t; |
| 581 | list_for_each(t, token) |
| 582 | printf("%s", t->text ? t->text : " "); |
| 583 | printf("\n\n"); |
| 584 | } |
| 585 | } |
| 586 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 587 | #else |
| 588 | #define nasm_trace(msg, ...) |
| 589 | #define nasm_dump_token(t) |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 590 | #define nasm_dump_stream(t) |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 591 | #endif |
| 592 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 593 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 594 | * nasm_unquote with error if the string contains NUL characters. |
| 595 | * If the string contains NUL characters, issue an error and return |
| 596 | * the C len, i.e. truncate at the NUL. |
| 597 | */ |
| 598 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 599 | { |
| 600 | size_t len = nasm_unquote(qstr, NULL); |
| 601 | size_t clen = strlen(qstr); |
| 602 | |
| 603 | if (len != clen) |
| 604 | error(ERR_NONFATAL, "NUL character in `%s' directive", |
| 605 | pp_directives[directive]); |
| 606 | |
| 607 | return clen; |
| 608 | } |
| 609 | |
| 610 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 611 | * In-place reverse a list of tokens. |
| 612 | */ |
| 613 | static Token *reverse_tokens(Token *t) |
| 614 | { |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 615 | Token *prev, *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 616 | |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 617 | list_reverse(t, prev, next); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 618 | |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 619 | return t; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 623 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 624 | * front of them. We do it here because I could not find any other |
| 625 | * place to do it for the moment, and it is a hack (ideally it would |
| 626 | * be nice to be able to use the NASM pre-processor to do it). |
| 627 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 628 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 629 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 630 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 631 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 632 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 633 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 634 | |
| 635 | /* Binary search for the directive name */ |
| 636 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 637 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 638 | q = nasm_skip_word(p); |
| 639 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 640 | if (len) { |
| 641 | oldchar = p[len]; |
| 642 | p[len] = 0; |
| 643 | while (j - i > 1) { |
| 644 | k = (j + i) / 2; |
| 645 | m = nasm_stricmp(p, tasm_directives[k]); |
| 646 | if (m == 0) { |
| 647 | /* We have found a directive, so jam a % in front of it |
| 648 | * so that NASM will then recognise it as one if it's own. |
| 649 | */ |
| 650 | p[len] = oldchar; |
| 651 | len = strlen(p); |
| 652 | oldline = line; |
| 653 | line = nasm_malloc(len + 2); |
| 654 | line[0] = '%'; |
| 655 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 656 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 657 | * NASM does not recognise IFDIFI, so we convert |
| 658 | * it to %if 0. This is not used in NASM |
| 659 | * compatible code, but does need to parse for the |
| 660 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 661 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 662 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 663 | } else { |
| 664 | memcpy(line + 1, p, len + 1); |
| 665 | } |
| 666 | nasm_free(oldline); |
| 667 | return line; |
| 668 | } else if (m < 0) { |
| 669 | j = k; |
| 670 | } else |
| 671 | i = k; |
| 672 | } |
| 673 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 674 | } |
| 675 | return line; |
| 676 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 677 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 678 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 679 | * The pre-preprocessing stage... This function translates line |
| 680 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 681 | * flags') into NASM preprocessor line number indications (`%line |
| 682 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 683 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 684 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 685 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 686 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 687 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 688 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 689 | if (line[0] == '#' && line[1] == ' ') { |
| 690 | oldline = line; |
| 691 | fname = oldline + 2; |
| 692 | lineno = atoi(fname); |
| 693 | fname += strspn(fname, "0123456789 "); |
| 694 | if (*fname == '"') |
| 695 | fname++; |
| 696 | fnlen = strcspn(fname, "\""); |
| 697 | line = nasm_malloc(20 + fnlen); |
| 698 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 699 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 700 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 701 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 702 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 703 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 707 | * Free a linked list of tokens. |
| 708 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 709 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 710 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 711 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 712 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Free a linked list of lines. |
| 717 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 718 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 719 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 720 | Line *l, *tmp; |
| 721 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 722 | free_tlist(l->first); |
| 723 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
| 727 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 728 | * Free an ExpDef |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 729 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 730 | static void free_expdef(ExpDef * ed) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 731 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 732 | nasm_free(ed->name); |
| 733 | free_tlist(ed->dlist); |
| 734 | nasm_free(ed->defaults); |
| 735 | free_llist(ed->line); |
| 736 | nasm_free(ed); |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * Free an ExpInv |
| 741 | */ |
| 742 | static void free_expinv(ExpInv * ei) |
| 743 | { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 744 | nasm_free(ei->name); |
| 745 | nasm_free(ei->label_text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 746 | nasm_free(ei); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 750 | * Free all currently defined macros, and free the hash tables |
| 751 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 752 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 753 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 754 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 755 | const char *key; |
| 756 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 757 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 758 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 759 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 760 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 761 | nasm_free(s->name); |
| 762 | free_tlist(s->expansion); |
| 763 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 764 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 765 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 766 | hash_free(smt); |
| 767 | } |
| 768 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 769 | static void free_expdef_table(struct hash_table *edt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 770 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 771 | ExpDef *ed, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 772 | const char *key; |
| 773 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 774 | |
| 775 | it = NULL; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 776 | while ((ed = hash_iterate(edt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 777 | nasm_free((void *)key); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 778 | list_for_each_safe(ed ,tmp, ed) |
| 779 | free_expdef(ed); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 780 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 781 | hash_free(edt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static void free_macros(void) |
| 785 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 786 | free_smacro_table(&smacros); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 787 | free_expdef_table(&expdefs); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Initialize the hash tables |
| 792 | */ |
| 793 | static void init_macros(void) |
| 794 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 795 | hash_init(&smacros, HASH_LARGE); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 796 | hash_init(&expdefs, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 800 | * Pop the context stack. |
| 801 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 802 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 803 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 804 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 805 | |
| 806 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 807 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 808 | nasm_free(c->name); |
| 809 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 810 | } |
| 811 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 812 | /* |
| 813 | * Search for a key in the hash index; adding it if necessary |
| 814 | * (in which case we initialize the data pointer to NULL.) |
| 815 | */ |
| 816 | static void ** |
| 817 | hash_findi_add(struct hash_table *hash, const char *str) |
| 818 | { |
| 819 | struct hash_insert hi; |
| 820 | void **r; |
| 821 | char *strx; |
| 822 | |
| 823 | r = hash_findi(hash, str, &hi); |
| 824 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 825 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 826 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 827 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 828 | return hash_add(&hi, strx, NULL); |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Like hash_findi, but returns the data element rather than a pointer |
| 833 | * to it. Used only when not adding a new element, hence no third |
| 834 | * argument. |
| 835 | */ |
| 836 | static void * |
| 837 | hash_findix(struct hash_table *hash, const char *str) |
| 838 | { |
| 839 | void **p; |
| 840 | |
| 841 | p = hash_findi(hash, str, NULL); |
| 842 | return p ? *p : NULL; |
| 843 | } |
| 844 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 845 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 846 | * read line from standard macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 847 | * if there no more left -- return NULL |
| 848 | */ |
| 849 | static char *line_from_stdmac(void) |
| 850 | { |
| 851 | unsigned char c; |
| 852 | const unsigned char *p = stdmacpos; |
| 853 | char *line, *q; |
| 854 | size_t len = 0; |
| 855 | |
| 856 | if (!stdmacpos) |
| 857 | return NULL; |
| 858 | |
| 859 | while ((c = *p++)) { |
| 860 | if (c >= 0x80) |
| 861 | len += pp_directives_len[c - 0x80] + 1; |
| 862 | else |
| 863 | len++; |
| 864 | } |
| 865 | |
| 866 | line = nasm_malloc(len + 1); |
| 867 | q = line; |
| 868 | while ((c = *stdmacpos++)) { |
| 869 | if (c >= 0x80) { |
| 870 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 871 | q += pp_directives_len[c - 0x80]; |
| 872 | *q++ = ' '; |
| 873 | } else { |
| 874 | *q++ = c; |
| 875 | } |
| 876 | } |
| 877 | stdmacpos = p; |
| 878 | *q = '\0'; |
| 879 | |
| 880 | if (!*stdmacpos) { |
| 881 | /* This was the last of the standard macro chain... */ |
| 882 | stdmacpos = NULL; |
| 883 | if (any_extrastdmac) { |
| 884 | stdmacpos = extrastdmac; |
| 885 | any_extrastdmac = false; |
| 886 | } else if (do_predef) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 887 | ExpInv *ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 888 | Line *pd, *l; |
| 889 | Token *head, **tail, *t; |
| 890 | |
| 891 | /* |
| 892 | * Nasty hack: here we push the contents of |
| 893 | * `predef' on to the top-level expansion stack, |
| 894 | * since this is the most convenient way to |
| 895 | * implement the pre-include and pre-define |
| 896 | * features. |
| 897 | */ |
| 898 | list_for_each(pd, predef) { |
| 899 | head = NULL; |
| 900 | tail = &head; |
| 901 | list_for_each(t, pd->first) { |
| 902 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 903 | tail = &(*tail)->next; |
| 904 | } |
| 905 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 906 | l = new_Line(); |
| 907 | l->first = head; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 908 | ei = new_ExpInv(EXP_PREDEF, NULL); |
| 909 | ei->current = l; |
| 910 | ei->emitting = true; |
| 911 | ei->prev = istk->expansion; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 912 | istk->expansion = ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 913 | } |
| 914 | do_predef = false; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | return line; |
| 919 | } |
| 920 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 921 | #define BUF_DELTA 512 |
| 922 | /* |
| 923 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 924 | * at the end of the line read, and handling spurious ^Zs. Will |
| 925 | * return lines from the standard macro set if this has not already |
| 926 | * been done. |
| 927 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 928 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 929 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 930 | char *buffer, *p, *q; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 931 | int bufsize, continued_count; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 932 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 933 | /* |
| 934 | * standart macros set (predefined) goes first |
| 935 | */ |
| 936 | p = line_from_stdmac(); |
| 937 | if (p) |
| 938 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 939 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 940 | /* |
| 941 | * regular read from a file |
| 942 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 943 | bufsize = BUF_DELTA; |
| 944 | buffer = nasm_malloc(BUF_DELTA); |
| 945 | p = buffer; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 946 | continued_count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 947 | while (1) { |
| 948 | q = fgets(p, bufsize - (p - buffer), istk->fp); |
| 949 | if (!q) |
| 950 | break; |
| 951 | p += strlen(p); |
| 952 | if (p > buffer && p[-1] == '\n') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 953 | /* |
| 954 | * Convert backslash-CRLF line continuation sequences into |
| 955 | * nothing at all (for DOS and Windows) |
| 956 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 957 | if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { |
| 958 | p -= 3; |
| 959 | *p = 0; |
| 960 | continued_count++; |
| 961 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 962 | /* |
| 963 | * Also convert backslash-LF line continuation sequences into |
| 964 | * nothing at all (for Unix) |
| 965 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 966 | else if (((p - 1) > buffer) && (p[-2] == '\\')) { |
| 967 | p -= 2; |
| 968 | *p = 0; |
| 969 | continued_count++; |
| 970 | } else { |
| 971 | break; |
| 972 | } |
| 973 | } |
| 974 | if (p - buffer > bufsize - 10) { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 975 | int32_t offset = p - buffer; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 976 | bufsize += BUF_DELTA; |
| 977 | buffer = nasm_realloc(buffer, bufsize); |
| 978 | p = buffer + offset; /* prevent stale-pointer problems */ |
| 979 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 980 | } |
| 981 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 982 | if (!q && p == buffer) { |
| 983 | nasm_free(buffer); |
| 984 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 987 | src_set_linnum(src_get_linnum() + istk->lineinc + |
| 988 | (continued_count * istk->lineinc)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 989 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 990 | /* |
| 991 | * Play safe: remove CRs as well as LFs, if any of either are |
| 992 | * present at the end of the line. |
| 993 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 994 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 995 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * Handle spurious ^Z, which may be inserted into source files |
| 999 | * by some file transfer utilities. |
| 1000 | */ |
| 1001 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 1002 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1003 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1004 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1005 | return buffer; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 1009 | * 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] | 1010 | * don't need to parse the value out of e.g. numeric tokens: we |
| 1011 | * simply split one string into many. |
| 1012 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1013 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1014 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 1015 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1016 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1017 | Token *list = NULL; |
| 1018 | Token *t, **tail = &list; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1019 | bool verbose = true; |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 1020 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1021 | nasm_trace("Tokenize for '%s'", line); |
| 1022 | |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1023 | if ((defining != NULL) && (defining->ignoring == true)) { |
| 1024 | verbose = false; |
| 1025 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1026 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1027 | while (*line) { |
| 1028 | p = line; |
| 1029 | if (*p == '%') { |
| 1030 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1031 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 1032 | p++; |
| 1033 | type = TOK_PASTE; |
| 1034 | } else if (nasm_isdigit(*p) || |
| 1035 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1036 | do { |
| 1037 | p++; |
| 1038 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1039 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1040 | type = TOK_PREPROC_ID; |
| 1041 | } else if (*p == '{') { |
| 1042 | p++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1043 | while (*p && *p != '}') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1044 | p[-1] = *p; |
| 1045 | p++; |
| 1046 | } |
| 1047 | p[-1] = '\0'; |
| 1048 | if (*p) |
| 1049 | p++; |
| 1050 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1051 | } else if (*p == '[') { |
| 1052 | int lvl = 1; |
| 1053 | line += 2; /* Skip the leading %[ */ |
| 1054 | p++; |
| 1055 | while (lvl && (c = *p++)) { |
| 1056 | switch (c) { |
| 1057 | case ']': |
| 1058 | lvl--; |
| 1059 | break; |
| 1060 | case '%': |
| 1061 | if (*p == '[') |
| 1062 | lvl++; |
| 1063 | break; |
| 1064 | case '\'': |
| 1065 | case '\"': |
| 1066 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 1067 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1068 | break; |
| 1069 | default: |
| 1070 | break; |
| 1071 | } |
| 1072 | } |
| 1073 | p--; |
| 1074 | if (*p) |
| 1075 | *p++ = '\0'; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1076 | if (lvl && verbose) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1077 | error(ERR_NONFATAL, "unterminated %[ construct"); |
| 1078 | type = TOK_INDIRECT; |
| 1079 | } else if (*p == '?') { |
| 1080 | type = TOK_PREPROC_Q; /* %? */ |
| 1081 | p++; |
| 1082 | if (*p == '?') { |
| 1083 | type = TOK_PREPROC_QQ; /* %?? */ |
| 1084 | p++; |
| 1085 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1086 | } else if (*p == '!') { |
| 1087 | type = TOK_PREPROC_ID; |
| 1088 | p++; |
| 1089 | if (isidchar(*p)) { |
| 1090 | do { |
| 1091 | p++; |
| 1092 | } while (isidchar(*p)); |
| 1093 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 1094 | p = nasm_skip_string(p); |
| 1095 | if (*p) |
| 1096 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1097 | else if(verbose) |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1098 | error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string"); |
| 1099 | } else { |
| 1100 | /* %! without string or identifier */ |
| 1101 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1102 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1103 | } else if (isidchar(*p) || |
| 1104 | ((*p == '!' || *p == '%' || *p == '$') && |
| 1105 | isidchar(p[1]))) { |
| 1106 | do { |
| 1107 | p++; |
| 1108 | } |
| 1109 | while (isidchar(*p)); |
| 1110 | type = TOK_PREPROC_ID; |
| 1111 | } else { |
| 1112 | type = TOK_OTHER; |
| 1113 | if (*p == '%') |
| 1114 | p++; |
| 1115 | } |
| 1116 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1117 | type = TOK_ID; |
| 1118 | p++; |
| 1119 | while (*p && isidchar(*p)) |
| 1120 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1121 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1122 | /* |
| 1123 | * A string token. |
| 1124 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1125 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1126 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1127 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1128 | if (*p) { |
| 1129 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1130 | } else if(verbose) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1131 | error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1132 | /* Handling unterminated strings by UNV */ |
| 1133 | /* type = -1; */ |
| 1134 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1135 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1136 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1137 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1138 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1139 | bool is_hex = false; |
| 1140 | bool is_float = false; |
| 1141 | bool has_e = false; |
| 1142 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1143 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1144 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1145 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1146 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1147 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1148 | if (*p == '$') { |
| 1149 | p++; |
| 1150 | is_hex = true; |
| 1151 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1152 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1153 | for (;;) { |
| 1154 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1155 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1156 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1157 | has_e = true; |
| 1158 | if (*p == '+' || *p == '-') { |
| 1159 | /* |
| 1160 | * e can only be followed by +/- if it is either a |
| 1161 | * prefixed hex number or a floating-point number |
| 1162 | */ |
| 1163 | p++; |
| 1164 | is_float = true; |
| 1165 | } |
| 1166 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1167 | is_hex = true; |
| 1168 | } else if (c == 'P' || c == 'p') { |
| 1169 | is_float = true; |
| 1170 | if (*p == '+' || *p == '-') |
| 1171 | p++; |
| 1172 | } else if (isnumchar(c) || c == '_') |
| 1173 | ; /* just advance */ |
| 1174 | else if (c == '.') { |
| 1175 | /* |
| 1176 | * we need to deal with consequences of the legacy |
| 1177 | * parser, like "1.nolist" being two tokens |
| 1178 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1179 | * a shot for now. In the future, we probably need |
| 1180 | * a flex-based scanner with proper pattern matching |
| 1181 | * to do it as well as it can be done. Nothing in |
| 1182 | * the world is going to help the person who wants |
| 1183 | * 0x123.p16 interpreted as two tokens, though. |
| 1184 | */ |
| 1185 | r = p; |
| 1186 | while (*r == '_') |
| 1187 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1188 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1189 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1190 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1191 | (*r == 'p' || *r == 'P')) { |
| 1192 | p = r; |
| 1193 | is_float = true; |
| 1194 | } else |
| 1195 | break; /* Terminate the token */ |
| 1196 | } else |
| 1197 | break; |
| 1198 | } |
| 1199 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1200 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1201 | if (p == line+1 && *line == '$') { |
| 1202 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1203 | } else { |
| 1204 | if (has_e && !is_hex) { |
| 1205 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1206 | is_float = true; |
| 1207 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1208 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1209 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1210 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1211 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1212 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1213 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1214 | /* |
| 1215 | * Whitespace just before end-of-line is discarded by |
| 1216 | * pretending it's a comment; whitespace just before a |
| 1217 | * comment gets lumped into the comment. |
| 1218 | */ |
| 1219 | if (!*p || *p == ';') { |
| 1220 | type = TOK_COMMENT; |
| 1221 | while (*p) |
| 1222 | p++; |
| 1223 | } |
| 1224 | } else if (*p == ';') { |
| 1225 | type = TOK_COMMENT; |
| 1226 | while (*p) |
| 1227 | p++; |
| 1228 | } else { |
| 1229 | /* |
| 1230 | * Anything else is an operator of some kind. We check |
| 1231 | * for all the double-character operators (>>, <<, //, |
| 1232 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1233 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1234 | */ |
| 1235 | type = TOK_OTHER; |
| 1236 | if ((p[0] == '>' && p[1] == '>') || |
| 1237 | (p[0] == '<' && p[1] == '<') || |
| 1238 | (p[0] == '/' && p[1] == '/') || |
| 1239 | (p[0] == '<' && p[1] == '=') || |
| 1240 | (p[0] == '>' && p[1] == '=') || |
| 1241 | (p[0] == '=' && p[1] == '=') || |
| 1242 | (p[0] == '!' && p[1] == '=') || |
| 1243 | (p[0] == '<' && p[1] == '>') || |
| 1244 | (p[0] == '&' && p[1] == '&') || |
| 1245 | (p[0] == '|' && p[1] == '|') || |
| 1246 | (p[0] == '^' && p[1] == '^')) { |
| 1247 | p++; |
| 1248 | } |
| 1249 | p++; |
| 1250 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1251 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1252 | /* Handling unterminated string by UNV */ |
| 1253 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1254 | { |
| 1255 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1256 | t->text[p-line] = *line; |
| 1257 | tail = &t->next; |
| 1258 | } |
| 1259 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1260 | if (type != TOK_COMMENT) { |
| 1261 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1262 | tail = &t->next; |
| 1263 | } |
| 1264 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1265 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1266 | |
| 1267 | nasm_dump_token(list); |
| 1268 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1269 | return list; |
| 1270 | } |
| 1271 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1272 | /* |
| 1273 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1274 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1275 | * deleted only all at once by the delete_Blocks function. |
| 1276 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1277 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1278 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1279 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1280 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1281 | /* first, get to the end of the linked list */ |
| 1282 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1283 | b = b->next; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1284 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1285 | /* now allocate the requested chunk */ |
| 1286 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1287 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1288 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1289 | b->next = nasm_zalloc(sizeof(Blocks)); |
| 1290 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1291 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * this function deletes all managed blocks of memory |
| 1296 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1297 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1298 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1299 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1300 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1301 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1302 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1303 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1304 | * free it. |
| 1305 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1306 | while (b) { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 1307 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1308 | a = b; |
| 1309 | b = b->next; |
| 1310 | if (a != &blocks) |
| 1311 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1312 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1313 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1314 | |
| 1315 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1316 | * 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] | 1317 | * 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] | 1318 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1319 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1320 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1321 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1322 | { |
| 1323 | Token *t; |
| 1324 | int i; |
| 1325 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1326 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1327 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1328 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1329 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1330 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1331 | } |
| 1332 | t = freeTokens; |
| 1333 | freeTokens = t->next; |
| 1334 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1335 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1336 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1337 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1338 | t->text = NULL; |
| 1339 | } else { |
| 1340 | if (txtlen == 0) |
| 1341 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1342 | t->text = nasm_malloc(txtlen+1); |
| 1343 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1344 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1345 | } |
| 1346 | return t; |
| 1347 | } |
| 1348 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1349 | static Token *copy_Token(Token * tline) |
| 1350 | { |
| 1351 | Token *t, *tt, *first = NULL, *prev = NULL; |
| 1352 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1353 | for (tt = tline; tt != NULL; tt = tt->next) { |
| 1354 | if (!freeTokens) { |
| 1355 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1356 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1357 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1358 | freeTokens[i].next = NULL; |
| 1359 | } |
| 1360 | t = freeTokens; |
| 1361 | freeTokens = t->next; |
| 1362 | t->next = NULL; |
| 1363 | t->text = tt->text ? nasm_strdup(tt->text) : NULL; |
| 1364 | t->a.mac = tt->a.mac; |
| 1365 | t->a.len = tt->a.len; |
| 1366 | t->type = tt->type; |
| 1367 | if (prev != NULL) { |
| 1368 | prev->next = t; |
| 1369 | } else { |
| 1370 | first = t; |
| 1371 | } |
| 1372 | prev = t; |
| 1373 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1374 | return first; |
| 1375 | } |
| 1376 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1377 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1378 | { |
| 1379 | Token *next = t->next; |
| 1380 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1381 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1382 | freeTokens = t; |
| 1383 | return next; |
| 1384 | } |
| 1385 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1386 | /* |
| 1387 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1388 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1389 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1390 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1391 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1392 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1393 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1394 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1395 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1396 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1397 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1398 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1399 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1400 | char *v; |
| 1401 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1402 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1403 | v = t->text + 2; |
| 1404 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1405 | size_t len = nasm_unquote(v, NULL); |
| 1406 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1407 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1408 | if (len != clen) { |
| 1409 | error(ERR_NONFATAL | ERR_PASS1, |
| 1410 | "NUL character in %! string"); |
| 1411 | v = NULL; |
| 1412 | } |
| 1413 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1414 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1415 | if (v) { |
| 1416 | char *p = getenv(v); |
| 1417 | if (!p) { |
| 1418 | error(ERR_NONFATAL | ERR_PASS1, |
| 1419 | "nonexistent environment variable `%s'", v); |
| 1420 | p = ""; |
| 1421 | } |
| 1422 | t->text = nasm_strdup(p); |
| 1423 | } |
| 1424 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1425 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1426 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1427 | /* Expand local macros here and not during preprocessing */ |
| 1428 | if (expand_locals && |
| 1429 | t->type == TOK_PREPROC_ID && t->text && |
| 1430 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1431 | const char *q; |
| 1432 | char *p; |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1433 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1434 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1435 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1436 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1437 | p = nasm_strcat(buffer, q); |
| 1438 | nasm_free(t->text); |
| 1439 | t->text = p; |
| 1440 | } |
| 1441 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1442 | |
| 1443 | /* Expand %? and %?? directives */ |
Keith Kanios | 3136d48 | 2010-11-13 09:34:34 -0600 | [diff] [blame] | 1444 | if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1445 | ((t->type == TOK_PREPROC_Q) || |
| 1446 | (t->type == TOK_PREPROC_QQ))) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1447 | ExpInv *ei; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1448 | for (ei = istk->expansion; ei != NULL; ei = ei->prev){ |
| 1449 | if (ei->type == EXP_MMACRO) { |
| 1450 | nasm_free(t->text); |
| 1451 | if (t->type == TOK_PREPROC_Q) { |
| 1452 | t->text = nasm_strdup(ei->name); |
| 1453 | } else { |
| 1454 | t->text = nasm_strdup(ei->def->name); |
| 1455 | } |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1460 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1461 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1462 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1463 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1464 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1465 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1466 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1467 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1468 | |
| 1469 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1470 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1471 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1472 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1473 | q = t->text; |
| 1474 | while (*q) |
| 1475 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1476 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1477 | } |
| 1478 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1479 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1480 | return line; |
| 1481 | } |
| 1482 | |
| 1483 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1484 | * Initialize a new Line |
| 1485 | */ |
Cyrill Gorcunov | 29cb0bb | 2010-11-11 11:19:43 +0300 | [diff] [blame] | 1486 | static inline Line *new_Line(void) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1487 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1488 | return (Line *)nasm_zalloc(sizeof(Line)); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | /* |
| 1493 | * Initialize a new Expansion Definition |
| 1494 | */ |
| 1495 | static ExpDef *new_ExpDef(int exp_type) |
| 1496 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1497 | ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef)); |
| 1498 | ed->type = exp_type; |
| 1499 | ed->casesense = true; |
| 1500 | ed->state = COND_NEVER; |
| 1501 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1502 | return ed; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | /* |
| 1507 | * Initialize a new Expansion Instance |
| 1508 | */ |
| 1509 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed) |
| 1510 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1511 | ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv)); |
| 1512 | ei->type = exp_type; |
| 1513 | ei->def = ed; |
| 1514 | ei->unique = ++unique; |
| 1515 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1516 | if ((istk->mmac_depth < 1) && |
| 1517 | (istk->expansion == NULL) && |
| 1518 | (ed != NULL) && |
| 1519 | (ed->type != EXP_MMACRO) && |
| 1520 | (ed->type != EXP_REP) && |
| 1521 | (ed->type != EXP_WHILE)) { |
| 1522 | ei->linnum = src_get_linnum(); |
| 1523 | src_set_linnum(ei->linnum - ed->linecount - 1); |
| 1524 | } else { |
| 1525 | ei->linnum = -1; |
| 1526 | } |
| 1527 | if ((istk->expansion == NULL) || |
| 1528 | (ei->type == EXP_MMACRO)) { |
| 1529 | ei->relno = 0; |
| 1530 | } else { |
| 1531 | ei->relno = istk->expansion->lineno; |
| 1532 | if (ed != NULL) { |
| 1533 | ei->relno -= (ed->linecount + 1); |
| 1534 | } |
| 1535 | } |
| 1536 | return ei; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1540 | * A scanner, suitable for use by the expression evaluator, which |
| 1541 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1542 | * the first token in the line to be passed in as its private_data |
| 1543 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1544 | * |
| 1545 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1546 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1547 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1548 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1549 | Token **tlineptr = private_data; |
| 1550 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1551 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1552 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1553 | do { |
| 1554 | tline = *tlineptr; |
| 1555 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1556 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1557 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1558 | |
| 1559 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1560 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1561 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1562 | tokval->t_charptr = tline->text; |
| 1563 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1564 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1565 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1566 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1567 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1568 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1569 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1570 | p = tokval->t_charptr = tline->text; |
| 1571 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1572 | tokval->t_charptr++; |
| 1573 | return tokval->t_type = TOKEN_ID; |
| 1574 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1575 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1576 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1577 | if (r >= p+MAX_KEYWORD) |
| 1578 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1579 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1580 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1581 | *s = '\0'; |
| 1582 | /* right, so we have an identifier sitting in temp storage. now, |
| 1583 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1584 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1587 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1588 | bool rn_error; |
| 1589 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1590 | tokval->t_charptr = tline->text; |
| 1591 | if (rn_error) |
| 1592 | return tokval->t_type = TOKEN_ERRNUM; |
| 1593 | else |
| 1594 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1595 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1596 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1597 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1598 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1601 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1602 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1603 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1604 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1605 | tokval->t_charptr = tline->text; |
| 1606 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1607 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1608 | if (ep[0] != bq || ep[1] != '\0') |
| 1609 | return tokval->t_type = TOKEN_ERRSTR; |
| 1610 | else |
| 1611 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1614 | if (tline->type == TOK_OTHER) { |
| 1615 | if (!strcmp(tline->text, "<<")) |
| 1616 | return tokval->t_type = TOKEN_SHL; |
| 1617 | if (!strcmp(tline->text, ">>")) |
| 1618 | return tokval->t_type = TOKEN_SHR; |
| 1619 | if (!strcmp(tline->text, "//")) |
| 1620 | return tokval->t_type = TOKEN_SDIV; |
| 1621 | if (!strcmp(tline->text, "%%")) |
| 1622 | return tokval->t_type = TOKEN_SMOD; |
| 1623 | if (!strcmp(tline->text, "==")) |
| 1624 | return tokval->t_type = TOKEN_EQ; |
| 1625 | if (!strcmp(tline->text, "<>")) |
| 1626 | return tokval->t_type = TOKEN_NE; |
| 1627 | if (!strcmp(tline->text, "!=")) |
| 1628 | return tokval->t_type = TOKEN_NE; |
| 1629 | if (!strcmp(tline->text, "<=")) |
| 1630 | return tokval->t_type = TOKEN_LE; |
| 1631 | if (!strcmp(tline->text, ">=")) |
| 1632 | return tokval->t_type = TOKEN_GE; |
| 1633 | if (!strcmp(tline->text, "&&")) |
| 1634 | return tokval->t_type = TOKEN_DBL_AND; |
| 1635 | if (!strcmp(tline->text, "^^")) |
| 1636 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1637 | if (!strcmp(tline->text, "||")) |
| 1638 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * We have no other options: just return the first character of |
| 1643 | * the token text. |
| 1644 | */ |
| 1645 | return tokval->t_type = tline->text[0]; |
| 1646 | } |
| 1647 | |
| 1648 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1649 | * Compare a string to the name of an existing macro; this is a |
| 1650 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1651 | * depending on the value of the `casesense' parameter. |
| 1652 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1653 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1654 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1655 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1659 | * Compare a string to the name of an existing macro; this is a |
| 1660 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1661 | * depending on the value of the `casesense' parameter. |
| 1662 | */ |
| 1663 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1664 | { |
| 1665 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1666 | } |
| 1667 | |
| 1668 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1669 | * Return the Context structure associated with a %$ token. Return |
| 1670 | * NULL, having _already_ reported an error condition, if the |
| 1671 | * context stack isn't deep enough for the supplied number of $ |
| 1672 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1673 | * |
| 1674 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1675 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1676 | */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1677 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1678 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1679 | Context *ctx; |
| 1680 | int i; |
| 1681 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1682 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1683 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1684 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1685 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1686 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1687 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | if (!cstk) { |
| 1689 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
| 1690 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1693 | name += 2; |
| 1694 | ctx = cstk; |
| 1695 | i = 0; |
| 1696 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1697 | name++; |
| 1698 | i++; |
| 1699 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1700 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1701 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1702 | if (!ctx) { |
| 1703 | error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1704 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1705 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1706 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1707 | |
| 1708 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1709 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1710 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1711 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1715 | * Check to see if a file is already in a string list |
| 1716 | */ |
| 1717 | static bool in_list(const StrList *list, const char *str) |
| 1718 | { |
| 1719 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1720 | if (!strcmp(list->str, str)) |
| 1721 | return true; |
| 1722 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1723 | } |
| 1724 | return false; |
| 1725 | } |
| 1726 | |
| 1727 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1728 | * Open an include file. This routine must always return a valid |
| 1729 | * file pointer if it returns - it's responsible for throwing an |
| 1730 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1731 | * the include path one by one until it finds the file or reaches |
| 1732 | * the end of the path. |
| 1733 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1734 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1735 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1736 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1737 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1738 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1739 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1740 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1741 | size_t prefix_len = 0; |
| 1742 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1743 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1744 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1745 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | 6f38fe6 | 2010-11-11 11:32:16 +0300 | [diff] [blame] | 1746 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1747 | memcpy(sl->str, prefix, prefix_len); |
| 1748 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1749 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1750 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1751 | **dtail = sl; |
| 1752 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1753 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1754 | nasm_free(sl); |
| 1755 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1756 | if (fp) |
| 1757 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1758 | if (!ip) { |
| 1759 | if (!missing_ok) |
| 1760 | break; |
| 1761 | prefix = NULL; |
| 1762 | } else { |
| 1763 | prefix = ip->path; |
| 1764 | ip = ip->next; |
| 1765 | } |
| 1766 | if (prefix) { |
| 1767 | prefix_len = strlen(prefix); |
| 1768 | } else { |
| 1769 | /* -MG given and file not found */ |
| 1770 | if (dhead && !in_list(*dhead, file)) { |
| 1771 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1772 | sl->next = NULL; |
| 1773 | strcpy(sl->str, file); |
| 1774 | **dtail = sl; |
| 1775 | *dtail = &sl->next; |
| 1776 | } |
| 1777 | return NULL; |
| 1778 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1779 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1780 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1781 | error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1782 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1786 | * 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] | 1787 | * 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] | 1788 | * return true if _any_ single-line macro of that name is defined. |
| 1789 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1790 | * `nparam' or no parameters is defined. |
| 1791 | * |
| 1792 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1793 | * defined, or nparam is -1, the address of the definition structure |
| 1794 | * 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] | 1795 | * is NULL, no action will be taken regarding its contents, and no |
| 1796 | * error will occur. |
| 1797 | * |
| 1798 | * Note that this is also called with nparam zero to resolve |
| 1799 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1800 | * |
| 1801 | * If you already know which context macro belongs to, you can pass |
| 1802 | * the context pointer as first parameter; if you won't but name begins |
| 1803 | * with %$ the context will be automatically computed. If all_contexts |
| 1804 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1805 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1806 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1807 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1808 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1809 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1810 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1811 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1812 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1813 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1814 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1815 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1816 | if (cstk) |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1817 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1818 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1819 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1820 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1821 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1822 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1823 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1824 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1825 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1826 | while (m) { |
| 1827 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1828 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1829 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1830 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1831 | *defn = m; |
| 1832 | else |
| 1833 | *defn = NULL; |
| 1834 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1835 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1836 | } |
| 1837 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1838 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1839 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1840 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * Count and mark off the parameters in a multi-line macro call. |
| 1845 | * This is called both from within the multi-line macro expansion |
| 1846 | * code, and also to mark off the default parameters when provided |
| 1847 | * in a %macro definition line. |
| 1848 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1849 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1850 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1851 | int paramsize, brace; |
| 1852 | |
| 1853 | *nparam = paramsize = 0; |
| 1854 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1855 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1856 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1857 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1858 | paramsize += PARAM_DELTA; |
| 1859 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1860 | } |
| 1861 | skip_white_(t); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1862 | brace = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1863 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1864 | brace = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1865 | (*params)[(*nparam)++] = t; |
| 1866 | while (tok_isnt_(t, brace ? "}" : ",")) |
| 1867 | t = t->next; |
| 1868 | if (t) { /* got a comma/brace */ |
| 1869 | t = t->next; |
| 1870 | if (brace) { |
| 1871 | /* |
| 1872 | * Now we've found the closing brace, look further |
| 1873 | * for the comma. |
| 1874 | */ |
| 1875 | skip_white_(t); |
| 1876 | if (tok_isnt_(t, ",")) { |
| 1877 | error(ERR_NONFATAL, |
| 1878 | "braces do not enclose all of macro parameter"); |
| 1879 | while (tok_isnt_(t, ",")) |
| 1880 | t = t->next; |
| 1881 | } |
| 1882 | if (t) |
| 1883 | t = t->next; /* eat the comma */ |
| 1884 | } |
| 1885 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1890 | * Determine whether one of the various `if' conditions is true or |
| 1891 | * not. |
| 1892 | * |
| 1893 | * We must free the tline we get passed. |
| 1894 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1895 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1896 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1897 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1898 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1899 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1900 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1901 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1902 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1903 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1904 | |
| 1905 | origline = tline; |
| 1906 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1907 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1908 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1909 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1910 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1911 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1912 | if (!tline) |
| 1913 | break; |
| 1914 | if (tline->type != TOK_ID) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1915 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1916 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1917 | free_tlist(origline); |
| 1918 | return -1; |
| 1919 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1920 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1921 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1922 | tline = tline->next; |
| 1923 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1924 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1925 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1926 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1927 | j = false; /* have we matched yet? */ |
Cyrill Gorcunov | 4c6f82f | 2011-10-02 01:06:29 +0400 | [diff] [blame] | 1928 | skip_white_(tline); |
| 1929 | do { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1930 | if (!tline || (tline->type != TOK_ID && |
| 1931 | (tline->type != TOK_PREPROC_ID || |
| 1932 | tline->text[1] != '$'))) { |
| 1933 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1934 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1935 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1936 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1937 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1938 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1939 | tline = tline->next; |
Cyrill Gorcunov | 4c6f82f | 2011-10-02 01:06:29 +0400 | [diff] [blame] | 1940 | skip_white_(tline); |
| 1941 | } while (tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1942 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1943 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1944 | case PPC_IFENV: |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1945 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1946 | j = false; /* have we matched yet? */ |
Cyrill Gorcunov | 6acada6 | 2011-10-02 09:18:34 +0400 | [diff] [blame] | 1947 | skip_white_(tline); |
| 1948 | do { |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1949 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1950 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1951 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1952 | tline->text[1] != '!'))) { |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1953 | error(ERR_NONFATAL, |
| 1954 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1955 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1956 | goto fail; |
| 1957 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1958 | p = tline->text; |
| 1959 | if (tline->type == TOK_PREPROC_ID) |
| 1960 | p += 2; /* Skip leading %! */ |
| 1961 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1962 | nasm_unquote_cstr(p, ct); |
| 1963 | if (getenv(p)) |
| 1964 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1965 | tline = tline->next; |
Cyrill Gorcunov | 6acada6 | 2011-10-02 09:18:34 +0400 | [diff] [blame] | 1966 | skip_white_(tline); |
| 1967 | } while (tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1968 | break; |
| 1969 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1970 | case PPC_IFIDN: |
| 1971 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1972 | tline = expand_smacro(tline); |
| 1973 | t = tt = tline; |
| 1974 | while (tok_isnt_(tt, ",")) |
| 1975 | tt = tt->next; |
| 1976 | if (!tt) { |
| 1977 | error(ERR_NONFATAL, |
| 1978 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1979 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1980 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1981 | } |
| 1982 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1983 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1984 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1985 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1986 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1987 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1988 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1989 | } |
| 1990 | if (t->type == TOK_WHITESPACE) { |
| 1991 | t = t->next; |
| 1992 | continue; |
| 1993 | } |
| 1994 | if (tt->type == TOK_WHITESPACE) { |
| 1995 | tt = tt->next; |
| 1996 | continue; |
| 1997 | } |
| 1998 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1999 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2000 | break; |
| 2001 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 2002 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2003 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2004 | size_t l1 = nasm_unquote(t->text, NULL); |
| 2005 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2006 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2007 | if (l1 != l2) { |
| 2008 | j = false; |
| 2009 | break; |
| 2010 | } |
| 2011 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 2012 | j = false; |
| 2013 | break; |
| 2014 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 2015 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2016 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2017 | break; |
| 2018 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 2019 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2020 | t = t->next; |
| 2021 | tt = tt->next; |
| 2022 | } |
| 2023 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2024 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2025 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2026 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2027 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2028 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2029 | bool found = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2030 | ExpDef searching, *ed; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2031 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2032 | skip_white_(tline); |
| 2033 | tline = expand_id(tline); |
| 2034 | if (!tok_type_(tline, TOK_ID)) { |
| 2035 | error(ERR_NONFATAL, |
| 2036 | "`%s' expects a macro name", pp_directives[ct]); |
| 2037 | goto fail; |
| 2038 | } |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2039 | memset(&searching, 0, sizeof(searching)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2040 | searching.name = nasm_strdup(tline->text); |
| 2041 | searching.casesense = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2042 | searching.nparam_max = INT_MAX; |
| 2043 | tline = expand_smacro(tline->next); |
| 2044 | skip_white_(tline); |
| 2045 | if (!tline) { |
| 2046 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2047 | error(ERR_NONFATAL, |
| 2048 | "`%s' expects a parameter count or nothing", |
| 2049 | pp_directives[ct]); |
| 2050 | } else { |
| 2051 | searching.nparam_min = searching.nparam_max = |
| 2052 | readnum(tline->text, &j); |
| 2053 | if (j) |
| 2054 | error(ERR_NONFATAL, |
| 2055 | "unable to parse parameter count `%s'", |
| 2056 | tline->text); |
| 2057 | } |
| 2058 | if (tline && tok_is_(tline->next, "-")) { |
| 2059 | tline = tline->next->next; |
| 2060 | if (tok_is_(tline, "*")) |
| 2061 | searching.nparam_max = INT_MAX; |
| 2062 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 2063 | error(ERR_NONFATAL, |
| 2064 | "`%s' expects a parameter count after `-'", |
| 2065 | pp_directives[ct]); |
| 2066 | else { |
| 2067 | searching.nparam_max = readnum(tline->text, &j); |
| 2068 | if (j) |
| 2069 | error(ERR_NONFATAL, |
| 2070 | "unable to parse parameter count `%s'", |
| 2071 | tline->text); |
| 2072 | if (searching.nparam_min > searching.nparam_max) |
| 2073 | error(ERR_NONFATAL, |
| 2074 | "minimum parameter count exceeds maximum"); |
| 2075 | } |
| 2076 | } |
| 2077 | if (tline && tok_is_(tline->next, "+")) { |
| 2078 | tline = tline->next; |
| 2079 | searching.plus = true; |
| 2080 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2081 | ed = (ExpDef *) hash_findix(&expdefs, searching.name); |
| 2082 | while (ed != NULL) { |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2083 | if (!strcmp(ed->name, searching.name) && |
| 2084 | (ed->nparam_min <= searching.nparam_max || searching.plus) && |
| 2085 | (searching.nparam_min <= ed->nparam_max || ed->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2086 | found = true; |
| 2087 | break; |
| 2088 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2089 | ed = ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2090 | } |
| 2091 | if (tline && tline->next) |
| 2092 | error(ERR_WARNING|ERR_PASS1, |
| 2093 | "trailing garbage after %%ifmacro ignored"); |
| 2094 | nasm_free(searching.name); |
| 2095 | j = found; |
| 2096 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2097 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2098 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2099 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2100 | needtype = TOK_ID; |
| 2101 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2102 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2103 | needtype = TOK_NUMBER; |
| 2104 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2105 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2106 | needtype = TOK_STRING; |
| 2107 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2108 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2109 | iftype: |
| 2110 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2111 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2112 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2113 | (needtype == TOK_NUMBER && |
| 2114 | tok_type_(t, TOK_OTHER) && |
| 2115 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2116 | !t->text[1])) |
| 2117 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2118 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2119 | j = tok_type_(t, needtype); |
| 2120 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2121 | |
| 2122 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2123 | t = tline = expand_smacro(tline); |
| 2124 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2125 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2126 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2127 | j = false; |
| 2128 | if (t) { |
| 2129 | t = t->next; /* Skip the actual token */ |
| 2130 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2131 | t = t->next; |
| 2132 | j = !t; /* Should be nothing left */ |
| 2133 | } |
| 2134 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2135 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2136 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2137 | t = tline = expand_smacro(tline); |
| 2138 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2139 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2140 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2141 | j = !t; /* Should be empty */ |
| 2142 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2143 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2144 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2145 | t = tline = expand_smacro(tline); |
| 2146 | tptr = &t; |
| 2147 | tokval.t_type = TOKEN_INVALID; |
| 2148 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 2149 | NULL, pass | CRITICAL, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2150 | if (!evalresult) |
| 2151 | return -1; |
| 2152 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2153 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2154 | "trailing garbage after expression ignored"); |
| 2155 | if (!is_simple(evalresult)) { |
| 2156 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2157 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2158 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2159 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2160 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2161 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2162 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2163 | default: |
| 2164 | error(ERR_FATAL, |
| 2165 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2166 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2167 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2168 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2169 | |
| 2170 | free_tlist(origline); |
| 2171 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2172 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2173 | fail: |
| 2174 | free_tlist(origline); |
| 2175 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2179 | * Common code for defining an smacro |
| 2180 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2181 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2182 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2183 | { |
| 2184 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2185 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2186 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2187 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2188 | if (!smac) { |
| 2189 | error(ERR_WARNING|ERR_PASS1, |
| 2190 | "single-line macro `%s' defined both with and" |
| 2191 | " without parameters", mname); |
| 2192 | /* |
| 2193 | * Some instances of the old code considered this a failure, |
| 2194 | * some others didn't. What is the right thing to do here? |
| 2195 | */ |
| 2196 | free_tlist(expansion); |
| 2197 | return false; /* Failure */ |
| 2198 | } else { |
| 2199 | /* |
| 2200 | * We're redefining, so we have to take over an |
| 2201 | * existing SMacro structure. This means freeing |
| 2202 | * what was already in it. |
| 2203 | */ |
| 2204 | nasm_free(smac->name); |
| 2205 | free_tlist(smac->expansion); |
| 2206 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2207 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2208 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2209 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2210 | smac = nasm_zalloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2211 | smac->next = *smhead; |
| 2212 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2213 | } |
| 2214 | smac->name = nasm_strdup(mname); |
| 2215 | smac->casesense = casesense; |
| 2216 | smac->nparam = nparam; |
| 2217 | smac->expansion = expansion; |
| 2218 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2219 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | /* |
| 2223 | * Undefine an smacro |
| 2224 | */ |
| 2225 | static void undef_smacro(Context *ctx, const char *mname) |
| 2226 | { |
| 2227 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2228 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2229 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2230 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2231 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2232 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2233 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2234 | /* |
| 2235 | * We now have a macro name... go hunt for it. |
| 2236 | */ |
| 2237 | sp = smhead; |
| 2238 | while ((s = *sp) != NULL) { |
| 2239 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2240 | *sp = s->next; |
| 2241 | nasm_free(s->name); |
| 2242 | free_tlist(s->expansion); |
| 2243 | nasm_free(s); |
| 2244 | } else { |
| 2245 | sp = &s->next; |
| 2246 | } |
| 2247 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2251 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2252 | * Parse a mmacro specification. |
| 2253 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2254 | 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] | 2255 | { |
| 2256 | bool err; |
| 2257 | |
| 2258 | tline = tline->next; |
| 2259 | skip_white_(tline); |
| 2260 | tline = expand_id(tline); |
| 2261 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2262 | error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
| 2263 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2264 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2265 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2266 | def->name = nasm_strdup(tline->text); |
| 2267 | def->plus = false; |
| 2268 | def->nolist = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2269 | // def->in_progress = 0; |
| 2270 | // def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2271 | def->nparam_min = 0; |
| 2272 | def->nparam_max = 0; |
| 2273 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2274 | tline = expand_smacro(tline->next); |
| 2275 | skip_white_(tline); |
| 2276 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2277 | error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2278 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2279 | def->nparam_min = def->nparam_max = |
| 2280 | readnum(tline->text, &err); |
| 2281 | if (err) |
| 2282 | error(ERR_NONFATAL, |
| 2283 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2284 | } |
| 2285 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2286 | tline = tline->next->next; |
| 2287 | if (tok_is_(tline, "*")) { |
| 2288 | def->nparam_max = INT_MAX; |
| 2289 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2290 | error(ERR_NONFATAL, |
| 2291 | "`%s' expects a parameter count after `-'", directive); |
| 2292 | } else { |
| 2293 | def->nparam_max = readnum(tline->text, &err); |
| 2294 | if (err) { |
| 2295 | error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
| 2296 | tline->text); |
| 2297 | } |
| 2298 | if (def->nparam_min > def->nparam_max) { |
| 2299 | error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
| 2300 | } |
| 2301 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2302 | } |
| 2303 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2304 | tline = tline->next; |
| 2305 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2306 | } |
| 2307 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2308 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2309 | tline = tline->next; |
| 2310 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2311 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2312 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2313 | /* |
| 2314 | * Handle default parameters. |
| 2315 | */ |
| 2316 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2317 | def->dlist = tline->next; |
| 2318 | tline->next = NULL; |
| 2319 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2320 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2321 | def->dlist = NULL; |
| 2322 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2323 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2324 | def->line = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2325 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2326 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2327 | !def->plus) |
| 2328 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
| 2329 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2330 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2331 | return true; |
| 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2336 | * Decode a size directive |
| 2337 | */ |
| 2338 | static int parse_size(const char *str) { |
| 2339 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2340 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2341 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2342 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2343 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2344 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2345 | } |
| 2346 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2347 | /** |
| 2348 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2349 | * Find out if a line contains a preprocessor directive, and deal |
| 2350 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2351 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2352 | * If a directive _is_ found, it is the responsibility of this routine |
| 2353 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2354 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2355 | * @param tline a pointer to the current tokeninzed line linked list |
| 2356 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2357 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2358 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2359 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2360 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2361 | enum preproc_token i; |
| 2362 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2363 | bool err; |
| 2364 | int nparam; |
| 2365 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2366 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2367 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2368 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2369 | char *p, *pp; |
| 2370 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2371 | Include *inc; |
| 2372 | Context *ctx; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2373 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2374 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2375 | struct tokenval tokval; |
| 2376 | expr *evalresult; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2377 | ExpDef *ed, *eed, **edhead; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2378 | ExpInv *ei, *eei; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2379 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2380 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2381 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2382 | |
| 2383 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2384 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2385 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2386 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2387 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2388 | || tline->text[1] == '!')) |
| 2389 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2390 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2391 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2392 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2393 | switch (i) { |
| 2394 | case PP_INVALID: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2395 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2396 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2397 | tline->text); |
| 2398 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2399 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2400 | case PP_STACKSIZE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2401 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2402 | /* Directive to tell NASM what the default stack size is. The |
| 2403 | * default is for a 16-bit stack, and this can be overriden with |
| 2404 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2405 | */ |
| 2406 | tline = tline->next; |
| 2407 | if (tline && tline->type == TOK_WHITESPACE) |
| 2408 | tline = tline->next; |
| 2409 | if (!tline || tline->type != TOK_ID) { |
| 2410 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 2411 | free_tlist(origline); |
| 2412 | return DIRECTIVE_FOUND; |
| 2413 | } |
| 2414 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2415 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2416 | StackSize = 4; |
| 2417 | StackPointer = "ebp"; |
| 2418 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2419 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2420 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2421 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2422 | StackSize = 8; |
| 2423 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2424 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2425 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2426 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2427 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2428 | * far function call. |
| 2429 | */ |
| 2430 | StackSize = 2; |
| 2431 | StackPointer = "bp"; |
| 2432 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2433 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2435 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2436 | * far function call. We don't support near functions. |
| 2437 | */ |
| 2438 | StackSize = 2; |
| 2439 | StackPointer = "bp"; |
| 2440 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2441 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2442 | } else { |
| 2443 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 2444 | free_tlist(origline); |
| 2445 | return DIRECTIVE_FOUND; |
| 2446 | } |
| 2447 | free_tlist(origline); |
| 2448 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2449 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2450 | case PP_ARG: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2451 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2452 | /* TASM like ARG directive to define arguments to functions, in |
| 2453 | * the following form: |
| 2454 | * |
| 2455 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2456 | */ |
| 2457 | offset = ArgOffset; |
| 2458 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2459 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2460 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2461 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2462 | /* Find the argument name */ |
| 2463 | tline = tline->next; |
| 2464 | if (tline && tline->type == TOK_WHITESPACE) |
| 2465 | tline = tline->next; |
| 2466 | if (!tline || tline->type != TOK_ID) { |
| 2467 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 2468 | free_tlist(origline); |
| 2469 | return DIRECTIVE_FOUND; |
| 2470 | } |
| 2471 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2472 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2473 | /* Find the argument size type */ |
| 2474 | tline = tline->next; |
| 2475 | if (!tline || tline->type != TOK_OTHER |
| 2476 | || tline->text[0] != ':') { |
| 2477 | error(ERR_NONFATAL, |
| 2478 | "Syntax error processing `%%arg' directive"); |
| 2479 | free_tlist(origline); |
| 2480 | return DIRECTIVE_FOUND; |
| 2481 | } |
| 2482 | tline = tline->next; |
| 2483 | if (!tline || tline->type != TOK_ID) { |
| 2484 | error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
| 2485 | free_tlist(origline); |
| 2486 | return DIRECTIVE_FOUND; |
| 2487 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2488 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2489 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2490 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2491 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2492 | size = parse_size(tt->text); |
| 2493 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2494 | error(ERR_NONFATAL, |
| 2495 | "Invalid size type for `%%arg' missing directive"); |
| 2496 | free_tlist(tt); |
| 2497 | free_tlist(origline); |
| 2498 | return DIRECTIVE_FOUND; |
| 2499 | } |
| 2500 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2501 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2502 | /* Round up to even stack slots */ |
| 2503 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2504 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2505 | /* Now define the macro for the argument */ |
| 2506 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2507 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2508 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2509 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2510 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2511 | /* Move to the next argument in the list */ |
| 2512 | tline = tline->next; |
| 2513 | if (tline && tline->type == TOK_WHITESPACE) |
| 2514 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2515 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2516 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2517 | free_tlist(origline); |
| 2518 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2519 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2520 | case PP_LOCAL: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2521 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2522 | /* TASM like LOCAL directive to define local variables for a |
| 2523 | * function, in the following form: |
| 2524 | * |
| 2525 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2526 | * |
| 2527 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2528 | * required by TASM to define the local parameter size (and used |
| 2529 | * by the TASM macro package). |
| 2530 | */ |
| 2531 | offset = LocalOffset; |
| 2532 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2533 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2534 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2535 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2536 | /* Find the argument name */ |
| 2537 | tline = tline->next; |
| 2538 | if (tline && tline->type == TOK_WHITESPACE) |
| 2539 | tline = tline->next; |
| 2540 | if (!tline || tline->type != TOK_ID) { |
| 2541 | error(ERR_NONFATAL, |
| 2542 | "`%%local' missing argument parameter"); |
| 2543 | free_tlist(origline); |
| 2544 | return DIRECTIVE_FOUND; |
| 2545 | } |
| 2546 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2547 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2548 | /* Find the argument size type */ |
| 2549 | tline = tline->next; |
| 2550 | if (!tline || tline->type != TOK_OTHER |
| 2551 | || tline->text[0] != ':') { |
| 2552 | error(ERR_NONFATAL, |
| 2553 | "Syntax error processing `%%local' directive"); |
| 2554 | free_tlist(origline); |
| 2555 | return DIRECTIVE_FOUND; |
| 2556 | } |
| 2557 | tline = tline->next; |
| 2558 | if (!tline || tline->type != TOK_ID) { |
| 2559 | error(ERR_NONFATAL, |
| 2560 | "`%%local' missing size type parameter"); |
| 2561 | free_tlist(origline); |
| 2562 | return DIRECTIVE_FOUND; |
| 2563 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2564 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2565 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2566 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2567 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2568 | size = parse_size(tt->text); |
| 2569 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2570 | error(ERR_NONFATAL, |
| 2571 | "Invalid size type for `%%local' missing directive"); |
| 2572 | free_tlist(tt); |
| 2573 | free_tlist(origline); |
| 2574 | return DIRECTIVE_FOUND; |
| 2575 | } |
| 2576 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2577 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2578 | /* Round up to even stack slots */ |
| 2579 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2580 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2581 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2582 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2583 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2584 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2585 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2586 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2587 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2588 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2589 | snprintf(directive, sizeof(directive), |
| 2590 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2591 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2592 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2593 | /* Move to the next argument in the list */ |
| 2594 | tline = tline->next; |
| 2595 | if (tline && tline->type == TOK_WHITESPACE) |
| 2596 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2597 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2598 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2599 | free_tlist(origline); |
| 2600 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2601 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2602 | case PP_CLEAR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2603 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2604 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2605 | error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2606 | "trailing garbage after `%%clear' ignored"); |
| 2607 | free_macros(); |
| 2608 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2609 | free_tlist(origline); |
| 2610 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2611 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2612 | case PP_DEPEND: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2613 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2614 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2615 | skip_white_(t); |
| 2616 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2617 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2618 | error(ERR_NONFATAL, "`%%depend' expects a file name"); |
| 2619 | free_tlist(origline); |
| 2620 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2621 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2622 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2623 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2624 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2625 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2626 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2627 | nasm_unquote_cstr(p, i); |
| 2628 | if (dephead && !in_list(*dephead, p)) { |
| 2629 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2630 | sl->next = NULL; |
| 2631 | strcpy(sl->str, p); |
| 2632 | *deptail = sl; |
| 2633 | deptail = &sl->next; |
| 2634 | } |
| 2635 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2636 | return DIRECTIVE_FOUND; |
| 2637 | |
| 2638 | case PP_INCLUDE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2639 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2640 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2641 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2642 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2643 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2644 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2645 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2646 | free_tlist(origline); |
| 2647 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2648 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2649 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2650 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2651 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2652 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2653 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2654 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 2655 | inc = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2656 | inc->next = istk; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2657 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2658 | if (!inc->fp) { |
| 2659 | /* -MG given but file not found */ |
| 2660 | nasm_free(inc); |
| 2661 | } else { |
| 2662 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2663 | inc->lineno = src_set_linnum(0); |
| 2664 | inc->lineinc = 1; |
| 2665 | inc->expansion = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2666 | istk = inc; |
| 2667 | list->uplevel(LIST_INCLUDE); |
| 2668 | } |
| 2669 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2670 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2671 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2672 | case PP_USE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2673 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2674 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2675 | static macros_t *use_pkg; |
| 2676 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2677 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2678 | tline = tline->next; |
| 2679 | skip_white_(tline); |
| 2680 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2681 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2682 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2683 | tline->type != TOK_INTERNAL_STRING && |
| 2684 | tline->type != TOK_ID)) { |
H. Peter Anvin | 926fc40 | 2008-06-19 16:26:12 -0700 | [diff] [blame] | 2685 | error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2686 | free_tlist(origline); |
| 2687 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2688 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2689 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2690 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2691 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2692 | if (tline->type == TOK_STRING) |
| 2693 | nasm_unquote_cstr(tline->text, i); |
| 2694 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2695 | if (!use_pkg) |
| 2696 | error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
| 2697 | else |
| 2698 | 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] | 2699 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2700 | /* Not already included, go ahead and include it */ |
| 2701 | stdmacpos = use_pkg; |
| 2702 | } |
| 2703 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2704 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2705 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2706 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2707 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2708 | case PP_POP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2709 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2710 | tline = tline->next; |
| 2711 | skip_white_(tline); |
| 2712 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2713 | if (tline) { |
| 2714 | if (!tok_type_(tline, TOK_ID)) { |
| 2715 | error(ERR_NONFATAL, "`%s' expects a context identifier", |
| 2716 | pp_directives[i]); |
| 2717 | free_tlist(origline); |
| 2718 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2719 | } |
| 2720 | if (tline->next) |
| 2721 | error(ERR_WARNING|ERR_PASS1, |
| 2722 | "trailing garbage after `%s' ignored", |
| 2723 | pp_directives[i]); |
| 2724 | p = nasm_strdup(tline->text); |
| 2725 | } else { |
| 2726 | p = NULL; /* Anonymous */ |
| 2727 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2728 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2729 | if (i == PP_PUSH) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2730 | ctx = nasm_zalloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2731 | ctx->next = cstk; |
| 2732 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2733 | ctx->name = p; |
| 2734 | ctx->number = unique++; |
| 2735 | cstk = ctx; |
| 2736 | } else { |
| 2737 | /* %pop or %repl */ |
| 2738 | if (!cstk) { |
| 2739 | error(ERR_NONFATAL, "`%s': context stack is empty", |
| 2740 | pp_directives[i]); |
| 2741 | } else if (i == PP_POP) { |
| 2742 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
| 2743 | error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
| 2744 | "expected %s", |
| 2745 | cstk->name ? cstk->name : "anonymous", p); |
| 2746 | else |
| 2747 | ctx_pop(); |
| 2748 | } else { |
| 2749 | /* i == PP_REPL */ |
| 2750 | nasm_free(cstk->name); |
| 2751 | cstk->name = p; |
| 2752 | p = NULL; |
| 2753 | } |
| 2754 | nasm_free(p); |
| 2755 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2756 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2757 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2758 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2759 | severity = ERR_FATAL; |
| 2760 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2761 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2762 | severity = ERR_NONFATAL; |
| 2763 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2764 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2765 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2766 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2767 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2768 | issue_error: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2769 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2770 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2771 | /* Only error out if this is the final pass */ |
| 2772 | if (pass != 2 && i != PP_FATAL) |
| 2773 | return DIRECTIVE_FOUND; |
| 2774 | |
| 2775 | tline->next = expand_smacro(tline->next); |
| 2776 | tline = tline->next; |
| 2777 | skip_white_(tline); |
| 2778 | t = tline ? tline->next : NULL; |
| 2779 | skip_white_(t); |
| 2780 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2781 | /* The line contains only a quoted string */ |
| 2782 | p = tline->text; |
| 2783 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
| 2784 | error(severity, "%s", p); |
| 2785 | } else { |
| 2786 | /* Not a quoted string, or more than a quoted string */ |
| 2787 | p = detoken(tline, false); |
| 2788 | error(severity, "%s", p); |
| 2789 | nasm_free(p); |
| 2790 | } |
| 2791 | free_tlist(origline); |
| 2792 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2793 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2794 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2795 | CASE_PP_IF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2796 | if (defining != NULL) { |
| 2797 | if (defining->type == EXP_IF) { |
| 2798 | defining->def_depth ++; |
| 2799 | } |
| 2800 | return NO_DIRECTIVE_FOUND; |
| 2801 | } |
| 2802 | if ((istk->expansion != NULL) && |
| 2803 | (istk->expansion->emitting == false)) { |
| 2804 | j = COND_NEVER; |
| 2805 | } else { |
| 2806 | j = if_condition(tline->next, i); |
| 2807 | tline->next = NULL; /* it got freed */ |
| 2808 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 2809 | } |
| 2810 | ed = new_ExpDef(EXP_IF); |
| 2811 | ed->state = j; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2812 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 2813 | ed->prev = defining; |
| 2814 | defining = ed; |
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 | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2818 | CASE_PP_ELIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2819 | if (defining != NULL) { |
| 2820 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2821 | return NO_DIRECTIVE_FOUND; |
| 2822 | } |
| 2823 | } |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 2824 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2825 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2826 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2827 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2828 | case COND_IF_TRUE: |
| 2829 | defining->state = COND_DONE; |
| 2830 | defining->ignoring = true; |
| 2831 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2832 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2833 | case COND_DONE: |
| 2834 | case COND_NEVER: |
| 2835 | defining->ignoring = true; |
| 2836 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2837 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2838 | case COND_ELSE_TRUE: |
| 2839 | case COND_ELSE_FALSE: |
| 2840 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2841 | "`%%elif' after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2842 | defining->state = COND_NEVER; |
| 2843 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 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_IF_FALSE: |
| 2847 | /* |
| 2848 | * IMPORTANT: In the case of %if, we will already have |
| 2849 | * called expand_mmac_params(); however, if we're |
| 2850 | * processing an %elif we must have been in a |
| 2851 | * non-emitting mode, which would have inhibited |
| 2852 | * the normal invocation of expand_mmac_params(). |
| 2853 | * Therefore, we have to do it explicitly here. |
| 2854 | */ |
| 2855 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2856 | tline->next = NULL; /* it got freed */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2857 | defining->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2858 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2859 | defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2860 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2861 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2862 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2863 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2864 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2865 | case PP_ELSE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2866 | if (defining != NULL) { |
| 2867 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2868 | return NO_DIRECTIVE_FOUND; |
| 2869 | } |
| 2870 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2871 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2872 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2873 | "trailing garbage after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2874 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2875 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2876 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2877 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2878 | case COND_IF_TRUE: |
| 2879 | case COND_DONE: |
| 2880 | defining->state = COND_ELSE_FALSE; |
| 2881 | defining->ignoring = true; |
| 2882 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2883 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2884 | case COND_NEVER: |
| 2885 | defining->ignoring = true; |
| 2886 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2887 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2888 | case COND_IF_FALSE: |
| 2889 | defining->state = COND_ELSE_TRUE; |
| 2890 | defining->ignoring = false; |
| 2891 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2892 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2893 | case COND_ELSE_TRUE: |
| 2894 | case COND_ELSE_FALSE: |
| 2895 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2896 | "`%%else' after `%%else' ignored."); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2897 | defining->state = COND_NEVER; |
| 2898 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2899 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2900 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2901 | free_tlist(origline); |
| 2902 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2903 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2904 | case PP_ENDIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2905 | if (defining != NULL) { |
| 2906 | if (defining->type == EXP_IF) { |
| 2907 | if (defining->def_depth > 0) { |
| 2908 | defining->def_depth --; |
| 2909 | return NO_DIRECTIVE_FOUND; |
| 2910 | } |
| 2911 | } else { |
| 2912 | return NO_DIRECTIVE_FOUND; |
| 2913 | } |
| 2914 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2915 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2916 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2917 | "trailing garbage after `%%endif' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2918 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2919 | error(ERR_NONFATAL, "`%%endif': no matching `%%if'"); |
| 2920 | return DIRECTIVE_FOUND; |
| 2921 | } |
| 2922 | ed = defining; |
| 2923 | defining = ed->prev; |
| 2924 | ed->prev = expansions; |
| 2925 | expansions = ed; |
| 2926 | ei = new_ExpInv(EXP_IF, ed); |
| 2927 | ei->current = ed->line; |
| 2928 | ei->emitting = true; |
| 2929 | ei->prev = istk->expansion; |
| 2930 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2931 | free_tlist(origline); |
| 2932 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2933 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2934 | case PP_RMACRO: |
| 2935 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2936 | case PP_MACRO: |
| 2937 | case PP_IMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2938 | if (defining != NULL) { |
| 2939 | if (defining->type == EXP_MMACRO) { |
| 2940 | defining->def_depth ++; |
| 2941 | } |
| 2942 | return NO_DIRECTIVE_FOUND; |
| 2943 | } |
| 2944 | ed = new_ExpDef(EXP_MMACRO); |
| 2945 | ed->max_depth = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2946 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2947 | ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2948 | if (!parse_mmacro_spec(tline, ed, pp_directives[i])) { |
| 2949 | nasm_free(ed); |
| 2950 | ed = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2951 | return DIRECTIVE_FOUND; |
| 2952 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2953 | ed->def_depth = 0; |
| 2954 | ed->cur_depth = 0; |
| 2955 | ed->max_depth = (ed->max_depth + 1); |
| 2956 | ed->ignoring = false; |
| 2957 | ed->prev = defining; |
| 2958 | defining = ed; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2959 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2960 | eed = (ExpDef *) hash_findix(&expdefs, ed->name); |
| 2961 | while (eed) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2962 | if (!strcmp(eed->name, ed->name) && |
| 2963 | (eed->nparam_min <= ed->nparam_max || ed->plus) && |
| 2964 | (ed->nparam_min <= eed->nparam_max || eed->plus)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2965 | error(ERR_WARNING|ERR_PASS1, |
| 2966 | "redefining multi-line macro `%s'", ed->name); |
| 2967 | return DIRECTIVE_FOUND; |
| 2968 | } |
| 2969 | eed = eed->next; |
| 2970 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2971 | free_tlist(origline); |
| 2972 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2973 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2974 | case PP_ENDM: |
| 2975 | case PP_ENDMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2976 | if (defining != NULL) { |
| 2977 | if (defining->type == EXP_MMACRO) { |
| 2978 | if (defining->def_depth > 0) { |
| 2979 | defining->def_depth --; |
| 2980 | return NO_DIRECTIVE_FOUND; |
| 2981 | } |
| 2982 | } else { |
| 2983 | return NO_DIRECTIVE_FOUND; |
| 2984 | } |
| 2985 | } |
| 2986 | if (!(defining) || (defining->type != EXP_MMACRO)) { |
| 2987 | error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
| 2988 | return DIRECTIVE_FOUND; |
| 2989 | } |
| 2990 | edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name); |
| 2991 | defining->next = *edhead; |
| 2992 | *edhead = defining; |
| 2993 | ed = defining; |
| 2994 | defining = ed->prev; |
| 2995 | ed->prev = expansions; |
| 2996 | expansions = ed; |
| 2997 | ed = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2998 | free_tlist(origline); |
| 2999 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3000 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3001 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3002 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3003 | /* |
| 3004 | * We must search along istk->expansion until we hit a |
| 3005 | * macro invocation. Then we disable the emitting state(s) |
| 3006 | * between exitmacro and endmacro. |
| 3007 | */ |
| 3008 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3009 | if(ei->type == EXP_MMACRO) { |
| 3010 | break; |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | if (ei != NULL) { |
| 3015 | /* |
| 3016 | * Set all invocations leading back to the macro |
| 3017 | * invocation to a non-emitting state. |
| 3018 | */ |
| 3019 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3020 | eei->emitting = false; |
| 3021 | } |
| 3022 | eei->emitting = false; |
| 3023 | } else { |
| 3024 | error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
| 3025 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 3026 | free_tlist(origline); |
| 3027 | return DIRECTIVE_FOUND; |
| 3028 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3029 | case PP_UNMACRO: |
| 3030 | case PP_UNIMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3031 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3032 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3033 | ExpDef **ed_p; |
| 3034 | ExpDef spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3035 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3036 | spec.casesense = (i == PP_UNMACRO); |
| 3037 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 3038 | return DIRECTIVE_FOUND; |
| 3039 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3040 | ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL); |
| 3041 | while (ed_p && *ed_p) { |
| 3042 | ed = *ed_p; |
| 3043 | if (ed->casesense == spec.casesense && |
| 3044 | !mstrcmp(ed->name, spec.name, spec.casesense) && |
| 3045 | ed->nparam_min == spec.nparam_min && |
| 3046 | ed->nparam_max == spec.nparam_max && |
| 3047 | ed->plus == spec.plus) { |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3048 | if (ed->cur_depth > 0) { |
Keith Kanios | 21d885b | 2010-12-18 12:22:21 -0600 | [diff] [blame] | 3049 | error(ERR_NONFATAL, "`%s' ignored on active macro", |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3050 | pp_directives[i]); |
| 3051 | break; |
| 3052 | } else { |
| 3053 | *ed_p = ed->next; |
| 3054 | free_expdef(ed); |
| 3055 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3056 | } else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3057 | ed_p = &ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3058 | } |
| 3059 | } |
| 3060 | free_tlist(origline); |
| 3061 | free_tlist(spec.dlist); |
| 3062 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3065 | case PP_ROTATE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3066 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3067 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3068 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3069 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3070 | free_tlist(origline); |
| 3071 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 3072 | return DIRECTIVE_FOUND; |
| 3073 | } |
| 3074 | t = expand_smacro(tline->next); |
| 3075 | tline->next = NULL; |
| 3076 | free_tlist(origline); |
| 3077 | tline = t; |
| 3078 | tptr = &t; |
| 3079 | tokval.t_type = TOKEN_INVALID; |
| 3080 | evalresult = |
| 3081 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3082 | free_tlist(tline); |
| 3083 | if (!evalresult) |
| 3084 | return DIRECTIVE_FOUND; |
| 3085 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3086 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3087 | "trailing garbage after expression ignored"); |
| 3088 | if (!is_simple(evalresult)) { |
| 3089 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
| 3090 | return DIRECTIVE_FOUND; |
| 3091 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3092 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3093 | if (ei->type == EXP_MMACRO) { |
| 3094 | break; |
| 3095 | } |
| 3096 | } |
| 3097 | if (ei == NULL) { |
| 3098 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
| 3099 | } else if (ei->nparam == 0) { |
| 3100 | error(ERR_NONFATAL, |
| 3101 | "`%%rotate' invoked within macro without parameters"); |
| 3102 | } else { |
| 3103 | int rotate = ei->rotate + reloc_value(evalresult); |
| 3104 | |
| 3105 | rotate %= (int)ei->nparam; |
| 3106 | if (rotate < 0) |
| 3107 | rotate += ei->nparam; |
| 3108 | ei->rotate = rotate; |
| 3109 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3110 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3111 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3112 | case PP_REP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3113 | if (defining != NULL) { |
| 3114 | if (defining->type == EXP_REP) { |
| 3115 | defining->def_depth ++; |
| 3116 | } |
| 3117 | return NO_DIRECTIVE_FOUND; |
| 3118 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3119 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3120 | do { |
| 3121 | tline = tline->next; |
| 3122 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3123 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3124 | if (tok_type_(tline, TOK_ID) && |
| 3125 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3126 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3127 | do { |
| 3128 | tline = tline->next; |
| 3129 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3130 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3131 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3132 | if (tline) { |
| 3133 | t = expand_smacro(tline); |
| 3134 | tptr = &t; |
| 3135 | tokval.t_type = TOKEN_INVALID; |
| 3136 | evalresult = |
| 3137 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3138 | if (!evalresult) { |
| 3139 | free_tlist(origline); |
| 3140 | return DIRECTIVE_FOUND; |
| 3141 | } |
| 3142 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3143 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3144 | "trailing garbage after expression ignored"); |
| 3145 | if (!is_simple(evalresult)) { |
| 3146 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 3147 | return DIRECTIVE_FOUND; |
| 3148 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3149 | count = reloc_value(evalresult); |
| 3150 | if (count >= REP_LIMIT) { |
Cyrill Gorcunov | 71f4f84 | 2010-08-09 20:17:17 +0400 | [diff] [blame] | 3151 | error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3152 | count = 0; |
| 3153 | } else |
| 3154 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3155 | } else { |
| 3156 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3157 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3158 | } |
| 3159 | free_tlist(origline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3160 | ed = new_ExpDef(EXP_REP); |
| 3161 | ed->nolist = nolist; |
| 3162 | ed->def_depth = 0; |
| 3163 | ed->cur_depth = 1; |
| 3164 | ed->max_depth = (count - 1); |
| 3165 | ed->ignoring = false; |
| 3166 | ed->prev = defining; |
| 3167 | defining = ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3168 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3169 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3170 | case PP_ENDREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3171 | if (defining != NULL) { |
| 3172 | if (defining->type == EXP_REP) { |
| 3173 | if (defining->def_depth > 0) { |
| 3174 | defining->def_depth --; |
| 3175 | return NO_DIRECTIVE_FOUND; |
| 3176 | } |
| 3177 | } else { |
| 3178 | return NO_DIRECTIVE_FOUND; |
| 3179 | } |
| 3180 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3181 | if ((defining == NULL) || (defining->type != EXP_REP)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3182 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
| 3183 | return DIRECTIVE_FOUND; |
| 3184 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3185 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3186 | /* |
| 3187 | * Now we have a "macro" defined - although it has no name |
| 3188 | * and we won't be entering it in the hash tables - we must |
| 3189 | * push a macro-end marker for it on to istk->expansion. |
| 3190 | * After that, it will take care of propagating itself (a |
| 3191 | * macro-end marker line for a macro which is really a %rep |
| 3192 | * block will cause the macro to be re-expanded, complete |
| 3193 | * with another macro-end marker to ensure the process |
| 3194 | * continues) until the whole expansion is forcibly removed |
| 3195 | * from istk->expansion by a %exitrep. |
| 3196 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3197 | ed = defining; |
| 3198 | defining = ed->prev; |
| 3199 | ed->prev = expansions; |
| 3200 | expansions = ed; |
| 3201 | ei = new_ExpInv(EXP_REP, ed); |
| 3202 | ei->current = ed->line; |
| 3203 | ei->emitting = ((ed->max_depth > 0) ? true : false); |
| 3204 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 3205 | ei->prev = istk->expansion; |
| 3206 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3207 | free_tlist(origline); |
| 3208 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3209 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3210 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3211 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3212 | /* |
| 3213 | * We must search along istk->expansion until we hit a |
| 3214 | * rep invocation. Then we disable the emitting state(s) |
| 3215 | * between exitrep and endrep. |
| 3216 | */ |
| 3217 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3218 | if (ei->type == EXP_REP) { |
| 3219 | break; |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | if (ei != NULL) { |
| 3224 | /* |
| 3225 | * Set all invocations leading back to the rep |
| 3226 | * invocation to a non-emitting state. |
| 3227 | */ |
| 3228 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3229 | eei->emitting = false; |
| 3230 | } |
| 3231 | eei->emitting = false; |
| 3232 | eei->current = NULL; |
| 3233 | eei->def->cur_depth = eei->def->max_depth; |
| 3234 | } else { |
| 3235 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 3236 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3237 | free_tlist(origline); |
| 3238 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3239 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3240 | case PP_XDEFINE: |
| 3241 | case PP_IXDEFINE: |
| 3242 | case PP_DEFINE: |
| 3243 | case PP_IDEFINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3244 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3245 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3246 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3247 | tline = tline->next; |
| 3248 | skip_white_(tline); |
| 3249 | tline = expand_id(tline); |
| 3250 | if (!tline || (tline->type != TOK_ID && |
| 3251 | (tline->type != TOK_PREPROC_ID || |
| 3252 | tline->text[1] != '$'))) { |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3253 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3254 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3255 | free_tlist(origline); |
| 3256 | return DIRECTIVE_FOUND; |
| 3257 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3258 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3259 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3260 | last = tline; |
| 3261 | param_start = tline = tline->next; |
| 3262 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3263 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3264 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3265 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3266 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3267 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3268 | if (tok_is_(tline, "(")) { |
| 3269 | /* |
| 3270 | * This macro has parameters. |
| 3271 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3272 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3273 | tline = tline->next; |
| 3274 | while (1) { |
| 3275 | skip_white_(tline); |
| 3276 | if (!tline) { |
| 3277 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 3278 | free_tlist(origline); |
| 3279 | return DIRECTIVE_FOUND; |
| 3280 | } |
| 3281 | if (tline->type != TOK_ID) { |
| 3282 | error(ERR_NONFATAL, |
| 3283 | "`%s': parameter identifier expected", |
| 3284 | tline->text); |
| 3285 | free_tlist(origline); |
| 3286 | return DIRECTIVE_FOUND; |
| 3287 | } |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 3288 | |
| 3289 | smacro_set_param_idx(tline, nparam); |
| 3290 | nparam++; |
| 3291 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3292 | tline = tline->next; |
| 3293 | skip_white_(tline); |
| 3294 | if (tok_is_(tline, ",")) { |
| 3295 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3296 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3297 | if (!tok_is_(tline, ")")) { |
| 3298 | error(ERR_NONFATAL, |
| 3299 | "`)' expected to terminate macro template"); |
| 3300 | free_tlist(origline); |
| 3301 | return DIRECTIVE_FOUND; |
| 3302 | } |
| 3303 | break; |
| 3304 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3305 | } |
| 3306 | last = tline; |
| 3307 | tline = tline->next; |
| 3308 | } |
| 3309 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3310 | last = tline, tline = tline->next; |
| 3311 | macro_start = NULL; |
| 3312 | last->next = NULL; |
| 3313 | t = tline; |
| 3314 | while (t) { |
| 3315 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3316 | list_for_each(tt, param_start) |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 3317 | if (is_smacro_param(tt) && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3318 | !strcmp(tt->text, t->text)) |
| 3319 | t->type = tt->type; |
| 3320 | } |
| 3321 | tt = t->next; |
| 3322 | t->next = macro_start; |
| 3323 | macro_start = t; |
| 3324 | t = tt; |
| 3325 | } |
| 3326 | /* |
| 3327 | * Good. We now have a macro name, a parameter count, and a |
| 3328 | * token list (in reverse order) for an expansion. We ought |
| 3329 | * to be OK just to create an SMacro, store it, and let |
| 3330 | * free_tlist have the rest of the line (which we have |
| 3331 | * carefully re-terminated after chopping off the expansion |
| 3332 | * from the end). |
| 3333 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3334 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3335 | free_tlist(origline); |
| 3336 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3337 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3338 | case PP_UNDEF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3339 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3340 | tline = tline->next; |
| 3341 | skip_white_(tline); |
| 3342 | tline = expand_id(tline); |
| 3343 | if (!tline || (tline->type != TOK_ID && |
| 3344 | (tline->type != TOK_PREPROC_ID || |
| 3345 | tline->text[1] != '$'))) { |
| 3346 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 3347 | free_tlist(origline); |
| 3348 | return DIRECTIVE_FOUND; |
| 3349 | } |
| 3350 | if (tline->next) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3351 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3352 | "trailing garbage after macro name ignored"); |
| 3353 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3354 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3355 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3356 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3357 | undef_smacro(ctx, mname); |
| 3358 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3359 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3360 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3361 | case PP_DEFSTR: |
| 3362 | case PP_IDEFSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3363 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3364 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3365 | |
| 3366 | tline = tline->next; |
| 3367 | skip_white_(tline); |
| 3368 | tline = expand_id(tline); |
| 3369 | if (!tline || (tline->type != TOK_ID && |
| 3370 | (tline->type != TOK_PREPROC_ID || |
| 3371 | tline->text[1] != '$'))) { |
| 3372 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3373 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3374 | free_tlist(origline); |
| 3375 | return DIRECTIVE_FOUND; |
| 3376 | } |
| 3377 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3378 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3379 | last = tline; |
| 3380 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3381 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3382 | |
| 3383 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3384 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3385 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3386 | p = detoken(tline, false); |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3387 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3388 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3389 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3390 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3391 | |
| 3392 | /* |
| 3393 | * We now have a macro name, an implicit parameter count of |
| 3394 | * zero, and a string 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); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3398 | free_tlist(origline); |
| 3399 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3400 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3401 | case PP_DEFTOK: |
| 3402 | case PP_IDEFTOK: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3403 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3404 | casesense = (i == PP_DEFTOK); |
| 3405 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3406 | tline = tline->next; |
| 3407 | skip_white_(tline); |
| 3408 | tline = expand_id(tline); |
| 3409 | if (!tline || (tline->type != TOK_ID && |
| 3410 | (tline->type != TOK_PREPROC_ID || |
| 3411 | tline->text[1] != '$'))) { |
| 3412 | error(ERR_NONFATAL, |
| 3413 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3414 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3415 | free_tlist(origline); |
| 3416 | return DIRECTIVE_FOUND; |
| 3417 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3418 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3419 | last = tline; |
| 3420 | tline = expand_smacro(tline->next); |
| 3421 | last->next = NULL; |
| 3422 | |
| 3423 | t = tline; |
| 3424 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3425 | t = t->next; |
| 3426 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3427 | if (!tok_type_(t, TOK_STRING)) { |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3428 | error(ERR_NONFATAL, |
| 3429 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3430 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3431 | free_tlist(tline); |
| 3432 | free_tlist(origline); |
| 3433 | return DIRECTIVE_FOUND; |
| 3434 | } |
| 3435 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3436 | /* |
| 3437 | * Convert the string to a token stream. Note that smacros |
| 3438 | * are stored with the token stream reversed, so we have to |
| 3439 | * reverse the output of tokenize(). |
| 3440 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3441 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3442 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3443 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3444 | /* |
| 3445 | * We now have a macro name, an implicit parameter count of |
| 3446 | * zero, and a numeric token to use as an expansion. Create |
| 3447 | * and store an SMacro. |
| 3448 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3449 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3450 | free_tlist(tline); |
| 3451 | free_tlist(origline); |
| 3452 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3453 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3454 | case PP_PATHSEARCH: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3455 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3456 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3457 | FILE *fp; |
| 3458 | StrList *xsl = NULL; |
| 3459 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3460 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3461 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3462 | |
| 3463 | tline = tline->next; |
| 3464 | skip_white_(tline); |
| 3465 | tline = expand_id(tline); |
| 3466 | if (!tline || (tline->type != TOK_ID && |
| 3467 | (tline->type != TOK_PREPROC_ID || |
| 3468 | tline->text[1] != '$'))) { |
| 3469 | error(ERR_NONFATAL, |
| 3470 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3471 | free_tlist(origline); |
| 3472 | return DIRECTIVE_FOUND; |
| 3473 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3474 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3475 | last = tline; |
| 3476 | tline = expand_smacro(tline->next); |
| 3477 | last->next = NULL; |
| 3478 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3479 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3480 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3481 | t = t->next; |
| 3482 | |
| 3483 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3484 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3485 | error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3486 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3487 | free_tlist(origline); |
| 3488 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3489 | } |
| 3490 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3491 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3492 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3493 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3494 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3495 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3496 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3497 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3498 | if (fp) { |
| 3499 | p = xsl->str; |
| 3500 | fclose(fp); /* Don't actually care about the file */ |
| 3501 | } |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3502 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3503 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3504 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 3505 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3506 | |
| 3507 | /* |
| 3508 | * We now have a macro name, an implicit parameter count of |
| 3509 | * zero, and a string token to use as an expansion. Create |
| 3510 | * and store an SMacro. |
| 3511 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3512 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3513 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3514 | free_tlist(origline); |
| 3515 | return DIRECTIVE_FOUND; |
| 3516 | } |
| 3517 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3518 | case PP_STRLEN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3519 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3520 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3521 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3522 | tline = tline->next; |
| 3523 | skip_white_(tline); |
| 3524 | tline = expand_id(tline); |
| 3525 | if (!tline || (tline->type != TOK_ID && |
| 3526 | (tline->type != TOK_PREPROC_ID || |
| 3527 | tline->text[1] != '$'))) { |
| 3528 | error(ERR_NONFATAL, |
| 3529 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3530 | free_tlist(origline); |
| 3531 | return DIRECTIVE_FOUND; |
| 3532 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3533 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3534 | last = tline; |
| 3535 | tline = expand_smacro(tline->next); |
| 3536 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3537 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3538 | t = tline; |
| 3539 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3540 | t = t->next; |
| 3541 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3542 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3543 | error(ERR_NONFATAL, |
| 3544 | "`%%strlen` requires string as second parameter"); |
| 3545 | free_tlist(tline); |
| 3546 | free_tlist(origline); |
| 3547 | return DIRECTIVE_FOUND; |
| 3548 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3549 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3550 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3551 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3552 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3553 | /* |
| 3554 | * We now have a macro name, an implicit parameter count of |
| 3555 | * zero, and a numeric token to use as an expansion. Create |
| 3556 | * and store an SMacro. |
| 3557 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3558 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3559 | free_tlist(tline); |
| 3560 | free_tlist(origline); |
| 3561 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3562 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3563 | case PP_STRCAT: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3564 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3565 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3566 | |
| 3567 | tline = tline->next; |
| 3568 | skip_white_(tline); |
| 3569 | tline = expand_id(tline); |
| 3570 | if (!tline || (tline->type != TOK_ID && |
| 3571 | (tline->type != TOK_PREPROC_ID || |
| 3572 | tline->text[1] != '$'))) { |
| 3573 | error(ERR_NONFATAL, |
| 3574 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3575 | free_tlist(origline); |
| 3576 | return DIRECTIVE_FOUND; |
| 3577 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3578 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3579 | last = tline; |
| 3580 | tline = expand_smacro(tline->next); |
| 3581 | last->next = NULL; |
| 3582 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3583 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3584 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3585 | switch (t->type) { |
| 3586 | case TOK_WHITESPACE: |
| 3587 | break; |
| 3588 | case TOK_STRING: |
| 3589 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3590 | break; |
| 3591 | case TOK_OTHER: |
| 3592 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3593 | break; |
| 3594 | /* else fall through */ |
| 3595 | default: |
| 3596 | error(ERR_NONFATAL, |
| 3597 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3598 | free_tlist(tline); |
| 3599 | free_tlist(origline); |
| 3600 | return DIRECTIVE_FOUND; |
| 3601 | } |
| 3602 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3603 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3604 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3605 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3606 | if (t->type == TOK_STRING) { |
| 3607 | memcpy(p, t->text, t->a.len); |
| 3608 | p += t->a.len; |
| 3609 | } |
| 3610 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3611 | |
| 3612 | /* |
| 3613 | * We now have a macro name, an implicit parameter count of |
| 3614 | * zero, and a numeric token to use as an expansion. Create |
| 3615 | * and store an SMacro. |
| 3616 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3617 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3618 | macro_start->text = nasm_quote(pp, len); |
| 3619 | nasm_free(pp); |
| 3620 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3621 | free_tlist(tline); |
| 3622 | free_tlist(origline); |
| 3623 | return DIRECTIVE_FOUND; |
| 3624 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3625 | case PP_SUBSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3626 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3627 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3628 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3629 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3630 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3631 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3632 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3633 | tline = tline->next; |
| 3634 | skip_white_(tline); |
| 3635 | tline = expand_id(tline); |
| 3636 | if (!tline || (tline->type != TOK_ID && |
| 3637 | (tline->type != TOK_PREPROC_ID || |
| 3638 | tline->text[1] != '$'))) { |
| 3639 | error(ERR_NONFATAL, |
| 3640 | "`%%substr' expects a macro identifier as first parameter"); |
| 3641 | free_tlist(origline); |
| 3642 | return DIRECTIVE_FOUND; |
| 3643 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3644 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3645 | last = tline; |
| 3646 | tline = expand_smacro(tline->next); |
| 3647 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3648 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3649 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3650 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3651 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3652 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3653 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3654 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3655 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3656 | error(ERR_NONFATAL, |
| 3657 | "`%%substr` requires string as second parameter"); |
| 3658 | free_tlist(tline); |
| 3659 | free_tlist(origline); |
| 3660 | return DIRECTIVE_FOUND; |
| 3661 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3662 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3663 | tt = t->next; |
| 3664 | tptr = &tt; |
| 3665 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3666 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3667 | pass, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3668 | if (!evalresult) { |
| 3669 | free_tlist(tline); |
| 3670 | free_tlist(origline); |
| 3671 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3672 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3673 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3674 | free_tlist(tline); |
| 3675 | free_tlist(origline); |
| 3676 | return DIRECTIVE_FOUND; |
| 3677 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3678 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3679 | |
| 3680 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3681 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3682 | if (!tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3683 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3684 | } else { |
| 3685 | tokval.t_type = TOKEN_INVALID; |
| 3686 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3687 | pass, error, NULL); |
| 3688 | if (!evalresult) { |
| 3689 | free_tlist(tline); |
| 3690 | free_tlist(origline); |
| 3691 | return DIRECTIVE_FOUND; |
| 3692 | } else if (!is_simple(evalresult)) { |
| 3693 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3694 | free_tlist(tline); |
| 3695 | free_tlist(origline); |
| 3696 | return DIRECTIVE_FOUND; |
| 3697 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3698 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3699 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3700 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3701 | len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3702 | /* make start and count being in range */ |
| 3703 | if (start < 0) |
| 3704 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3705 | if (count < 0) |
| 3706 | count = len + count + 1 - start; |
| 3707 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3708 | count = len - start; |
| 3709 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3710 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3711 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3712 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3713 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3714 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3715 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3716 | /* |
| 3717 | * We now have a macro name, an implicit parameter count of |
| 3718 | * zero, and a numeric token to use as an expansion. Create |
| 3719 | * and store an SMacro. |
| 3720 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3721 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3722 | free_tlist(tline); |
| 3723 | free_tlist(origline); |
| 3724 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 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 | case PP_ASSIGN: |
| 3728 | case PP_IASSIGN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3729 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3730 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3731 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3732 | tline = tline->next; |
| 3733 | skip_white_(tline); |
| 3734 | tline = expand_id(tline); |
| 3735 | if (!tline || (tline->type != TOK_ID && |
| 3736 | (tline->type != TOK_PREPROC_ID || |
| 3737 | tline->text[1] != '$'))) { |
| 3738 | error(ERR_NONFATAL, |
| 3739 | "`%%%sassign' expects a macro identifier", |
| 3740 | (i == PP_IASSIGN ? "i" : "")); |
| 3741 | free_tlist(origline); |
| 3742 | return DIRECTIVE_FOUND; |
| 3743 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3744 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3745 | last = tline; |
| 3746 | tline = expand_smacro(tline->next); |
| 3747 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3748 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3749 | t = tline; |
| 3750 | tptr = &t; |
| 3751 | tokval.t_type = TOKEN_INVALID; |
| 3752 | evalresult = |
| 3753 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3754 | free_tlist(tline); |
| 3755 | if (!evalresult) { |
| 3756 | free_tlist(origline); |
| 3757 | return DIRECTIVE_FOUND; |
| 3758 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3759 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3760 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3761 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3762 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3763 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3764 | if (!is_simple(evalresult)) { |
| 3765 | error(ERR_NONFATAL, |
| 3766 | "non-constant value given to `%%%sassign'", |
| 3767 | (i == PP_IASSIGN ? "i" : "")); |
| 3768 | free_tlist(origline); |
| 3769 | return DIRECTIVE_FOUND; |
| 3770 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3771 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3772 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3773 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3774 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3775 | /* |
| 3776 | * We now have a macro name, an implicit parameter count of |
| 3777 | * zero, and a numeric token to use as an expansion. Create |
| 3778 | * and store an SMacro. |
| 3779 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3780 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3781 | free_tlist(origline); |
| 3782 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3783 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3784 | case PP_LINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3785 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3786 | /* |
| 3787 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3788 | */ |
| 3789 | tline = tline->next; |
| 3790 | skip_white_(tline); |
| 3791 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3792 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3793 | free_tlist(origline); |
| 3794 | return DIRECTIVE_FOUND; |
| 3795 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3796 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3797 | m = 1; |
| 3798 | tline = tline->next; |
| 3799 | if (tok_is_(tline, "+")) { |
| 3800 | tline = tline->next; |
| 3801 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3802 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3803 | free_tlist(origline); |
| 3804 | return DIRECTIVE_FOUND; |
| 3805 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3806 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3807 | tline = tline->next; |
| 3808 | } |
| 3809 | skip_white_(tline); |
| 3810 | src_set_linnum(k); |
| 3811 | istk->lineinc = m; |
| 3812 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3813 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3814 | } |
| 3815 | free_tlist(origline); |
| 3816 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3817 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3818 | case PP_WHILE: |
| 3819 | if (defining != NULL) { |
| 3820 | if (defining->type == EXP_WHILE) { |
| 3821 | defining->def_depth ++; |
| 3822 | } |
| 3823 | return NO_DIRECTIVE_FOUND; |
| 3824 | } |
| 3825 | l = NULL; |
| 3826 | if ((istk->expansion != NULL) && |
| 3827 | (istk->expansion->emitting == false)) { |
| 3828 | j = COND_NEVER; |
| 3829 | } else { |
| 3830 | l = new_Line(); |
| 3831 | l->first = copy_Token(tline->next); |
| 3832 | j = if_condition(tline->next, i); |
| 3833 | tline->next = NULL; /* it got freed */ |
| 3834 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 3835 | } |
| 3836 | ed = new_ExpDef(EXP_WHILE); |
| 3837 | ed->state = j; |
| 3838 | ed->cur_depth = 1; |
| 3839 | ed->max_depth = DEADMAN_LIMIT; |
| 3840 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 3841 | if (ed->ignoring == false) { |
| 3842 | ed->line = l; |
| 3843 | ed->last = l; |
| 3844 | } else if (l != NULL) { |
| 3845 | delete_Token(l->first); |
| 3846 | nasm_free(l); |
| 3847 | l = NULL; |
| 3848 | } |
| 3849 | ed->prev = defining; |
| 3850 | defining = ed; |
| 3851 | free_tlist(origline); |
| 3852 | return DIRECTIVE_FOUND; |
| 3853 | |
| 3854 | case PP_ENDWHILE: |
| 3855 | if (defining != NULL) { |
| 3856 | if (defining->type == EXP_WHILE) { |
| 3857 | if (defining->def_depth > 0) { |
| 3858 | defining->def_depth --; |
| 3859 | return NO_DIRECTIVE_FOUND; |
| 3860 | } |
| 3861 | } else { |
| 3862 | return NO_DIRECTIVE_FOUND; |
| 3863 | } |
| 3864 | } |
| 3865 | if (tline->next != NULL) { |
| 3866 | error_precond(ERR_WARNING|ERR_PASS1, |
| 3867 | "trailing garbage after `%%endwhile' ignored"); |
| 3868 | } |
| 3869 | if ((defining == NULL) || (defining->type != EXP_WHILE)) { |
| 3870 | error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'"); |
| 3871 | return DIRECTIVE_FOUND; |
| 3872 | } |
| 3873 | ed = defining; |
| 3874 | defining = ed->prev; |
| 3875 | if (ed->ignoring == false) { |
| 3876 | ed->prev = expansions; |
| 3877 | expansions = ed; |
| 3878 | ei = new_ExpInv(EXP_WHILE, ed); |
| 3879 | ei->current = ed->line->next; |
| 3880 | ei->emitting = true; |
| 3881 | ei->prev = istk->expansion; |
| 3882 | istk->expansion = ei; |
| 3883 | } else { |
| 3884 | nasm_free(ed); |
| 3885 | } |
| 3886 | free_tlist(origline); |
| 3887 | return DIRECTIVE_FOUND; |
| 3888 | |
| 3889 | case PP_EXITWHILE: |
| 3890 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3891 | /* |
| 3892 | * We must search along istk->expansion until we hit a |
| 3893 | * while invocation. Then we disable the emitting state(s) |
| 3894 | * between exitwhile and endwhile. |
| 3895 | */ |
| 3896 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3897 | if (ei->type == EXP_WHILE) { |
| 3898 | break; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | if (ei != NULL) { |
| 3903 | /* |
| 3904 | * Set all invocations leading back to the while |
| 3905 | * invocation to a non-emitting state. |
| 3906 | */ |
| 3907 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3908 | eei->emitting = false; |
| 3909 | } |
| 3910 | eei->emitting = false; |
| 3911 | eei->current = NULL; |
| 3912 | eei->def->cur_depth = eei->def->max_depth; |
| 3913 | } else { |
| 3914 | error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block"); |
| 3915 | } |
| 3916 | free_tlist(origline); |
| 3917 | return DIRECTIVE_FOUND; |
| 3918 | |
| 3919 | case PP_COMMENT: |
| 3920 | if (defining != NULL) { |
| 3921 | if (defining->type == EXP_COMMENT) { |
| 3922 | defining->def_depth ++; |
| 3923 | } |
| 3924 | return NO_DIRECTIVE_FOUND; |
| 3925 | } |
| 3926 | ed = new_ExpDef(EXP_COMMENT); |
| 3927 | ed->ignoring = true; |
| 3928 | ed->prev = defining; |
| 3929 | defining = ed; |
| 3930 | free_tlist(origline); |
| 3931 | return DIRECTIVE_FOUND; |
| 3932 | |
| 3933 | case PP_ENDCOMMENT: |
| 3934 | if (defining != NULL) { |
| 3935 | if (defining->type == EXP_COMMENT) { |
| 3936 | if (defining->def_depth > 0) { |
| 3937 | defining->def_depth --; |
| 3938 | return NO_DIRECTIVE_FOUND; |
| 3939 | } |
| 3940 | } else { |
| 3941 | return NO_DIRECTIVE_FOUND; |
| 3942 | } |
| 3943 | } |
| 3944 | if ((defining == NULL) || (defining->type != EXP_COMMENT)) { |
| 3945 | error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'"); |
| 3946 | return DIRECTIVE_FOUND; |
| 3947 | } |
| 3948 | ed = defining; |
| 3949 | defining = ed->prev; |
| 3950 | nasm_free(ed); |
| 3951 | free_tlist(origline); |
| 3952 | return DIRECTIVE_FOUND; |
| 3953 | |
| 3954 | case PP_FINAL: |
| 3955 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3956 | if (in_final != false) { |
| 3957 | error(ERR_FATAL, "`%%final' cannot be used recursively"); |
| 3958 | } |
| 3959 | tline = tline->next; |
| 3960 | skip_white_(tline); |
| 3961 | if (tline == NULL) { |
| 3962 | error(ERR_NONFATAL, "`%%final' expects at least one parameter"); |
| 3963 | } else { |
| 3964 | l = new_Line(); |
| 3965 | l->first = copy_Token(tline); |
| 3966 | l->next = finals; |
| 3967 | finals = l; |
| 3968 | } |
| 3969 | free_tlist(origline); |
| 3970 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3971 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3972 | default: |
| 3973 | error(ERR_FATAL, |
| 3974 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3975 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3976 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3977 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3981 | * Ensure that a macro parameter contains a condition code and |
| 3982 | * nothing else. Return the condition code index if so, or -1 |
| 3983 | * otherwise. |
| 3984 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3985 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3986 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3987 | Token *tt; |
| 3988 | int i, j, k, m; |
| 3989 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3990 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3991 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3992 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3993 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3994 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3995 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3996 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3997 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3998 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3999 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4000 | |
| 4001 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 4002 | j = ARRAY_SIZE(conditions); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4003 | while (j - i > 1) { |
| 4004 | k = (j + i) / 2; |
| 4005 | m = nasm_stricmp(t->text, conditions[k]); |
| 4006 | if (m == 0) { |
| 4007 | i = k; |
| 4008 | j = -2; |
| 4009 | break; |
| 4010 | } else if (m < 0) { |
| 4011 | j = k; |
| 4012 | } else |
| 4013 | i = k; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4014 | } |
| 4015 | if (j != -2) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4016 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4017 | return i; |
| 4018 | } |
| 4019 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4020 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
| 4021 | int mnum, bool handle_paste_tokens) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4022 | { |
| 4023 | Token **tail, *t, *tt; |
| 4024 | Token **paste_head; |
| 4025 | bool did_paste = false; |
| 4026 | char *tmp; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4027 | int i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4028 | |
| 4029 | /* Now handle token pasting... */ |
| 4030 | paste_head = NULL; |
| 4031 | tail = head; |
| 4032 | while ((t = *tail) && (tt = t->next)) { |
| 4033 | switch (t->type) { |
| 4034 | case TOK_WHITESPACE: |
| 4035 | if (tt->type == TOK_WHITESPACE) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4036 | /* Zap adjacent whitespace tokens */ |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4037 | t->next = delete_Token(tt); |
| 4038 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4039 | /* Do not advance paste_head here */ |
| 4040 | tail = &t->next; |
| 4041 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4042 | break; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4043 | case TOK_PASTE: /* %+ */ |
| 4044 | if (handle_paste_tokens) { |
| 4045 | /* Zap %+ and whitespace tokens to the right */ |
| 4046 | while (t && (t->type == TOK_WHITESPACE || |
| 4047 | t->type == TOK_PASTE)) |
| 4048 | t = *tail = delete_Token(t); |
| 4049 | if (!paste_head || !t) |
| 4050 | break; /* Nothing to paste with */ |
| 4051 | tail = paste_head; |
| 4052 | t = *tail; |
| 4053 | tt = t->next; |
| 4054 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 4055 | tt = t->next = delete_Token(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4056 | if (tt) { |
| 4057 | tmp = nasm_strcat(t->text, tt->text); |
| 4058 | delete_Token(t); |
| 4059 | tt = delete_Token(tt); |
| 4060 | t = *tail = tokenize(tmp); |
| 4061 | nasm_free(tmp); |
| 4062 | while (t->next) { |
| 4063 | tail = &t->next; |
| 4064 | t = t->next; |
| 4065 | } |
| 4066 | t->next = tt; /* Attach the remaining token chain */ |
| 4067 | did_paste = true; |
| 4068 | } |
| 4069 | paste_head = tail; |
| 4070 | tail = &t->next; |
| 4071 | break; |
| 4072 | } |
| 4073 | /* else fall through */ |
| 4074 | default: |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4075 | /* |
| 4076 | * Concatenation of tokens might look nontrivial |
| 4077 | * but in real it's pretty simple -- the caller |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4078 | * prepares the masks of token types to be concatenated |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4079 | * and we simply find matched sequences and slip |
| 4080 | * them together |
| 4081 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4082 | for (i = 0; i < mnum; i++) { |
| 4083 | if (PP_CONCAT_MASK(t->type) & m[i].mask_head) { |
| 4084 | size_t len = 0; |
| 4085 | char *tmp, *p; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4086 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4087 | while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4088 | len += strlen(tt->text); |
| 4089 | tt = tt->next; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4090 | } |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4091 | |
Cyrill Gorcunov | fdd0ac5 | 2011-06-27 01:22:27 +0400 | [diff] [blame] | 4092 | nasm_dump_token(tt); |
| 4093 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4094 | /* |
| 4095 | * Now tt points to the first token after |
| 4096 | * the potential paste area... |
| 4097 | */ |
| 4098 | if (tt != t->next) { |
| 4099 | /* We have at least two tokens... */ |
| 4100 | len += strlen(t->text); |
| 4101 | p = tmp = nasm_malloc(len+1); |
| 4102 | while (t != tt) { |
| 4103 | strcpy(p, t->text); |
| 4104 | p = strchr(p, '\0'); |
| 4105 | t = delete_Token(t); |
| 4106 | } |
| 4107 | t = *tail = tokenize(tmp); |
| 4108 | nasm_free(tmp); |
| 4109 | while (t->next) { |
| 4110 | tail = &t->next; |
| 4111 | t = t->next; |
| 4112 | } |
| 4113 | t->next = tt; /* Attach the remaining token chain */ |
| 4114 | did_paste = true; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4115 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4116 | paste_head = tail; |
| 4117 | tail = &t->next; |
| 4118 | break; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4119 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4120 | } |
| 4121 | if (i >= mnum) { /* no match */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4122 | tail = &t->next; |
| 4123 | if (!tok_type_(t->next, TOK_WHITESPACE)) |
| 4124 | paste_head = tail; |
| 4125 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4126 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4127 | } |
| 4128 | } |
| 4129 | return did_paste; |
| 4130 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4131 | |
| 4132 | /* |
| 4133 | * expands to a list of tokens from %{x:y} |
| 4134 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4135 | static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4136 | { |
| 4137 | Token *t = tline, **tt, *tm, *head; |
| 4138 | char *pos; |
| 4139 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4140 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4141 | pos = strchr(tline->text, ':'); |
| 4142 | nasm_assert(pos); |
| 4143 | |
| 4144 | lst = atoi(pos + 1); |
| 4145 | fst = atoi(tline->text + 1); |
| 4146 | |
| 4147 | /* |
| 4148 | * only macros params are accounted so |
| 4149 | * if someone passes %0 -- we reject such |
| 4150 | * value(s) |
| 4151 | */ |
| 4152 | if (lst == 0 || fst == 0) |
| 4153 | goto err; |
| 4154 | |
| 4155 | /* the values should be sane */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4156 | if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) || |
| 4157 | (lst > (int)ei->nparam || lst < (-(int)ei->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4158 | goto err; |
| 4159 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4160 | fst = fst < 0 ? fst + (int)ei->nparam + 1: fst; |
| 4161 | lst = lst < 0 ? lst + (int)ei->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4162 | |
| 4163 | /* counted from zero */ |
| 4164 | fst--, lst--; |
| 4165 | |
| 4166 | /* |
| 4167 | * it will be at least one token |
| 4168 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4169 | tm = ei->params[(fst + ei->rotate) % ei->nparam]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4170 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4171 | head = t, tt = &t->next; |
| 4172 | if (fst < lst) { |
| 4173 | for (i = fst + 1; i <= lst; i++) { |
| 4174 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4175 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4176 | j = (i + ei->rotate) % ei->nparam; |
| 4177 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4178 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4179 | *tt = t, tt = &t->next; |
| 4180 | } |
| 4181 | } else { |
| 4182 | for (i = fst - 1; i >= lst; i--) { |
| 4183 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4184 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4185 | j = (i + ei->rotate) % ei->nparam; |
| 4186 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4187 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4188 | *tt = t, tt = &t->next; |
| 4189 | } |
| 4190 | } |
| 4191 | |
| 4192 | *last = tt; |
| 4193 | return head; |
| 4194 | |
| 4195 | err: |
| 4196 | error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
| 4197 | &tline->text[1]); |
| 4198 | return tline; |
| 4199 | } |
| 4200 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4201 | /* |
| 4202 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4203 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4204 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4205 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4206 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4207 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4208 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4209 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4210 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4211 | |
| 4212 | tail = &thead; |
| 4213 | thead = NULL; |
| 4214 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4215 | nasm_dump_stream(tline); |
| 4216 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4217 | while (tline) { |
| 4218 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4219 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4220 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4221 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4222 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4223 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4224 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4225 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4226 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4227 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4228 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4229 | t = tline; |
| 4230 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4231 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4232 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 4233 | if (ei->type == EXP_MMACRO) { |
| 4234 | break; |
| 4235 | } |
| 4236 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4237 | if (ei == NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4238 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4239 | } else { |
| 4240 | pos = strchr(t->text, ':'); |
| 4241 | if (!pos) { |
| 4242 | switch (t->text[1]) { |
| 4243 | /* |
| 4244 | * We have to make a substitution of one of the |
| 4245 | * forms %1, %-1, %+1, %%foo, %0. |
| 4246 | */ |
| 4247 | case '0': |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4248 | if ((strlen(t->text) > 2) && (t->text[2] == '0')) { |
| 4249 | type = TOK_ID; |
| 4250 | text = nasm_strdup(ei->label_text); |
| 4251 | } else { |
| 4252 | type = TOK_NUMBER; |
| 4253 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam); |
| 4254 | text = nasm_strdup(tmpbuf); |
| 4255 | } |
| 4256 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4257 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4258 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4259 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4260 | ei->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4261 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4262 | break; |
| 4263 | case '-': |
| 4264 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4265 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4266 | tt = NULL; |
| 4267 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4268 | if (ei->nparam > 1) |
| 4269 | n = (n + ei->rotate) % ei->nparam; |
| 4270 | tt = ei->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4271 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4272 | cc = find_cc(tt); |
| 4273 | if (cc == -1) { |
| 4274 | error(ERR_NONFATAL, |
| 4275 | "macro parameter %d is not a condition code", |
| 4276 | n + 1); |
| 4277 | text = NULL; |
| 4278 | } else { |
| 4279 | type = TOK_ID; |
| 4280 | if (inverse_ccs[cc] == -1) { |
| 4281 | error(ERR_NONFATAL, |
| 4282 | "condition code `%s' is not invertible", |
| 4283 | conditions[cc]); |
| 4284 | text = NULL; |
| 4285 | } else |
| 4286 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4287 | } |
| 4288 | break; |
| 4289 | case '+': |
| 4290 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4291 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4292 | tt = NULL; |
| 4293 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4294 | if (ei->nparam > 1) |
| 4295 | n = (n + ei->rotate) % ei->nparam; |
| 4296 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4297 | } |
| 4298 | cc = find_cc(tt); |
| 4299 | if (cc == -1) { |
| 4300 | error(ERR_NONFATAL, |
| 4301 | "macro parameter %d is not a condition code", |
| 4302 | n + 1); |
| 4303 | text = NULL; |
| 4304 | } else { |
| 4305 | type = TOK_ID; |
| 4306 | text = nasm_strdup(conditions[cc]); |
| 4307 | } |
| 4308 | break; |
| 4309 | default: |
| 4310 | n = atoi(t->text + 1) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4311 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4312 | tt = NULL; |
| 4313 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4314 | if (ei->nparam > 1) |
| 4315 | n = (n + ei->rotate) % ei->nparam; |
| 4316 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4317 | } |
| 4318 | if (tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4319 | for (i = 0; i < ei->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4320 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4321 | tail = &(*tail)->next; |
| 4322 | tt = tt->next; |
| 4323 | } |
| 4324 | } |
| 4325 | text = NULL; /* we've done it here */ |
| 4326 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4327 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4328 | } else { |
| 4329 | /* |
| 4330 | * seems we have a parameters range here |
| 4331 | */ |
| 4332 | Token *head, **last; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4333 | head = expand_mmac_params_range(ei, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4334 | if (head != t) { |
| 4335 | *tail = head; |
| 4336 | *last = tline; |
| 4337 | tline = head; |
| 4338 | text = NULL; |
| 4339 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4340 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4341 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4342 | if (!text) { |
| 4343 | delete_Token(t); |
| 4344 | } else { |
| 4345 | *tail = t; |
| 4346 | tail = &t->next; |
| 4347 | t->type = type; |
| 4348 | nasm_free(t->text); |
| 4349 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4350 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4351 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4352 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4353 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4354 | } else if (tline->type == TOK_INDIRECT) { |
| 4355 | t = tline; |
| 4356 | tline = tline->next; |
| 4357 | tt = tokenize(t->text); |
| 4358 | tt = expand_mmac_params(tt); |
| 4359 | tt = expand_smacro(tt); |
| 4360 | *tail = tt; |
| 4361 | while (tt) { |
| 4362 | tt->a.mac = NULL; /* Necessary? */ |
| 4363 | tail = &tt->next; |
| 4364 | tt = tt->next; |
| 4365 | } |
| 4366 | delete_Token(t); |
| 4367 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4368 | } else { |
| 4369 | t = *tail = tline; |
| 4370 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4371 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4372 | tail = &t->next; |
| 4373 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4374 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4375 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4376 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4377 | if (changed) { |
| 4378 | const struct tokseq_match t[] = { |
| 4379 | { |
| 4380 | PP_CONCAT_MASK(TOK_ID) | |
| 4381 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4382 | PP_CONCAT_MASK(TOK_ID) | |
| 4383 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4384 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4385 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4386 | }, |
| 4387 | { |
| 4388 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4389 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4390 | } |
| 4391 | }; |
| 4392 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4393 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4394 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4395 | nasm_dump_token(thead); |
| 4396 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4397 | return thead; |
| 4398 | } |
| 4399 | |
| 4400 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4401 | * Expand all single-line macro calls made in the given line. |
| 4402 | * Return the expanded version of the line. The original is deemed |
| 4403 | * to be destroyed in the process. (In reality we'll just move |
| 4404 | * Tokens from input to output a lot of the time, rather than |
| 4405 | * actually bothering to destroy and replicate.) |
| 4406 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4407 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4408 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4409 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4410 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4411 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4412 | Token **params; |
| 4413 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4414 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4415 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4416 | Token *org_tline = tline; |
| 4417 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4418 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4419 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4420 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4421 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4422 | /* |
| 4423 | * Trick: we should avoid changing the start token pointer since it can |
| 4424 | * be contained in "next" field of other token. Because of this |
| 4425 | * we allocate a copy of first token and work with it; at the end of |
| 4426 | * routine we copy it back |
| 4427 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4428 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4429 | tline = new_Token(org_tline->next, org_tline->type, |
| 4430 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4431 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4432 | nasm_free(org_tline->text); |
| 4433 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4436 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4437 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4438 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4439 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4440 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4441 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4442 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4443 | if (!--deadman) { |
| 4444 | error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4445 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4446 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4447 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4448 | if ((mname = tline->text)) { |
| 4449 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4450 | if (tline->type == TOK_ID) { |
| 4451 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4452 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 4453 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4454 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4455 | } else |
| 4456 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4457 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4458 | /* |
| 4459 | * We've hit an identifier. As in is_mmacro below, we first |
| 4460 | * check whether the identifier is a single-line macro at |
| 4461 | * all, then think about checking for parameters if |
| 4462 | * necessary. |
| 4463 | */ |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4464 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4465 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4466 | break; |
| 4467 | if (m) { |
| 4468 | mstart = tline; |
| 4469 | params = NULL; |
| 4470 | paramsize = NULL; |
| 4471 | if (m->nparam == 0) { |
| 4472 | /* |
| 4473 | * Simple case: the macro is parameterless. Discard the |
| 4474 | * one token that the macro call took, and push the |
| 4475 | * expansion back on the to-do stack. |
| 4476 | */ |
| 4477 | if (!m->expansion) { |
| 4478 | if (!strcmp("__FILE__", m->name)) { |
| 4479 | int32_t num = 0; |
| 4480 | char *file = NULL; |
| 4481 | src_get(&num, &file); |
| 4482 | tline->text = nasm_quote(file, strlen(file)); |
| 4483 | tline->type = TOK_STRING; |
| 4484 | nasm_free(file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4485 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4486 | } |
| 4487 | if (!strcmp("__LINE__", m->name)) { |
| 4488 | nasm_free(tline->text); |
| 4489 | make_tok_num(tline, src_get_linnum()); |
| 4490 | continue; |
| 4491 | } |
| 4492 | if (!strcmp("__BITS__", m->name)) { |
| 4493 | nasm_free(tline->text); |
| 4494 | make_tok_num(tline, globalbits); |
| 4495 | continue; |
| 4496 | } |
| 4497 | tline = delete_Token(tline); |
| 4498 | continue; |
| 4499 | } |
| 4500 | } else { |
| 4501 | /* |
| 4502 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4503 | * exists and takes parameters. We must find the |
| 4504 | * parameters in the call, count them, find the SMacro |
| 4505 | * that corresponds to that form of the macro call, and |
| 4506 | * substitute for the parameters when we expand. What a |
| 4507 | * pain. |
| 4508 | */ |
| 4509 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4510 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4511 | do { |
| 4512 | t = tline->next; |
| 4513 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4514 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4515 | t->text = NULL; |
| 4516 | t = tline->next = delete_Token(t); |
| 4517 | } |
| 4518 | tline = t; |
| 4519 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4520 | if (!tok_is_(tline, "(")) { |
| 4521 | /* |
| 4522 | * This macro wasn't called with parameters: ignore |
| 4523 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4524 | */ |
| 4525 | tline = mstart; |
| 4526 | m = NULL; |
| 4527 | } else { |
| 4528 | int paren = 0; |
| 4529 | int white = 0; |
| 4530 | brackets = 0; |
| 4531 | nparam = 0; |
| 4532 | sparam = PARAM_DELTA; |
| 4533 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4534 | params[0] = tline->next; |
| 4535 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4536 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4537 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4538 | /* |
| 4539 | * For some unusual expansions |
| 4540 | * which concatenates function call |
| 4541 | */ |
| 4542 | t = tline->next; |
| 4543 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4544 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4545 | t->text = NULL; |
| 4546 | t = tline->next = delete_Token(t); |
| 4547 | } |
| 4548 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4549 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4550 | if (!tline) { |
| 4551 | error(ERR_NONFATAL, |
| 4552 | "macro call expects terminating `)'"); |
| 4553 | break; |
| 4554 | } |
| 4555 | if (tline->type == TOK_WHITESPACE |
| 4556 | && brackets <= 0) { |
| 4557 | if (paramsize[nparam]) |
| 4558 | white++; |
| 4559 | else |
| 4560 | params[nparam] = tline->next; |
| 4561 | continue; /* parameter loop */ |
| 4562 | } |
| 4563 | if (tline->type == TOK_OTHER |
| 4564 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4565 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4566 | if (ch == ',' && !paren && brackets <= 0) { |
| 4567 | if (++nparam >= sparam) { |
| 4568 | sparam += PARAM_DELTA; |
| 4569 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4570 | sparam * sizeof(Token *)); |
| 4571 | paramsize = nasm_realloc(paramsize, |
| 4572 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4573 | } |
| 4574 | params[nparam] = tline->next; |
| 4575 | paramsize[nparam] = 0; |
| 4576 | white = 0; |
| 4577 | continue; /* parameter loop */ |
| 4578 | } |
| 4579 | if (ch == '{' && |
| 4580 | (brackets > 0 || (brackets == 0 && |
| 4581 | !paramsize[nparam]))) |
| 4582 | { |
| 4583 | if (!(brackets++)) { |
| 4584 | params[nparam] = tline->next; |
| 4585 | continue; /* parameter loop */ |
| 4586 | } |
| 4587 | } |
| 4588 | if (ch == '}' && brackets > 0) |
| 4589 | if (--brackets == 0) { |
| 4590 | brackets = -1; |
| 4591 | continue; /* parameter loop */ |
| 4592 | } |
| 4593 | if (ch == '(' && !brackets) |
| 4594 | paren++; |
| 4595 | if (ch == ')' && brackets <= 0) |
| 4596 | if (--paren < 0) |
| 4597 | break; |
| 4598 | } |
| 4599 | if (brackets < 0) { |
| 4600 | brackets = 0; |
| 4601 | error(ERR_NONFATAL, "braces do not " |
| 4602 | "enclose all of macro parameter"); |
| 4603 | } |
| 4604 | paramsize[nparam] += white + 1; |
| 4605 | white = 0; |
| 4606 | } /* parameter loop */ |
| 4607 | nparam++; |
| 4608 | while (m && (m->nparam != nparam || |
| 4609 | mstrcmp(m->name, mname, |
| 4610 | m->casesense))) |
| 4611 | m = m->next; |
| 4612 | if (!m) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4613 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4614 | "macro `%s' exists, " |
| 4615 | "but not taking %d parameters", |
| 4616 | mstart->text, nparam); |
| 4617 | } |
| 4618 | } |
| 4619 | if (m && m->in_progress) |
| 4620 | m = NULL; |
| 4621 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4622 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4623 | * Design question: should we handle !tline, which |
| 4624 | * indicates missing ')' here, or expand those |
| 4625 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4626 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4627 | */ |
| 4628 | nasm_free(params); |
| 4629 | nasm_free(paramsize); |
| 4630 | tline = mstart; |
| 4631 | } else { |
| 4632 | /* |
| 4633 | * Expand the macro: we are placed on the last token of the |
| 4634 | * call, so that we can easily split the call from the |
| 4635 | * following tokens. We also start by pushing an SMAC_END |
| 4636 | * token for the cycle removal. |
| 4637 | */ |
| 4638 | t = tline; |
| 4639 | if (t) { |
| 4640 | tline = t->next; |
| 4641 | t->next = NULL; |
| 4642 | } |
| 4643 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4644 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4645 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4646 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4647 | list_for_each(t, m->expansion) { |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4648 | if (is_smacro_param(t)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4649 | Token *pcopy = tline, **ptail = &pcopy; |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4650 | Token *ttt; |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4651 | int i, idx; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4652 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4653 | idx = smacro_get_param_idx(t); |
| 4654 | ttt = params[idx]; |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4655 | |
| 4656 | /* |
| 4657 | * We need smacro paramters appended. |
| 4658 | */ |
| 4659 | for (i = paramsize[idx]; i > 0; i--) { |
| 4660 | *ptail = new_Token(tline, ttt->type, ttt->text, 0); |
| 4661 | ptail = &(*ptail)->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4662 | ttt = ttt->next; |
| 4663 | } |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4664 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4665 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4666 | } else if (t->type == TOK_PREPROC_Q) { |
| 4667 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4668 | tline = tt; |
| 4669 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4670 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4671 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4672 | } else { |
| 4673 | tt = new_Token(tline, t->type, t->text, 0); |
| 4674 | tline = tt; |
| 4675 | } |
| 4676 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4677 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4678 | /* |
| 4679 | * Having done that, get rid of the macro call, and clean |
| 4680 | * up the parameters. |
| 4681 | */ |
| 4682 | nasm_free(params); |
| 4683 | nasm_free(paramsize); |
| 4684 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4685 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4686 | continue; /* main token loop */ |
| 4687 | } |
| 4688 | } |
| 4689 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4690 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4691 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4692 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4693 | tline = delete_Token(tline); |
| 4694 | } else { |
| 4695 | t = *tail = tline; |
| 4696 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4697 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4698 | t->next = NULL; |
| 4699 | tail = &t->next; |
| 4700 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4703 | /* |
| 4704 | * 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] | 4705 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4706 | * TOK_IDs should be concatenated. |
| 4707 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4708 | * them (without white spaces in between). |
| 4709 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4710 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4711 | const struct tokseq_match t[] = { |
| 4712 | { |
| 4713 | PP_CONCAT_MASK(TOK_ID) | |
| 4714 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4715 | PP_CONCAT_MASK(TOK_ID) | |
| 4716 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4717 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4718 | } |
| 4719 | }; |
| 4720 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4721 | /* |
| 4722 | * If we concatenated something, *and* we had previously expanded |
| 4723 | * an actual macro, scan the lines again for macros... |
| 4724 | */ |
| 4725 | tline = thead; |
| 4726 | expanded = false; |
| 4727 | goto again; |
| 4728 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4729 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4730 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4731 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4732 | if (org_tline) { |
| 4733 | if (thead) { |
| 4734 | *org_tline = *thead; |
| 4735 | /* since we just gave text to org_line, don't free it */ |
| 4736 | thead->text = NULL; |
| 4737 | delete_Token(thead); |
| 4738 | } else { |
| 4739 | /* the expression expanded to empty line; |
| 4740 | we can't return NULL for some reasons |
| 4741 | we just set the line to a single WHITESPACE token. */ |
| 4742 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4743 | org_tline->text = NULL; |
| 4744 | org_tline->type = TOK_WHITESPACE; |
| 4745 | } |
| 4746 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4749 | return thead; |
| 4750 | } |
| 4751 | |
| 4752 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4753 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4754 | * right before they are fetched in. The reason is that there can be |
| 4755 | * identifiers consisting of several subparts. We consider that if there |
| 4756 | * are more than one element forming the name, user wants a expansion, |
| 4757 | * otherwise it will be left as-is. Example: |
| 4758 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4759 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4760 | * |
| 4761 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4762 | * will suck it and define the corresponding value. Other case: |
| 4763 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4764 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4765 | * |
| 4766 | * In this case user wants name to be expanded *before* %define starts |
| 4767 | * working, so we'll expand %$abc into something (if it has a value; |
| 4768 | * otherwise it will be left as-is) then concatenate all successive |
| 4769 | * PP_IDs into one. |
| 4770 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4771 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4772 | { |
| 4773 | Token *cur, *oldnext = NULL; |
| 4774 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4775 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4776 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4777 | |
| 4778 | cur = tline; |
| 4779 | while (cur->next && |
Cyrill Gorcunov | 5b6c96b | 2011-06-30 00:22:53 +0400 | [diff] [blame] | 4780 | (cur->next->type == TOK_ID || |
| 4781 | cur->next->type == TOK_PREPROC_ID || |
| 4782 | cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4783 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4784 | |
| 4785 | /* If identifier consists of just one token, don't expand */ |
| 4786 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4787 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4788 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4789 | if (cur) { |
| 4790 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4791 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4794 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4795 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4796 | if (cur) { |
| 4797 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4798 | cur = tline; |
| 4799 | while (cur && cur->next) |
| 4800 | cur = cur->next; |
| 4801 | if (cur) |
| 4802 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | return tline; |
| 4806 | } |
| 4807 | |
| 4808 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4809 | * Determine whether the given line constitutes a multi-line macro |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4810 | * 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] | 4811 | * to check for an initial label - that's taken care of in |
| 4812 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4813 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4814 | * name is easy to find. |
| 4815 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4816 | static ExpDef *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4817 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4818 | ExpDef *head, *ed; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4819 | Token **params; |
| 4820 | int nparam; |
| 4821 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4822 | head = (ExpDef *) hash_findix(&expdefs, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4823 | |
| 4824 | /* |
| 4825 | * Efficiency: first we see if any macro exists with the given |
| 4826 | * name. If not, we can return NULL immediately. _Then_ we |
| 4827 | * count the parameters, and then we look further along the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4828 | * list if necessary to find the proper ExpDef. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4829 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4830 | list_for_each(ed, head) |
| 4831 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4832 | break; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4833 | if (!ed) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4834 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4835 | |
| 4836 | /* |
| 4837 | * OK, we have a potential macro. Count and demarcate the |
| 4838 | * parameters. |
| 4839 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4840 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4841 | |
| 4842 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4843 | * 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] | 4844 | * structure that handles this number. |
| 4845 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4846 | while (ed) { |
| 4847 | if (ed->nparam_min <= nparam |
| 4848 | && (ed->plus || nparam <= ed->nparam_max)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4849 | /* |
| 4850 | * It's right, and we can use it. Add its default |
| 4851 | * parameters to the end of our list if necessary. |
| 4852 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4853 | if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4854 | params = |
| 4855 | nasm_realloc(params, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4856 | ((ed->nparam_min + ed->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4857 | 1) * sizeof(*params))); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4858 | while (nparam < ed->nparam_min + ed->ndefs) { |
| 4859 | params[nparam] = ed->defaults[nparam - ed->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4860 | nparam++; |
| 4861 | } |
| 4862 | } |
| 4863 | /* |
| 4864 | * If we've gone over the maximum parameter count (and |
| 4865 | * we're in Plus mode), ignore parameters beyond |
| 4866 | * nparam_max. |
| 4867 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4868 | if (ed->plus && nparam > ed->nparam_max) |
| 4869 | nparam = ed->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4870 | /* |
| 4871 | * Then terminate the parameter list, and leave. |
| 4872 | */ |
| 4873 | if (!params) { /* need this special case */ |
| 4874 | params = nasm_malloc(sizeof(*params)); |
| 4875 | nparam = 0; |
| 4876 | } |
| 4877 | params[nparam] = NULL; |
| 4878 | *params_array = params; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4879 | return ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4880 | } |
| 4881 | /* |
| 4882 | * This one wasn't right: look for the next one with the |
| 4883 | * same name. |
| 4884 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4885 | list_for_each(ed, ed->next) |
| 4886 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4887 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | /* |
| 4891 | * After all that, we didn't find one with the right number of |
| 4892 | * parameters. Issue a warning, and fail to expand the macro. |
| 4893 | */ |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4894 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4895 | "macro `%s' exists, but not taking %d parameters", |
| 4896 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4897 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4898 | return NULL; |
| 4899 | } |
| 4900 | |
| 4901 | /* |
| 4902 | * Expand the multi-line macro call made by the given line, if |
| 4903 | * 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] | 4904 | * istk->expansion and return true. Otherwise return false. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4905 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4906 | static bool expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4907 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4908 | Token *label = NULL; |
| 4909 | int dont_prepend = 0; |
Cyrill Gorcunov | fb27fc2 | 2011-06-25 12:08:30 +0400 | [diff] [blame] | 4910 | Token **params, *t; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4911 | Line *l = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4912 | ExpDef *ed; |
| 4913 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4914 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4915 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4916 | |
| 4917 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4918 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4919 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4920 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4921 | return false; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4922 | ed = is_mmacro(t, ¶ms); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4923 | if (ed != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4924 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4925 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4926 | Token *last; |
| 4927 | /* |
| 4928 | * We have an id which isn't a macro call. We'll assume |
| 4929 | * it might be a label; we'll also check to see if a |
| 4930 | * colon follows it. Then, if there's another id after |
| 4931 | * that lot, we'll check it again for macro-hood. |
| 4932 | */ |
| 4933 | label = last = t; |
| 4934 | t = t->next; |
| 4935 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4936 | last = t, t = t->next; |
| 4937 | if (tok_is_(t, ":")) { |
| 4938 | dont_prepend = 1; |
| 4939 | last = t, t = t->next; |
| 4940 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4941 | last = t, t = t->next; |
| 4942 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4943 | if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, ¶ms))) |
| 4944 | return false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4945 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4946 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4947 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4948 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4949 | |
| 4950 | /* |
| 4951 | * Fix up the parameters: this involves stripping leading and |
| 4952 | * trailing whitespace, then stripping braces if they are |
| 4953 | * present. |
| 4954 | */ |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 4955 | for (nparam = 0; params[nparam]; nparam++) |
| 4956 | ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4957 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4958 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4959 | for (i = 0; params[i]; i++) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4960 | int brace = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4961 | int comma = (!ed->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4962 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4963 | t = params[i]; |
| 4964 | skip_white_(t); |
| 4965 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4966 | t = t->next, brace = true, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4967 | params[i] = t; |
| 4968 | paramlen[i] = 0; |
| 4969 | while (t) { |
| 4970 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4971 | break; /* ... because we have hit a comma */ |
| 4972 | if (comma && t->type == TOK_WHITESPACE |
| 4973 | && tok_is_(t->next, ",")) |
| 4974 | break; /* ... or a space then a comma */ |
| 4975 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
| 4976 | break; /* ... or a brace */ |
| 4977 | t = t->next; |
| 4978 | paramlen[i]++; |
| 4979 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4982 | if (ed->cur_depth >= ed->max_depth) { |
| 4983 | if (ed->max_depth > 1) { |
| 4984 | error(ERR_WARNING, |
| 4985 | "reached maximum macro recursion depth of %i for %s", |
| 4986 | ed->max_depth,ed->name); |
| 4987 | } |
| 4988 | return false; |
| 4989 | } else { |
| 4990 | ed->cur_depth ++; |
| 4991 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4992 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4993 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4994 | * OK, we have found a ExpDef structure representing a |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4995 | * previously defined mmacro. Create an expansion invocation |
| 4996 | * and point it back to the expansion definition. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4997 | * parameter tokens and macro-local tokens doesn't get done |
| 4998 | * until the single-line macro substitution process; this is |
| 4999 | * because delaying them allows us to change the semantics |
| 5000 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5001 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5002 | ei = new_ExpInv(EXP_MMACRO, ed); |
| 5003 | ei->name = nasm_strdup(mname); |
| 5004 | //ei->label = label; |
| 5005 | //ei->label_text = detoken(label, false); |
| 5006 | ei->current = ed->line; |
| 5007 | ei->emitting = true; |
| 5008 | //ei->iline = tline; |
| 5009 | ei->params = params; |
| 5010 | ei->nparam = nparam; |
| 5011 | ei->rotate = 0; |
| 5012 | ei->paramlen = paramlen; |
| 5013 | ei->lineno = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5014 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5015 | ei->prev = istk->expansion; |
| 5016 | istk->expansion = ei; |
| 5017 | |
| 5018 | /* |
| 5019 | * Special case: detect %00 on first invocation; if found, |
| 5020 | * avoid emitting any labels that precede the mmacro call. |
| 5021 | * ed->prepend is set to -1 when %00 is detected, else 1. |
| 5022 | */ |
| 5023 | if (ed->prepend == 0) { |
| 5024 | for (l = ed->line; l != NULL; l = l->next) { |
| 5025 | for (t = l->first; t != NULL; t = t->next) { |
| 5026 | if ((t->type == TOK_PREPROC_ID) && |
| 5027 | (strlen(t->text) == 3) && |
| 5028 | (t->text[1] == '0') && (t->text[2] == '0')) { |
| 5029 | dont_prepend = -1; |
| 5030 | break; |
| 5031 | } |
| 5032 | } |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5033 | if (dont_prepend < 0) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5034 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5035 | } |
| 5036 | ed->prepend = ((dont_prepend < 0) ? -1 : 1); |
| 5037 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5038 | |
| 5039 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5040 | * If we had a label, push it on as the first line of |
| 5041 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5042 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5043 | if (label != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5044 | if (ed->prepend < 0) { |
| 5045 | ei->label_text = detoken(label, false); |
| 5046 | } else { |
| 5047 | if (dont_prepend == 0) { |
| 5048 | t = label; |
| 5049 | while (t->next != NULL) { |
| 5050 | t = t->next; |
| 5051 | } |
| 5052 | t->next = new_Token(NULL, TOK_OTHER, ":", 0); |
| 5053 | } |
| 5054 | l = new_Line(); |
| 5055 | l->first = copy_Token(label); |
| 5056 | l->next = ei->current; |
| 5057 | ei->current = l; |
| 5058 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5059 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5060 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5061 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5062 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5063 | istk->mmac_depth++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5064 | return true; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5067 | /* The function that actually does the error reporting */ |
| 5068 | static void verror(int severity, const char *fmt, va_list arg) |
| 5069 | { |
| 5070 | char buff[1024]; |
| 5071 | |
| 5072 | vsnprintf(buff, sizeof(buff), fmt, arg); |
| 5073 | |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5074 | if (istk && istk->mmac_depth > 0) { |
| 5075 | ExpInv *ei = istk->expansion; |
| 5076 | int lineno = ei->lineno; |
| 5077 | while (ei) { |
| 5078 | if (ei->type == EXP_MMACRO) |
| 5079 | break; |
| 5080 | lineno += ei->relno; |
| 5081 | ei = ei->prev; |
| 5082 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5083 | nasm_error(severity, "(%s:%d) %s", ei->def->name, |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5084 | lineno, buff); |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5085 | } else { |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5086 | nasm_error(severity, "%s", buff); |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5087 | } |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5088 | } |
| 5089 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5090 | /* |
| 5091 | * Since preprocessor always operate only on the line that didn't |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5092 | * arrived yet, we should always use ERR_OFFBY1. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5093 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5094 | static void error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5095 | { |
| 5096 | va_list arg; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5097 | va_start(arg, fmt); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5098 | verror(severity, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5099 | va_end(arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5100 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5101 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5102 | /* |
| 5103 | * Because %else etc are evaluated in the state context |
| 5104 | * of the previous branch, errors might get lost with error(): |
| 5105 | * %if 0 ... %else trailing garbage ... %endif |
| 5106 | * So %else etc should report errors with this function. |
| 5107 | */ |
| 5108 | static void error_precond(int severity, const char *fmt, ...) |
| 5109 | { |
| 5110 | va_list arg; |
| 5111 | |
| 5112 | /* Only ignore the error if it's really in a dead branch */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5113 | if ((istk != NULL) && |
| 5114 | (istk->expansion != NULL) && |
| 5115 | (istk->expansion->type == EXP_IF) && |
| 5116 | (istk->expansion->def->state == COND_NEVER)) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5117 | return; |
| 5118 | |
| 5119 | va_start(arg, fmt); |
| 5120 | verror(severity, fmt, arg); |
| 5121 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5122 | } |
| 5123 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5124 | static void |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5125 | pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5126 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5127 | Token *t; |
| 5128 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5129 | cstk = NULL; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5130 | istk = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5131 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5132 | src_set_fname(nasm_strdup(file)); |
| 5133 | src_set_linnum(0); |
| 5134 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5135 | if (!istk->fp) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5136 | error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5137 | file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5138 | defining = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5139 | finals = NULL; |
| 5140 | in_final = false; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5141 | nested_mac_count = 0; |
| 5142 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5143 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5144 | unique = 0; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5145 | if (tasm_compatible_mode) |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5146 | stdmacpos = nasm_stdmac; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5147 | else |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5148 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5149 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 5150 | do_predef = true; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5151 | list = listgen; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5152 | |
| 5153 | /* |
| 5154 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 5155 | * The caller, however, will also pass in 3 for preprocess-only so |
| 5156 | * we can set __PASS__ accordingly. |
| 5157 | */ |
| 5158 | pass = apass > 2 ? 2 : apass; |
| 5159 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5160 | dephead = deptail = deplist; |
| 5161 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5162 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 5163 | sl->next = NULL; |
| 5164 | strcpy(sl->str, file); |
| 5165 | *deptail = sl; |
| 5166 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5167 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5168 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5169 | /* |
| 5170 | * Define the __PASS__ macro. This is defined here unlike |
| 5171 | * all the other builtins, because it is special -- it varies between |
| 5172 | * passes. |
| 5173 | */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5174 | t = nasm_zalloc(sizeof(*t)); |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5175 | make_tok_num(t, apass); |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5176 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5179 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5180 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5181 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5182 | Token *tline; |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame] | 5183 | ExpDef *ed; |
| 5184 | ExpInv *ei; |
| 5185 | Line *l; |
| 5186 | int j; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5187 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5188 | while (1) { |
| 5189 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5190 | * Fetch a tokenized line, either from the expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5191 | * buffer or from the input file. |
| 5192 | */ |
| 5193 | tline = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5194 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5195 | while (1) { /* until we get a line we can use */ |
| 5196 | /* |
| 5197 | * Fetch a tokenized line from the expansion buffer |
| 5198 | */ |
| 5199 | if (istk->expansion != NULL) { |
| 5200 | ei = istk->expansion; |
| 5201 | if (ei->current != NULL) { |
| 5202 | if (ei->emitting == false) { |
| 5203 | ei->current = NULL; |
| 5204 | continue; |
| 5205 | } |
| 5206 | l = ei->current; |
| 5207 | ei->current = l->next; |
| 5208 | ei->lineno++; |
| 5209 | tline = copy_Token(l->first); |
| 5210 | if (((ei->type == EXP_REP) || |
| 5211 | (ei->type == EXP_MMACRO) || |
| 5212 | (ei->type == EXP_WHILE)) |
| 5213 | && (ei->def->nolist == false)) { |
| 5214 | char *p = detoken(tline, false); |
| 5215 | list->line(LIST_MACRO, p); |
| 5216 | nasm_free(p); |
| 5217 | } |
| 5218 | if (ei->linnum > -1) { |
| 5219 | src_set_linnum(src_get_linnum() + 1); |
| 5220 | } |
| 5221 | break; |
| 5222 | } else if ((ei->type == EXP_REP) && |
| 5223 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5224 | ei->def->cur_depth ++; |
| 5225 | ei->current = ei->def->line; |
| 5226 | ei->lineno = 0; |
| 5227 | continue; |
| 5228 | } else if ((ei->type == EXP_WHILE) && |
| 5229 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5230 | ei->current = ei->def->line; |
| 5231 | ei->lineno = 0; |
| 5232 | tline = copy_Token(ei->current->first); |
| 5233 | j = if_condition(tline, PP_WHILE); |
| 5234 | tline = NULL; |
| 5235 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 5236 | if (j == COND_IF_TRUE) { |
| 5237 | ei->current = ei->current->next; |
| 5238 | ei->def->cur_depth ++; |
| 5239 | } else { |
| 5240 | ei->emitting = false; |
| 5241 | ei->current = NULL; |
| 5242 | ei->def->cur_depth = ei->def->max_depth; |
| 5243 | } |
| 5244 | continue; |
| 5245 | } else { |
| 5246 | istk->expansion = ei->prev; |
| 5247 | ed = ei->def; |
| 5248 | if (ed != NULL) { |
| 5249 | if ((ei->emitting == true) && |
| 5250 | (ed->max_depth == DEADMAN_LIMIT) && |
| 5251 | (ed->cur_depth == DEADMAN_LIMIT) |
| 5252 | ) { |
| 5253 | error(ERR_FATAL, "runaway expansion detected, aborting"); |
| 5254 | } |
| 5255 | if (ed->cur_depth > 0) { |
| 5256 | ed->cur_depth --; |
Keith Kanios | 9412465 | 2010-12-18 11:47:28 -0600 | [diff] [blame] | 5257 | } else if (ed->type != EXP_MMACRO) { |
Keith Kanios | 6a7c3e9 | 2010-12-18 11:49:53 -0600 | [diff] [blame] | 5258 | expansions = ed->prev; |
| 5259 | free_expdef(ed); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5260 | } |
| 5261 | if ((ei->type == EXP_REP) || |
| 5262 | (ei->type == EXP_MMACRO) || |
| 5263 | (ei->type == EXP_WHILE)) { |
| 5264 | list->downlevel(LIST_MACRO); |
| 5265 | if (ei->type == EXP_MMACRO) { |
| 5266 | istk->mmac_depth--; |
| 5267 | } |
| 5268 | } |
| 5269 | } |
| 5270 | if (ei->linnum > -1) { |
| 5271 | src_set_linnum(ei->linnum); |
| 5272 | } |
| 5273 | free_expinv(ei); |
| 5274 | continue; |
| 5275 | } |
| 5276 | } |
| 5277 | |
| 5278 | /* |
| 5279 | * Read in line from input and tokenize |
| 5280 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5281 | line = read_line(); |
| 5282 | if (line) { /* from the current input file */ |
| 5283 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5284 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5285 | nasm_free(line); |
| 5286 | break; |
| 5287 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5288 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5289 | /* |
| 5290 | * The current file has ended; work down the istk |
| 5291 | */ |
| 5292 | { |
| 5293 | Include *i = istk; |
| 5294 | fclose(i->fp); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5295 | if (i->expansion != NULL) { |
| 5296 | error(ERR_FATAL, |
| 5297 | "end of file while still in an expansion"); |
| 5298 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5299 | /* only set line and file name if there's a next node */ |
| 5300 | if (i->next) { |
| 5301 | src_set_linnum(i->lineno); |
Cyrill Gorcunov | d34a108 | 2011-03-07 11:23:08 +0300 | [diff] [blame] | 5302 | nasm_free(src_set_fname(nasm_strdup(i->fname))); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5303 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5304 | if ((i->next == NULL) && (finals != NULL)) { |
| 5305 | in_final = true; |
| 5306 | ei = new_ExpInv(EXP_FINAL, NULL); |
| 5307 | ei->emitting = true; |
| 5308 | ei->current = finals; |
| 5309 | istk->expansion = ei; |
| 5310 | finals = NULL; |
| 5311 | continue; |
| 5312 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5313 | istk = i->next; |
| 5314 | list->downlevel(LIST_INCLUDE); |
| 5315 | nasm_free(i); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5316 | if (istk == NULL) { |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5317 | if (finals != NULL) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5318 | in_final = true; |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5319 | else |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5320 | return NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5321 | } |
| 5322 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5323 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5324 | } |
| 5325 | |
Cyrill Gorcunov | 9900c6b | 2011-10-09 18:58:46 +0400 | [diff] [blame] | 5326 | if (defining == NULL) |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5327 | tline = expand_mmac_params(tline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5328 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5329 | /* |
| 5330 | * Check the line to see if it's a preprocessor directive. |
| 5331 | */ |
| 5332 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5333 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5334 | } else if (defining != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5335 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5336 | * We're defining an expansion. We emit nothing at all, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5337 | * and just shove the tokenized line on to the definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5338 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5339 | if (defining->ignoring == false) { |
| 5340 | Line *l = new_Line(); |
| 5341 | l->first = tline; |
| 5342 | if (defining->line == NULL) { |
| 5343 | defining->line = l; |
| 5344 | defining->last = l; |
| 5345 | } else { |
| 5346 | defining->last->next = l; |
| 5347 | defining->last = l; |
| 5348 | } |
| 5349 | } else { |
Keith Kanios | 104803d | 2010-12-18 11:05:46 -0600 | [diff] [blame] | 5350 | free_tlist(tline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5351 | } |
| 5352 | defining->linecount++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5353 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5354 | } else if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5355 | (istk->expansion->emitting != true)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5356 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5357 | * We're in a non-emitting branch of an expansion. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5358 | * Emit nothing at all, not even a blank line: when we |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5359 | * emerge from the expansion we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5360 | * directive so we keep our place correctly. |
| 5361 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5362 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5363 | continue; |
| 5364 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5365 | tline = expand_smacro(tline); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5366 | if (expand_mmacro(tline) != true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5367 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5368 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5369 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5370 | line = detoken(tline, true); |
| 5371 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5372 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5373 | } else { |
| 5374 | continue; |
| 5375 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5376 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5377 | } |
| 5378 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5381 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5382 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5383 | if (defining != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5384 | error(ERR_NONFATAL, "end of file while still defining an expansion"); |
| 5385 | while (defining != NULL) { |
| 5386 | ExpDef *ed = defining; |
| 5387 | defining = ed->prev; |
| 5388 | free_expdef(ed); |
| 5389 | } |
| 5390 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5391 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5392 | while (cstk != NULL) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5393 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5394 | free_macros(); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5395 | while (istk != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5396 | Include *i = istk; |
| 5397 | istk = istk->next; |
| 5398 | fclose(i->fp); |
| 5399 | nasm_free(i->fname); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5400 | while (i->expansion != NULL) { |
| 5401 | ExpInv *ei = i->expansion; |
| 5402 | i->expansion = ei->prev; |
| 5403 | free_expinv(ei); |
| 5404 | } |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5405 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5406 | } |
| 5407 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5408 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5409 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5410 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5411 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5412 | free_llist(predef); |
| 5413 | delete_Blocks(); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5414 | while ((i = ipath)) { |
| 5415 | ipath = i->next; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 5416 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5417 | nasm_free(i); |
| 5418 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5419 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5422 | void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5423 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5424 | IncPath *i = nasm_zalloc(sizeof(IncPath)); |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5425 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5426 | if (path) |
| 5427 | i->path = nasm_strdup(path); |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5428 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5429 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5430 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5431 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5432 | j = j->next; |
| 5433 | j->next = i; |
| 5434 | } else { |
| 5435 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5436 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5437 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5438 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5439 | void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5440 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5441 | Token *inc, *space, *name; |
| 5442 | Line *l; |
| 5443 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5444 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5445 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5446 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5447 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5448 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5449 | l->next = predef; |
| 5450 | l->first = inc; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5451 | predef = l; |
| 5452 | } |
| 5453 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5454 | void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5455 | { |
| 5456 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5457 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5458 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5459 | |
| 5460 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5461 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5462 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5463 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5464 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5465 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5466 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5467 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5468 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5469 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5470 | l->next = predef; |
| 5471 | l->first = def; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5472 | predef = l; |
| 5473 | } |
| 5474 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5475 | void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5476 | { |
| 5477 | Token *def, *space; |
| 5478 | Line *l; |
| 5479 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5480 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5481 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5482 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5483 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5484 | l = new_Line(); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5485 | l->next = predef; |
| 5486 | l->first = def; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5487 | predef = l; |
| 5488 | } |
| 5489 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5490 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5491 | * This function is used to assist with "runtime" preprocessor |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5492 | * directives, e.g. pp_runtime("%define __BITS__ 64"); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5493 | * |
| 5494 | * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU |
| 5495 | * PASS A VALID STRING TO THIS FUNCTION!!!!! |
| 5496 | */ |
| 5497 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5498 | void pp_runtime(char *definition) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5499 | { |
| 5500 | Token *def; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5501 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5502 | def = tokenize(definition); |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5503 | if (do_directive(def) == NO_DIRECTIVE_FOUND) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5504 | free_tlist(def); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5505 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 5508 | void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5509 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5510 | extrastdmac = macros; |
| 5511 | } |
| 5512 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5513 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5514 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5515 | char numbuf[20]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5516 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5517 | tok->text = nasm_strdup(numbuf); |
| 5518 | tok->type = TOK_NUMBER; |
| 5519 | } |
| 5520 | |
Cyrill Gorcunov | 86b2ad0 | 2011-07-01 10:38:25 +0400 | [diff] [blame] | 5521 | struct preproc_ops nasmpp = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5522 | pp_reset, |
| 5523 | pp_getline, |
| 5524 | pp_cleanup |
| 5525 | }; |