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 { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 108 | SMacro *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 109 | char *name; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 110 | bool casesense; |
H. Peter Anvin | 16ed438 | 2007-10-11 10:06:19 -0700 | [diff] [blame] | 111 | bool in_progress; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 112 | unsigned int nparam; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 113 | Token *expansion; |
| 114 | }; |
| 115 | |
| 116 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 117 | * The context stack is composed of a linked list of these. |
| 118 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 119 | struct Context { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 120 | Context *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 121 | char *name; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 122 | struct hash_table localmac; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 123 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * This is the internal form which we break input lines up into. |
| 128 | * Typically stored in linked lists. |
| 129 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 130 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 131 | * necessarily used as-is, but is intended to denote the number of |
| 132 | * the substituted parameter. So in the definition |
| 133 | * |
| 134 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 135 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 136 | * the token representing `x' will have its type changed to |
| 137 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 138 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 139 | * |
| 140 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 141 | * which doesn't need quotes around it. Used in the pre-include |
| 142 | * mechanism as an alternative to trying to find a sensible type of |
| 143 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 144 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 145 | enum pp_token_type { |
| 146 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 147 | TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 148 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
| 149 | TOK_INTERNAL_STRING, |
| 150 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 151 | TOK_PASTE, /* %+ */ |
| 152 | TOK_INDIRECT, /* %[...] */ |
| 153 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 154 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 157 | #define PP_CONCAT_MASK(x) (1 << (x)) |
| 158 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 159 | struct tokseq_match { |
| 160 | int mask_head; |
| 161 | int mask_tail; |
| 162 | }; |
| 163 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 164 | struct Token { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 165 | Token *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 166 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 167 | union { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 168 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 169 | size_t len; /* scratch length field */ |
| 170 | } a; /* Auxiliary data */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 171 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 175 | * Expansion definitions are stored as a linked list of |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 176 | * these, which is essentially a container to allow several linked |
| 177 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 178 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 179 | * Note that in this module, linked lists are treated as stacks |
| 180 | * wherever possible. For this reason, Lines are _pushed_ on to the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 181 | * `last' field in ExpDef structures, so that the linked list, |
| 182 | * if walked, would emit the expansion lines in the proper order. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 183 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 184 | struct Line { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 185 | Line *next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 186 | Token *first; |
| 187 | }; |
| 188 | |
| 189 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 190 | * Expansion Types |
| 191 | */ |
| 192 | enum pp_exp_type { |
| 193 | EXP_NONE = 0, EXP_PREDEF, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 194 | EXP_MMACRO, EXP_REP, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 195 | EXP_IF, EXP_WHILE, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 196 | EXP_COMMENT, EXP_FINAL, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 197 | EXP_MAX = INT_MAX /* Keep compiler from reducing the range */ |
| 198 | }; |
| 199 | |
| 200 | /* |
| 201 | * Store the definition of an expansion, in which is any |
| 202 | * preprocessor directive that has an ending pair. |
| 203 | * |
| 204 | * This design allows for arbitrary expansion/recursion depth, |
| 205 | * upto the DEADMAN_LIMIT. |
| 206 | * |
| 207 | * The `next' field is used for storing ExpDef in hash tables; the |
| 208 | * `prev' field is for the global `expansions` linked-list. |
| 209 | */ |
| 210 | struct ExpDef { |
| 211 | ExpDef *prev; /* previous definition */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 212 | ExpDef *next; /* next in hash table */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 213 | enum pp_exp_type type; /* expansion type */ |
| 214 | char *name; /* definition name */ |
| 215 | int nparam_min, nparam_max; |
| 216 | bool casesense; |
| 217 | bool plus; /* is the last parameter greedy? */ |
| 218 | bool nolist; /* is this expansion listing-inhibited? */ |
| 219 | Token *dlist; /* all defaults as one list */ |
| 220 | Token **defaults; /* parameter default pointers */ |
| 221 | int ndefs; /* number of default parameters */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 222 | |
| 223 | int prepend; /* label prepend state */ |
| 224 | Line *label; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 225 | Line *line; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 226 | Line *last; |
| 227 | int linecount; /* number of lines within expansion */ |
| 228 | |
| 229 | int64_t def_depth; /* current number of definition pairs deep */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 230 | int64_t cur_depth; /* current number of expansions */ |
| 231 | int64_t max_depth; /* maximum number of expansions allowed */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 232 | |
| 233 | int state; /* condition state */ |
| 234 | bool ignoring; /* ignoring definition lines */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * Store the invocation of an expansion. |
| 239 | * |
| 240 | * The `prev' field is for the `istk->expansion` linked-list. |
| 241 | * |
| 242 | * When an expansion is being expanded, `params', `iline', `nparam', |
| 243 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 244 | */ |
| 245 | struct ExpInv { |
| 246 | ExpInv *prev; /* previous invocation */ |
| 247 | enum pp_exp_type type; /* expansion type */ |
| 248 | ExpDef *def; /* pointer to expansion definition */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 249 | char *name; /* invocation name */ |
| 250 | Line *label; /* pointer to label */ |
| 251 | char *label_text; /* pointer to label text */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 252 | Line *current; /* pointer to current line in invocation */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 253 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 254 | Token **params; /* actual parameters */ |
| 255 | Token *iline; /* invocation line */ |
| 256 | unsigned int nparam, rotate; |
| 257 | int *paramlen; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 258 | |
| 259 | uint64_t unique; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 260 | bool emitting; |
| 261 | int lineno; /* current line number in expansion */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 262 | int linnum; /* line number at invocation */ |
| 263 | int relno; /* relative line number at invocation */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 267 | * To handle an arbitrary level of file inclusion, we maintain a |
| 268 | * stack (ie linked list) of these things. |
| 269 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 270 | struct Include { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 271 | Include *next; |
| 272 | FILE *fp; |
| 273 | Cond *conds; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 274 | ExpInv *expansion; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 275 | char *fname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 276 | int lineno, lineinc; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 277 | int mmac_depth; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 281 | * Include search path. This is simply a list of strings which get |
| 282 | * prepended, in turn, to the name of an include file, in an |
| 283 | * attempt to find the file if it's not in the current directory. |
| 284 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 285 | struct IncPath { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 286 | IncPath *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 287 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 291 | * Conditional assembly: we maintain a separate stack of these for |
| 292 | * each level of file inclusion. (The only reason we keep the |
| 293 | * stacks separate is to ensure that a stray `%endif' in a file |
| 294 | * included from within the true branch of a `%if' won't terminate |
| 295 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 296 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 297 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 298 | /* |
| 299 | * These states are for use just after %if or %elif: IF_TRUE |
| 300 | * means the condition has evaluated to truth so we are |
| 301 | * currently emitting, whereas IF_FALSE means we are not |
| 302 | * currently emitting but will start doing so if a %else comes |
| 303 | * up. In these states, all directives are admissible: %elif, |
| 304 | * %else and %endif. (And of course %if.) |
| 305 | */ |
| 306 | COND_IF_TRUE, COND_IF_FALSE, |
| 307 | /* |
| 308 | * These states come up after a %else: ELSE_TRUE means we're |
| 309 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 310 | * any %elif or %else will cause an error. |
| 311 | */ |
| 312 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
| 313 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 314 | * These states mean that we're not emitting now, and also that |
| 315 | * nothing until %endif will be emitted at all. COND_DONE is |
| 316 | * used when we've had our moment of emission |
| 317 | * and have now started seeing %elifs. COND_NEVER is used when |
| 318 | * the condition construct in question is contained within a |
| 319 | * non-emitting branch of a larger condition construct, |
| 320 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 321 | */ |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 322 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 323 | }; |
| 324 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
| 325 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 326 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 327 | * These defines are used as the possible return values for do_directive |
| 328 | */ |
| 329 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 330 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 331 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 332 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 333 | * This define sets the upper limit for smacro and expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 334 | */ |
| 335 | #define DEADMAN_LIMIT (1 << 20) |
| 336 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 337 | /* max reps */ |
| 338 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 339 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 340 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 341 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 342 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 343 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 344 | * ones, so we need a different enum... |
| 345 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 346 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 347 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 348 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 349 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 350 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 351 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 352 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 353 | c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO, |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 354 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 355 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 356 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 357 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 358 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 359 | c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S, |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 360 | c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 361 | }; |
| 362 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 363 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 364 | * conditional compilation directives. Using the NASM pre-processor does |
| 365 | * not work, so we look for them specifically from the following list and |
| 366 | * then jam in the equivalent NASM directive into the input stream. |
| 367 | */ |
| 368 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 369 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 370 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 371 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 372 | }; |
| 373 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 374 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 375 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 376 | "ifndef", "include", "local" |
| 377 | }; |
| 378 | |
| 379 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 380 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 381 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 382 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 383 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 384 | static Context *cstk; |
| 385 | static Include *istk; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 386 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 387 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 388 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 389 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 390 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 391 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 392 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 393 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 394 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 395 | |
| 396 | static ListGen *list; |
| 397 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 398 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 399 | * The current set of expansion definitions we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 400 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 401 | static struct hash_table expdefs; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * The current set of single-line macros we have defined. |
| 405 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 406 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 409 | * Linked List of all active expansion definitions |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 410 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 411 | struct ExpDef *expansions = NULL; |
| 412 | |
| 413 | /* |
| 414 | * The expansion we are currently defining |
| 415 | */ |
| 416 | static ExpDef *defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 417 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 418 | static uint64_t nested_mac_count; |
| 419 | static uint64_t nested_rep_count; |
| 420 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 421 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 422 | * Linked-list of lines to preprocess, prior to cleanup |
| 423 | */ |
| 424 | static Line *finals = NULL; |
| 425 | static bool in_final = false; |
| 426 | |
| 427 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 428 | * The number of macro parameters to allocate space for at a time. |
| 429 | */ |
| 430 | #define PARAM_DELTA 16 |
| 431 | |
| 432 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 433 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 434 | * This gives our position in the macro set, when we're processing it. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 435 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 436 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 437 | |
| 438 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 439 | * The extra standard macros that come from the object format, if |
| 440 | * any. |
| 441 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 442 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 443 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 444 | |
| 445 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 446 | * Tokens are allocated in blocks to improve speed |
| 447 | */ |
| 448 | #define TOKEN_BLOCKSIZE 4096 |
| 449 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 450 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 451 | Blocks *next; |
| 452 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 456 | |
| 457 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 458 | * Forward declarations. |
| 459 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 460 | static Token *expand_mmac_params(Token * tline); |
| 461 | static Token *expand_smacro(Token * tline); |
| 462 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 463 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 464 | static void make_tok_num(Token * tok, int64_t val); |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 465 | static void error(int severity, const char *fmt, ...); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 466 | static void error_precond(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 467 | static void *new_Block(size_t size); |
| 468 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 469 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 470 | const char *text, int txtlen); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 471 | static Token *copy_Token(Token * tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 472 | static Token *delete_Token(Token * t); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 473 | static Line *new_Line(void); |
| 474 | static ExpDef *new_ExpDef(int exp_type); |
| 475 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 479 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 480 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 481 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 482 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 483 | #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] | 484 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame^] | 485 | /* |
| 486 | * A few helpers for single macros |
| 487 | */ |
| 488 | |
| 489 | /* We might be not smacro parameter at all */ |
| 490 | static bool is_smacro_param(Token *t) |
| 491 | { |
| 492 | return t->type >= TOK_SMAC_PARAM; |
| 493 | } |
| 494 | |
| 495 | /* smacro parameters are counted in a special way */ |
| 496 | static int smacro_get_param_idx(Token *t) |
| 497 | { |
| 498 | return t->type - TOK_SMAC_PARAM; |
| 499 | } |
| 500 | |
| 501 | /* encode smacro parameter index */ |
| 502 | static int smacro_set_param_idx(Token *t, unsigned int index) |
| 503 | { |
| 504 | return t->type = TOK_SMAC_PARAM + index; |
| 505 | } |
| 506 | |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 507 | #ifdef NASM_TRACE |
| 508 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 509 | #define stringify(x) #x |
| 510 | |
Cyrill Gorcunov | 9d1141a | 2011-06-26 23:07:35 +0400 | [diff] [blame] | 511 | #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] | 512 | #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] | 513 | #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] | 514 | |
| 515 | /* FIXME: we really need some compound type here instead of inplace code */ |
| 516 | static const char *nasm_get_tok_type_str(enum pp_token_type type) |
| 517 | { |
| 518 | #define SWITCH_TOK_NAME(type) \ |
| 519 | case (type): \ |
| 520 | return stringify(type) |
| 521 | |
| 522 | switch (type) { |
| 523 | SWITCH_TOK_NAME(TOK_NONE); |
| 524 | SWITCH_TOK_NAME(TOK_WHITESPACE); |
| 525 | SWITCH_TOK_NAME(TOK_COMMENT); |
| 526 | SWITCH_TOK_NAME(TOK_ID); |
| 527 | SWITCH_TOK_NAME(TOK_PREPROC_ID); |
| 528 | SWITCH_TOK_NAME(TOK_STRING); |
| 529 | SWITCH_TOK_NAME(TOK_NUMBER); |
| 530 | SWITCH_TOK_NAME(TOK_FLOAT); |
| 531 | SWITCH_TOK_NAME(TOK_SMAC_END); |
| 532 | SWITCH_TOK_NAME(TOK_OTHER); |
| 533 | SWITCH_TOK_NAME(TOK_INTERNAL_STRING); |
| 534 | SWITCH_TOK_NAME(TOK_PREPROC_Q); |
| 535 | SWITCH_TOK_NAME(TOK_PREPROC_QQ); |
| 536 | SWITCH_TOK_NAME(TOK_PASTE); |
| 537 | SWITCH_TOK_NAME(TOK_INDIRECT); |
| 538 | SWITCH_TOK_NAME(TOK_SMAC_PARAM); |
| 539 | SWITCH_TOK_NAME(TOK_MAX); |
| 540 | } |
| 541 | |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | 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] | 546 | { |
| 547 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 548 | if (token) { |
| 549 | Token *t; |
| 550 | list_for_each(t, token) { |
| 551 | if (t->text) |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 552 | printf("'%s'(%s) ", t->text, |
| 553 | nasm_get_tok_type_str(t->type)); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 554 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 555 | printf("\n\n"); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 559 | static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func) |
| 560 | { |
| 561 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 562 | if (token) { |
| 563 | Token *t; |
| 564 | list_for_each(t, token) |
| 565 | printf("%s", t->text ? t->text : " "); |
| 566 | printf("\n\n"); |
| 567 | } |
| 568 | } |
| 569 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 570 | #else |
| 571 | #define nasm_trace(msg, ...) |
| 572 | #define nasm_dump_token(t) |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 573 | #define nasm_dump_stream(t) |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 574 | #endif |
| 575 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 576 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 577 | * nasm_unquote with error if the string contains NUL characters. |
| 578 | * If the string contains NUL characters, issue an error and return |
| 579 | * the C len, i.e. truncate at the NUL. |
| 580 | */ |
| 581 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 582 | { |
| 583 | size_t len = nasm_unquote(qstr, NULL); |
| 584 | size_t clen = strlen(qstr); |
| 585 | |
| 586 | if (len != clen) |
| 587 | error(ERR_NONFATAL, "NUL character in `%s' directive", |
| 588 | pp_directives[directive]); |
| 589 | |
| 590 | return clen; |
| 591 | } |
| 592 | |
| 593 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 594 | * In-place reverse a list of tokens. |
| 595 | */ |
| 596 | static Token *reverse_tokens(Token *t) |
| 597 | { |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 598 | Token *prev, *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 599 | |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 600 | list_reverse(t, prev, next); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 601 | |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 602 | return t; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 606 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 607 | * front of them. We do it here because I could not find any other |
| 608 | * place to do it for the moment, and it is a hack (ideally it would |
| 609 | * be nice to be able to use the NASM pre-processor to do it). |
| 610 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 611 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 612 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 613 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 614 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 615 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 616 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 617 | |
| 618 | /* Binary search for the directive name */ |
| 619 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 620 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 621 | q = nasm_skip_word(p); |
| 622 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 623 | if (len) { |
| 624 | oldchar = p[len]; |
| 625 | p[len] = 0; |
| 626 | while (j - i > 1) { |
| 627 | k = (j + i) / 2; |
| 628 | m = nasm_stricmp(p, tasm_directives[k]); |
| 629 | if (m == 0) { |
| 630 | /* We have found a directive, so jam a % in front of it |
| 631 | * so that NASM will then recognise it as one if it's own. |
| 632 | */ |
| 633 | p[len] = oldchar; |
| 634 | len = strlen(p); |
| 635 | oldline = line; |
| 636 | line = nasm_malloc(len + 2); |
| 637 | line[0] = '%'; |
| 638 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 639 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 640 | * NASM does not recognise IFDIFI, so we convert |
| 641 | * it to %if 0. This is not used in NASM |
| 642 | * compatible code, but does need to parse for the |
| 643 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 644 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 645 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 646 | } else { |
| 647 | memcpy(line + 1, p, len + 1); |
| 648 | } |
| 649 | nasm_free(oldline); |
| 650 | return line; |
| 651 | } else if (m < 0) { |
| 652 | j = k; |
| 653 | } else |
| 654 | i = k; |
| 655 | } |
| 656 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 657 | } |
| 658 | return line; |
| 659 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 660 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 661 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 662 | * The pre-preprocessing stage... This function translates line |
| 663 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 664 | * flags') into NASM preprocessor line number indications (`%line |
| 665 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 666 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 667 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 668 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 669 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 670 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 671 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 672 | if (line[0] == '#' && line[1] == ' ') { |
| 673 | oldline = line; |
| 674 | fname = oldline + 2; |
| 675 | lineno = atoi(fname); |
| 676 | fname += strspn(fname, "0123456789 "); |
| 677 | if (*fname == '"') |
| 678 | fname++; |
| 679 | fnlen = strcspn(fname, "\""); |
| 680 | line = nasm_malloc(20 + fnlen); |
| 681 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 682 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 683 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 684 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 685 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 686 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 690 | * Free a linked list of tokens. |
| 691 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 692 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 693 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 694 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 695 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Free a linked list of lines. |
| 700 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 701 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 702 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 703 | Line *l, *tmp; |
| 704 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 705 | free_tlist(l->first); |
| 706 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 711 | * Free an ExpDef |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 712 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 713 | static void free_expdef(ExpDef * ed) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 714 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 715 | nasm_free(ed->name); |
| 716 | free_tlist(ed->dlist); |
| 717 | nasm_free(ed->defaults); |
| 718 | free_llist(ed->line); |
| 719 | nasm_free(ed); |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Free an ExpInv |
| 724 | */ |
| 725 | static void free_expinv(ExpInv * ei) |
| 726 | { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 727 | nasm_free(ei->name); |
| 728 | nasm_free(ei->label_text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 729 | nasm_free(ei); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 733 | * Free all currently defined macros, and free the hash tables |
| 734 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 735 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 736 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 737 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 738 | const char *key; |
| 739 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 740 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 741 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 742 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 743 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 744 | nasm_free(s->name); |
| 745 | free_tlist(s->expansion); |
| 746 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 747 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 748 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 749 | hash_free(smt); |
| 750 | } |
| 751 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 752 | static void free_expdef_table(struct hash_table *edt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 753 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 754 | ExpDef *ed, *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 | |
| 758 | it = NULL; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 759 | while ((ed = hash_iterate(edt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 760 | nasm_free((void *)key); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 761 | list_for_each_safe(ed ,tmp, ed) |
| 762 | free_expdef(ed); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 763 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 764 | hash_free(edt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | static void free_macros(void) |
| 768 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 769 | free_smacro_table(&smacros); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 770 | free_expdef_table(&expdefs); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Initialize the hash tables |
| 775 | */ |
| 776 | static void init_macros(void) |
| 777 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 778 | hash_init(&smacros, HASH_LARGE); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 779 | hash_init(&expdefs, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 783 | * Pop the context stack. |
| 784 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 785 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 786 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 787 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 788 | |
| 789 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 790 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 791 | nasm_free(c->name); |
| 792 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 793 | } |
| 794 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 795 | /* |
| 796 | * Search for a key in the hash index; adding it if necessary |
| 797 | * (in which case we initialize the data pointer to NULL.) |
| 798 | */ |
| 799 | static void ** |
| 800 | hash_findi_add(struct hash_table *hash, const char *str) |
| 801 | { |
| 802 | struct hash_insert hi; |
| 803 | void **r; |
| 804 | char *strx; |
| 805 | |
| 806 | r = hash_findi(hash, str, &hi); |
| 807 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 808 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 809 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 810 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 811 | return hash_add(&hi, strx, NULL); |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * Like hash_findi, but returns the data element rather than a pointer |
| 816 | * to it. Used only when not adding a new element, hence no third |
| 817 | * argument. |
| 818 | */ |
| 819 | static void * |
| 820 | hash_findix(struct hash_table *hash, const char *str) |
| 821 | { |
| 822 | void **p; |
| 823 | |
| 824 | p = hash_findi(hash, str, NULL); |
| 825 | return p ? *p : NULL; |
| 826 | } |
| 827 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 828 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 829 | * read line from standard macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 830 | * if there no more left -- return NULL |
| 831 | */ |
| 832 | static char *line_from_stdmac(void) |
| 833 | { |
| 834 | unsigned char c; |
| 835 | const unsigned char *p = stdmacpos; |
| 836 | char *line, *q; |
| 837 | size_t len = 0; |
| 838 | |
| 839 | if (!stdmacpos) |
| 840 | return NULL; |
| 841 | |
| 842 | while ((c = *p++)) { |
| 843 | if (c >= 0x80) |
| 844 | len += pp_directives_len[c - 0x80] + 1; |
| 845 | else |
| 846 | len++; |
| 847 | } |
| 848 | |
| 849 | line = nasm_malloc(len + 1); |
| 850 | q = line; |
| 851 | while ((c = *stdmacpos++)) { |
| 852 | if (c >= 0x80) { |
| 853 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 854 | q += pp_directives_len[c - 0x80]; |
| 855 | *q++ = ' '; |
| 856 | } else { |
| 857 | *q++ = c; |
| 858 | } |
| 859 | } |
| 860 | stdmacpos = p; |
| 861 | *q = '\0'; |
| 862 | |
| 863 | if (!*stdmacpos) { |
| 864 | /* This was the last of the standard macro chain... */ |
| 865 | stdmacpos = NULL; |
| 866 | if (any_extrastdmac) { |
| 867 | stdmacpos = extrastdmac; |
| 868 | any_extrastdmac = false; |
| 869 | } else if (do_predef) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 870 | ExpInv *ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 871 | Line *pd, *l; |
| 872 | Token *head, **tail, *t; |
| 873 | |
| 874 | /* |
| 875 | * Nasty hack: here we push the contents of |
| 876 | * `predef' on to the top-level expansion stack, |
| 877 | * since this is the most convenient way to |
| 878 | * implement the pre-include and pre-define |
| 879 | * features. |
| 880 | */ |
| 881 | list_for_each(pd, predef) { |
| 882 | head = NULL; |
| 883 | tail = &head; |
| 884 | list_for_each(t, pd->first) { |
| 885 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 886 | tail = &(*tail)->next; |
| 887 | } |
| 888 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 889 | l = new_Line(); |
| 890 | l->first = head; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 891 | ei = new_ExpInv(EXP_PREDEF, NULL); |
| 892 | ei->current = l; |
| 893 | ei->emitting = true; |
| 894 | ei->prev = istk->expansion; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 895 | istk->expansion = ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 896 | } |
| 897 | do_predef = false; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | return line; |
| 902 | } |
| 903 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 904 | #define BUF_DELTA 512 |
| 905 | /* |
| 906 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 907 | * at the end of the line read, and handling spurious ^Zs. Will |
| 908 | * return lines from the standard macro set if this has not already |
| 909 | * been done. |
| 910 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 911 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 912 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 913 | char *buffer, *p, *q; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 914 | int bufsize, continued_count; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 915 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 916 | /* |
| 917 | * standart macros set (predefined) goes first |
| 918 | */ |
| 919 | p = line_from_stdmac(); |
| 920 | if (p) |
| 921 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 922 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 923 | /* |
| 924 | * regular read from a file |
| 925 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 926 | bufsize = BUF_DELTA; |
| 927 | buffer = nasm_malloc(BUF_DELTA); |
| 928 | p = buffer; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 929 | continued_count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 930 | while (1) { |
| 931 | q = fgets(p, bufsize - (p - buffer), istk->fp); |
| 932 | if (!q) |
| 933 | break; |
| 934 | p += strlen(p); |
| 935 | if (p > buffer && p[-1] == '\n') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 936 | /* |
| 937 | * Convert backslash-CRLF line continuation sequences into |
| 938 | * nothing at all (for DOS and Windows) |
| 939 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 940 | if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { |
| 941 | p -= 3; |
| 942 | *p = 0; |
| 943 | continued_count++; |
| 944 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 945 | /* |
| 946 | * Also convert backslash-LF line continuation sequences into |
| 947 | * nothing at all (for Unix) |
| 948 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 949 | else if (((p - 1) > buffer) && (p[-2] == '\\')) { |
| 950 | p -= 2; |
| 951 | *p = 0; |
| 952 | continued_count++; |
| 953 | } else { |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | if (p - buffer > bufsize - 10) { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 958 | int32_t offset = p - buffer; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 959 | bufsize += BUF_DELTA; |
| 960 | buffer = nasm_realloc(buffer, bufsize); |
| 961 | p = buffer + offset; /* prevent stale-pointer problems */ |
| 962 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 963 | } |
| 964 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 965 | if (!q && p == buffer) { |
| 966 | nasm_free(buffer); |
| 967 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 970 | src_set_linnum(src_get_linnum() + istk->lineinc + |
| 971 | (continued_count * istk->lineinc)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 972 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 973 | /* |
| 974 | * Play safe: remove CRs as well as LFs, if any of either are |
| 975 | * present at the end of the line. |
| 976 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 977 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 978 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 979 | |
| 980 | /* |
| 981 | * Handle spurious ^Z, which may be inserted into source files |
| 982 | * by some file transfer utilities. |
| 983 | */ |
| 984 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 985 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 986 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 987 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 988 | return buffer; |
| 989 | } |
| 990 | |
| 991 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 992 | * 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] | 993 | * don't need to parse the value out of e.g. numeric tokens: we |
| 994 | * simply split one string into many. |
| 995 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 996 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 997 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 998 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 999 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1000 | Token *list = NULL; |
| 1001 | Token *t, **tail = &list; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1002 | bool verbose = true; |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 1003 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1004 | nasm_trace("Tokenize for '%s'", line); |
| 1005 | |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1006 | if ((defining != NULL) && (defining->ignoring == true)) { |
| 1007 | verbose = false; |
| 1008 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1009 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1010 | while (*line) { |
| 1011 | p = line; |
| 1012 | if (*p == '%') { |
| 1013 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1014 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 1015 | p++; |
| 1016 | type = TOK_PASTE; |
| 1017 | } else if (nasm_isdigit(*p) || |
| 1018 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1019 | do { |
| 1020 | p++; |
| 1021 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1022 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1023 | type = TOK_PREPROC_ID; |
| 1024 | } else if (*p == '{') { |
| 1025 | p++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1026 | while (*p && *p != '}') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1027 | p[-1] = *p; |
| 1028 | p++; |
| 1029 | } |
| 1030 | p[-1] = '\0'; |
| 1031 | if (*p) |
| 1032 | p++; |
| 1033 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1034 | } else if (*p == '[') { |
| 1035 | int lvl = 1; |
| 1036 | line += 2; /* Skip the leading %[ */ |
| 1037 | p++; |
| 1038 | while (lvl && (c = *p++)) { |
| 1039 | switch (c) { |
| 1040 | case ']': |
| 1041 | lvl--; |
| 1042 | break; |
| 1043 | case '%': |
| 1044 | if (*p == '[') |
| 1045 | lvl++; |
| 1046 | break; |
| 1047 | case '\'': |
| 1048 | case '\"': |
| 1049 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 1050 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1051 | break; |
| 1052 | default: |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | p--; |
| 1057 | if (*p) |
| 1058 | *p++ = '\0'; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1059 | if (lvl && verbose) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1060 | error(ERR_NONFATAL, "unterminated %[ construct"); |
| 1061 | type = TOK_INDIRECT; |
| 1062 | } else if (*p == '?') { |
| 1063 | type = TOK_PREPROC_Q; /* %? */ |
| 1064 | p++; |
| 1065 | if (*p == '?') { |
| 1066 | type = TOK_PREPROC_QQ; /* %?? */ |
| 1067 | p++; |
| 1068 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1069 | } else if (*p == '!') { |
| 1070 | type = TOK_PREPROC_ID; |
| 1071 | p++; |
| 1072 | if (isidchar(*p)) { |
| 1073 | do { |
| 1074 | p++; |
| 1075 | } while (isidchar(*p)); |
| 1076 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 1077 | p = nasm_skip_string(p); |
| 1078 | if (*p) |
| 1079 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1080 | else if(verbose) |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1081 | error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string"); |
| 1082 | } else { |
| 1083 | /* %! without string or identifier */ |
| 1084 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1085 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1086 | } else if (isidchar(*p) || |
| 1087 | ((*p == '!' || *p == '%' || *p == '$') && |
| 1088 | isidchar(p[1]))) { |
| 1089 | do { |
| 1090 | p++; |
| 1091 | } |
| 1092 | while (isidchar(*p)); |
| 1093 | type = TOK_PREPROC_ID; |
| 1094 | } else { |
| 1095 | type = TOK_OTHER; |
| 1096 | if (*p == '%') |
| 1097 | p++; |
| 1098 | } |
| 1099 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1100 | type = TOK_ID; |
| 1101 | p++; |
| 1102 | while (*p && isidchar(*p)) |
| 1103 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1104 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1105 | /* |
| 1106 | * A string token. |
| 1107 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1108 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1109 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1110 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1111 | if (*p) { |
| 1112 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1113 | } else if(verbose) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1114 | error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1115 | /* Handling unterminated strings by UNV */ |
| 1116 | /* type = -1; */ |
| 1117 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1118 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1119 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1120 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1121 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1122 | bool is_hex = false; |
| 1123 | bool is_float = false; |
| 1124 | bool has_e = false; |
| 1125 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1126 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1127 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1128 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1129 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1130 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1131 | if (*p == '$') { |
| 1132 | p++; |
| 1133 | is_hex = true; |
| 1134 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1135 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1136 | for (;;) { |
| 1137 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1138 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1139 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1140 | has_e = true; |
| 1141 | if (*p == '+' || *p == '-') { |
| 1142 | /* |
| 1143 | * e can only be followed by +/- if it is either a |
| 1144 | * prefixed hex number or a floating-point number |
| 1145 | */ |
| 1146 | p++; |
| 1147 | is_float = true; |
| 1148 | } |
| 1149 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1150 | is_hex = true; |
| 1151 | } else if (c == 'P' || c == 'p') { |
| 1152 | is_float = true; |
| 1153 | if (*p == '+' || *p == '-') |
| 1154 | p++; |
| 1155 | } else if (isnumchar(c) || c == '_') |
| 1156 | ; /* just advance */ |
| 1157 | else if (c == '.') { |
| 1158 | /* |
| 1159 | * we need to deal with consequences of the legacy |
| 1160 | * parser, like "1.nolist" being two tokens |
| 1161 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1162 | * a shot for now. In the future, we probably need |
| 1163 | * a flex-based scanner with proper pattern matching |
| 1164 | * to do it as well as it can be done. Nothing in |
| 1165 | * the world is going to help the person who wants |
| 1166 | * 0x123.p16 interpreted as two tokens, though. |
| 1167 | */ |
| 1168 | r = p; |
| 1169 | while (*r == '_') |
| 1170 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1171 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1172 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1173 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1174 | (*r == 'p' || *r == 'P')) { |
| 1175 | p = r; |
| 1176 | is_float = true; |
| 1177 | } else |
| 1178 | break; /* Terminate the token */ |
| 1179 | } else |
| 1180 | break; |
| 1181 | } |
| 1182 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1183 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1184 | if (p == line+1 && *line == '$') { |
| 1185 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1186 | } else { |
| 1187 | if (has_e && !is_hex) { |
| 1188 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1189 | is_float = true; |
| 1190 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1191 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1192 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1193 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1194 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1195 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1196 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1197 | /* |
| 1198 | * Whitespace just before end-of-line is discarded by |
| 1199 | * pretending it's a comment; whitespace just before a |
| 1200 | * comment gets lumped into the comment. |
| 1201 | */ |
| 1202 | if (!*p || *p == ';') { |
| 1203 | type = TOK_COMMENT; |
| 1204 | while (*p) |
| 1205 | p++; |
| 1206 | } |
| 1207 | } else if (*p == ';') { |
| 1208 | type = TOK_COMMENT; |
| 1209 | while (*p) |
| 1210 | p++; |
| 1211 | } else { |
| 1212 | /* |
| 1213 | * Anything else is an operator of some kind. We check |
| 1214 | * for all the double-character operators (>>, <<, //, |
| 1215 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1216 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1217 | */ |
| 1218 | type = TOK_OTHER; |
| 1219 | if ((p[0] == '>' && p[1] == '>') || |
| 1220 | (p[0] == '<' && p[1] == '<') || |
| 1221 | (p[0] == '/' && p[1] == '/') || |
| 1222 | (p[0] == '<' && p[1] == '=') || |
| 1223 | (p[0] == '>' && p[1] == '=') || |
| 1224 | (p[0] == '=' && p[1] == '=') || |
| 1225 | (p[0] == '!' && p[1] == '=') || |
| 1226 | (p[0] == '<' && p[1] == '>') || |
| 1227 | (p[0] == '&' && p[1] == '&') || |
| 1228 | (p[0] == '|' && p[1] == '|') || |
| 1229 | (p[0] == '^' && p[1] == '^')) { |
| 1230 | p++; |
| 1231 | } |
| 1232 | p++; |
| 1233 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1234 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1235 | /* Handling unterminated string by UNV */ |
| 1236 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1237 | { |
| 1238 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1239 | t->text[p-line] = *line; |
| 1240 | tail = &t->next; |
| 1241 | } |
| 1242 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1243 | if (type != TOK_COMMENT) { |
| 1244 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1245 | tail = &t->next; |
| 1246 | } |
| 1247 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1248 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1249 | |
| 1250 | nasm_dump_token(list); |
| 1251 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1252 | return list; |
| 1253 | } |
| 1254 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1255 | /* |
| 1256 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1257 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1258 | * deleted only all at once by the delete_Blocks function. |
| 1259 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1260 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1261 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1262 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1263 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1264 | /* first, get to the end of the linked list */ |
| 1265 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1266 | b = b->next; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1267 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1268 | /* now allocate the requested chunk */ |
| 1269 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1270 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1271 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1272 | b->next = nasm_zalloc(sizeof(Blocks)); |
| 1273 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1274 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * this function deletes all managed blocks of memory |
| 1279 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1280 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1281 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1282 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1283 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1284 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1285 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1286 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1287 | * free it. |
| 1288 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1289 | while (b) { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 1290 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1291 | a = b; |
| 1292 | b = b->next; |
| 1293 | if (a != &blocks) |
| 1294 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1295 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1296 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1297 | |
| 1298 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1299 | * 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] | 1300 | * 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] | 1301 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1302 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1303 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1304 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1305 | { |
| 1306 | Token *t; |
| 1307 | int i; |
| 1308 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1309 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1310 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1311 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1312 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1313 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1314 | } |
| 1315 | t = freeTokens; |
| 1316 | freeTokens = t->next; |
| 1317 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1318 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1319 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1320 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1321 | t->text = NULL; |
| 1322 | } else { |
| 1323 | if (txtlen == 0) |
| 1324 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1325 | t->text = nasm_malloc(txtlen+1); |
| 1326 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1327 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1328 | } |
| 1329 | return t; |
| 1330 | } |
| 1331 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1332 | static Token *copy_Token(Token * tline) |
| 1333 | { |
| 1334 | Token *t, *tt, *first = NULL, *prev = NULL; |
| 1335 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1336 | for (tt = tline; tt != NULL; tt = tt->next) { |
| 1337 | if (!freeTokens) { |
| 1338 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1339 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1340 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1341 | freeTokens[i].next = NULL; |
| 1342 | } |
| 1343 | t = freeTokens; |
| 1344 | freeTokens = t->next; |
| 1345 | t->next = NULL; |
| 1346 | t->text = tt->text ? nasm_strdup(tt->text) : NULL; |
| 1347 | t->a.mac = tt->a.mac; |
| 1348 | t->a.len = tt->a.len; |
| 1349 | t->type = tt->type; |
| 1350 | if (prev != NULL) { |
| 1351 | prev->next = t; |
| 1352 | } else { |
| 1353 | first = t; |
| 1354 | } |
| 1355 | prev = t; |
| 1356 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1357 | return first; |
| 1358 | } |
| 1359 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1360 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1361 | { |
| 1362 | Token *next = t->next; |
| 1363 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1364 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1365 | freeTokens = t; |
| 1366 | return next; |
| 1367 | } |
| 1368 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1369 | /* |
| 1370 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1371 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1372 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1373 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1374 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1375 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1376 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1377 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1378 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1379 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1380 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1381 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1382 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1383 | char *v; |
| 1384 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1385 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1386 | v = t->text + 2; |
| 1387 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1388 | size_t len = nasm_unquote(v, NULL); |
| 1389 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1390 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1391 | if (len != clen) { |
| 1392 | error(ERR_NONFATAL | ERR_PASS1, |
| 1393 | "NUL character in %! string"); |
| 1394 | v = NULL; |
| 1395 | } |
| 1396 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1397 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1398 | if (v) { |
| 1399 | char *p = getenv(v); |
| 1400 | if (!p) { |
| 1401 | error(ERR_NONFATAL | ERR_PASS1, |
| 1402 | "nonexistent environment variable `%s'", v); |
| 1403 | p = ""; |
| 1404 | } |
| 1405 | t->text = nasm_strdup(p); |
| 1406 | } |
| 1407 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1408 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1409 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1410 | /* Expand local macros here and not during preprocessing */ |
| 1411 | if (expand_locals && |
| 1412 | t->type == TOK_PREPROC_ID && t->text && |
| 1413 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1414 | const char *q; |
| 1415 | char *p; |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1416 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1417 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1418 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1419 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1420 | p = nasm_strcat(buffer, q); |
| 1421 | nasm_free(t->text); |
| 1422 | t->text = p; |
| 1423 | } |
| 1424 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1425 | |
| 1426 | /* Expand %? and %?? directives */ |
Keith Kanios | 3136d48 | 2010-11-13 09:34:34 -0600 | [diff] [blame] | 1427 | if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1428 | ((t->type == TOK_PREPROC_Q) || |
| 1429 | (t->type == TOK_PREPROC_QQ))) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1430 | ExpInv *ei; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1431 | for (ei = istk->expansion; ei != NULL; ei = ei->prev){ |
| 1432 | if (ei->type == EXP_MMACRO) { |
| 1433 | nasm_free(t->text); |
| 1434 | if (t->type == TOK_PREPROC_Q) { |
| 1435 | t->text = nasm_strdup(ei->name); |
| 1436 | } else { |
| 1437 | t->text = nasm_strdup(ei->def->name); |
| 1438 | } |
| 1439 | break; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1443 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1444 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1445 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1446 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1447 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1448 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1449 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1450 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1451 | |
| 1452 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1453 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1454 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1455 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1456 | q = t->text; |
| 1457 | while (*q) |
| 1458 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1459 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1460 | } |
| 1461 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1462 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1463 | return line; |
| 1464 | } |
| 1465 | |
| 1466 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1467 | * Initialize a new Line |
| 1468 | */ |
Cyrill Gorcunov | 29cb0bb | 2010-11-11 11:19:43 +0300 | [diff] [blame] | 1469 | static inline Line *new_Line(void) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1470 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1471 | return (Line *)nasm_zalloc(sizeof(Line)); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | /* |
| 1476 | * Initialize a new Expansion Definition |
| 1477 | */ |
| 1478 | static ExpDef *new_ExpDef(int exp_type) |
| 1479 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1480 | ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef)); |
| 1481 | ed->type = exp_type; |
| 1482 | ed->casesense = true; |
| 1483 | ed->state = COND_NEVER; |
| 1484 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1485 | return ed; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | /* |
| 1490 | * Initialize a new Expansion Instance |
| 1491 | */ |
| 1492 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed) |
| 1493 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1494 | ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv)); |
| 1495 | ei->type = exp_type; |
| 1496 | ei->def = ed; |
| 1497 | ei->unique = ++unique; |
| 1498 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1499 | if ((istk->mmac_depth < 1) && |
| 1500 | (istk->expansion == NULL) && |
| 1501 | (ed != NULL) && |
| 1502 | (ed->type != EXP_MMACRO) && |
| 1503 | (ed->type != EXP_REP) && |
| 1504 | (ed->type != EXP_WHILE)) { |
| 1505 | ei->linnum = src_get_linnum(); |
| 1506 | src_set_linnum(ei->linnum - ed->linecount - 1); |
| 1507 | } else { |
| 1508 | ei->linnum = -1; |
| 1509 | } |
| 1510 | if ((istk->expansion == NULL) || |
| 1511 | (ei->type == EXP_MMACRO)) { |
| 1512 | ei->relno = 0; |
| 1513 | } else { |
| 1514 | ei->relno = istk->expansion->lineno; |
| 1515 | if (ed != NULL) { |
| 1516 | ei->relno -= (ed->linecount + 1); |
| 1517 | } |
| 1518 | } |
| 1519 | return ei; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1523 | * A scanner, suitable for use by the expression evaluator, which |
| 1524 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1525 | * the first token in the line to be passed in as its private_data |
| 1526 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1527 | * |
| 1528 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1529 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1530 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1531 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1532 | Token **tlineptr = private_data; |
| 1533 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1534 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1535 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1536 | do { |
| 1537 | tline = *tlineptr; |
| 1538 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1539 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1540 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1541 | |
| 1542 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1543 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1544 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1545 | tokval->t_charptr = tline->text; |
| 1546 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1547 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1548 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1549 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1550 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1551 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1552 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1553 | p = tokval->t_charptr = tline->text; |
| 1554 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1555 | tokval->t_charptr++; |
| 1556 | return tokval->t_type = TOKEN_ID; |
| 1557 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1558 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1559 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1560 | if (r >= p+MAX_KEYWORD) |
| 1561 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1562 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1563 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1564 | *s = '\0'; |
| 1565 | /* right, so we have an identifier sitting in temp storage. now, |
| 1566 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1567 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1570 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1571 | bool rn_error; |
| 1572 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1573 | tokval->t_charptr = tline->text; |
| 1574 | if (rn_error) |
| 1575 | return tokval->t_type = TOKEN_ERRNUM; |
| 1576 | else |
| 1577 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1578 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1579 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1580 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1581 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1584 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1585 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1586 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1587 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1588 | tokval->t_charptr = tline->text; |
| 1589 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1590 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1591 | if (ep[0] != bq || ep[1] != '\0') |
| 1592 | return tokval->t_type = TOKEN_ERRSTR; |
| 1593 | else |
| 1594 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1597 | if (tline->type == TOK_OTHER) { |
| 1598 | if (!strcmp(tline->text, "<<")) |
| 1599 | return tokval->t_type = TOKEN_SHL; |
| 1600 | if (!strcmp(tline->text, ">>")) |
| 1601 | return tokval->t_type = TOKEN_SHR; |
| 1602 | if (!strcmp(tline->text, "//")) |
| 1603 | return tokval->t_type = TOKEN_SDIV; |
| 1604 | if (!strcmp(tline->text, "%%")) |
| 1605 | return tokval->t_type = TOKEN_SMOD; |
| 1606 | if (!strcmp(tline->text, "==")) |
| 1607 | return tokval->t_type = TOKEN_EQ; |
| 1608 | if (!strcmp(tline->text, "<>")) |
| 1609 | return tokval->t_type = TOKEN_NE; |
| 1610 | if (!strcmp(tline->text, "!=")) |
| 1611 | return tokval->t_type = TOKEN_NE; |
| 1612 | if (!strcmp(tline->text, "<=")) |
| 1613 | return tokval->t_type = TOKEN_LE; |
| 1614 | if (!strcmp(tline->text, ">=")) |
| 1615 | return tokval->t_type = TOKEN_GE; |
| 1616 | if (!strcmp(tline->text, "&&")) |
| 1617 | return tokval->t_type = TOKEN_DBL_AND; |
| 1618 | if (!strcmp(tline->text, "^^")) |
| 1619 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1620 | if (!strcmp(tline->text, "||")) |
| 1621 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * We have no other options: just return the first character of |
| 1626 | * the token text. |
| 1627 | */ |
| 1628 | return tokval->t_type = tline->text[0]; |
| 1629 | } |
| 1630 | |
| 1631 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1632 | * Compare a string to the name of an existing macro; this is a |
| 1633 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1634 | * depending on the value of the `casesense' parameter. |
| 1635 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1636 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1637 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1638 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1642 | * Compare a string to the name of an existing macro; this is a |
| 1643 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1644 | * depending on the value of the `casesense' parameter. |
| 1645 | */ |
| 1646 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1647 | { |
| 1648 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1649 | } |
| 1650 | |
| 1651 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1652 | * Return the Context structure associated with a %$ token. Return |
| 1653 | * NULL, having _already_ reported an error condition, if the |
| 1654 | * context stack isn't deep enough for the supplied number of $ |
| 1655 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1656 | * |
| 1657 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1658 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1659 | */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1660 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1661 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1662 | Context *ctx; |
| 1663 | int i; |
| 1664 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1665 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1666 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1667 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1668 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1669 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1670 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1671 | if (!cstk) { |
| 1672 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
| 1673 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1676 | name += 2; |
| 1677 | ctx = cstk; |
| 1678 | i = 0; |
| 1679 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1680 | name++; |
| 1681 | i++; |
| 1682 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1683 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1684 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1685 | if (!ctx) { |
| 1686 | error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1687 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1689 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1690 | |
| 1691 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1692 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1693 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1694 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1698 | * Check to see if a file is already in a string list |
| 1699 | */ |
| 1700 | static bool in_list(const StrList *list, const char *str) |
| 1701 | { |
| 1702 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1703 | if (!strcmp(list->str, str)) |
| 1704 | return true; |
| 1705 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1706 | } |
| 1707 | return false; |
| 1708 | } |
| 1709 | |
| 1710 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1711 | * Open an include file. This routine must always return a valid |
| 1712 | * file pointer if it returns - it's responsible for throwing an |
| 1713 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1714 | * the include path one by one until it finds the file or reaches |
| 1715 | * the end of the path. |
| 1716 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1717 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1718 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1719 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1720 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1721 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1722 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1723 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1724 | size_t prefix_len = 0; |
| 1725 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1726 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1727 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1728 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | 6f38fe6 | 2010-11-11 11:32:16 +0300 | [diff] [blame] | 1729 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1730 | memcpy(sl->str, prefix, prefix_len); |
| 1731 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1732 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1733 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1734 | **dtail = sl; |
| 1735 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1736 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1737 | nasm_free(sl); |
| 1738 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1739 | if (fp) |
| 1740 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1741 | if (!ip) { |
| 1742 | if (!missing_ok) |
| 1743 | break; |
| 1744 | prefix = NULL; |
| 1745 | } else { |
| 1746 | prefix = ip->path; |
| 1747 | ip = ip->next; |
| 1748 | } |
| 1749 | if (prefix) { |
| 1750 | prefix_len = strlen(prefix); |
| 1751 | } else { |
| 1752 | /* -MG given and file not found */ |
| 1753 | if (dhead && !in_list(*dhead, file)) { |
| 1754 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1755 | sl->next = NULL; |
| 1756 | strcpy(sl->str, file); |
| 1757 | **dtail = sl; |
| 1758 | *dtail = &sl->next; |
| 1759 | } |
| 1760 | return NULL; |
| 1761 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1762 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1763 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1764 | error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1765 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1769 | * 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] | 1770 | * 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] | 1771 | * return true if _any_ single-line macro of that name is defined. |
| 1772 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1773 | * `nparam' or no parameters is defined. |
| 1774 | * |
| 1775 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1776 | * defined, or nparam is -1, the address of the definition structure |
| 1777 | * 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] | 1778 | * is NULL, no action will be taken regarding its contents, and no |
| 1779 | * error will occur. |
| 1780 | * |
| 1781 | * Note that this is also called with nparam zero to resolve |
| 1782 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1783 | * |
| 1784 | * If you already know which context macro belongs to, you can pass |
| 1785 | * the context pointer as first parameter; if you won't but name begins |
| 1786 | * with %$ the context will be automatically computed. If all_contexts |
| 1787 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1788 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1789 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1790 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1791 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1792 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1793 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1794 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1795 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1796 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1797 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1798 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1799 | if (cstk) |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1800 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1801 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1802 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1803 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1804 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1805 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1806 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1807 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1808 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1809 | while (m) { |
| 1810 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1811 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1812 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1813 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1814 | *defn = m; |
| 1815 | else |
| 1816 | *defn = NULL; |
| 1817 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1818 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1819 | } |
| 1820 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1821 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1822 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1823 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | /* |
| 1827 | * Count and mark off the parameters in a multi-line macro call. |
| 1828 | * This is called both from within the multi-line macro expansion |
| 1829 | * code, and also to mark off the default parameters when provided |
| 1830 | * in a %macro definition line. |
| 1831 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1832 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1833 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1834 | int paramsize, brace; |
| 1835 | |
| 1836 | *nparam = paramsize = 0; |
| 1837 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1838 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1839 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1840 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1841 | paramsize += PARAM_DELTA; |
| 1842 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1843 | } |
| 1844 | skip_white_(t); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1845 | brace = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1846 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1847 | brace = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1848 | (*params)[(*nparam)++] = t; |
| 1849 | while (tok_isnt_(t, brace ? "}" : ",")) |
| 1850 | t = t->next; |
| 1851 | if (t) { /* got a comma/brace */ |
| 1852 | t = t->next; |
| 1853 | if (brace) { |
| 1854 | /* |
| 1855 | * Now we've found the closing brace, look further |
| 1856 | * for the comma. |
| 1857 | */ |
| 1858 | skip_white_(t); |
| 1859 | if (tok_isnt_(t, ",")) { |
| 1860 | error(ERR_NONFATAL, |
| 1861 | "braces do not enclose all of macro parameter"); |
| 1862 | while (tok_isnt_(t, ",")) |
| 1863 | t = t->next; |
| 1864 | } |
| 1865 | if (t) |
| 1866 | t = t->next; /* eat the comma */ |
| 1867 | } |
| 1868 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1873 | * Determine whether one of the various `if' conditions is true or |
| 1874 | * not. |
| 1875 | * |
| 1876 | * We must free the tline we get passed. |
| 1877 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1878 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1879 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1880 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1881 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1882 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1883 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1884 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1885 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1886 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1887 | |
| 1888 | origline = tline; |
| 1889 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1890 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1891 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1892 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1893 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1894 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1895 | if (!tline) |
| 1896 | break; |
| 1897 | if (tline->type != TOK_ID) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1898 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1899 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1900 | free_tlist(origline); |
| 1901 | return -1; |
| 1902 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1903 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1904 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1905 | tline = tline->next; |
| 1906 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1907 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1908 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1909 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1910 | j = false; /* have we matched yet? */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1911 | while (tline) { |
| 1912 | skip_white_(tline); |
| 1913 | if (!tline || (tline->type != TOK_ID && |
| 1914 | (tline->type != TOK_PREPROC_ID || |
| 1915 | tline->text[1] != '$'))) { |
| 1916 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1917 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1918 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1919 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1920 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
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 | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1925 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1926 | case PPC_IFENV: |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1927 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1928 | j = false; /* have we matched yet? */ |
| 1929 | while (tline) { |
| 1930 | skip_white_(tline); |
| 1931 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1932 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1933 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1934 | tline->text[1] != '!'))) { |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1935 | error(ERR_NONFATAL, |
| 1936 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1937 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1938 | goto fail; |
| 1939 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1940 | p = tline->text; |
| 1941 | if (tline->type == TOK_PREPROC_ID) |
| 1942 | p += 2; /* Skip leading %! */ |
| 1943 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1944 | nasm_unquote_cstr(p, ct); |
| 1945 | if (getenv(p)) |
| 1946 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1947 | tline = tline->next; |
| 1948 | } |
| 1949 | break; |
| 1950 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1951 | case PPC_IFIDN: |
| 1952 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1953 | tline = expand_smacro(tline); |
| 1954 | t = tt = tline; |
| 1955 | while (tok_isnt_(tt, ",")) |
| 1956 | tt = tt->next; |
| 1957 | if (!tt) { |
| 1958 | error(ERR_NONFATAL, |
| 1959 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1960 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1961 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1962 | } |
| 1963 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1964 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1965 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1966 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1967 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1968 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1969 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1970 | } |
| 1971 | if (t->type == TOK_WHITESPACE) { |
| 1972 | t = t->next; |
| 1973 | continue; |
| 1974 | } |
| 1975 | if (tt->type == TOK_WHITESPACE) { |
| 1976 | tt = tt->next; |
| 1977 | continue; |
| 1978 | } |
| 1979 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1980 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1981 | break; |
| 1982 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1983 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1984 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1985 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1986 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1987 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1988 | if (l1 != l2) { |
| 1989 | j = false; |
| 1990 | break; |
| 1991 | } |
| 1992 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1993 | j = false; |
| 1994 | break; |
| 1995 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1996 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1997 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1998 | break; |
| 1999 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 2000 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2001 | t = t->next; |
| 2002 | tt = tt->next; |
| 2003 | } |
| 2004 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2005 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2006 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2007 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2008 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2009 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2010 | bool found = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2011 | ExpDef searching, *ed; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2012 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2013 | skip_white_(tline); |
| 2014 | tline = expand_id(tline); |
| 2015 | if (!tok_type_(tline, TOK_ID)) { |
| 2016 | error(ERR_NONFATAL, |
| 2017 | "`%s' expects a macro name", pp_directives[ct]); |
| 2018 | goto fail; |
| 2019 | } |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2020 | memset(&searching, 0, sizeof(searching)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2021 | searching.name = nasm_strdup(tline->text); |
| 2022 | searching.casesense = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2023 | searching.nparam_max = INT_MAX; |
| 2024 | tline = expand_smacro(tline->next); |
| 2025 | skip_white_(tline); |
| 2026 | if (!tline) { |
| 2027 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2028 | error(ERR_NONFATAL, |
| 2029 | "`%s' expects a parameter count or nothing", |
| 2030 | pp_directives[ct]); |
| 2031 | } else { |
| 2032 | searching.nparam_min = searching.nparam_max = |
| 2033 | readnum(tline->text, &j); |
| 2034 | if (j) |
| 2035 | error(ERR_NONFATAL, |
| 2036 | "unable to parse parameter count `%s'", |
| 2037 | tline->text); |
| 2038 | } |
| 2039 | if (tline && tok_is_(tline->next, "-")) { |
| 2040 | tline = tline->next->next; |
| 2041 | if (tok_is_(tline, "*")) |
| 2042 | searching.nparam_max = INT_MAX; |
| 2043 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 2044 | error(ERR_NONFATAL, |
| 2045 | "`%s' expects a parameter count after `-'", |
| 2046 | pp_directives[ct]); |
| 2047 | else { |
| 2048 | searching.nparam_max = readnum(tline->text, &j); |
| 2049 | if (j) |
| 2050 | error(ERR_NONFATAL, |
| 2051 | "unable to parse parameter count `%s'", |
| 2052 | tline->text); |
| 2053 | if (searching.nparam_min > searching.nparam_max) |
| 2054 | error(ERR_NONFATAL, |
| 2055 | "minimum parameter count exceeds maximum"); |
| 2056 | } |
| 2057 | } |
| 2058 | if (tline && tok_is_(tline->next, "+")) { |
| 2059 | tline = tline->next; |
| 2060 | searching.plus = true; |
| 2061 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2062 | ed = (ExpDef *) hash_findix(&expdefs, searching.name); |
| 2063 | while (ed != NULL) { |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2064 | if (!strcmp(ed->name, searching.name) && |
| 2065 | (ed->nparam_min <= searching.nparam_max || searching.plus) && |
| 2066 | (searching.nparam_min <= ed->nparam_max || ed->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2067 | found = true; |
| 2068 | break; |
| 2069 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2070 | ed = ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2071 | } |
| 2072 | if (tline && tline->next) |
| 2073 | error(ERR_WARNING|ERR_PASS1, |
| 2074 | "trailing garbage after %%ifmacro ignored"); |
| 2075 | nasm_free(searching.name); |
| 2076 | j = found; |
| 2077 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2078 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2079 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2080 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2081 | needtype = TOK_ID; |
| 2082 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2083 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2084 | needtype = TOK_NUMBER; |
| 2085 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2086 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2087 | needtype = TOK_STRING; |
| 2088 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2089 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2090 | iftype: |
| 2091 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2092 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2093 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2094 | (needtype == TOK_NUMBER && |
| 2095 | tok_type_(t, TOK_OTHER) && |
| 2096 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2097 | !t->text[1])) |
| 2098 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2099 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2100 | j = tok_type_(t, needtype); |
| 2101 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2102 | |
| 2103 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2104 | t = tline = expand_smacro(tline); |
| 2105 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2106 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2107 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2108 | j = false; |
| 2109 | if (t) { |
| 2110 | t = t->next; /* Skip the actual token */ |
| 2111 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2112 | t = t->next; |
| 2113 | j = !t; /* Should be nothing left */ |
| 2114 | } |
| 2115 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2116 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2117 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2118 | t = tline = expand_smacro(tline); |
| 2119 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2120 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2121 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2122 | j = !t; /* Should be empty */ |
| 2123 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2124 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2125 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2126 | t = tline = expand_smacro(tline); |
| 2127 | tptr = &t; |
| 2128 | tokval.t_type = TOKEN_INVALID; |
| 2129 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 2130 | NULL, pass | CRITICAL, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2131 | if (!evalresult) |
| 2132 | return -1; |
| 2133 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2134 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2135 | "trailing garbage after expression ignored"); |
| 2136 | if (!is_simple(evalresult)) { |
| 2137 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2138 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2139 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2140 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2141 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2142 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2143 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2144 | default: |
| 2145 | error(ERR_FATAL, |
| 2146 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2147 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2148 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2149 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2150 | |
| 2151 | free_tlist(origline); |
| 2152 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2153 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2154 | fail: |
| 2155 | free_tlist(origline); |
| 2156 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2160 | * Common code for defining an smacro |
| 2161 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2162 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2163 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2164 | { |
| 2165 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2166 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2167 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2168 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2169 | if (!smac) { |
| 2170 | error(ERR_WARNING|ERR_PASS1, |
| 2171 | "single-line macro `%s' defined both with and" |
| 2172 | " without parameters", mname); |
| 2173 | /* |
| 2174 | * Some instances of the old code considered this a failure, |
| 2175 | * some others didn't. What is the right thing to do here? |
| 2176 | */ |
| 2177 | free_tlist(expansion); |
| 2178 | return false; /* Failure */ |
| 2179 | } else { |
| 2180 | /* |
| 2181 | * We're redefining, so we have to take over an |
| 2182 | * existing SMacro structure. This means freeing |
| 2183 | * what was already in it. |
| 2184 | */ |
| 2185 | nasm_free(smac->name); |
| 2186 | free_tlist(smac->expansion); |
| 2187 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2188 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2189 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2190 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2191 | smac = nasm_zalloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2192 | smac->next = *smhead; |
| 2193 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2194 | } |
| 2195 | smac->name = nasm_strdup(mname); |
| 2196 | smac->casesense = casesense; |
| 2197 | smac->nparam = nparam; |
| 2198 | smac->expansion = expansion; |
| 2199 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2200 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Undefine an smacro |
| 2205 | */ |
| 2206 | static void undef_smacro(Context *ctx, const char *mname) |
| 2207 | { |
| 2208 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2209 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2210 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2211 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2212 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2213 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2214 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2215 | /* |
| 2216 | * We now have a macro name... go hunt for it. |
| 2217 | */ |
| 2218 | sp = smhead; |
| 2219 | while ((s = *sp) != NULL) { |
| 2220 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2221 | *sp = s->next; |
| 2222 | nasm_free(s->name); |
| 2223 | free_tlist(s->expansion); |
| 2224 | nasm_free(s); |
| 2225 | } else { |
| 2226 | sp = &s->next; |
| 2227 | } |
| 2228 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2232 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2233 | * Parse a mmacro specification. |
| 2234 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2235 | 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] | 2236 | { |
| 2237 | bool err; |
| 2238 | |
| 2239 | tline = tline->next; |
| 2240 | skip_white_(tline); |
| 2241 | tline = expand_id(tline); |
| 2242 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2243 | error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
| 2244 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2245 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2246 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2247 | def->name = nasm_strdup(tline->text); |
| 2248 | def->plus = false; |
| 2249 | def->nolist = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2250 | // def->in_progress = 0; |
| 2251 | // def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2252 | def->nparam_min = 0; |
| 2253 | def->nparam_max = 0; |
| 2254 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2255 | tline = expand_smacro(tline->next); |
| 2256 | skip_white_(tline); |
| 2257 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2258 | error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2259 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2260 | def->nparam_min = def->nparam_max = |
| 2261 | readnum(tline->text, &err); |
| 2262 | if (err) |
| 2263 | error(ERR_NONFATAL, |
| 2264 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2265 | } |
| 2266 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2267 | tline = tline->next->next; |
| 2268 | if (tok_is_(tline, "*")) { |
| 2269 | def->nparam_max = INT_MAX; |
| 2270 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2271 | error(ERR_NONFATAL, |
| 2272 | "`%s' expects a parameter count after `-'", directive); |
| 2273 | } else { |
| 2274 | def->nparam_max = readnum(tline->text, &err); |
| 2275 | if (err) { |
| 2276 | error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
| 2277 | tline->text); |
| 2278 | } |
| 2279 | if (def->nparam_min > def->nparam_max) { |
| 2280 | error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
| 2281 | } |
| 2282 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2283 | } |
| 2284 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2285 | tline = tline->next; |
| 2286 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2287 | } |
| 2288 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2289 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2290 | tline = tline->next; |
| 2291 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2292 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2293 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2294 | /* |
| 2295 | * Handle default parameters. |
| 2296 | */ |
| 2297 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2298 | def->dlist = tline->next; |
| 2299 | tline->next = NULL; |
| 2300 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2301 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2302 | def->dlist = NULL; |
| 2303 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2304 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2305 | def->line = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2306 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2307 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2308 | !def->plus) |
| 2309 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
| 2310 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2311 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2312 | return true; |
| 2313 | } |
| 2314 | |
| 2315 | |
| 2316 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2317 | * Decode a size directive |
| 2318 | */ |
| 2319 | static int parse_size(const char *str) { |
| 2320 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2321 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2322 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2323 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2324 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2325 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2326 | } |
| 2327 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2328 | /** |
| 2329 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2330 | * Find out if a line contains a preprocessor directive, and deal |
| 2331 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2332 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2333 | * If a directive _is_ found, it is the responsibility of this routine |
| 2334 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2335 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2336 | * @param tline a pointer to the current tokeninzed line linked list |
| 2337 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2338 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2339 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2340 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2341 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2342 | enum preproc_token i; |
| 2343 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2344 | bool err; |
| 2345 | int nparam; |
| 2346 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2347 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2348 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2349 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2350 | char *p, *pp; |
| 2351 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2352 | Include *inc; |
| 2353 | Context *ctx; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2354 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2355 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2356 | struct tokenval tokval; |
| 2357 | expr *evalresult; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2358 | ExpDef *ed, *eed, **edhead; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2359 | ExpInv *ei, *eei; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2360 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2361 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2362 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2363 | |
| 2364 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2365 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2366 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2367 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2368 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2369 | || tline->text[1] == '!')) |
| 2370 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2371 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2372 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2373 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2374 | switch (i) { |
| 2375 | case PP_INVALID: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2376 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2377 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2378 | tline->text); |
| 2379 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2380 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2381 | case PP_STACKSIZE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2382 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2383 | /* Directive to tell NASM what the default stack size is. The |
| 2384 | * default is for a 16-bit stack, and this can be overriden with |
| 2385 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2386 | */ |
| 2387 | tline = tline->next; |
| 2388 | if (tline && tline->type == TOK_WHITESPACE) |
| 2389 | tline = tline->next; |
| 2390 | if (!tline || tline->type != TOK_ID) { |
| 2391 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 2392 | free_tlist(origline); |
| 2393 | return DIRECTIVE_FOUND; |
| 2394 | } |
| 2395 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2396 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2397 | StackSize = 4; |
| 2398 | StackPointer = "ebp"; |
| 2399 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2400 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2401 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2402 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2403 | StackSize = 8; |
| 2404 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2405 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2406 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2407 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2408 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2409 | * far function call. |
| 2410 | */ |
| 2411 | StackSize = 2; |
| 2412 | StackPointer = "bp"; |
| 2413 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2414 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2415 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2416 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2417 | * far function call. We don't support near functions. |
| 2418 | */ |
| 2419 | StackSize = 2; |
| 2420 | StackPointer = "bp"; |
| 2421 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2422 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2423 | } else { |
| 2424 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 2425 | free_tlist(origline); |
| 2426 | return DIRECTIVE_FOUND; |
| 2427 | } |
| 2428 | free_tlist(origline); |
| 2429 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2430 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2431 | case PP_ARG: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2432 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2433 | /* TASM like ARG directive to define arguments to functions, in |
| 2434 | * the following form: |
| 2435 | * |
| 2436 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2437 | */ |
| 2438 | offset = ArgOffset; |
| 2439 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2440 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2441 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2442 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2443 | /* Find the argument name */ |
| 2444 | tline = tline->next; |
| 2445 | if (tline && tline->type == TOK_WHITESPACE) |
| 2446 | tline = tline->next; |
| 2447 | if (!tline || tline->type != TOK_ID) { |
| 2448 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 2449 | free_tlist(origline); |
| 2450 | return DIRECTIVE_FOUND; |
| 2451 | } |
| 2452 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2453 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2454 | /* Find the argument size type */ |
| 2455 | tline = tline->next; |
| 2456 | if (!tline || tline->type != TOK_OTHER |
| 2457 | || tline->text[0] != ':') { |
| 2458 | error(ERR_NONFATAL, |
| 2459 | "Syntax error processing `%%arg' directive"); |
| 2460 | free_tlist(origline); |
| 2461 | return DIRECTIVE_FOUND; |
| 2462 | } |
| 2463 | tline = tline->next; |
| 2464 | if (!tline || tline->type != TOK_ID) { |
| 2465 | error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
| 2466 | free_tlist(origline); |
| 2467 | return DIRECTIVE_FOUND; |
| 2468 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2469 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2470 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2471 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2472 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2473 | size = parse_size(tt->text); |
| 2474 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2475 | error(ERR_NONFATAL, |
| 2476 | "Invalid size type for `%%arg' missing directive"); |
| 2477 | free_tlist(tt); |
| 2478 | free_tlist(origline); |
| 2479 | return DIRECTIVE_FOUND; |
| 2480 | } |
| 2481 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2482 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2483 | /* Round up to even stack slots */ |
| 2484 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2485 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2486 | /* Now define the macro for the argument */ |
| 2487 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2488 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2489 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2490 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2491 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2492 | /* Move to the next argument in the list */ |
| 2493 | tline = tline->next; |
| 2494 | if (tline && tline->type == TOK_WHITESPACE) |
| 2495 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2496 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2497 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2498 | free_tlist(origline); |
| 2499 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2500 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2501 | case PP_LOCAL: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2502 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2503 | /* TASM like LOCAL directive to define local variables for a |
| 2504 | * function, in the following form: |
| 2505 | * |
| 2506 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2507 | * |
| 2508 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2509 | * required by TASM to define the local parameter size (and used |
| 2510 | * by the TASM macro package). |
| 2511 | */ |
| 2512 | offset = LocalOffset; |
| 2513 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2514 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2515 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2516 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2517 | /* Find the argument name */ |
| 2518 | tline = tline->next; |
| 2519 | if (tline && tline->type == TOK_WHITESPACE) |
| 2520 | tline = tline->next; |
| 2521 | if (!tline || tline->type != TOK_ID) { |
| 2522 | error(ERR_NONFATAL, |
| 2523 | "`%%local' missing argument parameter"); |
| 2524 | free_tlist(origline); |
| 2525 | return DIRECTIVE_FOUND; |
| 2526 | } |
| 2527 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2528 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2529 | /* Find the argument size type */ |
| 2530 | tline = tline->next; |
| 2531 | if (!tline || tline->type != TOK_OTHER |
| 2532 | || tline->text[0] != ':') { |
| 2533 | error(ERR_NONFATAL, |
| 2534 | "Syntax error processing `%%local' directive"); |
| 2535 | free_tlist(origline); |
| 2536 | return DIRECTIVE_FOUND; |
| 2537 | } |
| 2538 | tline = tline->next; |
| 2539 | if (!tline || tline->type != TOK_ID) { |
| 2540 | error(ERR_NONFATAL, |
| 2541 | "`%%local' missing size type parameter"); |
| 2542 | free_tlist(origline); |
| 2543 | return DIRECTIVE_FOUND; |
| 2544 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2545 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2546 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2547 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2548 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2549 | size = parse_size(tt->text); |
| 2550 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2551 | error(ERR_NONFATAL, |
| 2552 | "Invalid size type for `%%local' missing directive"); |
| 2553 | free_tlist(tt); |
| 2554 | free_tlist(origline); |
| 2555 | return DIRECTIVE_FOUND; |
| 2556 | } |
| 2557 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2558 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2559 | /* Round up to even stack slots */ |
| 2560 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2561 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2562 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2563 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2564 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2565 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2566 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2567 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2568 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2569 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2570 | snprintf(directive, sizeof(directive), |
| 2571 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2572 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2573 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2574 | /* Move to the next argument in the list */ |
| 2575 | tline = tline->next; |
| 2576 | if (tline && tline->type == TOK_WHITESPACE) |
| 2577 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2578 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2579 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2580 | free_tlist(origline); |
| 2581 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2582 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2583 | case PP_CLEAR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2584 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2585 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2586 | error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2587 | "trailing garbage after `%%clear' ignored"); |
| 2588 | free_macros(); |
| 2589 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2590 | free_tlist(origline); |
| 2591 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2592 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2593 | case PP_DEPEND: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2594 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2595 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2596 | skip_white_(t); |
| 2597 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2598 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2599 | error(ERR_NONFATAL, "`%%depend' expects a file name"); |
| 2600 | free_tlist(origline); |
| 2601 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2602 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2603 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2604 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2605 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2606 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2607 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | nasm_unquote_cstr(p, i); |
| 2609 | if (dephead && !in_list(*dephead, p)) { |
| 2610 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2611 | sl->next = NULL; |
| 2612 | strcpy(sl->str, p); |
| 2613 | *deptail = sl; |
| 2614 | deptail = &sl->next; |
| 2615 | } |
| 2616 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2617 | return DIRECTIVE_FOUND; |
| 2618 | |
| 2619 | case PP_INCLUDE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2620 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2621 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2622 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2623 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2624 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2625 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2626 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2627 | free_tlist(origline); |
| 2628 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2629 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2630 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2631 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2632 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2633 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2634 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2635 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 2636 | inc = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2637 | inc->next = istk; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2638 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2639 | if (!inc->fp) { |
| 2640 | /* -MG given but file not found */ |
| 2641 | nasm_free(inc); |
| 2642 | } else { |
| 2643 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2644 | inc->lineno = src_set_linnum(0); |
| 2645 | inc->lineinc = 1; |
| 2646 | inc->expansion = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2647 | istk = inc; |
| 2648 | list->uplevel(LIST_INCLUDE); |
| 2649 | } |
| 2650 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2651 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2652 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2653 | case PP_USE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2654 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2655 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2656 | static macros_t *use_pkg; |
| 2657 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2658 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2659 | tline = tline->next; |
| 2660 | skip_white_(tline); |
| 2661 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2662 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2663 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2664 | tline->type != TOK_INTERNAL_STRING && |
| 2665 | tline->type != TOK_ID)) { |
H. Peter Anvin | 926fc40 | 2008-06-19 16:26:12 -0700 | [diff] [blame] | 2666 | error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2667 | free_tlist(origline); |
| 2668 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2669 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2670 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2671 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2672 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2673 | if (tline->type == TOK_STRING) |
| 2674 | nasm_unquote_cstr(tline->text, i); |
| 2675 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2676 | if (!use_pkg) |
| 2677 | error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
| 2678 | else |
| 2679 | 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] | 2680 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2681 | /* Not already included, go ahead and include it */ |
| 2682 | stdmacpos = use_pkg; |
| 2683 | } |
| 2684 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2685 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2686 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2687 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2688 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2689 | case PP_POP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2690 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2691 | tline = tline->next; |
| 2692 | skip_white_(tline); |
| 2693 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2694 | if (tline) { |
| 2695 | if (!tok_type_(tline, TOK_ID)) { |
| 2696 | error(ERR_NONFATAL, "`%s' expects a context identifier", |
| 2697 | pp_directives[i]); |
| 2698 | free_tlist(origline); |
| 2699 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2700 | } |
| 2701 | if (tline->next) |
| 2702 | error(ERR_WARNING|ERR_PASS1, |
| 2703 | "trailing garbage after `%s' ignored", |
| 2704 | pp_directives[i]); |
| 2705 | p = nasm_strdup(tline->text); |
| 2706 | } else { |
| 2707 | p = NULL; /* Anonymous */ |
| 2708 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2709 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2710 | if (i == PP_PUSH) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2711 | ctx = nasm_zalloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2712 | ctx->next = cstk; |
| 2713 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2714 | ctx->name = p; |
| 2715 | ctx->number = unique++; |
| 2716 | cstk = ctx; |
| 2717 | } else { |
| 2718 | /* %pop or %repl */ |
| 2719 | if (!cstk) { |
| 2720 | error(ERR_NONFATAL, "`%s': context stack is empty", |
| 2721 | pp_directives[i]); |
| 2722 | } else if (i == PP_POP) { |
| 2723 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
| 2724 | error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
| 2725 | "expected %s", |
| 2726 | cstk->name ? cstk->name : "anonymous", p); |
| 2727 | else |
| 2728 | ctx_pop(); |
| 2729 | } else { |
| 2730 | /* i == PP_REPL */ |
| 2731 | nasm_free(cstk->name); |
| 2732 | cstk->name = p; |
| 2733 | p = NULL; |
| 2734 | } |
| 2735 | nasm_free(p); |
| 2736 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2737 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2738 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2739 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2740 | severity = ERR_FATAL; |
| 2741 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2742 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2743 | severity = ERR_NONFATAL; |
| 2744 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2745 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2746 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2747 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2748 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2749 | issue_error: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2750 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2751 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2752 | /* Only error out if this is the final pass */ |
| 2753 | if (pass != 2 && i != PP_FATAL) |
| 2754 | return DIRECTIVE_FOUND; |
| 2755 | |
| 2756 | tline->next = expand_smacro(tline->next); |
| 2757 | tline = tline->next; |
| 2758 | skip_white_(tline); |
| 2759 | t = tline ? tline->next : NULL; |
| 2760 | skip_white_(t); |
| 2761 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2762 | /* The line contains only a quoted string */ |
| 2763 | p = tline->text; |
| 2764 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
| 2765 | error(severity, "%s", p); |
| 2766 | } else { |
| 2767 | /* Not a quoted string, or more than a quoted string */ |
| 2768 | p = detoken(tline, false); |
| 2769 | error(severity, "%s", p); |
| 2770 | nasm_free(p); |
| 2771 | } |
| 2772 | free_tlist(origline); |
| 2773 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2774 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2775 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2776 | CASE_PP_IF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2777 | if (defining != NULL) { |
| 2778 | if (defining->type == EXP_IF) { |
| 2779 | defining->def_depth ++; |
| 2780 | } |
| 2781 | return NO_DIRECTIVE_FOUND; |
| 2782 | } |
| 2783 | if ((istk->expansion != NULL) && |
| 2784 | (istk->expansion->emitting == false)) { |
| 2785 | j = COND_NEVER; |
| 2786 | } else { |
| 2787 | j = if_condition(tline->next, i); |
| 2788 | tline->next = NULL; /* it got freed */ |
| 2789 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 2790 | } |
| 2791 | ed = new_ExpDef(EXP_IF); |
| 2792 | ed->state = j; |
| 2793 | ed->nolist = NULL; |
| 2794 | ed->def_depth = 0; |
| 2795 | ed->cur_depth = 0; |
| 2796 | ed->max_depth = 0; |
| 2797 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 2798 | ed->prev = defining; |
| 2799 | defining = ed; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2800 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2801 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2802 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2803 | CASE_PP_ELIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2804 | if (defining != NULL) { |
| 2805 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2806 | return NO_DIRECTIVE_FOUND; |
| 2807 | } |
| 2808 | } |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 2809 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2810 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2811 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2812 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2813 | case COND_IF_TRUE: |
| 2814 | defining->state = COND_DONE; |
| 2815 | defining->ignoring = true; |
| 2816 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2817 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2818 | case COND_DONE: |
| 2819 | case COND_NEVER: |
| 2820 | defining->ignoring = true; |
| 2821 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2822 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2823 | case COND_ELSE_TRUE: |
| 2824 | case COND_ELSE_FALSE: |
| 2825 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2826 | "`%%elif' after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2827 | defining->state = COND_NEVER; |
| 2828 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2829 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2830 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2831 | case COND_IF_FALSE: |
| 2832 | /* |
| 2833 | * IMPORTANT: In the case of %if, we will already have |
| 2834 | * called expand_mmac_params(); however, if we're |
| 2835 | * processing an %elif we must have been in a |
| 2836 | * non-emitting mode, which would have inhibited |
| 2837 | * the normal invocation of expand_mmac_params(). |
| 2838 | * Therefore, we have to do it explicitly here. |
| 2839 | */ |
| 2840 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2841 | tline->next = NULL; /* it got freed */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2842 | defining->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2843 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2844 | defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2845 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2846 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2847 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2848 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2849 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2850 | case PP_ELSE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2851 | if (defining != NULL) { |
| 2852 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2853 | return NO_DIRECTIVE_FOUND; |
| 2854 | } |
| 2855 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2856 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2857 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2858 | "trailing garbage after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2859 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2860 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2861 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2862 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2863 | case COND_IF_TRUE: |
| 2864 | case COND_DONE: |
| 2865 | defining->state = COND_ELSE_FALSE; |
| 2866 | defining->ignoring = true; |
| 2867 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2868 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2869 | case COND_NEVER: |
| 2870 | defining->ignoring = true; |
| 2871 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2872 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2873 | case COND_IF_FALSE: |
| 2874 | defining->state = COND_ELSE_TRUE; |
| 2875 | defining->ignoring = false; |
| 2876 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2877 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2878 | case COND_ELSE_TRUE: |
| 2879 | case COND_ELSE_FALSE: |
| 2880 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2881 | "`%%else' after `%%else' ignored."); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2882 | defining->state = COND_NEVER; |
| 2883 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2884 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2885 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2886 | free_tlist(origline); |
| 2887 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2888 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2889 | case PP_ENDIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2890 | if (defining != NULL) { |
| 2891 | if (defining->type == EXP_IF) { |
| 2892 | if (defining->def_depth > 0) { |
| 2893 | defining->def_depth --; |
| 2894 | return NO_DIRECTIVE_FOUND; |
| 2895 | } |
| 2896 | } else { |
| 2897 | return NO_DIRECTIVE_FOUND; |
| 2898 | } |
| 2899 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2900 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2901 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2902 | "trailing garbage after `%%endif' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2903 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2904 | error(ERR_NONFATAL, "`%%endif': no matching `%%if'"); |
| 2905 | return DIRECTIVE_FOUND; |
| 2906 | } |
| 2907 | ed = defining; |
| 2908 | defining = ed->prev; |
| 2909 | ed->prev = expansions; |
| 2910 | expansions = ed; |
| 2911 | ei = new_ExpInv(EXP_IF, ed); |
| 2912 | ei->current = ed->line; |
| 2913 | ei->emitting = true; |
| 2914 | ei->prev = istk->expansion; |
| 2915 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2916 | free_tlist(origline); |
| 2917 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2918 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2919 | case PP_RMACRO: |
| 2920 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2921 | case PP_MACRO: |
| 2922 | case PP_IMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2923 | if (defining != NULL) { |
| 2924 | if (defining->type == EXP_MMACRO) { |
| 2925 | defining->def_depth ++; |
| 2926 | } |
| 2927 | return NO_DIRECTIVE_FOUND; |
| 2928 | } |
| 2929 | ed = new_ExpDef(EXP_MMACRO); |
| 2930 | ed->max_depth = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2931 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2932 | ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2933 | if (!parse_mmacro_spec(tline, ed, pp_directives[i])) { |
| 2934 | nasm_free(ed); |
| 2935 | ed = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2936 | return DIRECTIVE_FOUND; |
| 2937 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2938 | ed->def_depth = 0; |
| 2939 | ed->cur_depth = 0; |
| 2940 | ed->max_depth = (ed->max_depth + 1); |
| 2941 | ed->ignoring = false; |
| 2942 | ed->prev = defining; |
| 2943 | defining = ed; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2944 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2945 | eed = (ExpDef *) hash_findix(&expdefs, ed->name); |
| 2946 | while (eed) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2947 | if (!strcmp(eed->name, ed->name) && |
| 2948 | (eed->nparam_min <= ed->nparam_max || ed->plus) && |
| 2949 | (ed->nparam_min <= eed->nparam_max || eed->plus)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2950 | error(ERR_WARNING|ERR_PASS1, |
| 2951 | "redefining multi-line macro `%s'", ed->name); |
| 2952 | return DIRECTIVE_FOUND; |
| 2953 | } |
| 2954 | eed = eed->next; |
| 2955 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2956 | free_tlist(origline); |
| 2957 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2958 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2959 | case PP_ENDM: |
| 2960 | case PP_ENDMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2961 | if (defining != NULL) { |
| 2962 | if (defining->type == EXP_MMACRO) { |
| 2963 | if (defining->def_depth > 0) { |
| 2964 | defining->def_depth --; |
| 2965 | return NO_DIRECTIVE_FOUND; |
| 2966 | } |
| 2967 | } else { |
| 2968 | return NO_DIRECTIVE_FOUND; |
| 2969 | } |
| 2970 | } |
| 2971 | if (!(defining) || (defining->type != EXP_MMACRO)) { |
| 2972 | error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
| 2973 | return DIRECTIVE_FOUND; |
| 2974 | } |
| 2975 | edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name); |
| 2976 | defining->next = *edhead; |
| 2977 | *edhead = defining; |
| 2978 | ed = defining; |
| 2979 | defining = ed->prev; |
| 2980 | ed->prev = expansions; |
| 2981 | expansions = ed; |
| 2982 | ed = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2983 | free_tlist(origline); |
| 2984 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2985 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2986 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2987 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 2988 | /* |
| 2989 | * We must search along istk->expansion until we hit a |
| 2990 | * macro invocation. Then we disable the emitting state(s) |
| 2991 | * between exitmacro and endmacro. |
| 2992 | */ |
| 2993 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 2994 | if(ei->type == EXP_MMACRO) { |
| 2995 | break; |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | if (ei != NULL) { |
| 3000 | /* |
| 3001 | * Set all invocations leading back to the macro |
| 3002 | * invocation to a non-emitting state. |
| 3003 | */ |
| 3004 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3005 | eei->emitting = false; |
| 3006 | } |
| 3007 | eei->emitting = false; |
| 3008 | } else { |
| 3009 | error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
| 3010 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 3011 | free_tlist(origline); |
| 3012 | return DIRECTIVE_FOUND; |
| 3013 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3014 | case PP_UNMACRO: |
| 3015 | case PP_UNIMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3016 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3017 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3018 | ExpDef **ed_p; |
| 3019 | ExpDef spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3020 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3021 | spec.casesense = (i == PP_UNMACRO); |
| 3022 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 3023 | return DIRECTIVE_FOUND; |
| 3024 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3025 | ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL); |
| 3026 | while (ed_p && *ed_p) { |
| 3027 | ed = *ed_p; |
| 3028 | if (ed->casesense == spec.casesense && |
| 3029 | !mstrcmp(ed->name, spec.name, spec.casesense) && |
| 3030 | ed->nparam_min == spec.nparam_min && |
| 3031 | ed->nparam_max == spec.nparam_max && |
| 3032 | ed->plus == spec.plus) { |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3033 | if (ed->cur_depth > 0) { |
Keith Kanios | 21d885b | 2010-12-18 12:22:21 -0600 | [diff] [blame] | 3034 | error(ERR_NONFATAL, "`%s' ignored on active macro", |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3035 | pp_directives[i]); |
| 3036 | break; |
| 3037 | } else { |
| 3038 | *ed_p = ed->next; |
| 3039 | free_expdef(ed); |
| 3040 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3041 | } else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3042 | ed_p = &ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | free_tlist(origline); |
| 3046 | free_tlist(spec.dlist); |
| 3047 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3050 | case PP_ROTATE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3051 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3052 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3053 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3054 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3055 | free_tlist(origline); |
| 3056 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 3057 | return DIRECTIVE_FOUND; |
| 3058 | } |
| 3059 | t = expand_smacro(tline->next); |
| 3060 | tline->next = NULL; |
| 3061 | free_tlist(origline); |
| 3062 | tline = t; |
| 3063 | tptr = &t; |
| 3064 | tokval.t_type = TOKEN_INVALID; |
| 3065 | evalresult = |
| 3066 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3067 | free_tlist(tline); |
| 3068 | if (!evalresult) |
| 3069 | return DIRECTIVE_FOUND; |
| 3070 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3071 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3072 | "trailing garbage after expression ignored"); |
| 3073 | if (!is_simple(evalresult)) { |
| 3074 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
| 3075 | return DIRECTIVE_FOUND; |
| 3076 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3077 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3078 | if (ei->type == EXP_MMACRO) { |
| 3079 | break; |
| 3080 | } |
| 3081 | } |
| 3082 | if (ei == NULL) { |
| 3083 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
| 3084 | } else if (ei->nparam == 0) { |
| 3085 | error(ERR_NONFATAL, |
| 3086 | "`%%rotate' invoked within macro without parameters"); |
| 3087 | } else { |
| 3088 | int rotate = ei->rotate + reloc_value(evalresult); |
| 3089 | |
| 3090 | rotate %= (int)ei->nparam; |
| 3091 | if (rotate < 0) |
| 3092 | rotate += ei->nparam; |
| 3093 | ei->rotate = rotate; |
| 3094 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3095 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3096 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3097 | case PP_REP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3098 | if (defining != NULL) { |
| 3099 | if (defining->type == EXP_REP) { |
| 3100 | defining->def_depth ++; |
| 3101 | } |
| 3102 | return NO_DIRECTIVE_FOUND; |
| 3103 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3104 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3105 | do { |
| 3106 | tline = tline->next; |
| 3107 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3108 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3109 | if (tok_type_(tline, TOK_ID) && |
| 3110 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3111 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3112 | do { |
| 3113 | tline = tline->next; |
| 3114 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3115 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3116 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3117 | if (tline) { |
| 3118 | t = expand_smacro(tline); |
| 3119 | tptr = &t; |
| 3120 | tokval.t_type = TOKEN_INVALID; |
| 3121 | evalresult = |
| 3122 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3123 | if (!evalresult) { |
| 3124 | free_tlist(origline); |
| 3125 | return DIRECTIVE_FOUND; |
| 3126 | } |
| 3127 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3128 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3129 | "trailing garbage after expression ignored"); |
| 3130 | if (!is_simple(evalresult)) { |
| 3131 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 3132 | return DIRECTIVE_FOUND; |
| 3133 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3134 | count = reloc_value(evalresult); |
| 3135 | if (count >= REP_LIMIT) { |
Cyrill Gorcunov | 71f4f84 | 2010-08-09 20:17:17 +0400 | [diff] [blame] | 3136 | error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3137 | count = 0; |
| 3138 | } else |
| 3139 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3140 | } else { |
| 3141 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3142 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3143 | } |
| 3144 | free_tlist(origline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3145 | ed = new_ExpDef(EXP_REP); |
| 3146 | ed->nolist = nolist; |
| 3147 | ed->def_depth = 0; |
| 3148 | ed->cur_depth = 1; |
| 3149 | ed->max_depth = (count - 1); |
| 3150 | ed->ignoring = false; |
| 3151 | ed->prev = defining; |
| 3152 | defining = ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3153 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3154 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3155 | case PP_ENDREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3156 | if (defining != NULL) { |
| 3157 | if (defining->type == EXP_REP) { |
| 3158 | if (defining->def_depth > 0) { |
| 3159 | defining->def_depth --; |
| 3160 | return NO_DIRECTIVE_FOUND; |
| 3161 | } |
| 3162 | } else { |
| 3163 | return NO_DIRECTIVE_FOUND; |
| 3164 | } |
| 3165 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3166 | if ((defining == NULL) || (defining->type != EXP_REP)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3167 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
| 3168 | return DIRECTIVE_FOUND; |
| 3169 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3170 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3171 | /* |
| 3172 | * Now we have a "macro" defined - although it has no name |
| 3173 | * and we won't be entering it in the hash tables - we must |
| 3174 | * push a macro-end marker for it on to istk->expansion. |
| 3175 | * After that, it will take care of propagating itself (a |
| 3176 | * macro-end marker line for a macro which is really a %rep |
| 3177 | * block will cause the macro to be re-expanded, complete |
| 3178 | * with another macro-end marker to ensure the process |
| 3179 | * continues) until the whole expansion is forcibly removed |
| 3180 | * from istk->expansion by a %exitrep. |
| 3181 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3182 | ed = defining; |
| 3183 | defining = ed->prev; |
| 3184 | ed->prev = expansions; |
| 3185 | expansions = ed; |
| 3186 | ei = new_ExpInv(EXP_REP, ed); |
| 3187 | ei->current = ed->line; |
| 3188 | ei->emitting = ((ed->max_depth > 0) ? true : false); |
| 3189 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 3190 | ei->prev = istk->expansion; |
| 3191 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3192 | free_tlist(origline); |
| 3193 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3194 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3195 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3196 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3197 | /* |
| 3198 | * We must search along istk->expansion until we hit a |
| 3199 | * rep invocation. Then we disable the emitting state(s) |
| 3200 | * between exitrep and endrep. |
| 3201 | */ |
| 3202 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3203 | if (ei->type == EXP_REP) { |
| 3204 | break; |
| 3205 | } |
| 3206 | } |
| 3207 | |
| 3208 | if (ei != NULL) { |
| 3209 | /* |
| 3210 | * Set all invocations leading back to the rep |
| 3211 | * invocation to a non-emitting state. |
| 3212 | */ |
| 3213 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3214 | eei->emitting = false; |
| 3215 | } |
| 3216 | eei->emitting = false; |
| 3217 | eei->current = NULL; |
| 3218 | eei->def->cur_depth = eei->def->max_depth; |
| 3219 | } else { |
| 3220 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 3221 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3222 | free_tlist(origline); |
| 3223 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3224 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3225 | case PP_XDEFINE: |
| 3226 | case PP_IXDEFINE: |
| 3227 | case PP_DEFINE: |
| 3228 | case PP_IDEFINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3229 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3230 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3231 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3232 | tline = tline->next; |
| 3233 | skip_white_(tline); |
| 3234 | tline = expand_id(tline); |
| 3235 | if (!tline || (tline->type != TOK_ID && |
| 3236 | (tline->type != TOK_PREPROC_ID || |
| 3237 | tline->text[1] != '$'))) { |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3238 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3239 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3240 | free_tlist(origline); |
| 3241 | return DIRECTIVE_FOUND; |
| 3242 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3243 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3244 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3245 | last = tline; |
| 3246 | param_start = tline = tline->next; |
| 3247 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3248 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3249 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3250 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3251 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3252 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3253 | if (tok_is_(tline, "(")) { |
| 3254 | /* |
| 3255 | * This macro has parameters. |
| 3256 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3257 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3258 | tline = tline->next; |
| 3259 | while (1) { |
| 3260 | skip_white_(tline); |
| 3261 | if (!tline) { |
| 3262 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 3263 | free_tlist(origline); |
| 3264 | return DIRECTIVE_FOUND; |
| 3265 | } |
| 3266 | if (tline->type != TOK_ID) { |
| 3267 | error(ERR_NONFATAL, |
| 3268 | "`%s': parameter identifier expected", |
| 3269 | tline->text); |
| 3270 | free_tlist(origline); |
| 3271 | return DIRECTIVE_FOUND; |
| 3272 | } |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame^] | 3273 | |
| 3274 | smacro_set_param_idx(tline, nparam); |
| 3275 | nparam++; |
| 3276 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3277 | tline = tline->next; |
| 3278 | skip_white_(tline); |
| 3279 | if (tok_is_(tline, ",")) { |
| 3280 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3281 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3282 | if (!tok_is_(tline, ")")) { |
| 3283 | error(ERR_NONFATAL, |
| 3284 | "`)' expected to terminate macro template"); |
| 3285 | free_tlist(origline); |
| 3286 | return DIRECTIVE_FOUND; |
| 3287 | } |
| 3288 | break; |
| 3289 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3290 | } |
| 3291 | last = tline; |
| 3292 | tline = tline->next; |
| 3293 | } |
| 3294 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3295 | last = tline, tline = tline->next; |
| 3296 | macro_start = NULL; |
| 3297 | last->next = NULL; |
| 3298 | t = tline; |
| 3299 | while (t) { |
| 3300 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3301 | list_for_each(tt, param_start) |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame^] | 3302 | if (is_smacro_param(tt) && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3303 | !strcmp(tt->text, t->text)) |
| 3304 | t->type = tt->type; |
| 3305 | } |
| 3306 | tt = t->next; |
| 3307 | t->next = macro_start; |
| 3308 | macro_start = t; |
| 3309 | t = tt; |
| 3310 | } |
| 3311 | /* |
| 3312 | * Good. We now have a macro name, a parameter count, and a |
| 3313 | * token list (in reverse order) for an expansion. We ought |
| 3314 | * to be OK just to create an SMacro, store it, and let |
| 3315 | * free_tlist have the rest of the line (which we have |
| 3316 | * carefully re-terminated after chopping off the expansion |
| 3317 | * from the end). |
| 3318 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3319 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3320 | free_tlist(origline); |
| 3321 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3322 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3323 | case PP_UNDEF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3324 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3325 | tline = tline->next; |
| 3326 | skip_white_(tline); |
| 3327 | tline = expand_id(tline); |
| 3328 | if (!tline || (tline->type != TOK_ID && |
| 3329 | (tline->type != TOK_PREPROC_ID || |
| 3330 | tline->text[1] != '$'))) { |
| 3331 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 3332 | free_tlist(origline); |
| 3333 | return DIRECTIVE_FOUND; |
| 3334 | } |
| 3335 | if (tline->next) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3336 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3337 | "trailing garbage after macro name ignored"); |
| 3338 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3339 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3340 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3341 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3342 | undef_smacro(ctx, mname); |
| 3343 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3344 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3345 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3346 | case PP_DEFSTR: |
| 3347 | case PP_IDEFSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3348 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3349 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3350 | |
| 3351 | tline = tline->next; |
| 3352 | skip_white_(tline); |
| 3353 | tline = expand_id(tline); |
| 3354 | if (!tline || (tline->type != TOK_ID && |
| 3355 | (tline->type != TOK_PREPROC_ID || |
| 3356 | tline->text[1] != '$'))) { |
| 3357 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3358 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3359 | free_tlist(origline); |
| 3360 | return DIRECTIVE_FOUND; |
| 3361 | } |
| 3362 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3363 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3364 | last = tline; |
| 3365 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3366 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3367 | |
| 3368 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3369 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3370 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3371 | p = detoken(tline, false); |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3372 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3373 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3374 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3375 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3376 | |
| 3377 | /* |
| 3378 | * We now have a macro name, an implicit parameter count of |
| 3379 | * zero, and a string token to use as an expansion. Create |
| 3380 | * and store an SMacro. |
| 3381 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3382 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3383 | free_tlist(origline); |
| 3384 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3385 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3386 | case PP_DEFTOK: |
| 3387 | case PP_IDEFTOK: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3388 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3389 | casesense = (i == PP_DEFTOK); |
| 3390 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3391 | tline = tline->next; |
| 3392 | skip_white_(tline); |
| 3393 | tline = expand_id(tline); |
| 3394 | if (!tline || (tline->type != TOK_ID && |
| 3395 | (tline->type != TOK_PREPROC_ID || |
| 3396 | tline->text[1] != '$'))) { |
| 3397 | error(ERR_NONFATAL, |
| 3398 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3399 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3400 | free_tlist(origline); |
| 3401 | return DIRECTIVE_FOUND; |
| 3402 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3403 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3404 | last = tline; |
| 3405 | tline = expand_smacro(tline->next); |
| 3406 | last->next = NULL; |
| 3407 | |
| 3408 | t = tline; |
| 3409 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3410 | t = t->next; |
| 3411 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3412 | if (!tok_type_(t, TOK_STRING)) { |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3413 | error(ERR_NONFATAL, |
| 3414 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3415 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3416 | free_tlist(tline); |
| 3417 | free_tlist(origline); |
| 3418 | return DIRECTIVE_FOUND; |
| 3419 | } |
| 3420 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3421 | /* |
| 3422 | * Convert the string to a token stream. Note that smacros |
| 3423 | * are stored with the token stream reversed, so we have to |
| 3424 | * reverse the output of tokenize(). |
| 3425 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3426 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3427 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3428 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3429 | /* |
| 3430 | * We now have a macro name, an implicit parameter count of |
| 3431 | * zero, and a numeric token to use as an expansion. Create |
| 3432 | * and store an SMacro. |
| 3433 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3434 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3435 | free_tlist(tline); |
| 3436 | free_tlist(origline); |
| 3437 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3438 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3439 | case PP_PATHSEARCH: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3440 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3441 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3442 | FILE *fp; |
| 3443 | StrList *xsl = NULL; |
| 3444 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3445 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3446 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3447 | |
| 3448 | tline = tline->next; |
| 3449 | skip_white_(tline); |
| 3450 | tline = expand_id(tline); |
| 3451 | if (!tline || (tline->type != TOK_ID && |
| 3452 | (tline->type != TOK_PREPROC_ID || |
| 3453 | tline->text[1] != '$'))) { |
| 3454 | error(ERR_NONFATAL, |
| 3455 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3456 | free_tlist(origline); |
| 3457 | return DIRECTIVE_FOUND; |
| 3458 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3459 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3460 | last = tline; |
| 3461 | tline = expand_smacro(tline->next); |
| 3462 | last->next = NULL; |
| 3463 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3464 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3465 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3466 | t = t->next; |
| 3467 | |
| 3468 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3469 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3470 | error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3471 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3472 | free_tlist(origline); |
| 3473 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3474 | } |
| 3475 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3476 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3477 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3478 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3479 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3480 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3481 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3482 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3483 | if (fp) { |
| 3484 | p = xsl->str; |
| 3485 | fclose(fp); /* Don't actually care about the file */ |
| 3486 | } |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3487 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3488 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3489 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 3490 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3491 | |
| 3492 | /* |
| 3493 | * We now have a macro name, an implicit parameter count of |
| 3494 | * zero, and a string token to use as an expansion. Create |
| 3495 | * and store an SMacro. |
| 3496 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3497 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3498 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3499 | free_tlist(origline); |
| 3500 | return DIRECTIVE_FOUND; |
| 3501 | } |
| 3502 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3503 | case PP_STRLEN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3504 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3505 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3506 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3507 | tline = tline->next; |
| 3508 | skip_white_(tline); |
| 3509 | tline = expand_id(tline); |
| 3510 | if (!tline || (tline->type != TOK_ID && |
| 3511 | (tline->type != TOK_PREPROC_ID || |
| 3512 | tline->text[1] != '$'))) { |
| 3513 | error(ERR_NONFATAL, |
| 3514 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3515 | free_tlist(origline); |
| 3516 | return DIRECTIVE_FOUND; |
| 3517 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3518 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3519 | last = tline; |
| 3520 | tline = expand_smacro(tline->next); |
| 3521 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3522 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3523 | t = tline; |
| 3524 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3525 | t = t->next; |
| 3526 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3527 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3528 | error(ERR_NONFATAL, |
| 3529 | "`%%strlen` requires string as second parameter"); |
| 3530 | free_tlist(tline); |
| 3531 | free_tlist(origline); |
| 3532 | return DIRECTIVE_FOUND; |
| 3533 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3534 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3535 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3536 | make_tok_num(macro_start, nasm_unquote(t->text, 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 | /* |
| 3539 | * We now have a macro name, an implicit parameter count of |
| 3540 | * zero, and a numeric token to use as an expansion. Create |
| 3541 | * and store an SMacro. |
| 3542 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3543 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3544 | free_tlist(tline); |
| 3545 | free_tlist(origline); |
| 3546 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3547 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3548 | case PP_STRCAT: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3549 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3550 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3551 | |
| 3552 | tline = tline->next; |
| 3553 | skip_white_(tline); |
| 3554 | tline = expand_id(tline); |
| 3555 | if (!tline || (tline->type != TOK_ID && |
| 3556 | (tline->type != TOK_PREPROC_ID || |
| 3557 | tline->text[1] != '$'))) { |
| 3558 | error(ERR_NONFATAL, |
| 3559 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3560 | free_tlist(origline); |
| 3561 | return DIRECTIVE_FOUND; |
| 3562 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3563 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3564 | last = tline; |
| 3565 | tline = expand_smacro(tline->next); |
| 3566 | last->next = NULL; |
| 3567 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3568 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3569 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3570 | switch (t->type) { |
| 3571 | case TOK_WHITESPACE: |
| 3572 | break; |
| 3573 | case TOK_STRING: |
| 3574 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3575 | break; |
| 3576 | case TOK_OTHER: |
| 3577 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3578 | break; |
| 3579 | /* else fall through */ |
| 3580 | default: |
| 3581 | error(ERR_NONFATAL, |
| 3582 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3583 | free_tlist(tline); |
| 3584 | free_tlist(origline); |
| 3585 | return DIRECTIVE_FOUND; |
| 3586 | } |
| 3587 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3588 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3589 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3590 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3591 | if (t->type == TOK_STRING) { |
| 3592 | memcpy(p, t->text, t->a.len); |
| 3593 | p += t->a.len; |
| 3594 | } |
| 3595 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3596 | |
| 3597 | /* |
| 3598 | * We now have a macro name, an implicit parameter count of |
| 3599 | * zero, and a numeric token to use as an expansion. Create |
| 3600 | * and store an SMacro. |
| 3601 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3602 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3603 | macro_start->text = nasm_quote(pp, len); |
| 3604 | nasm_free(pp); |
| 3605 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3606 | free_tlist(tline); |
| 3607 | free_tlist(origline); |
| 3608 | return DIRECTIVE_FOUND; |
| 3609 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3610 | case PP_SUBSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3611 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3612 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3613 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3614 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3615 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3616 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3617 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3618 | tline = tline->next; |
| 3619 | skip_white_(tline); |
| 3620 | tline = expand_id(tline); |
| 3621 | if (!tline || (tline->type != TOK_ID && |
| 3622 | (tline->type != TOK_PREPROC_ID || |
| 3623 | tline->text[1] != '$'))) { |
| 3624 | error(ERR_NONFATAL, |
| 3625 | "`%%substr' expects a macro identifier as first parameter"); |
| 3626 | free_tlist(origline); |
| 3627 | return DIRECTIVE_FOUND; |
| 3628 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3629 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3630 | last = tline; |
| 3631 | tline = expand_smacro(tline->next); |
| 3632 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3633 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3634 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3635 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3636 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3637 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3638 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3639 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3640 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3641 | error(ERR_NONFATAL, |
| 3642 | "`%%substr` requires string as second parameter"); |
| 3643 | free_tlist(tline); |
| 3644 | free_tlist(origline); |
| 3645 | return DIRECTIVE_FOUND; |
| 3646 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3647 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3648 | tt = t->next; |
| 3649 | tptr = &tt; |
| 3650 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3651 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3652 | pass, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3653 | if (!evalresult) { |
| 3654 | free_tlist(tline); |
| 3655 | free_tlist(origline); |
| 3656 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3657 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3658 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3659 | free_tlist(tline); |
| 3660 | free_tlist(origline); |
| 3661 | return DIRECTIVE_FOUND; |
| 3662 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3663 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3664 | |
| 3665 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3666 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3667 | if (!tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3668 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3669 | } else { |
| 3670 | tokval.t_type = TOKEN_INVALID; |
| 3671 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3672 | pass, error, NULL); |
| 3673 | if (!evalresult) { |
| 3674 | free_tlist(tline); |
| 3675 | free_tlist(origline); |
| 3676 | return DIRECTIVE_FOUND; |
| 3677 | } else if (!is_simple(evalresult)) { |
| 3678 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3679 | free_tlist(tline); |
| 3680 | free_tlist(origline); |
| 3681 | return DIRECTIVE_FOUND; |
| 3682 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3683 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3684 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3685 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3686 | len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3687 | /* make start and count being in range */ |
| 3688 | if (start < 0) |
| 3689 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3690 | if (count < 0) |
| 3691 | count = len + count + 1 - start; |
| 3692 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3693 | count = len - start; |
| 3694 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3695 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3696 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3697 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3698 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3699 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3700 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3701 | /* |
| 3702 | * We now have a macro name, an implicit parameter count of |
| 3703 | * zero, and a numeric token to use as an expansion. Create |
| 3704 | * and store an SMacro. |
| 3705 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3706 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3707 | free_tlist(tline); |
| 3708 | free_tlist(origline); |
| 3709 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3710 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3711 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3712 | case PP_ASSIGN: |
| 3713 | case PP_IASSIGN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3714 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3715 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3716 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3717 | tline = tline->next; |
| 3718 | skip_white_(tline); |
| 3719 | tline = expand_id(tline); |
| 3720 | if (!tline || (tline->type != TOK_ID && |
| 3721 | (tline->type != TOK_PREPROC_ID || |
| 3722 | tline->text[1] != '$'))) { |
| 3723 | error(ERR_NONFATAL, |
| 3724 | "`%%%sassign' expects a macro identifier", |
| 3725 | (i == PP_IASSIGN ? "i" : "")); |
| 3726 | free_tlist(origline); |
| 3727 | return DIRECTIVE_FOUND; |
| 3728 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3729 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3730 | last = tline; |
| 3731 | tline = expand_smacro(tline->next); |
| 3732 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3733 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3734 | t = tline; |
| 3735 | tptr = &t; |
| 3736 | tokval.t_type = TOKEN_INVALID; |
| 3737 | evalresult = |
| 3738 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3739 | free_tlist(tline); |
| 3740 | if (!evalresult) { |
| 3741 | free_tlist(origline); |
| 3742 | return DIRECTIVE_FOUND; |
| 3743 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3744 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3745 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3746 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3747 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3748 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3749 | if (!is_simple(evalresult)) { |
| 3750 | error(ERR_NONFATAL, |
| 3751 | "non-constant value given to `%%%sassign'", |
| 3752 | (i == PP_IASSIGN ? "i" : "")); |
| 3753 | free_tlist(origline); |
| 3754 | return DIRECTIVE_FOUND; |
| 3755 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3756 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3757 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3758 | make_tok_num(macro_start, reloc_value(evalresult)); |
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 | /* |
| 3761 | * We now have a macro name, an implicit parameter count of |
| 3762 | * zero, and a numeric token to use as an expansion. Create |
| 3763 | * and store an SMacro. |
| 3764 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3765 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3766 | free_tlist(origline); |
| 3767 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3768 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3769 | case PP_LINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3770 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3771 | /* |
| 3772 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3773 | */ |
| 3774 | tline = tline->next; |
| 3775 | skip_white_(tline); |
| 3776 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3777 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3778 | free_tlist(origline); |
| 3779 | return DIRECTIVE_FOUND; |
| 3780 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3781 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3782 | m = 1; |
| 3783 | tline = tline->next; |
| 3784 | if (tok_is_(tline, "+")) { |
| 3785 | tline = tline->next; |
| 3786 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3787 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3788 | free_tlist(origline); |
| 3789 | return DIRECTIVE_FOUND; |
| 3790 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3791 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3792 | tline = tline->next; |
| 3793 | } |
| 3794 | skip_white_(tline); |
| 3795 | src_set_linnum(k); |
| 3796 | istk->lineinc = m; |
| 3797 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3798 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3799 | } |
| 3800 | free_tlist(origline); |
| 3801 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3802 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3803 | case PP_WHILE: |
| 3804 | if (defining != NULL) { |
| 3805 | if (defining->type == EXP_WHILE) { |
| 3806 | defining->def_depth ++; |
| 3807 | } |
| 3808 | return NO_DIRECTIVE_FOUND; |
| 3809 | } |
| 3810 | l = NULL; |
| 3811 | if ((istk->expansion != NULL) && |
| 3812 | (istk->expansion->emitting == false)) { |
| 3813 | j = COND_NEVER; |
| 3814 | } else { |
| 3815 | l = new_Line(); |
| 3816 | l->first = copy_Token(tline->next); |
| 3817 | j = if_condition(tline->next, i); |
| 3818 | tline->next = NULL; /* it got freed */ |
| 3819 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 3820 | } |
| 3821 | ed = new_ExpDef(EXP_WHILE); |
| 3822 | ed->state = j; |
| 3823 | ed->cur_depth = 1; |
| 3824 | ed->max_depth = DEADMAN_LIMIT; |
| 3825 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 3826 | if (ed->ignoring == false) { |
| 3827 | ed->line = l; |
| 3828 | ed->last = l; |
| 3829 | } else if (l != NULL) { |
| 3830 | delete_Token(l->first); |
| 3831 | nasm_free(l); |
| 3832 | l = NULL; |
| 3833 | } |
| 3834 | ed->prev = defining; |
| 3835 | defining = ed; |
| 3836 | free_tlist(origline); |
| 3837 | return DIRECTIVE_FOUND; |
| 3838 | |
| 3839 | case PP_ENDWHILE: |
| 3840 | if (defining != NULL) { |
| 3841 | if (defining->type == EXP_WHILE) { |
| 3842 | if (defining->def_depth > 0) { |
| 3843 | defining->def_depth --; |
| 3844 | return NO_DIRECTIVE_FOUND; |
| 3845 | } |
| 3846 | } else { |
| 3847 | return NO_DIRECTIVE_FOUND; |
| 3848 | } |
| 3849 | } |
| 3850 | if (tline->next != NULL) { |
| 3851 | error_precond(ERR_WARNING|ERR_PASS1, |
| 3852 | "trailing garbage after `%%endwhile' ignored"); |
| 3853 | } |
| 3854 | if ((defining == NULL) || (defining->type != EXP_WHILE)) { |
| 3855 | error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'"); |
| 3856 | return DIRECTIVE_FOUND; |
| 3857 | } |
| 3858 | ed = defining; |
| 3859 | defining = ed->prev; |
| 3860 | if (ed->ignoring == false) { |
| 3861 | ed->prev = expansions; |
| 3862 | expansions = ed; |
| 3863 | ei = new_ExpInv(EXP_WHILE, ed); |
| 3864 | ei->current = ed->line->next; |
| 3865 | ei->emitting = true; |
| 3866 | ei->prev = istk->expansion; |
| 3867 | istk->expansion = ei; |
| 3868 | } else { |
| 3869 | nasm_free(ed); |
| 3870 | } |
| 3871 | free_tlist(origline); |
| 3872 | return DIRECTIVE_FOUND; |
| 3873 | |
| 3874 | case PP_EXITWHILE: |
| 3875 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3876 | /* |
| 3877 | * We must search along istk->expansion until we hit a |
| 3878 | * while invocation. Then we disable the emitting state(s) |
| 3879 | * between exitwhile and endwhile. |
| 3880 | */ |
| 3881 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3882 | if (ei->type == EXP_WHILE) { |
| 3883 | break; |
| 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | if (ei != NULL) { |
| 3888 | /* |
| 3889 | * Set all invocations leading back to the while |
| 3890 | * invocation to a non-emitting state. |
| 3891 | */ |
| 3892 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3893 | eei->emitting = false; |
| 3894 | } |
| 3895 | eei->emitting = false; |
| 3896 | eei->current = NULL; |
| 3897 | eei->def->cur_depth = eei->def->max_depth; |
| 3898 | } else { |
| 3899 | error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block"); |
| 3900 | } |
| 3901 | free_tlist(origline); |
| 3902 | return DIRECTIVE_FOUND; |
| 3903 | |
| 3904 | case PP_COMMENT: |
| 3905 | if (defining != NULL) { |
| 3906 | if (defining->type == EXP_COMMENT) { |
| 3907 | defining->def_depth ++; |
| 3908 | } |
| 3909 | return NO_DIRECTIVE_FOUND; |
| 3910 | } |
| 3911 | ed = new_ExpDef(EXP_COMMENT); |
| 3912 | ed->ignoring = true; |
| 3913 | ed->prev = defining; |
| 3914 | defining = ed; |
| 3915 | free_tlist(origline); |
| 3916 | return DIRECTIVE_FOUND; |
| 3917 | |
| 3918 | case PP_ENDCOMMENT: |
| 3919 | if (defining != NULL) { |
| 3920 | if (defining->type == EXP_COMMENT) { |
| 3921 | if (defining->def_depth > 0) { |
| 3922 | defining->def_depth --; |
| 3923 | return NO_DIRECTIVE_FOUND; |
| 3924 | } |
| 3925 | } else { |
| 3926 | return NO_DIRECTIVE_FOUND; |
| 3927 | } |
| 3928 | } |
| 3929 | if ((defining == NULL) || (defining->type != EXP_COMMENT)) { |
| 3930 | error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'"); |
| 3931 | return DIRECTIVE_FOUND; |
| 3932 | } |
| 3933 | ed = defining; |
| 3934 | defining = ed->prev; |
| 3935 | nasm_free(ed); |
| 3936 | free_tlist(origline); |
| 3937 | return DIRECTIVE_FOUND; |
| 3938 | |
| 3939 | case PP_FINAL: |
| 3940 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3941 | if (in_final != false) { |
| 3942 | error(ERR_FATAL, "`%%final' cannot be used recursively"); |
| 3943 | } |
| 3944 | tline = tline->next; |
| 3945 | skip_white_(tline); |
| 3946 | if (tline == NULL) { |
| 3947 | error(ERR_NONFATAL, "`%%final' expects at least one parameter"); |
| 3948 | } else { |
| 3949 | l = new_Line(); |
| 3950 | l->first = copy_Token(tline); |
| 3951 | l->next = finals; |
| 3952 | finals = l; |
| 3953 | } |
| 3954 | free_tlist(origline); |
| 3955 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3956 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3957 | default: |
| 3958 | error(ERR_FATAL, |
| 3959 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3960 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3961 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3962 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3966 | * Ensure that a macro parameter contains a condition code and |
| 3967 | * nothing else. Return the condition code index if so, or -1 |
| 3968 | * otherwise. |
| 3969 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3970 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3971 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3972 | Token *tt; |
| 3973 | int i, j, k, m; |
| 3974 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3975 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3976 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3977 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3978 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3979 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3980 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3981 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3982 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3983 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3984 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3985 | |
| 3986 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 3987 | j = ARRAY_SIZE(conditions); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3988 | while (j - i > 1) { |
| 3989 | k = (j + i) / 2; |
| 3990 | m = nasm_stricmp(t->text, conditions[k]); |
| 3991 | if (m == 0) { |
| 3992 | i = k; |
| 3993 | j = -2; |
| 3994 | break; |
| 3995 | } else if (m < 0) { |
| 3996 | j = k; |
| 3997 | } else |
| 3998 | i = k; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3999 | } |
| 4000 | if (j != -2) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4001 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4002 | return i; |
| 4003 | } |
| 4004 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4005 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
| 4006 | int mnum, bool handle_paste_tokens) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4007 | { |
| 4008 | Token **tail, *t, *tt; |
| 4009 | Token **paste_head; |
| 4010 | bool did_paste = false; |
| 4011 | char *tmp; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4012 | int i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4013 | |
| 4014 | /* Now handle token pasting... */ |
| 4015 | paste_head = NULL; |
| 4016 | tail = head; |
| 4017 | while ((t = *tail) && (tt = t->next)) { |
| 4018 | switch (t->type) { |
| 4019 | case TOK_WHITESPACE: |
| 4020 | if (tt->type == TOK_WHITESPACE) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4021 | /* Zap adjacent whitespace tokens */ |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4022 | t->next = delete_Token(tt); |
| 4023 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4024 | /* Do not advance paste_head here */ |
| 4025 | tail = &t->next; |
| 4026 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4027 | break; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4028 | case TOK_PASTE: /* %+ */ |
| 4029 | if (handle_paste_tokens) { |
| 4030 | /* Zap %+ and whitespace tokens to the right */ |
| 4031 | while (t && (t->type == TOK_WHITESPACE || |
| 4032 | t->type == TOK_PASTE)) |
| 4033 | t = *tail = delete_Token(t); |
| 4034 | if (!paste_head || !t) |
| 4035 | break; /* Nothing to paste with */ |
| 4036 | tail = paste_head; |
| 4037 | t = *tail; |
| 4038 | tt = t->next; |
| 4039 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 4040 | tt = t->next = delete_Token(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4041 | if (tt) { |
| 4042 | tmp = nasm_strcat(t->text, tt->text); |
| 4043 | delete_Token(t); |
| 4044 | tt = delete_Token(tt); |
| 4045 | t = *tail = tokenize(tmp); |
| 4046 | nasm_free(tmp); |
| 4047 | while (t->next) { |
| 4048 | tail = &t->next; |
| 4049 | t = t->next; |
| 4050 | } |
| 4051 | t->next = tt; /* Attach the remaining token chain */ |
| 4052 | did_paste = true; |
| 4053 | } |
| 4054 | paste_head = tail; |
| 4055 | tail = &t->next; |
| 4056 | break; |
| 4057 | } |
| 4058 | /* else fall through */ |
| 4059 | default: |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4060 | /* |
| 4061 | * Concatenation of tokens might look nontrivial |
| 4062 | * but in real it's pretty simple -- the caller |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4063 | * prepares the masks of token types to be concatenated |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4064 | * and we simply find matched sequences and slip |
| 4065 | * them together |
| 4066 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4067 | for (i = 0; i < mnum; i++) { |
| 4068 | if (PP_CONCAT_MASK(t->type) & m[i].mask_head) { |
| 4069 | size_t len = 0; |
| 4070 | char *tmp, *p; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4071 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4072 | while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4073 | len += strlen(tt->text); |
| 4074 | tt = tt->next; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4075 | } |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4076 | |
Cyrill Gorcunov | fdd0ac5 | 2011-06-27 01:22:27 +0400 | [diff] [blame] | 4077 | nasm_dump_token(tt); |
| 4078 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4079 | /* |
| 4080 | * Now tt points to the first token after |
| 4081 | * the potential paste area... |
| 4082 | */ |
| 4083 | if (tt != t->next) { |
| 4084 | /* We have at least two tokens... */ |
| 4085 | len += strlen(t->text); |
| 4086 | p = tmp = nasm_malloc(len+1); |
| 4087 | while (t != tt) { |
| 4088 | strcpy(p, t->text); |
| 4089 | p = strchr(p, '\0'); |
| 4090 | t = delete_Token(t); |
| 4091 | } |
| 4092 | t = *tail = tokenize(tmp); |
| 4093 | nasm_free(tmp); |
| 4094 | while (t->next) { |
| 4095 | tail = &t->next; |
| 4096 | t = t->next; |
| 4097 | } |
| 4098 | t->next = tt; /* Attach the remaining token chain */ |
| 4099 | did_paste = true; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4100 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4101 | paste_head = tail; |
| 4102 | tail = &t->next; |
| 4103 | break; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4104 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4105 | } |
| 4106 | if (i >= mnum) { /* no match */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4107 | tail = &t->next; |
| 4108 | if (!tok_type_(t->next, TOK_WHITESPACE)) |
| 4109 | paste_head = tail; |
| 4110 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4111 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4112 | } |
| 4113 | } |
| 4114 | return did_paste; |
| 4115 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4116 | |
| 4117 | /* |
| 4118 | * expands to a list of tokens from %{x:y} |
| 4119 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4120 | static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4121 | { |
| 4122 | Token *t = tline, **tt, *tm, *head; |
| 4123 | char *pos; |
| 4124 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4125 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4126 | pos = strchr(tline->text, ':'); |
| 4127 | nasm_assert(pos); |
| 4128 | |
| 4129 | lst = atoi(pos + 1); |
| 4130 | fst = atoi(tline->text + 1); |
| 4131 | |
| 4132 | /* |
| 4133 | * only macros params are accounted so |
| 4134 | * if someone passes %0 -- we reject such |
| 4135 | * value(s) |
| 4136 | */ |
| 4137 | if (lst == 0 || fst == 0) |
| 4138 | goto err; |
| 4139 | |
| 4140 | /* the values should be sane */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4141 | if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) || |
| 4142 | (lst > (int)ei->nparam || lst < (-(int)ei->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4143 | goto err; |
| 4144 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4145 | fst = fst < 0 ? fst + (int)ei->nparam + 1: fst; |
| 4146 | lst = lst < 0 ? lst + (int)ei->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4147 | |
| 4148 | /* counted from zero */ |
| 4149 | fst--, lst--; |
| 4150 | |
| 4151 | /* |
| 4152 | * it will be at least one token |
| 4153 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4154 | tm = ei->params[(fst + ei->rotate) % ei->nparam]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4155 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4156 | head = t, tt = &t->next; |
| 4157 | if (fst < lst) { |
| 4158 | for (i = fst + 1; i <= lst; i++) { |
| 4159 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4160 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4161 | j = (i + ei->rotate) % ei->nparam; |
| 4162 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4163 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4164 | *tt = t, tt = &t->next; |
| 4165 | } |
| 4166 | } else { |
| 4167 | for (i = fst - 1; i >= lst; i--) { |
| 4168 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4169 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4170 | j = (i + ei->rotate) % ei->nparam; |
| 4171 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4172 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4173 | *tt = t, tt = &t->next; |
| 4174 | } |
| 4175 | } |
| 4176 | |
| 4177 | *last = tt; |
| 4178 | return head; |
| 4179 | |
| 4180 | err: |
| 4181 | error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
| 4182 | &tline->text[1]); |
| 4183 | return tline; |
| 4184 | } |
| 4185 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4186 | /* |
| 4187 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4188 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4189 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4190 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4191 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4192 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4193 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4194 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4195 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4196 | |
| 4197 | tail = &thead; |
| 4198 | thead = NULL; |
| 4199 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4200 | nasm_dump_stream(tline); |
| 4201 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4202 | while (tline) { |
| 4203 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4204 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4205 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4206 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4207 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4208 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4209 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4210 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4211 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4212 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4213 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4214 | t = tline; |
| 4215 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4216 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4217 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 4218 | if (ei->type == EXP_MMACRO) { |
| 4219 | break; |
| 4220 | } |
| 4221 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4222 | if (ei == NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4223 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4224 | } else { |
| 4225 | pos = strchr(t->text, ':'); |
| 4226 | if (!pos) { |
| 4227 | switch (t->text[1]) { |
| 4228 | /* |
| 4229 | * We have to make a substitution of one of the |
| 4230 | * forms %1, %-1, %+1, %%foo, %0. |
| 4231 | */ |
| 4232 | case '0': |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4233 | if ((strlen(t->text) > 2) && (t->text[2] == '0')) { |
| 4234 | type = TOK_ID; |
| 4235 | text = nasm_strdup(ei->label_text); |
| 4236 | } else { |
| 4237 | type = TOK_NUMBER; |
| 4238 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam); |
| 4239 | text = nasm_strdup(tmpbuf); |
| 4240 | } |
| 4241 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4242 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4243 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4244 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4245 | ei->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4246 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4247 | break; |
| 4248 | case '-': |
| 4249 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4250 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4251 | tt = NULL; |
| 4252 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4253 | if (ei->nparam > 1) |
| 4254 | n = (n + ei->rotate) % ei->nparam; |
| 4255 | tt = ei->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4256 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4257 | cc = find_cc(tt); |
| 4258 | if (cc == -1) { |
| 4259 | error(ERR_NONFATAL, |
| 4260 | "macro parameter %d is not a condition code", |
| 4261 | n + 1); |
| 4262 | text = NULL; |
| 4263 | } else { |
| 4264 | type = TOK_ID; |
| 4265 | if (inverse_ccs[cc] == -1) { |
| 4266 | error(ERR_NONFATAL, |
| 4267 | "condition code `%s' is not invertible", |
| 4268 | conditions[cc]); |
| 4269 | text = NULL; |
| 4270 | } else |
| 4271 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4272 | } |
| 4273 | break; |
| 4274 | case '+': |
| 4275 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4276 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4277 | tt = NULL; |
| 4278 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4279 | if (ei->nparam > 1) |
| 4280 | n = (n + ei->rotate) % ei->nparam; |
| 4281 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4282 | } |
| 4283 | cc = find_cc(tt); |
| 4284 | if (cc == -1) { |
| 4285 | error(ERR_NONFATAL, |
| 4286 | "macro parameter %d is not a condition code", |
| 4287 | n + 1); |
| 4288 | text = NULL; |
| 4289 | } else { |
| 4290 | type = TOK_ID; |
| 4291 | text = nasm_strdup(conditions[cc]); |
| 4292 | } |
| 4293 | break; |
| 4294 | default: |
| 4295 | n = atoi(t->text + 1) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4296 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4297 | tt = NULL; |
| 4298 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4299 | if (ei->nparam > 1) |
| 4300 | n = (n + ei->rotate) % ei->nparam; |
| 4301 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4302 | } |
| 4303 | if (tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4304 | for (i = 0; i < ei->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4305 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4306 | tail = &(*tail)->next; |
| 4307 | tt = tt->next; |
| 4308 | } |
| 4309 | } |
| 4310 | text = NULL; /* we've done it here */ |
| 4311 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4312 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4313 | } else { |
| 4314 | /* |
| 4315 | * seems we have a parameters range here |
| 4316 | */ |
| 4317 | Token *head, **last; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4318 | head = expand_mmac_params_range(ei, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4319 | if (head != t) { |
| 4320 | *tail = head; |
| 4321 | *last = tline; |
| 4322 | tline = head; |
| 4323 | text = NULL; |
| 4324 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4325 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4326 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4327 | if (!text) { |
| 4328 | delete_Token(t); |
| 4329 | } else { |
| 4330 | *tail = t; |
| 4331 | tail = &t->next; |
| 4332 | t->type = type; |
| 4333 | nasm_free(t->text); |
| 4334 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4335 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4336 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4337 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4338 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4339 | } else if (tline->type == TOK_INDIRECT) { |
| 4340 | t = tline; |
| 4341 | tline = tline->next; |
| 4342 | tt = tokenize(t->text); |
| 4343 | tt = expand_mmac_params(tt); |
| 4344 | tt = expand_smacro(tt); |
| 4345 | *tail = tt; |
| 4346 | while (tt) { |
| 4347 | tt->a.mac = NULL; /* Necessary? */ |
| 4348 | tail = &tt->next; |
| 4349 | tt = tt->next; |
| 4350 | } |
| 4351 | delete_Token(t); |
| 4352 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4353 | } else { |
| 4354 | t = *tail = tline; |
| 4355 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4356 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4357 | tail = &t->next; |
| 4358 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4359 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4360 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4361 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4362 | if (changed) { |
| 4363 | const struct tokseq_match t[] = { |
| 4364 | { |
| 4365 | PP_CONCAT_MASK(TOK_ID) | |
| 4366 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4367 | PP_CONCAT_MASK(TOK_ID) | |
| 4368 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4369 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4370 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4371 | }, |
| 4372 | { |
| 4373 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4374 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4375 | } |
| 4376 | }; |
| 4377 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4378 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4379 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4380 | nasm_dump_token(thead); |
| 4381 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4382 | return thead; |
| 4383 | } |
| 4384 | |
| 4385 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4386 | * Expand all single-line macro calls made in the given line. |
| 4387 | * Return the expanded version of the line. The original is deemed |
| 4388 | * to be destroyed in the process. (In reality we'll just move |
| 4389 | * Tokens from input to output a lot of the time, rather than |
| 4390 | * actually bothering to destroy and replicate.) |
| 4391 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4392 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4393 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4394 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4395 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4396 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4397 | Token **params; |
| 4398 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4399 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4400 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4401 | Token *org_tline = tline; |
| 4402 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4403 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4404 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4405 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4406 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4407 | /* |
| 4408 | * Trick: we should avoid changing the start token pointer since it can |
| 4409 | * be contained in "next" field of other token. Because of this |
| 4410 | * we allocate a copy of first token and work with it; at the end of |
| 4411 | * routine we copy it back |
| 4412 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4413 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4414 | tline = new_Token(org_tline->next, org_tline->type, |
| 4415 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4416 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4417 | nasm_free(org_tline->text); |
| 4418 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4421 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4422 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4423 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4424 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4425 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4426 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4427 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4428 | if (!--deadman) { |
| 4429 | error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4430 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4431 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4432 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4433 | if ((mname = tline->text)) { |
| 4434 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4435 | if (tline->type == TOK_ID) { |
| 4436 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4437 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 4438 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4439 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4440 | } else |
| 4441 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4442 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4443 | /* |
| 4444 | * We've hit an identifier. As in is_mmacro below, we first |
| 4445 | * check whether the identifier is a single-line macro at |
| 4446 | * all, then think about checking for parameters if |
| 4447 | * necessary. |
| 4448 | */ |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4449 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4450 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4451 | break; |
| 4452 | if (m) { |
| 4453 | mstart = tline; |
| 4454 | params = NULL; |
| 4455 | paramsize = NULL; |
| 4456 | if (m->nparam == 0) { |
| 4457 | /* |
| 4458 | * Simple case: the macro is parameterless. Discard the |
| 4459 | * one token that the macro call took, and push the |
| 4460 | * expansion back on the to-do stack. |
| 4461 | */ |
| 4462 | if (!m->expansion) { |
| 4463 | if (!strcmp("__FILE__", m->name)) { |
| 4464 | int32_t num = 0; |
| 4465 | char *file = NULL; |
| 4466 | src_get(&num, &file); |
| 4467 | tline->text = nasm_quote(file, strlen(file)); |
| 4468 | tline->type = TOK_STRING; |
| 4469 | nasm_free(file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4470 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4471 | } |
| 4472 | if (!strcmp("__LINE__", m->name)) { |
| 4473 | nasm_free(tline->text); |
| 4474 | make_tok_num(tline, src_get_linnum()); |
| 4475 | continue; |
| 4476 | } |
| 4477 | if (!strcmp("__BITS__", m->name)) { |
| 4478 | nasm_free(tline->text); |
| 4479 | make_tok_num(tline, globalbits); |
| 4480 | continue; |
| 4481 | } |
| 4482 | tline = delete_Token(tline); |
| 4483 | continue; |
| 4484 | } |
| 4485 | } else { |
| 4486 | /* |
| 4487 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4488 | * exists and takes parameters. We must find the |
| 4489 | * parameters in the call, count them, find the SMacro |
| 4490 | * that corresponds to that form of the macro call, and |
| 4491 | * substitute for the parameters when we expand. What a |
| 4492 | * pain. |
| 4493 | */ |
| 4494 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4495 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4496 | do { |
| 4497 | t = tline->next; |
| 4498 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4499 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4500 | t->text = NULL; |
| 4501 | t = tline->next = delete_Token(t); |
| 4502 | } |
| 4503 | tline = t; |
| 4504 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4505 | if (!tok_is_(tline, "(")) { |
| 4506 | /* |
| 4507 | * This macro wasn't called with parameters: ignore |
| 4508 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4509 | */ |
| 4510 | tline = mstart; |
| 4511 | m = NULL; |
| 4512 | } else { |
| 4513 | int paren = 0; |
| 4514 | int white = 0; |
| 4515 | brackets = 0; |
| 4516 | nparam = 0; |
| 4517 | sparam = PARAM_DELTA; |
| 4518 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4519 | params[0] = tline->next; |
| 4520 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4521 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4522 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4523 | /* |
| 4524 | * For some unusual expansions |
| 4525 | * which concatenates function call |
| 4526 | */ |
| 4527 | t = tline->next; |
| 4528 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4529 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4530 | t->text = NULL; |
| 4531 | t = tline->next = delete_Token(t); |
| 4532 | } |
| 4533 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4534 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4535 | if (!tline) { |
| 4536 | error(ERR_NONFATAL, |
| 4537 | "macro call expects terminating `)'"); |
| 4538 | break; |
| 4539 | } |
| 4540 | if (tline->type == TOK_WHITESPACE |
| 4541 | && brackets <= 0) { |
| 4542 | if (paramsize[nparam]) |
| 4543 | white++; |
| 4544 | else |
| 4545 | params[nparam] = tline->next; |
| 4546 | continue; /* parameter loop */ |
| 4547 | } |
| 4548 | if (tline->type == TOK_OTHER |
| 4549 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4550 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4551 | if (ch == ',' && !paren && brackets <= 0) { |
| 4552 | if (++nparam >= sparam) { |
| 4553 | sparam += PARAM_DELTA; |
| 4554 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4555 | sparam * sizeof(Token *)); |
| 4556 | paramsize = nasm_realloc(paramsize, |
| 4557 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4558 | } |
| 4559 | params[nparam] = tline->next; |
| 4560 | paramsize[nparam] = 0; |
| 4561 | white = 0; |
| 4562 | continue; /* parameter loop */ |
| 4563 | } |
| 4564 | if (ch == '{' && |
| 4565 | (brackets > 0 || (brackets == 0 && |
| 4566 | !paramsize[nparam]))) |
| 4567 | { |
| 4568 | if (!(brackets++)) { |
| 4569 | params[nparam] = tline->next; |
| 4570 | continue; /* parameter loop */ |
| 4571 | } |
| 4572 | } |
| 4573 | if (ch == '}' && brackets > 0) |
| 4574 | if (--brackets == 0) { |
| 4575 | brackets = -1; |
| 4576 | continue; /* parameter loop */ |
| 4577 | } |
| 4578 | if (ch == '(' && !brackets) |
| 4579 | paren++; |
| 4580 | if (ch == ')' && brackets <= 0) |
| 4581 | if (--paren < 0) |
| 4582 | break; |
| 4583 | } |
| 4584 | if (brackets < 0) { |
| 4585 | brackets = 0; |
| 4586 | error(ERR_NONFATAL, "braces do not " |
| 4587 | "enclose all of macro parameter"); |
| 4588 | } |
| 4589 | paramsize[nparam] += white + 1; |
| 4590 | white = 0; |
| 4591 | } /* parameter loop */ |
| 4592 | nparam++; |
| 4593 | while (m && (m->nparam != nparam || |
| 4594 | mstrcmp(m->name, mname, |
| 4595 | m->casesense))) |
| 4596 | m = m->next; |
| 4597 | if (!m) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4598 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4599 | "macro `%s' exists, " |
| 4600 | "but not taking %d parameters", |
| 4601 | mstart->text, nparam); |
| 4602 | } |
| 4603 | } |
| 4604 | if (m && m->in_progress) |
| 4605 | m = NULL; |
| 4606 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4607 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4608 | * Design question: should we handle !tline, which |
| 4609 | * indicates missing ')' here, or expand those |
| 4610 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4611 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4612 | */ |
| 4613 | nasm_free(params); |
| 4614 | nasm_free(paramsize); |
| 4615 | tline = mstart; |
| 4616 | } else { |
| 4617 | /* |
| 4618 | * Expand the macro: we are placed on the last token of the |
| 4619 | * call, so that we can easily split the call from the |
| 4620 | * following tokens. We also start by pushing an SMAC_END |
| 4621 | * token for the cycle removal. |
| 4622 | */ |
| 4623 | t = tline; |
| 4624 | if (t) { |
| 4625 | tline = t->next; |
| 4626 | t->next = NULL; |
| 4627 | } |
| 4628 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4629 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4630 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4631 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4632 | list_for_each(t, m->expansion) { |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame^] | 4633 | if (is_smacro_param(t)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4634 | Token *pcopy = tline, **ptail = &pcopy; |
| 4635 | Token *ttt, *pt; |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame^] | 4636 | int i, idx; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4637 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame^] | 4638 | idx = smacro_get_param_idx(t); |
| 4639 | ttt = params[idx]; |
| 4640 | i = paramsize[idx]; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4641 | while (--i >= 0) { |
| 4642 | pt = *ptail = new_Token(tline, ttt->type, |
| 4643 | ttt->text, 0); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4644 | ptail = &pt->next; |
| 4645 | ttt = ttt->next; |
| 4646 | } |
| 4647 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4648 | } else if (t->type == TOK_PREPROC_Q) { |
| 4649 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4650 | tline = tt; |
| 4651 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4652 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4653 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4654 | } else { |
| 4655 | tt = new_Token(tline, t->type, t->text, 0); |
| 4656 | tline = tt; |
| 4657 | } |
| 4658 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4659 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4660 | /* |
| 4661 | * Having done that, get rid of the macro call, and clean |
| 4662 | * up the parameters. |
| 4663 | */ |
| 4664 | nasm_free(params); |
| 4665 | nasm_free(paramsize); |
| 4666 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4667 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4668 | continue; /* main token loop */ |
| 4669 | } |
| 4670 | } |
| 4671 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4672 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4673 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4674 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4675 | tline = delete_Token(tline); |
| 4676 | } else { |
| 4677 | t = *tail = tline; |
| 4678 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4679 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4680 | t->next = NULL; |
| 4681 | tail = &t->next; |
| 4682 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4685 | /* |
| 4686 | * 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] | 4687 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4688 | * TOK_IDs should be concatenated. |
| 4689 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4690 | * them (without white spaces in between). |
| 4691 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4692 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4693 | const struct tokseq_match t[] = { |
| 4694 | { |
| 4695 | PP_CONCAT_MASK(TOK_ID) | |
| 4696 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4697 | PP_CONCAT_MASK(TOK_ID) | |
| 4698 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4699 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4700 | } |
| 4701 | }; |
| 4702 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4703 | /* |
| 4704 | * If we concatenated something, *and* we had previously expanded |
| 4705 | * an actual macro, scan the lines again for macros... |
| 4706 | */ |
| 4707 | tline = thead; |
| 4708 | expanded = false; |
| 4709 | goto again; |
| 4710 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4711 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4712 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4713 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4714 | if (org_tline) { |
| 4715 | if (thead) { |
| 4716 | *org_tline = *thead; |
| 4717 | /* since we just gave text to org_line, don't free it */ |
| 4718 | thead->text = NULL; |
| 4719 | delete_Token(thead); |
| 4720 | } else { |
| 4721 | /* the expression expanded to empty line; |
| 4722 | we can't return NULL for some reasons |
| 4723 | we just set the line to a single WHITESPACE token. */ |
| 4724 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4725 | org_tline->text = NULL; |
| 4726 | org_tline->type = TOK_WHITESPACE; |
| 4727 | } |
| 4728 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4731 | return thead; |
| 4732 | } |
| 4733 | |
| 4734 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4735 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4736 | * right before they are fetched in. The reason is that there can be |
| 4737 | * identifiers consisting of several subparts. We consider that if there |
| 4738 | * are more than one element forming the name, user wants a expansion, |
| 4739 | * otherwise it will be left as-is. Example: |
| 4740 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4741 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4742 | * |
| 4743 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4744 | * will suck it and define the corresponding value. Other case: |
| 4745 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4746 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4747 | * |
| 4748 | * In this case user wants name to be expanded *before* %define starts |
| 4749 | * working, so we'll expand %$abc into something (if it has a value; |
| 4750 | * otherwise it will be left as-is) then concatenate all successive |
| 4751 | * PP_IDs into one. |
| 4752 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4753 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4754 | { |
| 4755 | Token *cur, *oldnext = NULL; |
| 4756 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4757 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4758 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4759 | |
| 4760 | cur = tline; |
| 4761 | while (cur->next && |
Cyrill Gorcunov | 5b6c96b | 2011-06-30 00:22:53 +0400 | [diff] [blame] | 4762 | (cur->next->type == TOK_ID || |
| 4763 | cur->next->type == TOK_PREPROC_ID || |
| 4764 | cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4765 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4766 | |
| 4767 | /* If identifier consists of just one token, don't expand */ |
| 4768 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4769 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4770 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4771 | if (cur) { |
| 4772 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4773 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4776 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4777 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4778 | if (cur) { |
| 4779 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4780 | cur = tline; |
| 4781 | while (cur && cur->next) |
| 4782 | cur = cur->next; |
| 4783 | if (cur) |
| 4784 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | return tline; |
| 4788 | } |
| 4789 | |
| 4790 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4791 | * Determine whether the given line constitutes a multi-line macro |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4792 | * 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] | 4793 | * to check for an initial label - that's taken care of in |
| 4794 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4795 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4796 | * name is easy to find. |
| 4797 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4798 | static ExpDef *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4799 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4800 | ExpDef *head, *ed; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4801 | Token **params; |
| 4802 | int nparam; |
| 4803 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4804 | head = (ExpDef *) hash_findix(&expdefs, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4805 | |
| 4806 | /* |
| 4807 | * Efficiency: first we see if any macro exists with the given |
| 4808 | * name. If not, we can return NULL immediately. _Then_ we |
| 4809 | * count the parameters, and then we look further along the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4810 | * list if necessary to find the proper ExpDef. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4811 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4812 | list_for_each(ed, head) |
| 4813 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4814 | break; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4815 | if (!ed) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4816 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4817 | |
| 4818 | /* |
| 4819 | * OK, we have a potential macro. Count and demarcate the |
| 4820 | * parameters. |
| 4821 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4822 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4823 | |
| 4824 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4825 | * 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] | 4826 | * structure that handles this number. |
| 4827 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4828 | while (ed) { |
| 4829 | if (ed->nparam_min <= nparam |
| 4830 | && (ed->plus || nparam <= ed->nparam_max)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4831 | /* |
| 4832 | * It's right, and we can use it. Add its default |
| 4833 | * parameters to the end of our list if necessary. |
| 4834 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4835 | if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4836 | params = |
| 4837 | nasm_realloc(params, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4838 | ((ed->nparam_min + ed->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4839 | 1) * sizeof(*params))); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4840 | while (nparam < ed->nparam_min + ed->ndefs) { |
| 4841 | params[nparam] = ed->defaults[nparam - ed->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4842 | nparam++; |
| 4843 | } |
| 4844 | } |
| 4845 | /* |
| 4846 | * If we've gone over the maximum parameter count (and |
| 4847 | * we're in Plus mode), ignore parameters beyond |
| 4848 | * nparam_max. |
| 4849 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4850 | if (ed->plus && nparam > ed->nparam_max) |
| 4851 | nparam = ed->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4852 | /* |
| 4853 | * Then terminate the parameter list, and leave. |
| 4854 | */ |
| 4855 | if (!params) { /* need this special case */ |
| 4856 | params = nasm_malloc(sizeof(*params)); |
| 4857 | nparam = 0; |
| 4858 | } |
| 4859 | params[nparam] = NULL; |
| 4860 | *params_array = params; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4861 | return ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4862 | } |
| 4863 | /* |
| 4864 | * This one wasn't right: look for the next one with the |
| 4865 | * same name. |
| 4866 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4867 | list_for_each(ed, ed->next) |
| 4868 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4869 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | /* |
| 4873 | * After all that, we didn't find one with the right number of |
| 4874 | * parameters. Issue a warning, and fail to expand the macro. |
| 4875 | */ |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4876 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4877 | "macro `%s' exists, but not taking %d parameters", |
| 4878 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4879 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4880 | return NULL; |
| 4881 | } |
| 4882 | |
| 4883 | /* |
| 4884 | * Expand the multi-line macro call made by the given line, if |
| 4885 | * 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] | 4886 | * istk->expansion and return true. Otherwise return false. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4887 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4888 | static bool expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4889 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4890 | Token *label = NULL; |
| 4891 | int dont_prepend = 0; |
Cyrill Gorcunov | fb27fc2 | 2011-06-25 12:08:30 +0400 | [diff] [blame] | 4892 | Token **params, *t; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4893 | Line *l = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4894 | ExpDef *ed; |
| 4895 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4896 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4897 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4898 | |
| 4899 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4900 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4901 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4902 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4903 | return false; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4904 | ed = is_mmacro(t, ¶ms); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4905 | if (ed != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4906 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4907 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4908 | Token *last; |
| 4909 | /* |
| 4910 | * We have an id which isn't a macro call. We'll assume |
| 4911 | * it might be a label; we'll also check to see if a |
| 4912 | * colon follows it. Then, if there's another id after |
| 4913 | * that lot, we'll check it again for macro-hood. |
| 4914 | */ |
| 4915 | label = last = t; |
| 4916 | t = t->next; |
| 4917 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4918 | last = t, t = t->next; |
| 4919 | if (tok_is_(t, ":")) { |
| 4920 | dont_prepend = 1; |
| 4921 | last = t, t = t->next; |
| 4922 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4923 | last = t, t = t->next; |
| 4924 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4925 | if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, ¶ms))) |
| 4926 | return false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4927 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4928 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4929 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4930 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4931 | |
| 4932 | /* |
| 4933 | * Fix up the parameters: this involves stripping leading and |
| 4934 | * trailing whitespace, then stripping braces if they are |
| 4935 | * present. |
| 4936 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4937 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4938 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4939 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4940 | for (i = 0; params[i]; i++) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4941 | int brace = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4942 | int comma = (!ed->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4943 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4944 | t = params[i]; |
| 4945 | skip_white_(t); |
| 4946 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4947 | t = t->next, brace = true, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4948 | params[i] = t; |
| 4949 | paramlen[i] = 0; |
| 4950 | while (t) { |
| 4951 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4952 | break; /* ... because we have hit a comma */ |
| 4953 | if (comma && t->type == TOK_WHITESPACE |
| 4954 | && tok_is_(t->next, ",")) |
| 4955 | break; /* ... or a space then a comma */ |
| 4956 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
| 4957 | break; /* ... or a brace */ |
| 4958 | t = t->next; |
| 4959 | paramlen[i]++; |
| 4960 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4963 | if (ed->cur_depth >= ed->max_depth) { |
| 4964 | if (ed->max_depth > 1) { |
| 4965 | error(ERR_WARNING, |
| 4966 | "reached maximum macro recursion depth of %i for %s", |
| 4967 | ed->max_depth,ed->name); |
| 4968 | } |
| 4969 | return false; |
| 4970 | } else { |
| 4971 | ed->cur_depth ++; |
| 4972 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4973 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4974 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4975 | * OK, we have found a ExpDef structure representing a |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4976 | * previously defined mmacro. Create an expansion invocation |
| 4977 | * and point it back to the expansion definition. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4978 | * parameter tokens and macro-local tokens doesn't get done |
| 4979 | * until the single-line macro substitution process; this is |
| 4980 | * because delaying them allows us to change the semantics |
| 4981 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4982 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4983 | ei = new_ExpInv(EXP_MMACRO, ed); |
| 4984 | ei->name = nasm_strdup(mname); |
| 4985 | //ei->label = label; |
| 4986 | //ei->label_text = detoken(label, false); |
| 4987 | ei->current = ed->line; |
| 4988 | ei->emitting = true; |
| 4989 | //ei->iline = tline; |
| 4990 | ei->params = params; |
| 4991 | ei->nparam = nparam; |
| 4992 | ei->rotate = 0; |
| 4993 | ei->paramlen = paramlen; |
| 4994 | ei->lineno = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4995 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4996 | ei->prev = istk->expansion; |
| 4997 | istk->expansion = ei; |
| 4998 | |
| 4999 | /* |
| 5000 | * Special case: detect %00 on first invocation; if found, |
| 5001 | * avoid emitting any labels that precede the mmacro call. |
| 5002 | * ed->prepend is set to -1 when %00 is detected, else 1. |
| 5003 | */ |
| 5004 | if (ed->prepend == 0) { |
| 5005 | for (l = ed->line; l != NULL; l = l->next) { |
| 5006 | for (t = l->first; t != NULL; t = t->next) { |
| 5007 | if ((t->type == TOK_PREPROC_ID) && |
| 5008 | (strlen(t->text) == 3) && |
| 5009 | (t->text[1] == '0') && (t->text[2] == '0')) { |
| 5010 | dont_prepend = -1; |
| 5011 | break; |
| 5012 | } |
| 5013 | } |
| 5014 | if (dont_prepend < 0) { |
| 5015 | break; |
| 5016 | } |
| 5017 | } |
| 5018 | ed->prepend = ((dont_prepend < 0) ? -1 : 1); |
| 5019 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5020 | |
| 5021 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5022 | * If we had a label, push it on as the first line of |
| 5023 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5024 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5025 | if (label != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5026 | if (ed->prepend < 0) { |
| 5027 | ei->label_text = detoken(label, false); |
| 5028 | } else { |
| 5029 | if (dont_prepend == 0) { |
| 5030 | t = label; |
| 5031 | while (t->next != NULL) { |
| 5032 | t = t->next; |
| 5033 | } |
| 5034 | t->next = new_Token(NULL, TOK_OTHER, ":", 0); |
| 5035 | } |
| 5036 | l = new_Line(); |
| 5037 | l->first = copy_Token(label); |
| 5038 | l->next = ei->current; |
| 5039 | ei->current = l; |
| 5040 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5041 | } |
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 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5044 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5045 | istk->mmac_depth++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5046 | return true; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5049 | /* The function that actually does the error reporting */ |
| 5050 | static void verror(int severity, const char *fmt, va_list arg) |
| 5051 | { |
| 5052 | char buff[1024]; |
| 5053 | |
| 5054 | vsnprintf(buff, sizeof(buff), fmt, arg); |
| 5055 | |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5056 | if (istk && istk->mmac_depth > 0) { |
| 5057 | ExpInv *ei = istk->expansion; |
| 5058 | int lineno = ei->lineno; |
| 5059 | while (ei) { |
| 5060 | if (ei->type == EXP_MMACRO) |
| 5061 | break; |
| 5062 | lineno += ei->relno; |
| 5063 | ei = ei->prev; |
| 5064 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5065 | nasm_error(severity, "(%s:%d) %s", ei->def->name, |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5066 | lineno, buff); |
| 5067 | } else |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5068 | nasm_error(severity, "%s", buff); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5069 | } |
| 5070 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5071 | /* |
| 5072 | * Since preprocessor always operate only on the line that didn't |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5073 | * arrived yet, we should always use ERR_OFFBY1. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5074 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5075 | static void error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5076 | { |
| 5077 | va_list arg; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5078 | va_start(arg, fmt); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5079 | verror(severity, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5080 | va_end(arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5081 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5082 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5083 | /* |
| 5084 | * Because %else etc are evaluated in the state context |
| 5085 | * of the previous branch, errors might get lost with error(): |
| 5086 | * %if 0 ... %else trailing garbage ... %endif |
| 5087 | * So %else etc should report errors with this function. |
| 5088 | */ |
| 5089 | static void error_precond(int severity, const char *fmt, ...) |
| 5090 | { |
| 5091 | va_list arg; |
| 5092 | |
| 5093 | /* Only ignore the error if it's really in a dead branch */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5094 | if ((istk != NULL) && |
| 5095 | (istk->expansion != NULL) && |
| 5096 | (istk->expansion->type == EXP_IF) && |
| 5097 | (istk->expansion->def->state == COND_NEVER)) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5098 | return; |
| 5099 | |
| 5100 | va_start(arg, fmt); |
| 5101 | verror(severity, fmt, arg); |
| 5102 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5105 | static void |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5106 | pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5107 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5108 | Token *t; |
| 5109 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5110 | cstk = NULL; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5111 | istk = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5112 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5113 | src_set_fname(nasm_strdup(file)); |
| 5114 | src_set_linnum(0); |
| 5115 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5116 | if (!istk->fp) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5117 | error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5118 | file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5119 | defining = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5120 | finals = NULL; |
| 5121 | in_final = false; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5122 | nested_mac_count = 0; |
| 5123 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5124 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5125 | unique = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5126 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5127 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5128 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5129 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5130 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5131 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 5132 | do_predef = true; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5133 | list = listgen; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5134 | |
| 5135 | /* |
| 5136 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 5137 | * The caller, however, will also pass in 3 for preprocess-only so |
| 5138 | * we can set __PASS__ accordingly. |
| 5139 | */ |
| 5140 | pass = apass > 2 ? 2 : apass; |
| 5141 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5142 | dephead = deptail = deplist; |
| 5143 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5144 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 5145 | sl->next = NULL; |
| 5146 | strcpy(sl->str, file); |
| 5147 | *deptail = sl; |
| 5148 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5149 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5150 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5151 | /* |
| 5152 | * Define the __PASS__ macro. This is defined here unlike |
| 5153 | * all the other builtins, because it is special -- it varies between |
| 5154 | * passes. |
| 5155 | */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5156 | t = nasm_zalloc(sizeof(*t)); |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5157 | make_tok_num(t, apass); |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5158 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5161 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5162 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5163 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5164 | Token *tline; |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame] | 5165 | ExpDef *ed; |
| 5166 | ExpInv *ei; |
| 5167 | Line *l; |
| 5168 | int j; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5169 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5170 | while (1) { |
| 5171 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5172 | * Fetch a tokenized line, either from the expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5173 | * buffer or from the input file. |
| 5174 | */ |
| 5175 | tline = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5176 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5177 | while (1) { /* until we get a line we can use */ |
| 5178 | /* |
| 5179 | * Fetch a tokenized line from the expansion buffer |
| 5180 | */ |
| 5181 | if (istk->expansion != NULL) { |
| 5182 | ei = istk->expansion; |
| 5183 | if (ei->current != NULL) { |
| 5184 | if (ei->emitting == false) { |
| 5185 | ei->current = NULL; |
| 5186 | continue; |
| 5187 | } |
| 5188 | l = ei->current; |
| 5189 | ei->current = l->next; |
| 5190 | ei->lineno++; |
| 5191 | tline = copy_Token(l->first); |
| 5192 | if (((ei->type == EXP_REP) || |
| 5193 | (ei->type == EXP_MMACRO) || |
| 5194 | (ei->type == EXP_WHILE)) |
| 5195 | && (ei->def->nolist == false)) { |
| 5196 | char *p = detoken(tline, false); |
| 5197 | list->line(LIST_MACRO, p); |
| 5198 | nasm_free(p); |
| 5199 | } |
| 5200 | if (ei->linnum > -1) { |
| 5201 | src_set_linnum(src_get_linnum() + 1); |
| 5202 | } |
| 5203 | break; |
| 5204 | } else if ((ei->type == EXP_REP) && |
| 5205 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5206 | ei->def->cur_depth ++; |
| 5207 | ei->current = ei->def->line; |
| 5208 | ei->lineno = 0; |
| 5209 | continue; |
| 5210 | } else if ((ei->type == EXP_WHILE) && |
| 5211 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5212 | ei->current = ei->def->line; |
| 5213 | ei->lineno = 0; |
| 5214 | tline = copy_Token(ei->current->first); |
| 5215 | j = if_condition(tline, PP_WHILE); |
| 5216 | tline = NULL; |
| 5217 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 5218 | if (j == COND_IF_TRUE) { |
| 5219 | ei->current = ei->current->next; |
| 5220 | ei->def->cur_depth ++; |
| 5221 | } else { |
| 5222 | ei->emitting = false; |
| 5223 | ei->current = NULL; |
| 5224 | ei->def->cur_depth = ei->def->max_depth; |
| 5225 | } |
| 5226 | continue; |
| 5227 | } else { |
| 5228 | istk->expansion = ei->prev; |
| 5229 | ed = ei->def; |
| 5230 | if (ed != NULL) { |
| 5231 | if ((ei->emitting == true) && |
| 5232 | (ed->max_depth == DEADMAN_LIMIT) && |
| 5233 | (ed->cur_depth == DEADMAN_LIMIT) |
| 5234 | ) { |
| 5235 | error(ERR_FATAL, "runaway expansion detected, aborting"); |
| 5236 | } |
| 5237 | if (ed->cur_depth > 0) { |
| 5238 | ed->cur_depth --; |
Keith Kanios | 9412465 | 2010-12-18 11:47:28 -0600 | [diff] [blame] | 5239 | } else if (ed->type != EXP_MMACRO) { |
Keith Kanios | 6a7c3e9 | 2010-12-18 11:49:53 -0600 | [diff] [blame] | 5240 | expansions = ed->prev; |
| 5241 | free_expdef(ed); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5242 | } |
| 5243 | if ((ei->type == EXP_REP) || |
| 5244 | (ei->type == EXP_MMACRO) || |
| 5245 | (ei->type == EXP_WHILE)) { |
| 5246 | list->downlevel(LIST_MACRO); |
| 5247 | if (ei->type == EXP_MMACRO) { |
| 5248 | istk->mmac_depth--; |
| 5249 | } |
| 5250 | } |
| 5251 | } |
| 5252 | if (ei->linnum > -1) { |
| 5253 | src_set_linnum(ei->linnum); |
| 5254 | } |
| 5255 | free_expinv(ei); |
| 5256 | continue; |
| 5257 | } |
| 5258 | } |
| 5259 | |
| 5260 | /* |
| 5261 | * Read in line from input and tokenize |
| 5262 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5263 | line = read_line(); |
| 5264 | if (line) { /* from the current input file */ |
| 5265 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5266 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5267 | nasm_free(line); |
| 5268 | break; |
| 5269 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5270 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5271 | /* |
| 5272 | * The current file has ended; work down the istk |
| 5273 | */ |
| 5274 | { |
| 5275 | Include *i = istk; |
| 5276 | fclose(i->fp); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5277 | if (i->expansion != NULL) { |
| 5278 | error(ERR_FATAL, |
| 5279 | "end of file while still in an expansion"); |
| 5280 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5281 | /* only set line and file name if there's a next node */ |
| 5282 | if (i->next) { |
| 5283 | src_set_linnum(i->lineno); |
Cyrill Gorcunov | d34a108 | 2011-03-07 11:23:08 +0300 | [diff] [blame] | 5284 | nasm_free(src_set_fname(nasm_strdup(i->fname))); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5285 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5286 | if ((i->next == NULL) && (finals != NULL)) { |
| 5287 | in_final = true; |
| 5288 | ei = new_ExpInv(EXP_FINAL, NULL); |
| 5289 | ei->emitting = true; |
| 5290 | ei->current = finals; |
| 5291 | istk->expansion = ei; |
| 5292 | finals = NULL; |
| 5293 | continue; |
| 5294 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5295 | istk = i->next; |
| 5296 | list->downlevel(LIST_INCLUDE); |
| 5297 | nasm_free(i); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5298 | if (istk == NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5299 | if (finals != NULL) { |
| 5300 | in_final = true; |
| 5301 | } else { |
| 5302 | return NULL; |
| 5303 | } |
| 5304 | } |
| 5305 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5306 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5307 | } |
| 5308 | |
| 5309 | if (defining == NULL) { |
| 5310 | tline = expand_mmac_params(tline); |
| 5311 | } |
| 5312 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5313 | /* |
| 5314 | * Check the line to see if it's a preprocessor directive. |
| 5315 | */ |
| 5316 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5317 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5318 | } else if (defining != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5319 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5320 | * We're defining an expansion. We emit nothing at all, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5321 | * and just shove the tokenized line on to the definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5322 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5323 | if (defining->ignoring == false) { |
| 5324 | Line *l = new_Line(); |
| 5325 | l->first = tline; |
| 5326 | if (defining->line == NULL) { |
| 5327 | defining->line = l; |
| 5328 | defining->last = l; |
| 5329 | } else { |
| 5330 | defining->last->next = l; |
| 5331 | defining->last = l; |
| 5332 | } |
| 5333 | } else { |
Keith Kanios | 104803d | 2010-12-18 11:05:46 -0600 | [diff] [blame] | 5334 | free_tlist(tline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5335 | } |
| 5336 | defining->linecount++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5337 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5338 | } else if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5339 | (istk->expansion->emitting != true)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5340 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5341 | * We're in a non-emitting branch of an expansion. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5342 | * Emit nothing at all, not even a blank line: when we |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5343 | * emerge from the expansion we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5344 | * directive so we keep our place correctly. |
| 5345 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5346 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5347 | continue; |
| 5348 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5349 | tline = expand_smacro(tline); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5350 | if (expand_mmacro(tline) != true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5351 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5352 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5353 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5354 | line = detoken(tline, true); |
| 5355 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5356 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5357 | } else { |
| 5358 | continue; |
| 5359 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5360 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5361 | } |
| 5362 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5365 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5366 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5367 | if (defining != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5368 | error(ERR_NONFATAL, "end of file while still defining an expansion"); |
| 5369 | while (defining != NULL) { |
| 5370 | ExpDef *ed = defining; |
| 5371 | defining = ed->prev; |
| 5372 | free_expdef(ed); |
| 5373 | } |
| 5374 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5375 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5376 | while (cstk != NULL) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5377 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5378 | free_macros(); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5379 | while (istk != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5380 | Include *i = istk; |
| 5381 | istk = istk->next; |
| 5382 | fclose(i->fp); |
| 5383 | nasm_free(i->fname); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5384 | while (i->expansion != NULL) { |
| 5385 | ExpInv *ei = i->expansion; |
| 5386 | i->expansion = ei->prev; |
| 5387 | free_expinv(ei); |
| 5388 | } |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5389 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5390 | } |
| 5391 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5392 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5393 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5394 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5395 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5396 | free_llist(predef); |
| 5397 | delete_Blocks(); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5398 | while ((i = ipath)) { |
| 5399 | ipath = i->next; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 5400 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5401 | nasm_free(i); |
| 5402 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5403 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5406 | void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5407 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5408 | IncPath *i = nasm_zalloc(sizeof(IncPath)); |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5409 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5410 | if (path) |
| 5411 | i->path = nasm_strdup(path); |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5412 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5413 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5414 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5415 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5416 | j = j->next; |
| 5417 | j->next = i; |
| 5418 | } else { |
| 5419 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5420 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5421 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5422 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5423 | void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5424 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5425 | Token *inc, *space, *name; |
| 5426 | Line *l; |
| 5427 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5428 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5429 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5430 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5431 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5432 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5433 | l->next = predef; |
| 5434 | l->first = inc; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5435 | predef = l; |
| 5436 | } |
| 5437 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5438 | void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5439 | { |
| 5440 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5441 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5442 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5443 | |
| 5444 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5445 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5446 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5447 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5448 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5449 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5450 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5451 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5452 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5453 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5454 | l->next = predef; |
| 5455 | l->first = def; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5456 | predef = l; |
| 5457 | } |
| 5458 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5459 | void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5460 | { |
| 5461 | Token *def, *space; |
| 5462 | Line *l; |
| 5463 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5464 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5465 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5466 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5467 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5468 | l = new_Line(); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5469 | l->next = predef; |
| 5470 | l->first = def; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5471 | predef = l; |
| 5472 | } |
| 5473 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5474 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5475 | * This function is used to assist with "runtime" preprocessor |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5476 | * directives, e.g. pp_runtime("%define __BITS__ 64"); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5477 | * |
| 5478 | * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU |
| 5479 | * PASS A VALID STRING TO THIS FUNCTION!!!!! |
| 5480 | */ |
| 5481 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5482 | void pp_runtime(char *definition) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5483 | { |
| 5484 | Token *def; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5485 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5486 | def = tokenize(definition); |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5487 | if (do_directive(def) == NO_DIRECTIVE_FOUND) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5488 | free_tlist(def); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5489 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 5492 | void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5493 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5494 | extrastdmac = macros; |
| 5495 | } |
| 5496 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5497 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5498 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5499 | char numbuf[20]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5500 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5501 | tok->text = nasm_strdup(numbuf); |
| 5502 | tok->type = TOK_NUMBER; |
| 5503 | } |
| 5504 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5505 | Preproc nasmpp = { |
| 5506 | pp_reset, |
| 5507 | pp_getline, |
| 5508 | pp_cleanup |
| 5509 | }; |