H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2 | * |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 3 | * Copyright 1996-2011 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 6 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 10 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 17 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * preproc.c macro preprocessor for the Netwide Assembler |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 38 | /* Typical flow of text through preproc |
| 39 | * |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 40 | * pp_getline gets tokenized lines, either |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 41 | * |
| 42 | * from a macro expansion |
| 43 | * |
| 44 | * or |
| 45 | * { |
| 46 | * read_line gets raw text from stdmacpos, or predef, or current input file |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 47 | * tokenize converts to tokens |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 48 | * } |
| 49 | * |
| 50 | * expand_mmac_params is used to expand %1 etc., unless a macro is being |
| 51 | * defined or a false conditional is being processed |
| 52 | * (%0, %1, %+1, %-1, %%foo |
| 53 | * |
| 54 | * do_directive checks for directives |
| 55 | * |
| 56 | * expand_smacro is used to expand single line macros |
| 57 | * |
| 58 | * expand_mmacro is used to expand multi-line macros |
| 59 | * |
| 60 | * detoken is used to convert the line back to text |
| 61 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 62 | |
H. Peter Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 63 | #include "compiler.h" |
| 64 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 65 | #include <stdio.h> |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 66 | #include <stdarg.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 67 | #include <stdlib.h> |
| 68 | #include <stddef.h> |
| 69 | #include <string.h> |
| 70 | #include <ctype.h> |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 71 | #include <limits.h> |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 72 | #include <inttypes.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 73 | |
| 74 | #include "nasm.h" |
| 75 | #include "nasmlib.h" |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 76 | #include "preproc.h" |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 77 | #include "hashtbl.h" |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 78 | #include "quote.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 79 | #include "stdscan.h" |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 80 | #include "eval.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 81 | #include "tokens.h" |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 82 | #include "tables.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 83 | |
| 84 | typedef struct SMacro SMacro; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 85 | typedef struct ExpDef ExpDef; |
| 86 | typedef struct ExpInv ExpInv; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 87 | typedef struct Context Context; |
| 88 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 89 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 90 | typedef struct Line Line; |
| 91 | typedef struct Include Include; |
| 92 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 93 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 96 | * Note on the storage of both SMacro and MMacros: the hash table |
| 97 | * indexes them case-insensitively, and we then have to go through a |
| 98 | * linked list of potential case aliases (and, for MMacros, parameter |
| 99 | * ranges); this is to preserve the matching semantics of the earlier |
| 100 | * code. If the number of case aliases for a specific macro is a |
| 101 | * performance issue, you may want to reconsider your coding style. |
| 102 | */ |
| 103 | |
| 104 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 105 | * Store the definition of a single-line macro. |
| 106 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 107 | struct SMacro { |
Cyrill Gorcunov | 10083ae | 2011-07-17 20:06:20 +0400 | [diff] [blame] | 108 | SMacro *next; |
| 109 | char *name; |
| 110 | Token *expansion; |
| 111 | unsigned int nparam; |
| 112 | bool casesense; |
| 113 | bool in_progress; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 117 | * The context stack is composed of a linked list of these. |
| 118 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 119 | struct Context { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 120 | Context *next; |
| 121 | char *name; |
| 122 | struct hash_table localmac; |
| 123 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * This is the internal form which we break input lines up into. |
| 128 | * Typically stored in linked lists. |
| 129 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 130 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 131 | * necessarily used as-is, but is intended to denote the number of |
| 132 | * the substituted parameter. So in the definition |
| 133 | * |
| 134 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 135 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 136 | * the token representing `x' will have its type changed to |
| 137 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 138 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 139 | * |
| 140 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 141 | * which doesn't need quotes around it. Used in the pre-include |
| 142 | * mechanism as an alternative to trying to find a sensible type of |
| 143 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 144 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 145 | enum pp_token_type { |
| 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 { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 165 | Token *next; |
| 166 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 167 | union { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 168 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 169 | size_t len; /* scratch length field */ |
| 170 | } a; /* Auxiliary data */ |
| 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 { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 185 | Line *next; |
| 186 | Token *first; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 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 { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 211 | ExpDef *prev; /* previous definition */ |
| 212 | ExpDef *next; /* next in hash table */ |
| 213 | enum pp_exp_type type; /* expansion type */ |
| 214 | char *name; /* definition name */ |
| 215 | int nparam_min; |
| 216 | int nparam_max; |
| 217 | bool casesense; |
| 218 | bool plus; /* is the last parameter greedy? */ |
| 219 | bool nolist; /* is this expansion listing-inhibited? */ |
| 220 | Token *dlist; /* all defaults as one list */ |
| 221 | Token **defaults; /* parameter default pointers */ |
| 222 | int ndefs; /* number of default parameters */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 223 | |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 224 | int prepend; /* label prepend state */ |
| 225 | Line *label; |
| 226 | Line *line; |
| 227 | Line *last; |
| 228 | int linecount; /* number of lines within expansion */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 229 | |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 230 | int64_t def_depth; /* current number of definition pairs deep */ |
| 231 | int64_t cur_depth; /* current number of expansions */ |
| 232 | int64_t max_depth; /* maximum number of expansions allowed */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 233 | |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 234 | int state; /* condition state */ |
| 235 | bool ignoring; /* ignoring definition lines */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | /* |
| 239 | * Store the invocation of an expansion. |
| 240 | * |
| 241 | * The `prev' field is for the `istk->expansion` linked-list. |
| 242 | * |
| 243 | * When an expansion is being expanded, `params', `iline', `nparam', |
| 244 | * `paramlen', `rotate' and `unique' are local to the invocation. |
| 245 | */ |
| 246 | struct ExpInv { |
Cyrill Gorcunov | d57a031 | 2011-07-17 20:11:08 +0400 | [diff] [blame] | 247 | ExpInv *prev; /* previous invocation */ |
| 248 | ExpDef *def; /* pointer to expansion definition */ |
| 249 | char *name; /* invocation name */ |
| 250 | Line *label; /* pointer to label */ |
| 251 | char *label_text; /* pointer to label text */ |
| 252 | Line *current; /* pointer to current line in invocation */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 253 | |
Cyrill Gorcunov | d57a031 | 2011-07-17 20:11:08 +0400 | [diff] [blame] | 254 | Token **params; /* actual parameters */ |
| 255 | Token *iline; /* invocation line */ |
| 256 | int *paramlen; |
| 257 | unsigned int nparam; |
| 258 | unsigned int rotate; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 259 | |
Cyrill Gorcunov | d57a031 | 2011-07-17 20:11:08 +0400 | [diff] [blame] | 260 | uint64_t unique; |
| 261 | int lineno; /* current line number in expansion */ |
| 262 | int linnum; /* line number at invocation */ |
| 263 | int relno; /* relative line number at invocation */ |
| 264 | enum pp_exp_type type; /* expansion type */ |
| 265 | bool emitting; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 269 | * To handle an arbitrary level of file inclusion, we maintain a |
| 270 | * stack (ie linked list) of these things. |
| 271 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 272 | struct Include { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 273 | Include *next; |
| 274 | FILE *fp; |
| 275 | Cond *conds; |
| 276 | ExpInv *expansion; |
| 277 | char *fname; |
| 278 | int lineno; |
| 279 | int lineinc; |
| 280 | int mmac_depth; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 284 | * Include search path. This is simply a list of strings which get |
| 285 | * prepended, in turn, to the name of an include file, in an |
| 286 | * attempt to find the file if it's not in the current directory. |
| 287 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 288 | struct IncPath { |
Cyrill Gorcunov | f30cf73 | 2011-07-17 20:20:14 +0400 | [diff] [blame] | 289 | IncPath *next; |
| 290 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 294 | * Conditional assembly: we maintain a separate stack of these for |
| 295 | * each level of file inclusion. (The only reason we keep the |
| 296 | * stacks separate is to ensure that a stray `%endif' in a file |
| 297 | * included from within the true branch of a `%if' won't terminate |
| 298 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 299 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 300 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 301 | /* |
| 302 | * These states are for use just after %if or %elif: IF_TRUE |
| 303 | * means the condition has evaluated to truth so we are |
| 304 | * currently emitting, whereas IF_FALSE means we are not |
| 305 | * currently emitting but will start doing so if a %else comes |
| 306 | * up. In these states, all directives are admissible: %elif, |
| 307 | * %else and %endif. (And of course %if.) |
| 308 | */ |
| 309 | COND_IF_TRUE, COND_IF_FALSE, |
| 310 | /* |
| 311 | * These states come up after a %else: ELSE_TRUE means we're |
| 312 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 313 | * any %elif or %else will cause an error. |
| 314 | */ |
| 315 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
| 316 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 317 | * These states mean that we're not emitting now, and also that |
| 318 | * nothing until %endif will be emitted at all. COND_DONE is |
| 319 | * used when we've had our moment of emission |
| 320 | * and have now started seeing %elifs. COND_NEVER is used when |
| 321 | * the condition construct in question is contained within a |
| 322 | * non-emitting branch of a larger condition construct, |
| 323 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 324 | */ |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 325 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 326 | }; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 327 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 328 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 329 | * These defines are used as the possible return values for do_directive |
| 330 | */ |
| 331 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 332 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 333 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 334 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 335 | * This define sets the upper limit for smacro and expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 336 | */ |
| 337 | #define DEADMAN_LIMIT (1 << 20) |
| 338 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 339 | /* max reps */ |
| 340 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 341 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 342 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 343 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 344 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 345 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 346 | * ones, so we need a different enum... |
| 347 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 348 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 349 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 350 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 351 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 352 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 353 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 354 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 355 | 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] | 356 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 357 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 358 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 359 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 360 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 361 | 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] | 362 | 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] | 363 | }; |
| 364 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 365 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 366 | * conditional compilation directives. Using the NASM pre-processor does |
| 367 | * not work, so we look for them specifically from the following list and |
| 368 | * then jam in the equivalent NASM directive into the input stream. |
| 369 | */ |
| 370 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 371 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 372 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 373 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 374 | }; |
| 375 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 376 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 377 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 378 | "ifndef", "include", "local" |
| 379 | }; |
| 380 | |
| 381 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 382 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 383 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 384 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 385 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 386 | static Context *cstk; |
| 387 | static Include *istk; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 388 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 389 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 390 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 391 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 392 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 393 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 394 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 395 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 396 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 397 | |
| 398 | static ListGen *list; |
| 399 | |
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 | * The current set of expansion definitions we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 402 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 403 | static struct hash_table expdefs; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 404 | |
| 405 | /* |
| 406 | * The current set of single-line macros we have defined. |
| 407 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 408 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 409 | |
| 410 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 411 | * Linked List of all active expansion definitions |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 412 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 413 | struct ExpDef *expansions = NULL; |
| 414 | |
| 415 | /* |
| 416 | * The expansion we are currently defining |
| 417 | */ |
| 418 | static ExpDef *defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 419 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 420 | static uint64_t nested_mac_count; |
| 421 | static uint64_t nested_rep_count; |
| 422 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 423 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 424 | * Linked-list of lines to preprocess, prior to cleanup |
| 425 | */ |
| 426 | static Line *finals = NULL; |
| 427 | static bool in_final = false; |
| 428 | |
| 429 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 430 | * The number of macro parameters to allocate space for at a time. |
| 431 | */ |
| 432 | #define PARAM_DELTA 16 |
| 433 | |
| 434 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 435 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 436 | * 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] | 437 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 438 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 439 | |
| 440 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 441 | * The extra standard macros that come from the object format, if |
| 442 | * any. |
| 443 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 444 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 445 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 446 | |
| 447 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 448 | * Tokens are allocated in blocks to improve speed |
| 449 | */ |
| 450 | #define TOKEN_BLOCKSIZE 4096 |
| 451 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 452 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 453 | Blocks *next; |
| 454 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 458 | |
| 459 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 460 | * Forward declarations. |
| 461 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 462 | static Token *expand_mmac_params(Token * tline); |
| 463 | static Token *expand_smacro(Token * tline); |
| 464 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 465 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 466 | static void make_tok_num(Token * tok, int64_t val); |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 467 | static void error(int severity, const char *fmt, ...); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 468 | static void error_precond(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 469 | static void *new_Block(size_t size); |
| 470 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 471 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 472 | const char *text, int txtlen); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 473 | static Token *copy_Token(Token * tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 474 | static Token *delete_Token(Token * t); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 475 | static Line *new_Line(void); |
| 476 | static ExpDef *new_ExpDef(int exp_type); |
| 477 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 481 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 482 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 483 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 484 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 485 | #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] | 486 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 487 | /* |
| 488 | * A few helpers for single macros |
| 489 | */ |
| 490 | |
| 491 | /* We might be not smacro parameter at all */ |
| 492 | static bool is_smacro_param(Token *t) |
| 493 | { |
| 494 | return t->type >= TOK_SMAC_PARAM; |
| 495 | } |
| 496 | |
| 497 | /* smacro parameters are counted in a special way */ |
| 498 | static int smacro_get_param_idx(Token *t) |
| 499 | { |
| 500 | return t->type - TOK_SMAC_PARAM; |
| 501 | } |
| 502 | |
| 503 | /* encode smacro parameter index */ |
| 504 | static int smacro_set_param_idx(Token *t, unsigned int index) |
| 505 | { |
| 506 | return t->type = TOK_SMAC_PARAM + index; |
| 507 | } |
| 508 | |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 509 | #ifdef NASM_TRACE |
| 510 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 511 | #define stringify(x) #x |
| 512 | |
Cyrill Gorcunov | 9d1141a | 2011-06-26 23:07:35 +0400 | [diff] [blame] | 513 | #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] | 514 | #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] | 515 | #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] | 516 | |
| 517 | /* FIXME: we really need some compound type here instead of inplace code */ |
| 518 | static const char *nasm_get_tok_type_str(enum pp_token_type type) |
| 519 | { |
| 520 | #define SWITCH_TOK_NAME(type) \ |
| 521 | case (type): \ |
| 522 | return stringify(type) |
| 523 | |
| 524 | switch (type) { |
| 525 | SWITCH_TOK_NAME(TOK_NONE); |
| 526 | SWITCH_TOK_NAME(TOK_WHITESPACE); |
| 527 | SWITCH_TOK_NAME(TOK_COMMENT); |
| 528 | SWITCH_TOK_NAME(TOK_ID); |
| 529 | SWITCH_TOK_NAME(TOK_PREPROC_ID); |
| 530 | SWITCH_TOK_NAME(TOK_STRING); |
| 531 | SWITCH_TOK_NAME(TOK_NUMBER); |
| 532 | SWITCH_TOK_NAME(TOK_FLOAT); |
| 533 | SWITCH_TOK_NAME(TOK_SMAC_END); |
| 534 | SWITCH_TOK_NAME(TOK_OTHER); |
| 535 | SWITCH_TOK_NAME(TOK_INTERNAL_STRING); |
| 536 | SWITCH_TOK_NAME(TOK_PREPROC_Q); |
| 537 | SWITCH_TOK_NAME(TOK_PREPROC_QQ); |
| 538 | SWITCH_TOK_NAME(TOK_PASTE); |
| 539 | SWITCH_TOK_NAME(TOK_INDIRECT); |
| 540 | SWITCH_TOK_NAME(TOK_SMAC_PARAM); |
| 541 | SWITCH_TOK_NAME(TOK_MAX); |
| 542 | } |
| 543 | |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | 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] | 548 | { |
| 549 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 550 | if (token) { |
| 551 | Token *t; |
| 552 | list_for_each(t, token) { |
| 553 | if (t->text) |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 554 | printf("'%s'(%s) ", t->text, |
| 555 | nasm_get_tok_type_str(t->type)); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 556 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 557 | printf("\n\n"); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 561 | static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func) |
| 562 | { |
| 563 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 564 | if (token) { |
| 565 | Token *t; |
| 566 | list_for_each(t, token) |
| 567 | printf("%s", t->text ? t->text : " "); |
| 568 | printf("\n\n"); |
| 569 | } |
| 570 | } |
| 571 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 572 | #else |
| 573 | #define nasm_trace(msg, ...) |
| 574 | #define nasm_dump_token(t) |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 575 | #define nasm_dump_stream(t) |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 576 | #endif |
| 577 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 578 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 579 | * nasm_unquote with error if the string contains NUL characters. |
| 580 | * If the string contains NUL characters, issue an error and return |
| 581 | * the C len, i.e. truncate at the NUL. |
| 582 | */ |
| 583 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 584 | { |
| 585 | size_t len = nasm_unquote(qstr, NULL); |
| 586 | size_t clen = strlen(qstr); |
| 587 | |
| 588 | if (len != clen) |
| 589 | error(ERR_NONFATAL, "NUL character in `%s' directive", |
| 590 | pp_directives[directive]); |
| 591 | |
| 592 | return clen; |
| 593 | } |
| 594 | |
| 595 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 596 | * In-place reverse a list of tokens. |
| 597 | */ |
| 598 | static Token *reverse_tokens(Token *t) |
| 599 | { |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 600 | Token *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 | list_reverse(t, prev, next); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 603 | |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 604 | return t; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 608 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 609 | * front of them. We do it here because I could not find any other |
| 610 | * place to do it for the moment, and it is a hack (ideally it would |
| 611 | * be nice to be able to use the NASM pre-processor to do it). |
| 612 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 613 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 614 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 615 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 616 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 617 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 618 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 619 | |
| 620 | /* Binary search for the directive name */ |
| 621 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 622 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 623 | q = nasm_skip_word(p); |
| 624 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 625 | if (len) { |
| 626 | oldchar = p[len]; |
| 627 | p[len] = 0; |
| 628 | while (j - i > 1) { |
| 629 | k = (j + i) / 2; |
| 630 | m = nasm_stricmp(p, tasm_directives[k]); |
| 631 | if (m == 0) { |
| 632 | /* We have found a directive, so jam a % in front of it |
| 633 | * so that NASM will then recognise it as one if it's own. |
| 634 | */ |
| 635 | p[len] = oldchar; |
| 636 | len = strlen(p); |
| 637 | oldline = line; |
| 638 | line = nasm_malloc(len + 2); |
| 639 | line[0] = '%'; |
| 640 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 641 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 642 | * NASM does not recognise IFDIFI, so we convert |
| 643 | * it to %if 0. This is not used in NASM |
| 644 | * compatible code, but does need to parse for the |
| 645 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 646 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 647 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 648 | } else { |
| 649 | memcpy(line + 1, p, len + 1); |
| 650 | } |
| 651 | nasm_free(oldline); |
| 652 | return line; |
| 653 | } else if (m < 0) { |
| 654 | j = k; |
| 655 | } else |
| 656 | i = k; |
| 657 | } |
| 658 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 659 | } |
| 660 | return line; |
| 661 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 662 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 663 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 664 | * The pre-preprocessing stage... This function translates line |
| 665 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 666 | * flags') into NASM preprocessor line number indications (`%line |
| 667 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 668 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 669 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 670 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 671 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 672 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 673 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 674 | if (line[0] == '#' && line[1] == ' ') { |
| 675 | oldline = line; |
| 676 | fname = oldline + 2; |
| 677 | lineno = atoi(fname); |
| 678 | fname += strspn(fname, "0123456789 "); |
| 679 | if (*fname == '"') |
| 680 | fname++; |
| 681 | fnlen = strcspn(fname, "\""); |
| 682 | line = nasm_malloc(20 + fnlen); |
| 683 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 684 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 685 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 686 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 687 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 688 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 692 | * Free a linked list of tokens. |
| 693 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 694 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 695 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 696 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 697 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Free a linked list of lines. |
| 702 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 703 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 704 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 705 | Line *l, *tmp; |
| 706 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 707 | free_tlist(l->first); |
| 708 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 713 | * Free an ExpDef |
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 | static void free_expdef(ExpDef * ed) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 716 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 717 | nasm_free(ed->name); |
| 718 | free_tlist(ed->dlist); |
| 719 | nasm_free(ed->defaults); |
| 720 | free_llist(ed->line); |
| 721 | nasm_free(ed); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Free an ExpInv |
| 726 | */ |
| 727 | static void free_expinv(ExpInv * ei) |
| 728 | { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 729 | nasm_free(ei->name); |
| 730 | nasm_free(ei->label_text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 731 | nasm_free(ei); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 735 | * Free all currently defined macros, and free the hash tables |
| 736 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 737 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 738 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 739 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 740 | const char *key; |
| 741 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 742 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 743 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 744 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 745 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 746 | nasm_free(s->name); |
| 747 | free_tlist(s->expansion); |
| 748 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 749 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 750 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 751 | hash_free(smt); |
| 752 | } |
| 753 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 754 | static void free_expdef_table(struct hash_table *edt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 755 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 756 | ExpDef *ed, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 757 | const char *key; |
| 758 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 759 | |
| 760 | it = NULL; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 761 | while ((ed = hash_iterate(edt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 762 | nasm_free((void *)key); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 763 | list_for_each_safe(ed ,tmp, ed) |
| 764 | free_expdef(ed); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 765 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 766 | hash_free(edt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | static void free_macros(void) |
| 770 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 771 | free_smacro_table(&smacros); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 772 | free_expdef_table(&expdefs); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Initialize the hash tables |
| 777 | */ |
| 778 | static void init_macros(void) |
| 779 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 780 | hash_init(&smacros, HASH_LARGE); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 781 | hash_init(&expdefs, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 785 | * Pop the context stack. |
| 786 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 787 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 788 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 789 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 790 | |
| 791 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 792 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 793 | nasm_free(c->name); |
| 794 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 795 | } |
| 796 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 797 | /* |
| 798 | * Search for a key in the hash index; adding it if necessary |
| 799 | * (in which case we initialize the data pointer to NULL.) |
| 800 | */ |
| 801 | static void ** |
| 802 | hash_findi_add(struct hash_table *hash, const char *str) |
| 803 | { |
| 804 | struct hash_insert hi; |
| 805 | void **r; |
| 806 | char *strx; |
| 807 | |
| 808 | r = hash_findi(hash, str, &hi); |
| 809 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 810 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 811 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 812 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 813 | return hash_add(&hi, strx, NULL); |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Like hash_findi, but returns the data element rather than a pointer |
| 818 | * to it. Used only when not adding a new element, hence no third |
| 819 | * argument. |
| 820 | */ |
| 821 | static void * |
| 822 | hash_findix(struct hash_table *hash, const char *str) |
| 823 | { |
| 824 | void **p; |
| 825 | |
| 826 | p = hash_findi(hash, str, NULL); |
| 827 | return p ? *p : NULL; |
| 828 | } |
| 829 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 830 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 831 | * read line from standard macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 832 | * if there no more left -- return NULL |
| 833 | */ |
| 834 | static char *line_from_stdmac(void) |
| 835 | { |
| 836 | unsigned char c; |
| 837 | const unsigned char *p = stdmacpos; |
| 838 | char *line, *q; |
| 839 | size_t len = 0; |
| 840 | |
| 841 | if (!stdmacpos) |
| 842 | return NULL; |
| 843 | |
| 844 | while ((c = *p++)) { |
| 845 | if (c >= 0x80) |
| 846 | len += pp_directives_len[c - 0x80] + 1; |
| 847 | else |
| 848 | len++; |
| 849 | } |
| 850 | |
| 851 | line = nasm_malloc(len + 1); |
| 852 | q = line; |
| 853 | while ((c = *stdmacpos++)) { |
| 854 | if (c >= 0x80) { |
| 855 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 856 | q += pp_directives_len[c - 0x80]; |
| 857 | *q++ = ' '; |
| 858 | } else { |
| 859 | *q++ = c; |
| 860 | } |
| 861 | } |
| 862 | stdmacpos = p; |
| 863 | *q = '\0'; |
| 864 | |
| 865 | if (!*stdmacpos) { |
| 866 | /* This was the last of the standard macro chain... */ |
| 867 | stdmacpos = NULL; |
| 868 | if (any_extrastdmac) { |
| 869 | stdmacpos = extrastdmac; |
| 870 | any_extrastdmac = false; |
| 871 | } else if (do_predef) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 872 | ExpInv *ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 873 | Line *pd, *l; |
| 874 | Token *head, **tail, *t; |
| 875 | |
| 876 | /* |
| 877 | * Nasty hack: here we push the contents of |
| 878 | * `predef' on to the top-level expansion stack, |
| 879 | * since this is the most convenient way to |
| 880 | * implement the pre-include and pre-define |
| 881 | * features. |
| 882 | */ |
| 883 | list_for_each(pd, predef) { |
| 884 | head = NULL; |
| 885 | tail = &head; |
| 886 | list_for_each(t, pd->first) { |
| 887 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 888 | tail = &(*tail)->next; |
| 889 | } |
| 890 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 891 | l = new_Line(); |
| 892 | l->first = head; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 893 | ei = new_ExpInv(EXP_PREDEF, NULL); |
| 894 | ei->current = l; |
| 895 | ei->emitting = true; |
| 896 | ei->prev = istk->expansion; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 897 | istk->expansion = ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 898 | } |
| 899 | do_predef = false; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return line; |
| 904 | } |
| 905 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 906 | #define BUF_DELTA 512 |
| 907 | /* |
| 908 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 909 | * at the end of the line read, and handling spurious ^Zs. Will |
| 910 | * return lines from the standard macro set if this has not already |
| 911 | * been done. |
| 912 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 913 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 914 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 915 | char *buffer, *p, *q; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 916 | int bufsize, continued_count; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 917 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 918 | /* |
| 919 | * standart macros set (predefined) goes first |
| 920 | */ |
| 921 | p = line_from_stdmac(); |
| 922 | if (p) |
| 923 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 924 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 925 | /* |
| 926 | * regular read from a file |
| 927 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 928 | bufsize = BUF_DELTA; |
| 929 | buffer = nasm_malloc(BUF_DELTA); |
| 930 | p = buffer; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 931 | continued_count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 932 | while (1) { |
| 933 | q = fgets(p, bufsize - (p - buffer), istk->fp); |
| 934 | if (!q) |
| 935 | break; |
| 936 | p += strlen(p); |
| 937 | if (p > buffer && p[-1] == '\n') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 938 | /* |
| 939 | * Convert backslash-CRLF line continuation sequences into |
| 940 | * nothing at all (for DOS and Windows) |
| 941 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 942 | if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { |
| 943 | p -= 3; |
| 944 | *p = 0; |
| 945 | continued_count++; |
| 946 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 947 | /* |
| 948 | * Also convert backslash-LF line continuation sequences into |
| 949 | * nothing at all (for Unix) |
| 950 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 951 | else if (((p - 1) > buffer) && (p[-2] == '\\')) { |
| 952 | p -= 2; |
| 953 | *p = 0; |
| 954 | continued_count++; |
| 955 | } else { |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | if (p - buffer > bufsize - 10) { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 960 | int32_t offset = p - buffer; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 961 | bufsize += BUF_DELTA; |
| 962 | buffer = nasm_realloc(buffer, bufsize); |
| 963 | p = buffer + offset; /* prevent stale-pointer problems */ |
| 964 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 965 | } |
| 966 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 967 | if (!q && p == buffer) { |
| 968 | nasm_free(buffer); |
| 969 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 972 | src_set_linnum(src_get_linnum() + istk->lineinc + |
| 973 | (continued_count * istk->lineinc)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 974 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 975 | /* |
| 976 | * Play safe: remove CRs as well as LFs, if any of either are |
| 977 | * present at the end of the line. |
| 978 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 979 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 980 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 981 | |
| 982 | /* |
| 983 | * Handle spurious ^Z, which may be inserted into source files |
| 984 | * by some file transfer utilities. |
| 985 | */ |
| 986 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 987 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 988 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 989 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 990 | return buffer; |
| 991 | } |
| 992 | |
| 993 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 994 | * 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] | 995 | * don't need to parse the value out of e.g. numeric tokens: we |
| 996 | * simply split one string into many. |
| 997 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 998 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 999 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 1000 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1001 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1002 | Token *list = NULL; |
| 1003 | Token *t, **tail = &list; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1004 | bool verbose = true; |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 1005 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1006 | nasm_trace("Tokenize for '%s'", line); |
| 1007 | |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1008 | if ((defining != NULL) && (defining->ignoring == true)) { |
| 1009 | verbose = false; |
| 1010 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1011 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1012 | while (*line) { |
| 1013 | p = line; |
| 1014 | if (*p == '%') { |
| 1015 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1016 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 1017 | p++; |
| 1018 | type = TOK_PASTE; |
| 1019 | } else if (nasm_isdigit(*p) || |
| 1020 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1021 | do { |
| 1022 | p++; |
| 1023 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1024 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1025 | type = TOK_PREPROC_ID; |
| 1026 | } else if (*p == '{') { |
| 1027 | p++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1028 | while (*p && *p != '}') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1029 | p[-1] = *p; |
| 1030 | p++; |
| 1031 | } |
| 1032 | p[-1] = '\0'; |
| 1033 | if (*p) |
| 1034 | p++; |
| 1035 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1036 | } else if (*p == '[') { |
| 1037 | int lvl = 1; |
| 1038 | line += 2; /* Skip the leading %[ */ |
| 1039 | p++; |
| 1040 | while (lvl && (c = *p++)) { |
| 1041 | switch (c) { |
| 1042 | case ']': |
| 1043 | lvl--; |
| 1044 | break; |
| 1045 | case '%': |
| 1046 | if (*p == '[') |
| 1047 | lvl++; |
| 1048 | break; |
| 1049 | case '\'': |
| 1050 | case '\"': |
| 1051 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 1052 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1053 | break; |
| 1054 | default: |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | p--; |
| 1059 | if (*p) |
| 1060 | *p++ = '\0'; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1061 | if (lvl && verbose) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1062 | error(ERR_NONFATAL, "unterminated %[ construct"); |
| 1063 | type = TOK_INDIRECT; |
| 1064 | } else if (*p == '?') { |
| 1065 | type = TOK_PREPROC_Q; /* %? */ |
| 1066 | p++; |
| 1067 | if (*p == '?') { |
| 1068 | type = TOK_PREPROC_QQ; /* %?? */ |
| 1069 | p++; |
| 1070 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1071 | } else if (*p == '!') { |
| 1072 | type = TOK_PREPROC_ID; |
| 1073 | p++; |
| 1074 | if (isidchar(*p)) { |
| 1075 | do { |
| 1076 | p++; |
| 1077 | } while (isidchar(*p)); |
| 1078 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 1079 | p = nasm_skip_string(p); |
| 1080 | if (*p) |
| 1081 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1082 | else if(verbose) |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1083 | error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string"); |
| 1084 | } else { |
| 1085 | /* %! without string or identifier */ |
| 1086 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1087 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1088 | } else if (isidchar(*p) || |
| 1089 | ((*p == '!' || *p == '%' || *p == '$') && |
| 1090 | isidchar(p[1]))) { |
| 1091 | do { |
| 1092 | p++; |
| 1093 | } |
| 1094 | while (isidchar(*p)); |
| 1095 | type = TOK_PREPROC_ID; |
| 1096 | } else { |
| 1097 | type = TOK_OTHER; |
| 1098 | if (*p == '%') |
| 1099 | p++; |
| 1100 | } |
| 1101 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1102 | type = TOK_ID; |
| 1103 | p++; |
| 1104 | while (*p && isidchar(*p)) |
| 1105 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1106 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1107 | /* |
| 1108 | * A string token. |
| 1109 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1110 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1111 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1112 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1113 | if (*p) { |
| 1114 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1115 | } else if(verbose) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1116 | error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1117 | /* Handling unterminated strings by UNV */ |
| 1118 | /* type = -1; */ |
| 1119 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1120 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1121 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1122 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1123 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1124 | bool is_hex = false; |
| 1125 | bool is_float = false; |
| 1126 | bool has_e = false; |
| 1127 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1128 | |
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 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1131 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1132 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1133 | if (*p == '$') { |
| 1134 | p++; |
| 1135 | is_hex = true; |
| 1136 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1137 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1138 | for (;;) { |
| 1139 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1140 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1141 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1142 | has_e = true; |
| 1143 | if (*p == '+' || *p == '-') { |
| 1144 | /* |
| 1145 | * e can only be followed by +/- if it is either a |
| 1146 | * prefixed hex number or a floating-point number |
| 1147 | */ |
| 1148 | p++; |
| 1149 | is_float = true; |
| 1150 | } |
| 1151 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1152 | is_hex = true; |
| 1153 | } else if (c == 'P' || c == 'p') { |
| 1154 | is_float = true; |
| 1155 | if (*p == '+' || *p == '-') |
| 1156 | p++; |
| 1157 | } else if (isnumchar(c) || c == '_') |
| 1158 | ; /* just advance */ |
| 1159 | else if (c == '.') { |
| 1160 | /* |
| 1161 | * we need to deal with consequences of the legacy |
| 1162 | * parser, like "1.nolist" being two tokens |
| 1163 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1164 | * a shot for now. In the future, we probably need |
| 1165 | * a flex-based scanner with proper pattern matching |
| 1166 | * to do it as well as it can be done. Nothing in |
| 1167 | * the world is going to help the person who wants |
| 1168 | * 0x123.p16 interpreted as two tokens, though. |
| 1169 | */ |
| 1170 | r = p; |
| 1171 | while (*r == '_') |
| 1172 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1173 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1174 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1175 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1176 | (*r == 'p' || *r == 'P')) { |
| 1177 | p = r; |
| 1178 | is_float = true; |
| 1179 | } else |
| 1180 | break; /* Terminate the token */ |
| 1181 | } else |
| 1182 | break; |
| 1183 | } |
| 1184 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1185 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1186 | if (p == line+1 && *line == '$') { |
| 1187 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1188 | } else { |
| 1189 | if (has_e && !is_hex) { |
| 1190 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1191 | is_float = true; |
| 1192 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1193 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1194 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1195 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1196 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1197 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1198 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | * Whitespace just before end-of-line is discarded by |
| 1201 | * pretending it's a comment; whitespace just before a |
| 1202 | * comment gets lumped into the comment. |
| 1203 | */ |
| 1204 | if (!*p || *p == ';') { |
| 1205 | type = TOK_COMMENT; |
| 1206 | while (*p) |
| 1207 | p++; |
| 1208 | } |
| 1209 | } else if (*p == ';') { |
| 1210 | type = TOK_COMMENT; |
| 1211 | while (*p) |
| 1212 | p++; |
| 1213 | } else { |
| 1214 | /* |
| 1215 | * Anything else is an operator of some kind. We check |
| 1216 | * for all the double-character operators (>>, <<, //, |
| 1217 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1218 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1219 | */ |
| 1220 | type = TOK_OTHER; |
| 1221 | if ((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[0] == '|' && p[1] == '|') || |
| 1231 | (p[0] == '^' && p[1] == '^')) { |
| 1232 | p++; |
| 1233 | } |
| 1234 | p++; |
| 1235 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1236 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1237 | /* Handling unterminated string by UNV */ |
| 1238 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1239 | { |
| 1240 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1241 | t->text[p-line] = *line; |
| 1242 | tail = &t->next; |
| 1243 | } |
| 1244 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1245 | if (type != TOK_COMMENT) { |
| 1246 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1247 | tail = &t->next; |
| 1248 | } |
| 1249 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1250 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1251 | |
| 1252 | nasm_dump_token(list); |
| 1253 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1254 | return list; |
| 1255 | } |
| 1256 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1257 | /* |
| 1258 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1259 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1260 | * deleted only all at once by the delete_Blocks function. |
| 1261 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1262 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1263 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1264 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1265 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1266 | /* first, get to the end of the linked list */ |
| 1267 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1268 | b = b->next; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1269 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1270 | /* now allocate the requested chunk */ |
| 1271 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1272 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1273 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1274 | b->next = nasm_zalloc(sizeof(Blocks)); |
| 1275 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1276 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * this function deletes all managed blocks of memory |
| 1281 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1282 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1283 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1284 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1285 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1286 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1287 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1288 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1289 | * free it. |
| 1290 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1291 | while (b) { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 1292 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1293 | a = b; |
| 1294 | b = b->next; |
| 1295 | if (a != &blocks) |
| 1296 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1297 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1298 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1299 | |
| 1300 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1301 | * 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] | 1302 | * 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] | 1303 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1304 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1305 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1306 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1307 | { |
| 1308 | Token *t; |
| 1309 | int i; |
| 1310 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1311 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1312 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1313 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1314 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1315 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1316 | } |
| 1317 | t = freeTokens; |
| 1318 | freeTokens = t->next; |
| 1319 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1320 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1321 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1322 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1323 | t->text = NULL; |
| 1324 | } else { |
| 1325 | if (txtlen == 0) |
| 1326 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1327 | t->text = nasm_malloc(txtlen+1); |
| 1328 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1329 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1330 | } |
| 1331 | return t; |
| 1332 | } |
| 1333 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1334 | static Token *copy_Token(Token * tline) |
| 1335 | { |
| 1336 | Token *t, *tt, *first = NULL, *prev = NULL; |
| 1337 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1338 | for (tt = tline; tt != NULL; tt = tt->next) { |
| 1339 | if (!freeTokens) { |
| 1340 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1341 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1342 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1343 | freeTokens[i].next = NULL; |
| 1344 | } |
| 1345 | t = freeTokens; |
| 1346 | freeTokens = t->next; |
| 1347 | t->next = NULL; |
| 1348 | t->text = tt->text ? nasm_strdup(tt->text) : NULL; |
| 1349 | t->a.mac = tt->a.mac; |
| 1350 | t->a.len = tt->a.len; |
| 1351 | t->type = tt->type; |
| 1352 | if (prev != NULL) { |
| 1353 | prev->next = t; |
| 1354 | } else { |
| 1355 | first = t; |
| 1356 | } |
| 1357 | prev = t; |
| 1358 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1359 | return first; |
| 1360 | } |
| 1361 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1362 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1363 | { |
| 1364 | Token *next = t->next; |
| 1365 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1366 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1367 | freeTokens = t; |
| 1368 | return next; |
| 1369 | } |
| 1370 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1371 | /* |
| 1372 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1373 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1374 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1375 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1376 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1377 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1378 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1379 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1380 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1381 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1382 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1383 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1384 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1385 | char *v; |
| 1386 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1387 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1388 | v = t->text + 2; |
| 1389 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1390 | size_t len = nasm_unquote(v, NULL); |
| 1391 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1392 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1393 | if (len != clen) { |
| 1394 | error(ERR_NONFATAL | ERR_PASS1, |
| 1395 | "NUL character in %! string"); |
| 1396 | v = NULL; |
| 1397 | } |
| 1398 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1399 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1400 | if (v) { |
| 1401 | char *p = getenv(v); |
| 1402 | if (!p) { |
| 1403 | error(ERR_NONFATAL | ERR_PASS1, |
| 1404 | "nonexistent environment variable `%s'", v); |
| 1405 | p = ""; |
| 1406 | } |
| 1407 | t->text = nasm_strdup(p); |
| 1408 | } |
| 1409 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1410 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1411 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1412 | /* Expand local macros here and not during preprocessing */ |
| 1413 | if (expand_locals && |
| 1414 | t->type == TOK_PREPROC_ID && t->text && |
| 1415 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1416 | const char *q; |
| 1417 | char *p; |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1418 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1419 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1420 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1421 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1422 | p = nasm_strcat(buffer, q); |
| 1423 | nasm_free(t->text); |
| 1424 | t->text = p; |
| 1425 | } |
| 1426 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1427 | |
| 1428 | /* Expand %? and %?? directives */ |
Keith Kanios | 3136d48 | 2010-11-13 09:34:34 -0600 | [diff] [blame] | 1429 | if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1430 | ((t->type == TOK_PREPROC_Q) || |
| 1431 | (t->type == TOK_PREPROC_QQ))) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1432 | ExpInv *ei; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1433 | for (ei = istk->expansion; ei != NULL; ei = ei->prev){ |
| 1434 | if (ei->type == EXP_MMACRO) { |
| 1435 | nasm_free(t->text); |
| 1436 | if (t->type == TOK_PREPROC_Q) { |
| 1437 | t->text = nasm_strdup(ei->name); |
| 1438 | } else { |
| 1439 | t->text = nasm_strdup(ei->def->name); |
| 1440 | } |
| 1441 | break; |
| 1442 | } |
| 1443 | } |
| 1444 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1445 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1446 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1447 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1448 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1449 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1450 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1451 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1452 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1453 | |
| 1454 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1455 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1456 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1457 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1458 | q = t->text; |
| 1459 | while (*q) |
| 1460 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1461 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1462 | } |
| 1463 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1464 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1465 | return line; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1469 | * Initialize a new Line |
| 1470 | */ |
Cyrill Gorcunov | 29cb0bb | 2010-11-11 11:19:43 +0300 | [diff] [blame] | 1471 | static inline Line *new_Line(void) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1472 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1473 | return (Line *)nasm_zalloc(sizeof(Line)); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | |
| 1477 | /* |
| 1478 | * Initialize a new Expansion Definition |
| 1479 | */ |
| 1480 | static ExpDef *new_ExpDef(int exp_type) |
| 1481 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1482 | ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef)); |
| 1483 | ed->type = exp_type; |
| 1484 | ed->casesense = true; |
| 1485 | ed->state = COND_NEVER; |
| 1486 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1487 | return ed; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | /* |
| 1492 | * Initialize a new Expansion Instance |
| 1493 | */ |
| 1494 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed) |
| 1495 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1496 | ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv)); |
| 1497 | ei->type = exp_type; |
| 1498 | ei->def = ed; |
| 1499 | ei->unique = ++unique; |
| 1500 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1501 | if ((istk->mmac_depth < 1) && |
| 1502 | (istk->expansion == NULL) && |
| 1503 | (ed != NULL) && |
| 1504 | (ed->type != EXP_MMACRO) && |
| 1505 | (ed->type != EXP_REP) && |
| 1506 | (ed->type != EXP_WHILE)) { |
| 1507 | ei->linnum = src_get_linnum(); |
| 1508 | src_set_linnum(ei->linnum - ed->linecount - 1); |
| 1509 | } else { |
| 1510 | ei->linnum = -1; |
| 1511 | } |
| 1512 | if ((istk->expansion == NULL) || |
| 1513 | (ei->type == EXP_MMACRO)) { |
| 1514 | ei->relno = 0; |
| 1515 | } else { |
| 1516 | ei->relno = istk->expansion->lineno; |
| 1517 | if (ed != NULL) { |
| 1518 | ei->relno -= (ed->linecount + 1); |
| 1519 | } |
| 1520 | } |
| 1521 | return ei; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1525 | * A scanner, suitable for use by the expression evaluator, which |
| 1526 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1527 | * the first token in the line to be passed in as its private_data |
| 1528 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1529 | * |
| 1530 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1531 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1532 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1533 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1534 | Token **tlineptr = private_data; |
| 1535 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1536 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1537 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1538 | do { |
| 1539 | tline = *tlineptr; |
| 1540 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1541 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1542 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1543 | |
| 1544 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1545 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1546 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1547 | tokval->t_charptr = tline->text; |
| 1548 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1549 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1550 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1551 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1552 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1553 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1554 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1555 | p = tokval->t_charptr = tline->text; |
| 1556 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1557 | tokval->t_charptr++; |
| 1558 | return tokval->t_type = TOKEN_ID; |
| 1559 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1560 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1561 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1562 | if (r >= p+MAX_KEYWORD) |
| 1563 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1564 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1565 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1566 | *s = '\0'; |
| 1567 | /* right, so we have an identifier sitting in temp storage. now, |
| 1568 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1569 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1572 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1573 | bool rn_error; |
| 1574 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1575 | tokval->t_charptr = tline->text; |
| 1576 | if (rn_error) |
| 1577 | return tokval->t_type = TOKEN_ERRNUM; |
| 1578 | else |
| 1579 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1580 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1581 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1582 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1583 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1586 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1587 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1588 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1589 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1590 | tokval->t_charptr = tline->text; |
| 1591 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1592 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1593 | if (ep[0] != bq || ep[1] != '\0') |
| 1594 | return tokval->t_type = TOKEN_ERRSTR; |
| 1595 | else |
| 1596 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1599 | if (tline->type == TOK_OTHER) { |
| 1600 | if (!strcmp(tline->text, "<<")) |
| 1601 | return tokval->t_type = TOKEN_SHL; |
| 1602 | if (!strcmp(tline->text, ">>")) |
| 1603 | return tokval->t_type = TOKEN_SHR; |
| 1604 | if (!strcmp(tline->text, "//")) |
| 1605 | return tokval->t_type = TOKEN_SDIV; |
| 1606 | if (!strcmp(tline->text, "%%")) |
| 1607 | return tokval->t_type = TOKEN_SMOD; |
| 1608 | if (!strcmp(tline->text, "==")) |
| 1609 | return tokval->t_type = TOKEN_EQ; |
| 1610 | if (!strcmp(tline->text, "<>")) |
| 1611 | return tokval->t_type = TOKEN_NE; |
| 1612 | if (!strcmp(tline->text, "!=")) |
| 1613 | return tokval->t_type = TOKEN_NE; |
| 1614 | if (!strcmp(tline->text, "<=")) |
| 1615 | return tokval->t_type = TOKEN_LE; |
| 1616 | if (!strcmp(tline->text, ">=")) |
| 1617 | return tokval->t_type = TOKEN_GE; |
| 1618 | if (!strcmp(tline->text, "&&")) |
| 1619 | return tokval->t_type = TOKEN_DBL_AND; |
| 1620 | if (!strcmp(tline->text, "^^")) |
| 1621 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1622 | if (!strcmp(tline->text, "||")) |
| 1623 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | * We have no other options: just return the first character of |
| 1628 | * the token text. |
| 1629 | */ |
| 1630 | return tokval->t_type = tline->text[0]; |
| 1631 | } |
| 1632 | |
| 1633 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1634 | * Compare a string to the name of an existing macro; this is a |
| 1635 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1636 | * depending on the value of the `casesense' parameter. |
| 1637 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1638 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1639 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1640 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1644 | * Compare a string to the name of an existing macro; this is a |
| 1645 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1646 | * depending on the value of the `casesense' parameter. |
| 1647 | */ |
| 1648 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1649 | { |
| 1650 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1651 | } |
| 1652 | |
| 1653 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1654 | * Return the Context structure associated with a %$ token. Return |
| 1655 | * NULL, having _already_ reported an error condition, if the |
| 1656 | * context stack isn't deep enough for the supplied number of $ |
| 1657 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1658 | * |
| 1659 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1660 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1661 | */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1662 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1663 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1664 | Context *ctx; |
| 1665 | int i; |
| 1666 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1667 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1668 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1669 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1670 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1671 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1672 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1673 | if (!cstk) { |
| 1674 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
| 1675 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1678 | name += 2; |
| 1679 | ctx = cstk; |
| 1680 | i = 0; |
| 1681 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1682 | name++; |
| 1683 | i++; |
| 1684 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1685 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1686 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1687 | if (!ctx) { |
| 1688 | error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1689 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1690 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1691 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1692 | |
| 1693 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1694 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1695 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1696 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1700 | * Check to see if a file is already in a string list |
| 1701 | */ |
| 1702 | static bool in_list(const StrList *list, const char *str) |
| 1703 | { |
| 1704 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1705 | if (!strcmp(list->str, str)) |
| 1706 | return true; |
| 1707 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1708 | } |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1713 | * Open an include file. This routine must always return a valid |
| 1714 | * file pointer if it returns - it's responsible for throwing an |
| 1715 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1716 | * the include path one by one until it finds the file or reaches |
| 1717 | * the end of the path. |
| 1718 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1719 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1720 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1721 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1722 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1723 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1724 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1725 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1726 | size_t prefix_len = 0; |
| 1727 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1728 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1729 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1730 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | 6f38fe6 | 2010-11-11 11:32:16 +0300 | [diff] [blame] | 1731 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1732 | memcpy(sl->str, prefix, prefix_len); |
| 1733 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1734 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1735 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1736 | **dtail = sl; |
| 1737 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1738 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1739 | nasm_free(sl); |
| 1740 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1741 | if (fp) |
| 1742 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1743 | if (!ip) { |
| 1744 | if (!missing_ok) |
| 1745 | break; |
| 1746 | prefix = NULL; |
| 1747 | } else { |
| 1748 | prefix = ip->path; |
| 1749 | ip = ip->next; |
| 1750 | } |
| 1751 | if (prefix) { |
| 1752 | prefix_len = strlen(prefix); |
| 1753 | } else { |
| 1754 | /* -MG given and file not found */ |
| 1755 | if (dhead && !in_list(*dhead, file)) { |
| 1756 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1757 | sl->next = NULL; |
| 1758 | strcpy(sl->str, file); |
| 1759 | **dtail = sl; |
| 1760 | *dtail = &sl->next; |
| 1761 | } |
| 1762 | return NULL; |
| 1763 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1764 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1765 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1766 | error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1767 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1771 | * 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] | 1772 | * 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] | 1773 | * return true if _any_ single-line macro of that name is defined. |
| 1774 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1775 | * `nparam' or no parameters is defined. |
| 1776 | * |
| 1777 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1778 | * defined, or nparam is -1, the address of the definition structure |
| 1779 | * 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] | 1780 | * is NULL, no action will be taken regarding its contents, and no |
| 1781 | * error will occur. |
| 1782 | * |
| 1783 | * Note that this is also called with nparam zero to resolve |
| 1784 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1785 | * |
| 1786 | * If you already know which context macro belongs to, you can pass |
| 1787 | * the context pointer as first parameter; if you won't but name begins |
| 1788 | * with %$ the context will be automatically computed. If all_contexts |
| 1789 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1790 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1791 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1792 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1793 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1794 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1795 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1796 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1797 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1798 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1799 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1800 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1801 | if (cstk) |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1802 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1803 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1804 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1805 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1806 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1807 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1808 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1809 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1810 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1811 | while (m) { |
| 1812 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1813 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1814 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1815 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1816 | *defn = m; |
| 1817 | else |
| 1818 | *defn = NULL; |
| 1819 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1820 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1821 | } |
| 1822 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1823 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1824 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1825 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * Count and mark off the parameters in a multi-line macro call. |
| 1830 | * This is called both from within the multi-line macro expansion |
| 1831 | * code, and also to mark off the default parameters when provided |
| 1832 | * in a %macro definition line. |
| 1833 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1834 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1835 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1836 | int paramsize, brace; |
| 1837 | |
| 1838 | *nparam = paramsize = 0; |
| 1839 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1840 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1841 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1842 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1843 | paramsize += PARAM_DELTA; |
| 1844 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1845 | } |
| 1846 | skip_white_(t); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1847 | brace = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1848 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1849 | brace = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1850 | (*params)[(*nparam)++] = t; |
| 1851 | while (tok_isnt_(t, brace ? "}" : ",")) |
| 1852 | t = t->next; |
| 1853 | if (t) { /* got a comma/brace */ |
| 1854 | t = t->next; |
| 1855 | if (brace) { |
| 1856 | /* |
| 1857 | * Now we've found the closing brace, look further |
| 1858 | * for the comma. |
| 1859 | */ |
| 1860 | skip_white_(t); |
| 1861 | if (tok_isnt_(t, ",")) { |
| 1862 | error(ERR_NONFATAL, |
| 1863 | "braces do not enclose all of macro parameter"); |
| 1864 | while (tok_isnt_(t, ",")) |
| 1865 | t = t->next; |
| 1866 | } |
| 1867 | if (t) |
| 1868 | t = t->next; /* eat the comma */ |
| 1869 | } |
| 1870 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1875 | * Determine whether one of the various `if' conditions is true or |
| 1876 | * not. |
| 1877 | * |
| 1878 | * We must free the tline we get passed. |
| 1879 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1880 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1881 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1882 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1883 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1884 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1885 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1886 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1887 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1888 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1889 | |
| 1890 | origline = tline; |
| 1891 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1892 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1893 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1894 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1895 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1896 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1897 | if (!tline) |
| 1898 | break; |
| 1899 | if (tline->type != TOK_ID) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1900 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1901 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1902 | free_tlist(origline); |
| 1903 | return -1; |
| 1904 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1905 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1906 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1907 | tline = tline->next; |
| 1908 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1909 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1910 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1911 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1912 | j = false; /* have we matched yet? */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1913 | while (tline) { |
| 1914 | skip_white_(tline); |
| 1915 | if (!tline || (tline->type != TOK_ID && |
| 1916 | (tline->type != TOK_PREPROC_ID || |
| 1917 | tline->text[1] != '$'))) { |
| 1918 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1919 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1920 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1921 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1922 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1923 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1924 | tline = tline->next; |
| 1925 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1926 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1927 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1928 | case PPC_IFENV: |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1929 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1930 | j = false; /* have we matched yet? */ |
| 1931 | while (tline) { |
| 1932 | skip_white_(tline); |
| 1933 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1934 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1935 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1936 | tline->text[1] != '!'))) { |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1937 | error(ERR_NONFATAL, |
| 1938 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1939 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1940 | goto fail; |
| 1941 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1942 | p = tline->text; |
| 1943 | if (tline->type == TOK_PREPROC_ID) |
| 1944 | p += 2; /* Skip leading %! */ |
| 1945 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1946 | nasm_unquote_cstr(p, ct); |
| 1947 | if (getenv(p)) |
| 1948 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1949 | tline = tline->next; |
| 1950 | } |
| 1951 | break; |
| 1952 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1953 | case PPC_IFIDN: |
| 1954 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1955 | tline = expand_smacro(tline); |
| 1956 | t = tt = tline; |
| 1957 | while (tok_isnt_(tt, ",")) |
| 1958 | tt = tt->next; |
| 1959 | if (!tt) { |
| 1960 | error(ERR_NONFATAL, |
| 1961 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1962 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1963 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1964 | } |
| 1965 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1966 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1967 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1968 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1969 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1970 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1971 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1972 | } |
| 1973 | if (t->type == TOK_WHITESPACE) { |
| 1974 | t = t->next; |
| 1975 | continue; |
| 1976 | } |
| 1977 | if (tt->type == TOK_WHITESPACE) { |
| 1978 | tt = tt->next; |
| 1979 | continue; |
| 1980 | } |
| 1981 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1982 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1983 | break; |
| 1984 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1985 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1986 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1987 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1988 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1989 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1990 | if (l1 != l2) { |
| 1991 | j = false; |
| 1992 | break; |
| 1993 | } |
| 1994 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1995 | j = false; |
| 1996 | break; |
| 1997 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1998 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1999 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2000 | break; |
| 2001 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 2002 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2003 | t = t->next; |
| 2004 | tt = tt->next; |
| 2005 | } |
| 2006 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2007 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2008 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2009 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2010 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2011 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2012 | bool found = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2013 | ExpDef searching, *ed; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2014 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2015 | skip_white_(tline); |
| 2016 | tline = expand_id(tline); |
| 2017 | if (!tok_type_(tline, TOK_ID)) { |
| 2018 | error(ERR_NONFATAL, |
| 2019 | "`%s' expects a macro name", pp_directives[ct]); |
| 2020 | goto fail; |
| 2021 | } |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2022 | memset(&searching, 0, sizeof(searching)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2023 | searching.name = nasm_strdup(tline->text); |
| 2024 | searching.casesense = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2025 | searching.nparam_max = INT_MAX; |
| 2026 | tline = expand_smacro(tline->next); |
| 2027 | skip_white_(tline); |
| 2028 | if (!tline) { |
| 2029 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2030 | error(ERR_NONFATAL, |
| 2031 | "`%s' expects a parameter count or nothing", |
| 2032 | pp_directives[ct]); |
| 2033 | } else { |
| 2034 | searching.nparam_min = searching.nparam_max = |
| 2035 | readnum(tline->text, &j); |
| 2036 | if (j) |
| 2037 | error(ERR_NONFATAL, |
| 2038 | "unable to parse parameter count `%s'", |
| 2039 | tline->text); |
| 2040 | } |
| 2041 | if (tline && tok_is_(tline->next, "-")) { |
| 2042 | tline = tline->next->next; |
| 2043 | if (tok_is_(tline, "*")) |
| 2044 | searching.nparam_max = INT_MAX; |
| 2045 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 2046 | error(ERR_NONFATAL, |
| 2047 | "`%s' expects a parameter count after `-'", |
| 2048 | pp_directives[ct]); |
| 2049 | else { |
| 2050 | searching.nparam_max = readnum(tline->text, &j); |
| 2051 | if (j) |
| 2052 | error(ERR_NONFATAL, |
| 2053 | "unable to parse parameter count `%s'", |
| 2054 | tline->text); |
| 2055 | if (searching.nparam_min > searching.nparam_max) |
| 2056 | error(ERR_NONFATAL, |
| 2057 | "minimum parameter count exceeds maximum"); |
| 2058 | } |
| 2059 | } |
| 2060 | if (tline && tok_is_(tline->next, "+")) { |
| 2061 | tline = tline->next; |
| 2062 | searching.plus = true; |
| 2063 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2064 | ed = (ExpDef *) hash_findix(&expdefs, searching.name); |
| 2065 | while (ed != NULL) { |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2066 | if (!strcmp(ed->name, searching.name) && |
| 2067 | (ed->nparam_min <= searching.nparam_max || searching.plus) && |
| 2068 | (searching.nparam_min <= ed->nparam_max || ed->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2069 | found = true; |
| 2070 | break; |
| 2071 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2072 | ed = ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2073 | } |
| 2074 | if (tline && tline->next) |
| 2075 | error(ERR_WARNING|ERR_PASS1, |
| 2076 | "trailing garbage after %%ifmacro ignored"); |
| 2077 | nasm_free(searching.name); |
| 2078 | j = found; |
| 2079 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2080 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2081 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2082 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2083 | needtype = TOK_ID; |
| 2084 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2085 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2086 | needtype = TOK_NUMBER; |
| 2087 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2088 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2089 | needtype = TOK_STRING; |
| 2090 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2091 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2092 | iftype: |
| 2093 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2094 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2095 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2096 | (needtype == TOK_NUMBER && |
| 2097 | tok_type_(t, TOK_OTHER) && |
| 2098 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2099 | !t->text[1])) |
| 2100 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2101 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2102 | j = tok_type_(t, needtype); |
| 2103 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2104 | |
| 2105 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2106 | t = tline = expand_smacro(tline); |
| 2107 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2108 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2109 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2110 | j = false; |
| 2111 | if (t) { |
| 2112 | t = t->next; /* Skip the actual token */ |
| 2113 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2114 | t = t->next; |
| 2115 | j = !t; /* Should be nothing left */ |
| 2116 | } |
| 2117 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2118 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2119 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2120 | t = tline = expand_smacro(tline); |
| 2121 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2122 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2123 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2124 | j = !t; /* Should be empty */ |
| 2125 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2126 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2127 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2128 | t = tline = expand_smacro(tline); |
| 2129 | tptr = &t; |
| 2130 | tokval.t_type = TOKEN_INVALID; |
| 2131 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 2132 | NULL, pass | CRITICAL, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2133 | if (!evalresult) |
| 2134 | return -1; |
| 2135 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2136 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2137 | "trailing garbage after expression ignored"); |
| 2138 | if (!is_simple(evalresult)) { |
| 2139 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2140 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2141 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2142 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2143 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2144 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2145 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2146 | default: |
| 2147 | error(ERR_FATAL, |
| 2148 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2149 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2150 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2151 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2152 | |
| 2153 | free_tlist(origline); |
| 2154 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2155 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2156 | fail: |
| 2157 | free_tlist(origline); |
| 2158 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2162 | * Common code for defining an smacro |
| 2163 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2164 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2165 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2166 | { |
| 2167 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2168 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2169 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2170 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2171 | if (!smac) { |
| 2172 | error(ERR_WARNING|ERR_PASS1, |
| 2173 | "single-line macro `%s' defined both with and" |
| 2174 | " without parameters", mname); |
| 2175 | /* |
| 2176 | * Some instances of the old code considered this a failure, |
| 2177 | * some others didn't. What is the right thing to do here? |
| 2178 | */ |
| 2179 | free_tlist(expansion); |
| 2180 | return false; /* Failure */ |
| 2181 | } else { |
| 2182 | /* |
| 2183 | * We're redefining, so we have to take over an |
| 2184 | * existing SMacro structure. This means freeing |
| 2185 | * what was already in it. |
| 2186 | */ |
| 2187 | nasm_free(smac->name); |
| 2188 | free_tlist(smac->expansion); |
| 2189 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2190 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2191 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2192 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2193 | smac = nasm_zalloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2194 | smac->next = *smhead; |
| 2195 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2196 | } |
| 2197 | smac->name = nasm_strdup(mname); |
| 2198 | smac->casesense = casesense; |
| 2199 | smac->nparam = nparam; |
| 2200 | smac->expansion = expansion; |
| 2201 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2202 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * Undefine an smacro |
| 2207 | */ |
| 2208 | static void undef_smacro(Context *ctx, const char *mname) |
| 2209 | { |
| 2210 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2211 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2212 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2213 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2214 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2215 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2216 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2217 | /* |
| 2218 | * We now have a macro name... go hunt for it. |
| 2219 | */ |
| 2220 | sp = smhead; |
| 2221 | while ((s = *sp) != NULL) { |
| 2222 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2223 | *sp = s->next; |
| 2224 | nasm_free(s->name); |
| 2225 | free_tlist(s->expansion); |
| 2226 | nasm_free(s); |
| 2227 | } else { |
| 2228 | sp = &s->next; |
| 2229 | } |
| 2230 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2234 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2235 | * Parse a mmacro specification. |
| 2236 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2237 | 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] | 2238 | { |
| 2239 | bool err; |
| 2240 | |
| 2241 | tline = tline->next; |
| 2242 | skip_white_(tline); |
| 2243 | tline = expand_id(tline); |
| 2244 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2245 | error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
| 2246 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2247 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2248 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2249 | def->name = nasm_strdup(tline->text); |
| 2250 | def->plus = false; |
| 2251 | def->nolist = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2252 | // def->in_progress = 0; |
| 2253 | // def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2254 | def->nparam_min = 0; |
| 2255 | def->nparam_max = 0; |
| 2256 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2257 | tline = expand_smacro(tline->next); |
| 2258 | skip_white_(tline); |
| 2259 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2260 | error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2261 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2262 | def->nparam_min = def->nparam_max = |
| 2263 | readnum(tline->text, &err); |
| 2264 | if (err) |
| 2265 | error(ERR_NONFATAL, |
| 2266 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2267 | } |
| 2268 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2269 | tline = tline->next->next; |
| 2270 | if (tok_is_(tline, "*")) { |
| 2271 | def->nparam_max = INT_MAX; |
| 2272 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2273 | error(ERR_NONFATAL, |
| 2274 | "`%s' expects a parameter count after `-'", directive); |
| 2275 | } else { |
| 2276 | def->nparam_max = readnum(tline->text, &err); |
| 2277 | if (err) { |
| 2278 | error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
| 2279 | tline->text); |
| 2280 | } |
| 2281 | if (def->nparam_min > def->nparam_max) { |
| 2282 | error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
| 2283 | } |
| 2284 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2285 | } |
| 2286 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2287 | tline = tline->next; |
| 2288 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2289 | } |
| 2290 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2291 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2292 | tline = tline->next; |
| 2293 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2294 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2295 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2296 | /* |
| 2297 | * Handle default parameters. |
| 2298 | */ |
| 2299 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2300 | def->dlist = tline->next; |
| 2301 | tline->next = NULL; |
| 2302 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2303 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2304 | def->dlist = NULL; |
| 2305 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2306 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2307 | def->line = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2308 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2309 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2310 | !def->plus) |
| 2311 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
| 2312 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2313 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2314 | return true; |
| 2315 | } |
| 2316 | |
| 2317 | |
| 2318 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2319 | * Decode a size directive |
| 2320 | */ |
| 2321 | static int parse_size(const char *str) { |
| 2322 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2323 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2324 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2325 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2326 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2327 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2328 | } |
| 2329 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2330 | /** |
| 2331 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2332 | * Find out if a line contains a preprocessor directive, and deal |
| 2333 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2334 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2335 | * If a directive _is_ found, it is the responsibility of this routine |
| 2336 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2337 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2338 | * @param tline a pointer to the current tokeninzed line linked list |
| 2339 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2340 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2341 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2342 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2343 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2344 | enum preproc_token i; |
| 2345 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2346 | bool err; |
| 2347 | int nparam; |
| 2348 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2349 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2350 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2351 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2352 | char *p, *pp; |
| 2353 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2354 | Include *inc; |
| 2355 | Context *ctx; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2356 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2357 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2358 | struct tokenval tokval; |
| 2359 | expr *evalresult; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2360 | ExpDef *ed, *eed, **edhead; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2361 | ExpInv *ei, *eei; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2362 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2363 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2364 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2365 | |
| 2366 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2367 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2368 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2369 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2370 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2371 | || tline->text[1] == '!')) |
| 2372 | return NO_DIRECTIVE_FOUND; |
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 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2375 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2376 | switch (i) { |
| 2377 | case PP_INVALID: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2378 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2379 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2380 | tline->text); |
| 2381 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2382 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2383 | case PP_STACKSIZE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2384 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2385 | /* Directive to tell NASM what the default stack size is. The |
| 2386 | * default is for a 16-bit stack, and this can be overriden with |
| 2387 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2388 | */ |
| 2389 | tline = tline->next; |
| 2390 | if (tline && tline->type == TOK_WHITESPACE) |
| 2391 | tline = tline->next; |
| 2392 | if (!tline || tline->type != TOK_ID) { |
| 2393 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 2394 | free_tlist(origline); |
| 2395 | return DIRECTIVE_FOUND; |
| 2396 | } |
| 2397 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2398 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2399 | StackSize = 4; |
| 2400 | StackPointer = "ebp"; |
| 2401 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2402 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2403 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2404 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2405 | StackSize = 8; |
| 2406 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2407 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2408 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2409 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2410 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2411 | * far function call. |
| 2412 | */ |
| 2413 | StackSize = 2; |
| 2414 | StackPointer = "bp"; |
| 2415 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2416 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2417 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2418 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2419 | * far function call. We don't support near functions. |
| 2420 | */ |
| 2421 | StackSize = 2; |
| 2422 | StackPointer = "bp"; |
| 2423 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2424 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2425 | } else { |
| 2426 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 2427 | free_tlist(origline); |
| 2428 | return DIRECTIVE_FOUND; |
| 2429 | } |
| 2430 | free_tlist(origline); |
| 2431 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2432 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2433 | case PP_ARG: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2434 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2435 | /* TASM like ARG directive to define arguments to functions, in |
| 2436 | * the following form: |
| 2437 | * |
| 2438 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2439 | */ |
| 2440 | offset = ArgOffset; |
| 2441 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2442 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2443 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2444 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2445 | /* Find the argument name */ |
| 2446 | tline = tline->next; |
| 2447 | if (tline && tline->type == TOK_WHITESPACE) |
| 2448 | tline = tline->next; |
| 2449 | if (!tline || tline->type != TOK_ID) { |
| 2450 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 2451 | free_tlist(origline); |
| 2452 | return DIRECTIVE_FOUND; |
| 2453 | } |
| 2454 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2455 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2456 | /* Find the argument size type */ |
| 2457 | tline = tline->next; |
| 2458 | if (!tline || tline->type != TOK_OTHER |
| 2459 | || tline->text[0] != ':') { |
| 2460 | error(ERR_NONFATAL, |
| 2461 | "Syntax error processing `%%arg' directive"); |
| 2462 | free_tlist(origline); |
| 2463 | return DIRECTIVE_FOUND; |
| 2464 | } |
| 2465 | tline = tline->next; |
| 2466 | if (!tline || tline->type != TOK_ID) { |
| 2467 | error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
| 2468 | free_tlist(origline); |
| 2469 | return DIRECTIVE_FOUND; |
| 2470 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2471 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2472 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2473 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2474 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2475 | size = parse_size(tt->text); |
| 2476 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2477 | error(ERR_NONFATAL, |
| 2478 | "Invalid size type for `%%arg' missing directive"); |
| 2479 | free_tlist(tt); |
| 2480 | free_tlist(origline); |
| 2481 | return DIRECTIVE_FOUND; |
| 2482 | } |
| 2483 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2484 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2485 | /* Round up to even stack slots */ |
| 2486 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2487 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2488 | /* Now define the macro for the argument */ |
| 2489 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2490 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2491 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2492 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2493 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2494 | /* Move to the next argument in the list */ |
| 2495 | tline = tline->next; |
| 2496 | if (tline && tline->type == TOK_WHITESPACE) |
| 2497 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2498 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2499 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2500 | free_tlist(origline); |
| 2501 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2502 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2503 | case PP_LOCAL: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2504 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2505 | /* TASM like LOCAL directive to define local variables for a |
| 2506 | * function, in the following form: |
| 2507 | * |
| 2508 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2509 | * |
| 2510 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2511 | * required by TASM to define the local parameter size (and used |
| 2512 | * by the TASM macro package). |
| 2513 | */ |
| 2514 | offset = LocalOffset; |
| 2515 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2516 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2517 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2518 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2519 | /* Find the argument name */ |
| 2520 | tline = tline->next; |
| 2521 | if (tline && tline->type == TOK_WHITESPACE) |
| 2522 | tline = tline->next; |
| 2523 | if (!tline || tline->type != TOK_ID) { |
| 2524 | error(ERR_NONFATAL, |
| 2525 | "`%%local' missing argument parameter"); |
| 2526 | free_tlist(origline); |
| 2527 | return DIRECTIVE_FOUND; |
| 2528 | } |
| 2529 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2530 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2531 | /* Find the argument size type */ |
| 2532 | tline = tline->next; |
| 2533 | if (!tline || tline->type != TOK_OTHER |
| 2534 | || tline->text[0] != ':') { |
| 2535 | error(ERR_NONFATAL, |
| 2536 | "Syntax error processing `%%local' directive"); |
| 2537 | free_tlist(origline); |
| 2538 | return DIRECTIVE_FOUND; |
| 2539 | } |
| 2540 | tline = tline->next; |
| 2541 | if (!tline || tline->type != TOK_ID) { |
| 2542 | error(ERR_NONFATAL, |
| 2543 | "`%%local' missing size type parameter"); |
| 2544 | free_tlist(origline); |
| 2545 | return DIRECTIVE_FOUND; |
| 2546 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2547 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2548 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2549 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2550 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2551 | size = parse_size(tt->text); |
| 2552 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2553 | error(ERR_NONFATAL, |
| 2554 | "Invalid size type for `%%local' missing directive"); |
| 2555 | free_tlist(tt); |
| 2556 | free_tlist(origline); |
| 2557 | return DIRECTIVE_FOUND; |
| 2558 | } |
| 2559 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2560 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2561 | /* Round up to even stack slots */ |
| 2562 | size = ALIGN(size, StackSize); |
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 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2565 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2566 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2567 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2568 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2569 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2570 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2571 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2572 | snprintf(directive, sizeof(directive), |
| 2573 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2574 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2575 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2576 | /* Move to the next argument in the list */ |
| 2577 | tline = tline->next; |
| 2578 | if (tline && tline->type == TOK_WHITESPACE) |
| 2579 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2580 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2581 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2582 | free_tlist(origline); |
| 2583 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2584 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2585 | case PP_CLEAR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2586 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2587 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2588 | error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2589 | "trailing garbage after `%%clear' ignored"); |
| 2590 | free_macros(); |
| 2591 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2592 | free_tlist(origline); |
| 2593 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2594 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2595 | case PP_DEPEND: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2596 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2597 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2598 | skip_white_(t); |
| 2599 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2600 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2601 | error(ERR_NONFATAL, "`%%depend' expects a file name"); |
| 2602 | free_tlist(origline); |
| 2603 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2604 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2605 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2606 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2607 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2608 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2609 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2610 | nasm_unquote_cstr(p, i); |
| 2611 | if (dephead && !in_list(*dephead, p)) { |
| 2612 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2613 | sl->next = NULL; |
| 2614 | strcpy(sl->str, p); |
| 2615 | *deptail = sl; |
| 2616 | deptail = &sl->next; |
| 2617 | } |
| 2618 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2619 | return DIRECTIVE_FOUND; |
| 2620 | |
| 2621 | case PP_INCLUDE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2622 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2623 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2624 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2625 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2626 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2627 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2628 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2629 | free_tlist(origline); |
| 2630 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2631 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2632 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2633 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2634 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2635 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2636 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2637 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 2638 | inc = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2639 | inc->next = istk; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2640 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2641 | if (!inc->fp) { |
| 2642 | /* -MG given but file not found */ |
| 2643 | nasm_free(inc); |
| 2644 | } else { |
| 2645 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2646 | inc->lineno = src_set_linnum(0); |
| 2647 | inc->lineinc = 1; |
| 2648 | inc->expansion = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2649 | istk = inc; |
| 2650 | list->uplevel(LIST_INCLUDE); |
| 2651 | } |
| 2652 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2653 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2654 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2655 | case PP_USE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2656 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2657 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2658 | static macros_t *use_pkg; |
| 2659 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2660 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2661 | tline = tline->next; |
| 2662 | skip_white_(tline); |
| 2663 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2664 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2665 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2666 | tline->type != TOK_INTERNAL_STRING && |
| 2667 | tline->type != TOK_ID)) { |
H. Peter Anvin | 926fc40 | 2008-06-19 16:26:12 -0700 | [diff] [blame] | 2668 | error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2669 | free_tlist(origline); |
| 2670 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2671 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2672 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2673 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2674 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2675 | if (tline->type == TOK_STRING) |
| 2676 | nasm_unquote_cstr(tline->text, i); |
| 2677 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2678 | if (!use_pkg) |
| 2679 | error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
| 2680 | else |
| 2681 | 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] | 2682 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2683 | /* Not already included, go ahead and include it */ |
| 2684 | stdmacpos = use_pkg; |
| 2685 | } |
| 2686 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2687 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2688 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2689 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2690 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2691 | case PP_POP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2692 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2693 | tline = tline->next; |
| 2694 | skip_white_(tline); |
| 2695 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2696 | if (tline) { |
| 2697 | if (!tok_type_(tline, TOK_ID)) { |
| 2698 | error(ERR_NONFATAL, "`%s' expects a context identifier", |
| 2699 | pp_directives[i]); |
| 2700 | free_tlist(origline); |
| 2701 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2702 | } |
| 2703 | if (tline->next) |
| 2704 | error(ERR_WARNING|ERR_PASS1, |
| 2705 | "trailing garbage after `%s' ignored", |
| 2706 | pp_directives[i]); |
| 2707 | p = nasm_strdup(tline->text); |
| 2708 | } else { |
| 2709 | p = NULL; /* Anonymous */ |
| 2710 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2711 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2712 | if (i == PP_PUSH) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2713 | ctx = nasm_zalloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2714 | ctx->next = cstk; |
| 2715 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2716 | ctx->name = p; |
| 2717 | ctx->number = unique++; |
| 2718 | cstk = ctx; |
| 2719 | } else { |
| 2720 | /* %pop or %repl */ |
| 2721 | if (!cstk) { |
| 2722 | error(ERR_NONFATAL, "`%s': context stack is empty", |
| 2723 | pp_directives[i]); |
| 2724 | } else if (i == PP_POP) { |
| 2725 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
| 2726 | error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
| 2727 | "expected %s", |
| 2728 | cstk->name ? cstk->name : "anonymous", p); |
| 2729 | else |
| 2730 | ctx_pop(); |
| 2731 | } else { |
| 2732 | /* i == PP_REPL */ |
| 2733 | nasm_free(cstk->name); |
| 2734 | cstk->name = p; |
| 2735 | p = NULL; |
| 2736 | } |
| 2737 | nasm_free(p); |
| 2738 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2739 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2740 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2741 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2742 | severity = ERR_FATAL; |
| 2743 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2744 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2745 | severity = ERR_NONFATAL; |
| 2746 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2747 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2748 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2749 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2750 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2751 | issue_error: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2752 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2753 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2754 | /* Only error out if this is the final pass */ |
| 2755 | if (pass != 2 && i != PP_FATAL) |
| 2756 | return DIRECTIVE_FOUND; |
| 2757 | |
| 2758 | tline->next = expand_smacro(tline->next); |
| 2759 | tline = tline->next; |
| 2760 | skip_white_(tline); |
| 2761 | t = tline ? tline->next : NULL; |
| 2762 | skip_white_(t); |
| 2763 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2764 | /* The line contains only a quoted string */ |
| 2765 | p = tline->text; |
| 2766 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
| 2767 | error(severity, "%s", p); |
| 2768 | } else { |
| 2769 | /* Not a quoted string, or more than a quoted string */ |
| 2770 | p = detoken(tline, false); |
| 2771 | error(severity, "%s", p); |
| 2772 | nasm_free(p); |
| 2773 | } |
| 2774 | free_tlist(origline); |
| 2775 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2776 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2777 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2778 | CASE_PP_IF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2779 | if (defining != NULL) { |
| 2780 | if (defining->type == EXP_IF) { |
| 2781 | defining->def_depth ++; |
| 2782 | } |
| 2783 | return NO_DIRECTIVE_FOUND; |
| 2784 | } |
| 2785 | if ((istk->expansion != NULL) && |
| 2786 | (istk->expansion->emitting == false)) { |
| 2787 | j = COND_NEVER; |
| 2788 | } else { |
| 2789 | j = if_condition(tline->next, i); |
| 2790 | tline->next = NULL; /* it got freed */ |
| 2791 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 2792 | } |
| 2793 | ed = new_ExpDef(EXP_IF); |
| 2794 | ed->state = j; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2795 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 2796 | ed->prev = defining; |
| 2797 | defining = ed; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2798 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2799 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2800 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2801 | CASE_PP_ELIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2802 | if (defining != NULL) { |
| 2803 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2804 | return NO_DIRECTIVE_FOUND; |
| 2805 | } |
| 2806 | } |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 2807 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2808 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2809 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2810 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2811 | case COND_IF_TRUE: |
| 2812 | defining->state = COND_DONE; |
| 2813 | defining->ignoring = true; |
| 2814 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2815 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2816 | case COND_DONE: |
| 2817 | case COND_NEVER: |
| 2818 | defining->ignoring = true; |
| 2819 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2820 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2821 | case COND_ELSE_TRUE: |
| 2822 | case COND_ELSE_FALSE: |
| 2823 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2824 | "`%%elif' after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2825 | defining->state = COND_NEVER; |
| 2826 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2827 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2828 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2829 | case COND_IF_FALSE: |
| 2830 | /* |
| 2831 | * IMPORTANT: In the case of %if, we will already have |
| 2832 | * called expand_mmac_params(); however, if we're |
| 2833 | * processing an %elif we must have been in a |
| 2834 | * non-emitting mode, which would have inhibited |
| 2835 | * the normal invocation of expand_mmac_params(). |
| 2836 | * Therefore, we have to do it explicitly here. |
| 2837 | */ |
| 2838 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2839 | tline->next = NULL; /* it got freed */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2840 | defining->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2841 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2842 | defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2843 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2844 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2845 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2846 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2847 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2848 | case PP_ELSE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2849 | if (defining != NULL) { |
| 2850 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2851 | return NO_DIRECTIVE_FOUND; |
| 2852 | } |
| 2853 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2854 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2855 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2856 | "trailing garbage after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2857 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2858 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2859 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2860 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2861 | case COND_IF_TRUE: |
| 2862 | case COND_DONE: |
| 2863 | defining->state = COND_ELSE_FALSE; |
| 2864 | defining->ignoring = true; |
| 2865 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2866 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2867 | case COND_NEVER: |
| 2868 | defining->ignoring = true; |
| 2869 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2870 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2871 | case COND_IF_FALSE: |
| 2872 | defining->state = COND_ELSE_TRUE; |
| 2873 | defining->ignoring = false; |
| 2874 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2875 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2876 | case COND_ELSE_TRUE: |
| 2877 | case COND_ELSE_FALSE: |
| 2878 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2879 | "`%%else' after `%%else' ignored."); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2880 | defining->state = COND_NEVER; |
| 2881 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2882 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2883 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2884 | free_tlist(origline); |
| 2885 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2886 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2887 | case PP_ENDIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2888 | if (defining != NULL) { |
| 2889 | if (defining->type == EXP_IF) { |
| 2890 | if (defining->def_depth > 0) { |
| 2891 | defining->def_depth --; |
| 2892 | return NO_DIRECTIVE_FOUND; |
| 2893 | } |
| 2894 | } else { |
| 2895 | return NO_DIRECTIVE_FOUND; |
| 2896 | } |
| 2897 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2898 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2899 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2900 | "trailing garbage after `%%endif' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2901 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2902 | error(ERR_NONFATAL, "`%%endif': no matching `%%if'"); |
| 2903 | return DIRECTIVE_FOUND; |
| 2904 | } |
| 2905 | ed = defining; |
| 2906 | defining = ed->prev; |
| 2907 | ed->prev = expansions; |
| 2908 | expansions = ed; |
| 2909 | ei = new_ExpInv(EXP_IF, ed); |
| 2910 | ei->current = ed->line; |
| 2911 | ei->emitting = true; |
| 2912 | ei->prev = istk->expansion; |
| 2913 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2914 | free_tlist(origline); |
| 2915 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2916 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2917 | case PP_RMACRO: |
| 2918 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2919 | case PP_MACRO: |
| 2920 | case PP_IMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2921 | if (defining != NULL) { |
| 2922 | if (defining->type == EXP_MMACRO) { |
| 2923 | defining->def_depth ++; |
| 2924 | } |
| 2925 | return NO_DIRECTIVE_FOUND; |
| 2926 | } |
| 2927 | ed = new_ExpDef(EXP_MMACRO); |
| 2928 | ed->max_depth = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2929 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2930 | ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2931 | if (!parse_mmacro_spec(tline, ed, pp_directives[i])) { |
| 2932 | nasm_free(ed); |
| 2933 | ed = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2934 | return DIRECTIVE_FOUND; |
| 2935 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2936 | ed->def_depth = 0; |
| 2937 | ed->cur_depth = 0; |
| 2938 | ed->max_depth = (ed->max_depth + 1); |
| 2939 | ed->ignoring = false; |
| 2940 | ed->prev = defining; |
| 2941 | defining = ed; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2942 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2943 | eed = (ExpDef *) hash_findix(&expdefs, ed->name); |
| 2944 | while (eed) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2945 | if (!strcmp(eed->name, ed->name) && |
| 2946 | (eed->nparam_min <= ed->nparam_max || ed->plus) && |
| 2947 | (ed->nparam_min <= eed->nparam_max || eed->plus)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2948 | error(ERR_WARNING|ERR_PASS1, |
| 2949 | "redefining multi-line macro `%s'", ed->name); |
| 2950 | return DIRECTIVE_FOUND; |
| 2951 | } |
| 2952 | eed = eed->next; |
| 2953 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2954 | free_tlist(origline); |
| 2955 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2956 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2957 | case PP_ENDM: |
| 2958 | case PP_ENDMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2959 | if (defining != NULL) { |
| 2960 | if (defining->type == EXP_MMACRO) { |
| 2961 | if (defining->def_depth > 0) { |
| 2962 | defining->def_depth --; |
| 2963 | return NO_DIRECTIVE_FOUND; |
| 2964 | } |
| 2965 | } else { |
| 2966 | return NO_DIRECTIVE_FOUND; |
| 2967 | } |
| 2968 | } |
| 2969 | if (!(defining) || (defining->type != EXP_MMACRO)) { |
| 2970 | error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
| 2971 | return DIRECTIVE_FOUND; |
| 2972 | } |
| 2973 | edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name); |
| 2974 | defining->next = *edhead; |
| 2975 | *edhead = defining; |
| 2976 | ed = defining; |
| 2977 | defining = ed->prev; |
| 2978 | ed->prev = expansions; |
| 2979 | expansions = ed; |
| 2980 | ed = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2981 | free_tlist(origline); |
| 2982 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2983 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2984 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2985 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 2986 | /* |
| 2987 | * We must search along istk->expansion until we hit a |
| 2988 | * macro invocation. Then we disable the emitting state(s) |
| 2989 | * between exitmacro and endmacro. |
| 2990 | */ |
| 2991 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 2992 | if(ei->type == EXP_MMACRO) { |
| 2993 | break; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | if (ei != NULL) { |
| 2998 | /* |
| 2999 | * Set all invocations leading back to the macro |
| 3000 | * invocation to a non-emitting state. |
| 3001 | */ |
| 3002 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3003 | eei->emitting = false; |
| 3004 | } |
| 3005 | eei->emitting = false; |
| 3006 | } else { |
| 3007 | error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
| 3008 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 3009 | free_tlist(origline); |
| 3010 | return DIRECTIVE_FOUND; |
| 3011 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3012 | case PP_UNMACRO: |
| 3013 | case PP_UNIMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3014 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3015 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3016 | ExpDef **ed_p; |
| 3017 | ExpDef spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3018 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3019 | spec.casesense = (i == PP_UNMACRO); |
| 3020 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 3021 | return DIRECTIVE_FOUND; |
| 3022 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3023 | ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL); |
| 3024 | while (ed_p && *ed_p) { |
| 3025 | ed = *ed_p; |
| 3026 | if (ed->casesense == spec.casesense && |
| 3027 | !mstrcmp(ed->name, spec.name, spec.casesense) && |
| 3028 | ed->nparam_min == spec.nparam_min && |
| 3029 | ed->nparam_max == spec.nparam_max && |
| 3030 | ed->plus == spec.plus) { |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3031 | if (ed->cur_depth > 0) { |
Keith Kanios | 21d885b | 2010-12-18 12:22:21 -0600 | [diff] [blame] | 3032 | error(ERR_NONFATAL, "`%s' ignored on active macro", |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3033 | pp_directives[i]); |
| 3034 | break; |
| 3035 | } else { |
| 3036 | *ed_p = ed->next; |
| 3037 | free_expdef(ed); |
| 3038 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3039 | } else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3040 | ed_p = &ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3041 | } |
| 3042 | } |
| 3043 | free_tlist(origline); |
| 3044 | free_tlist(spec.dlist); |
| 3045 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3048 | case PP_ROTATE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3049 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3050 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3051 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3052 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3053 | free_tlist(origline); |
| 3054 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 3055 | return DIRECTIVE_FOUND; |
| 3056 | } |
| 3057 | t = expand_smacro(tline->next); |
| 3058 | tline->next = NULL; |
| 3059 | free_tlist(origline); |
| 3060 | tline = t; |
| 3061 | tptr = &t; |
| 3062 | tokval.t_type = TOKEN_INVALID; |
| 3063 | evalresult = |
| 3064 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3065 | free_tlist(tline); |
| 3066 | if (!evalresult) |
| 3067 | return DIRECTIVE_FOUND; |
| 3068 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3069 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3070 | "trailing garbage after expression ignored"); |
| 3071 | if (!is_simple(evalresult)) { |
| 3072 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
| 3073 | return DIRECTIVE_FOUND; |
| 3074 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3075 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3076 | if (ei->type == EXP_MMACRO) { |
| 3077 | break; |
| 3078 | } |
| 3079 | } |
| 3080 | if (ei == NULL) { |
| 3081 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
| 3082 | } else if (ei->nparam == 0) { |
| 3083 | error(ERR_NONFATAL, |
| 3084 | "`%%rotate' invoked within macro without parameters"); |
| 3085 | } else { |
| 3086 | int rotate = ei->rotate + reloc_value(evalresult); |
| 3087 | |
| 3088 | rotate %= (int)ei->nparam; |
| 3089 | if (rotate < 0) |
| 3090 | rotate += ei->nparam; |
| 3091 | ei->rotate = rotate; |
| 3092 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3093 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3094 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3095 | case PP_REP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3096 | if (defining != NULL) { |
| 3097 | if (defining->type == EXP_REP) { |
| 3098 | defining->def_depth ++; |
| 3099 | } |
| 3100 | return NO_DIRECTIVE_FOUND; |
| 3101 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3102 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3103 | do { |
| 3104 | tline = tline->next; |
| 3105 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3106 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3107 | if (tok_type_(tline, TOK_ID) && |
| 3108 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3109 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3110 | do { |
| 3111 | tline = tline->next; |
| 3112 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3113 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3114 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3115 | if (tline) { |
| 3116 | t = expand_smacro(tline); |
| 3117 | tptr = &t; |
| 3118 | tokval.t_type = TOKEN_INVALID; |
| 3119 | evalresult = |
| 3120 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3121 | if (!evalresult) { |
| 3122 | free_tlist(origline); |
| 3123 | return DIRECTIVE_FOUND; |
| 3124 | } |
| 3125 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3126 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3127 | "trailing garbage after expression ignored"); |
| 3128 | if (!is_simple(evalresult)) { |
| 3129 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 3130 | return DIRECTIVE_FOUND; |
| 3131 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3132 | count = reloc_value(evalresult); |
| 3133 | if (count >= REP_LIMIT) { |
Cyrill Gorcunov | 71f4f84 | 2010-08-09 20:17:17 +0400 | [diff] [blame] | 3134 | error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3135 | count = 0; |
| 3136 | } else |
| 3137 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3138 | } else { |
| 3139 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3140 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3141 | } |
| 3142 | free_tlist(origline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3143 | ed = new_ExpDef(EXP_REP); |
| 3144 | ed->nolist = nolist; |
| 3145 | ed->def_depth = 0; |
| 3146 | ed->cur_depth = 1; |
| 3147 | ed->max_depth = (count - 1); |
| 3148 | ed->ignoring = false; |
| 3149 | ed->prev = defining; |
| 3150 | defining = ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3151 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3152 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3153 | case PP_ENDREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3154 | if (defining != NULL) { |
| 3155 | if (defining->type == EXP_REP) { |
| 3156 | if (defining->def_depth > 0) { |
| 3157 | defining->def_depth --; |
| 3158 | return NO_DIRECTIVE_FOUND; |
| 3159 | } |
| 3160 | } else { |
| 3161 | return NO_DIRECTIVE_FOUND; |
| 3162 | } |
| 3163 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3164 | if ((defining == NULL) || (defining->type != EXP_REP)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3165 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
| 3166 | return DIRECTIVE_FOUND; |
| 3167 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3168 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3169 | /* |
| 3170 | * Now we have a "macro" defined - although it has no name |
| 3171 | * and we won't be entering it in the hash tables - we must |
| 3172 | * push a macro-end marker for it on to istk->expansion. |
| 3173 | * After that, it will take care of propagating itself (a |
| 3174 | * macro-end marker line for a macro which is really a %rep |
| 3175 | * block will cause the macro to be re-expanded, complete |
| 3176 | * with another macro-end marker to ensure the process |
| 3177 | * continues) until the whole expansion is forcibly removed |
| 3178 | * from istk->expansion by a %exitrep. |
| 3179 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3180 | ed = defining; |
| 3181 | defining = ed->prev; |
| 3182 | ed->prev = expansions; |
| 3183 | expansions = ed; |
| 3184 | ei = new_ExpInv(EXP_REP, ed); |
| 3185 | ei->current = ed->line; |
| 3186 | ei->emitting = ((ed->max_depth > 0) ? true : false); |
| 3187 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 3188 | ei->prev = istk->expansion; |
| 3189 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3190 | free_tlist(origline); |
| 3191 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3192 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3193 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3194 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3195 | /* |
| 3196 | * We must search along istk->expansion until we hit a |
| 3197 | * rep invocation. Then we disable the emitting state(s) |
| 3198 | * between exitrep and endrep. |
| 3199 | */ |
| 3200 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3201 | if (ei->type == EXP_REP) { |
| 3202 | break; |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | if (ei != NULL) { |
| 3207 | /* |
| 3208 | * Set all invocations leading back to the rep |
| 3209 | * invocation to a non-emitting state. |
| 3210 | */ |
| 3211 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3212 | eei->emitting = false; |
| 3213 | } |
| 3214 | eei->emitting = false; |
| 3215 | eei->current = NULL; |
| 3216 | eei->def->cur_depth = eei->def->max_depth; |
| 3217 | } else { |
| 3218 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 3219 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3220 | free_tlist(origline); |
| 3221 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3222 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3223 | case PP_XDEFINE: |
| 3224 | case PP_IXDEFINE: |
| 3225 | case PP_DEFINE: |
| 3226 | case PP_IDEFINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3227 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3228 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3229 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3230 | tline = tline->next; |
| 3231 | skip_white_(tline); |
| 3232 | tline = expand_id(tline); |
| 3233 | if (!tline || (tline->type != TOK_ID && |
| 3234 | (tline->type != TOK_PREPROC_ID || |
| 3235 | tline->text[1] != '$'))) { |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3236 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3237 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3238 | free_tlist(origline); |
| 3239 | return DIRECTIVE_FOUND; |
| 3240 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3241 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3242 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3243 | last = tline; |
| 3244 | param_start = tline = tline->next; |
| 3245 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3246 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3247 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3248 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3249 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3250 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3251 | if (tok_is_(tline, "(")) { |
| 3252 | /* |
| 3253 | * This macro has parameters. |
| 3254 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3255 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3256 | tline = tline->next; |
| 3257 | while (1) { |
| 3258 | skip_white_(tline); |
| 3259 | if (!tline) { |
| 3260 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 3261 | free_tlist(origline); |
| 3262 | return DIRECTIVE_FOUND; |
| 3263 | } |
| 3264 | if (tline->type != TOK_ID) { |
| 3265 | error(ERR_NONFATAL, |
| 3266 | "`%s': parameter identifier expected", |
| 3267 | tline->text); |
| 3268 | free_tlist(origline); |
| 3269 | return DIRECTIVE_FOUND; |
| 3270 | } |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 3271 | |
| 3272 | smacro_set_param_idx(tline, nparam); |
| 3273 | nparam++; |
| 3274 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3275 | tline = tline->next; |
| 3276 | skip_white_(tline); |
| 3277 | if (tok_is_(tline, ",")) { |
| 3278 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3279 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3280 | if (!tok_is_(tline, ")")) { |
| 3281 | error(ERR_NONFATAL, |
| 3282 | "`)' expected to terminate macro template"); |
| 3283 | free_tlist(origline); |
| 3284 | return DIRECTIVE_FOUND; |
| 3285 | } |
| 3286 | break; |
| 3287 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3288 | } |
| 3289 | last = tline; |
| 3290 | tline = tline->next; |
| 3291 | } |
| 3292 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3293 | last = tline, tline = tline->next; |
| 3294 | macro_start = NULL; |
| 3295 | last->next = NULL; |
| 3296 | t = tline; |
| 3297 | while (t) { |
| 3298 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3299 | list_for_each(tt, param_start) |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 3300 | if (is_smacro_param(tt) && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3301 | !strcmp(tt->text, t->text)) |
| 3302 | t->type = tt->type; |
| 3303 | } |
| 3304 | tt = t->next; |
| 3305 | t->next = macro_start; |
| 3306 | macro_start = t; |
| 3307 | t = tt; |
| 3308 | } |
| 3309 | /* |
| 3310 | * Good. We now have a macro name, a parameter count, and a |
| 3311 | * token list (in reverse order) for an expansion. We ought |
| 3312 | * to be OK just to create an SMacro, store it, and let |
| 3313 | * free_tlist have the rest of the line (which we have |
| 3314 | * carefully re-terminated after chopping off the expansion |
| 3315 | * from the end). |
| 3316 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3317 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3318 | free_tlist(origline); |
| 3319 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3320 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3321 | case PP_UNDEF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3322 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3323 | tline = tline->next; |
| 3324 | skip_white_(tline); |
| 3325 | tline = expand_id(tline); |
| 3326 | if (!tline || (tline->type != TOK_ID && |
| 3327 | (tline->type != TOK_PREPROC_ID || |
| 3328 | tline->text[1] != '$'))) { |
| 3329 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 3330 | free_tlist(origline); |
| 3331 | return DIRECTIVE_FOUND; |
| 3332 | } |
| 3333 | if (tline->next) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3334 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3335 | "trailing garbage after macro name ignored"); |
| 3336 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3337 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3338 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3339 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3340 | undef_smacro(ctx, mname); |
| 3341 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3342 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3343 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3344 | case PP_DEFSTR: |
| 3345 | case PP_IDEFSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3346 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3347 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3348 | |
| 3349 | tline = tline->next; |
| 3350 | skip_white_(tline); |
| 3351 | tline = expand_id(tline); |
| 3352 | if (!tline || (tline->type != TOK_ID && |
| 3353 | (tline->type != TOK_PREPROC_ID || |
| 3354 | tline->text[1] != '$'))) { |
| 3355 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3356 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3357 | free_tlist(origline); |
| 3358 | return DIRECTIVE_FOUND; |
| 3359 | } |
| 3360 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3361 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3362 | last = tline; |
| 3363 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3364 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3365 | |
| 3366 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3367 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3368 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3369 | p = detoken(tline, false); |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3370 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3371 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3372 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3373 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3374 | |
| 3375 | /* |
| 3376 | * We now have a macro name, an implicit parameter count of |
| 3377 | * zero, and a string token to use as an expansion. Create |
| 3378 | * and store an SMacro. |
| 3379 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3380 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3381 | free_tlist(origline); |
| 3382 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3383 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3384 | case PP_DEFTOK: |
| 3385 | case PP_IDEFTOK: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3386 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3387 | casesense = (i == PP_DEFTOK); |
| 3388 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3389 | tline = tline->next; |
| 3390 | skip_white_(tline); |
| 3391 | tline = expand_id(tline); |
| 3392 | if (!tline || (tline->type != TOK_ID && |
| 3393 | (tline->type != TOK_PREPROC_ID || |
| 3394 | tline->text[1] != '$'))) { |
| 3395 | error(ERR_NONFATAL, |
| 3396 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3397 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3398 | free_tlist(origline); |
| 3399 | return DIRECTIVE_FOUND; |
| 3400 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3401 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3402 | last = tline; |
| 3403 | tline = expand_smacro(tline->next); |
| 3404 | last->next = NULL; |
| 3405 | |
| 3406 | t = tline; |
| 3407 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3408 | t = t->next; |
| 3409 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3410 | if (!tok_type_(t, TOK_STRING)) { |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3411 | error(ERR_NONFATAL, |
| 3412 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3413 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3414 | free_tlist(tline); |
| 3415 | free_tlist(origline); |
| 3416 | return DIRECTIVE_FOUND; |
| 3417 | } |
| 3418 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3419 | /* |
| 3420 | * Convert the string to a token stream. Note that smacros |
| 3421 | * are stored with the token stream reversed, so we have to |
| 3422 | * reverse the output of tokenize(). |
| 3423 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3424 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3425 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3426 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3427 | /* |
| 3428 | * We now have a macro name, an implicit parameter count of |
| 3429 | * zero, and a numeric token to use as an expansion. Create |
| 3430 | * and store an SMacro. |
| 3431 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3432 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3433 | free_tlist(tline); |
| 3434 | free_tlist(origline); |
| 3435 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3436 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3437 | case PP_PATHSEARCH: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3438 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3439 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3440 | FILE *fp; |
| 3441 | StrList *xsl = NULL; |
| 3442 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3443 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3444 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3445 | |
| 3446 | tline = tline->next; |
| 3447 | skip_white_(tline); |
| 3448 | tline = expand_id(tline); |
| 3449 | if (!tline || (tline->type != TOK_ID && |
| 3450 | (tline->type != TOK_PREPROC_ID || |
| 3451 | tline->text[1] != '$'))) { |
| 3452 | error(ERR_NONFATAL, |
| 3453 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3454 | free_tlist(origline); |
| 3455 | return DIRECTIVE_FOUND; |
| 3456 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3457 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3458 | last = tline; |
| 3459 | tline = expand_smacro(tline->next); |
| 3460 | last->next = NULL; |
| 3461 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3462 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3463 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3464 | t = t->next; |
| 3465 | |
| 3466 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3467 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3468 | error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3469 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3470 | free_tlist(origline); |
| 3471 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3472 | } |
| 3473 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3474 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3475 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3476 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3477 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3478 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3479 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3480 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3481 | if (fp) { |
| 3482 | p = xsl->str; |
| 3483 | fclose(fp); /* Don't actually care about the file */ |
| 3484 | } |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3485 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3486 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3487 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 3488 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3489 | |
| 3490 | /* |
| 3491 | * We now have a macro name, an implicit parameter count of |
| 3492 | * zero, and a string token to use as an expansion. Create |
| 3493 | * and store an SMacro. |
| 3494 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3495 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3496 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3497 | free_tlist(origline); |
| 3498 | return DIRECTIVE_FOUND; |
| 3499 | } |
| 3500 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3501 | case PP_STRLEN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3502 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3503 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3504 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3505 | tline = tline->next; |
| 3506 | skip_white_(tline); |
| 3507 | tline = expand_id(tline); |
| 3508 | if (!tline || (tline->type != TOK_ID && |
| 3509 | (tline->type != TOK_PREPROC_ID || |
| 3510 | tline->text[1] != '$'))) { |
| 3511 | error(ERR_NONFATAL, |
| 3512 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3513 | free_tlist(origline); |
| 3514 | return DIRECTIVE_FOUND; |
| 3515 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3516 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3517 | last = tline; |
| 3518 | tline = expand_smacro(tline->next); |
| 3519 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3520 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3521 | t = tline; |
| 3522 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3523 | t = t->next; |
| 3524 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3525 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3526 | error(ERR_NONFATAL, |
| 3527 | "`%%strlen` requires string as second parameter"); |
| 3528 | free_tlist(tline); |
| 3529 | free_tlist(origline); |
| 3530 | return DIRECTIVE_FOUND; |
| 3531 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3532 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3533 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3534 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3535 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3536 | /* |
| 3537 | * We now have a macro name, an implicit parameter count of |
| 3538 | * zero, and a numeric token to use as an expansion. Create |
| 3539 | * and store an SMacro. |
| 3540 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3541 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3542 | free_tlist(tline); |
| 3543 | free_tlist(origline); |
| 3544 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3545 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3546 | case PP_STRCAT: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3547 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3548 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3549 | |
| 3550 | tline = tline->next; |
| 3551 | skip_white_(tline); |
| 3552 | tline = expand_id(tline); |
| 3553 | if (!tline || (tline->type != TOK_ID && |
| 3554 | (tline->type != TOK_PREPROC_ID || |
| 3555 | tline->text[1] != '$'))) { |
| 3556 | error(ERR_NONFATAL, |
| 3557 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3558 | free_tlist(origline); |
| 3559 | return DIRECTIVE_FOUND; |
| 3560 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3561 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3562 | last = tline; |
| 3563 | tline = expand_smacro(tline->next); |
| 3564 | last->next = NULL; |
| 3565 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3566 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3567 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3568 | switch (t->type) { |
| 3569 | case TOK_WHITESPACE: |
| 3570 | break; |
| 3571 | case TOK_STRING: |
| 3572 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3573 | break; |
| 3574 | case TOK_OTHER: |
| 3575 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3576 | break; |
| 3577 | /* else fall through */ |
| 3578 | default: |
| 3579 | error(ERR_NONFATAL, |
| 3580 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3581 | free_tlist(tline); |
| 3582 | free_tlist(origline); |
| 3583 | return DIRECTIVE_FOUND; |
| 3584 | } |
| 3585 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3586 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3587 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3588 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3589 | if (t->type == TOK_STRING) { |
| 3590 | memcpy(p, t->text, t->a.len); |
| 3591 | p += t->a.len; |
| 3592 | } |
| 3593 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3594 | |
| 3595 | /* |
| 3596 | * We now have a macro name, an implicit parameter count of |
| 3597 | * zero, and a numeric token to use as an expansion. Create |
| 3598 | * and store an SMacro. |
| 3599 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3600 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3601 | macro_start->text = nasm_quote(pp, len); |
| 3602 | nasm_free(pp); |
| 3603 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3604 | free_tlist(tline); |
| 3605 | free_tlist(origline); |
| 3606 | return DIRECTIVE_FOUND; |
| 3607 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3608 | case PP_SUBSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3609 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3610 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3611 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3612 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3613 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3614 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3615 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3616 | tline = tline->next; |
| 3617 | skip_white_(tline); |
| 3618 | tline = expand_id(tline); |
| 3619 | if (!tline || (tline->type != TOK_ID && |
| 3620 | (tline->type != TOK_PREPROC_ID || |
| 3621 | tline->text[1] != '$'))) { |
| 3622 | error(ERR_NONFATAL, |
| 3623 | "`%%substr' expects a macro identifier as first parameter"); |
| 3624 | free_tlist(origline); |
| 3625 | return DIRECTIVE_FOUND; |
| 3626 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3627 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3628 | last = tline; |
| 3629 | tline = expand_smacro(tline->next); |
| 3630 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3631 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3632 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3633 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3634 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3635 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3636 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3637 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3638 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3639 | error(ERR_NONFATAL, |
| 3640 | "`%%substr` requires string as second parameter"); |
| 3641 | free_tlist(tline); |
| 3642 | free_tlist(origline); |
| 3643 | return DIRECTIVE_FOUND; |
| 3644 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3645 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3646 | tt = t->next; |
| 3647 | tptr = &tt; |
| 3648 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3649 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3650 | pass, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3651 | if (!evalresult) { |
| 3652 | free_tlist(tline); |
| 3653 | free_tlist(origline); |
| 3654 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3655 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3656 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3657 | free_tlist(tline); |
| 3658 | free_tlist(origline); |
| 3659 | return DIRECTIVE_FOUND; |
| 3660 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3661 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3662 | |
| 3663 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3664 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3665 | if (!tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3666 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3667 | } else { |
| 3668 | tokval.t_type = TOKEN_INVALID; |
| 3669 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3670 | pass, error, NULL); |
| 3671 | if (!evalresult) { |
| 3672 | free_tlist(tline); |
| 3673 | free_tlist(origline); |
| 3674 | return DIRECTIVE_FOUND; |
| 3675 | } else if (!is_simple(evalresult)) { |
| 3676 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3677 | free_tlist(tline); |
| 3678 | free_tlist(origline); |
| 3679 | return DIRECTIVE_FOUND; |
| 3680 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3681 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3682 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3683 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3684 | len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3685 | /* make start and count being in range */ |
| 3686 | if (start < 0) |
| 3687 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3688 | if (count < 0) |
| 3689 | count = len + count + 1 - start; |
| 3690 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3691 | count = len - start; |
| 3692 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3693 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3694 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3695 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3696 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3697 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3698 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3699 | /* |
| 3700 | * We now have a macro name, an implicit parameter count of |
| 3701 | * zero, and a numeric token to use as an expansion. Create |
| 3702 | * and store an SMacro. |
| 3703 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3704 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3705 | free_tlist(tline); |
| 3706 | free_tlist(origline); |
| 3707 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3708 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3709 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3710 | case PP_ASSIGN: |
| 3711 | case PP_IASSIGN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3712 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3713 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3714 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3715 | tline = tline->next; |
| 3716 | skip_white_(tline); |
| 3717 | tline = expand_id(tline); |
| 3718 | if (!tline || (tline->type != TOK_ID && |
| 3719 | (tline->type != TOK_PREPROC_ID || |
| 3720 | tline->text[1] != '$'))) { |
| 3721 | error(ERR_NONFATAL, |
| 3722 | "`%%%sassign' expects a macro identifier", |
| 3723 | (i == PP_IASSIGN ? "i" : "")); |
| 3724 | free_tlist(origline); |
| 3725 | return DIRECTIVE_FOUND; |
| 3726 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3727 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3728 | last = tline; |
| 3729 | tline = expand_smacro(tline->next); |
| 3730 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3731 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3732 | t = tline; |
| 3733 | tptr = &t; |
| 3734 | tokval.t_type = TOKEN_INVALID; |
| 3735 | evalresult = |
| 3736 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3737 | free_tlist(tline); |
| 3738 | if (!evalresult) { |
| 3739 | free_tlist(origline); |
| 3740 | return DIRECTIVE_FOUND; |
| 3741 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3742 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3743 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3744 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3745 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3746 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3747 | if (!is_simple(evalresult)) { |
| 3748 | error(ERR_NONFATAL, |
| 3749 | "non-constant value given to `%%%sassign'", |
| 3750 | (i == PP_IASSIGN ? "i" : "")); |
| 3751 | free_tlist(origline); |
| 3752 | return DIRECTIVE_FOUND; |
| 3753 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3754 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3755 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3756 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3757 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3758 | /* |
| 3759 | * We now have a macro name, an implicit parameter count of |
| 3760 | * zero, and a numeric token to use as an expansion. Create |
| 3761 | * and store an SMacro. |
| 3762 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3763 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3764 | free_tlist(origline); |
| 3765 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3766 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3767 | case PP_LINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3768 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3769 | /* |
| 3770 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3771 | */ |
| 3772 | tline = tline->next; |
| 3773 | skip_white_(tline); |
| 3774 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3775 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3776 | free_tlist(origline); |
| 3777 | return DIRECTIVE_FOUND; |
| 3778 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3779 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3780 | m = 1; |
| 3781 | tline = tline->next; |
| 3782 | if (tok_is_(tline, "+")) { |
| 3783 | tline = tline->next; |
| 3784 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3785 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3786 | free_tlist(origline); |
| 3787 | return DIRECTIVE_FOUND; |
| 3788 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3789 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3790 | tline = tline->next; |
| 3791 | } |
| 3792 | skip_white_(tline); |
| 3793 | src_set_linnum(k); |
| 3794 | istk->lineinc = m; |
| 3795 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3796 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3797 | } |
| 3798 | free_tlist(origline); |
| 3799 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3800 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3801 | case PP_WHILE: |
| 3802 | if (defining != NULL) { |
| 3803 | if (defining->type == EXP_WHILE) { |
| 3804 | defining->def_depth ++; |
| 3805 | } |
| 3806 | return NO_DIRECTIVE_FOUND; |
| 3807 | } |
| 3808 | l = NULL; |
| 3809 | if ((istk->expansion != NULL) && |
| 3810 | (istk->expansion->emitting == false)) { |
| 3811 | j = COND_NEVER; |
| 3812 | } else { |
| 3813 | l = new_Line(); |
| 3814 | l->first = copy_Token(tline->next); |
| 3815 | j = if_condition(tline->next, i); |
| 3816 | tline->next = NULL; /* it got freed */ |
| 3817 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 3818 | } |
| 3819 | ed = new_ExpDef(EXP_WHILE); |
| 3820 | ed->state = j; |
| 3821 | ed->cur_depth = 1; |
| 3822 | ed->max_depth = DEADMAN_LIMIT; |
| 3823 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 3824 | if (ed->ignoring == false) { |
| 3825 | ed->line = l; |
| 3826 | ed->last = l; |
| 3827 | } else if (l != NULL) { |
| 3828 | delete_Token(l->first); |
| 3829 | nasm_free(l); |
| 3830 | l = NULL; |
| 3831 | } |
| 3832 | ed->prev = defining; |
| 3833 | defining = ed; |
| 3834 | free_tlist(origline); |
| 3835 | return DIRECTIVE_FOUND; |
| 3836 | |
| 3837 | case PP_ENDWHILE: |
| 3838 | if (defining != NULL) { |
| 3839 | if (defining->type == EXP_WHILE) { |
| 3840 | if (defining->def_depth > 0) { |
| 3841 | defining->def_depth --; |
| 3842 | return NO_DIRECTIVE_FOUND; |
| 3843 | } |
| 3844 | } else { |
| 3845 | return NO_DIRECTIVE_FOUND; |
| 3846 | } |
| 3847 | } |
| 3848 | if (tline->next != NULL) { |
| 3849 | error_precond(ERR_WARNING|ERR_PASS1, |
| 3850 | "trailing garbage after `%%endwhile' ignored"); |
| 3851 | } |
| 3852 | if ((defining == NULL) || (defining->type != EXP_WHILE)) { |
| 3853 | error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'"); |
| 3854 | return DIRECTIVE_FOUND; |
| 3855 | } |
| 3856 | ed = defining; |
| 3857 | defining = ed->prev; |
| 3858 | if (ed->ignoring == false) { |
| 3859 | ed->prev = expansions; |
| 3860 | expansions = ed; |
| 3861 | ei = new_ExpInv(EXP_WHILE, ed); |
| 3862 | ei->current = ed->line->next; |
| 3863 | ei->emitting = true; |
| 3864 | ei->prev = istk->expansion; |
| 3865 | istk->expansion = ei; |
| 3866 | } else { |
| 3867 | nasm_free(ed); |
| 3868 | } |
| 3869 | free_tlist(origline); |
| 3870 | return DIRECTIVE_FOUND; |
| 3871 | |
| 3872 | case PP_EXITWHILE: |
| 3873 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3874 | /* |
| 3875 | * We must search along istk->expansion until we hit a |
| 3876 | * while invocation. Then we disable the emitting state(s) |
| 3877 | * between exitwhile and endwhile. |
| 3878 | */ |
| 3879 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3880 | if (ei->type == EXP_WHILE) { |
| 3881 | break; |
| 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | if (ei != NULL) { |
| 3886 | /* |
| 3887 | * Set all invocations leading back to the while |
| 3888 | * invocation to a non-emitting state. |
| 3889 | */ |
| 3890 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3891 | eei->emitting = false; |
| 3892 | } |
| 3893 | eei->emitting = false; |
| 3894 | eei->current = NULL; |
| 3895 | eei->def->cur_depth = eei->def->max_depth; |
| 3896 | } else { |
| 3897 | error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block"); |
| 3898 | } |
| 3899 | free_tlist(origline); |
| 3900 | return DIRECTIVE_FOUND; |
| 3901 | |
| 3902 | case PP_COMMENT: |
| 3903 | if (defining != NULL) { |
| 3904 | if (defining->type == EXP_COMMENT) { |
| 3905 | defining->def_depth ++; |
| 3906 | } |
| 3907 | return NO_DIRECTIVE_FOUND; |
| 3908 | } |
| 3909 | ed = new_ExpDef(EXP_COMMENT); |
| 3910 | ed->ignoring = true; |
| 3911 | ed->prev = defining; |
| 3912 | defining = ed; |
| 3913 | free_tlist(origline); |
| 3914 | return DIRECTIVE_FOUND; |
| 3915 | |
| 3916 | case PP_ENDCOMMENT: |
| 3917 | if (defining != NULL) { |
| 3918 | if (defining->type == EXP_COMMENT) { |
| 3919 | if (defining->def_depth > 0) { |
| 3920 | defining->def_depth --; |
| 3921 | return NO_DIRECTIVE_FOUND; |
| 3922 | } |
| 3923 | } else { |
| 3924 | return NO_DIRECTIVE_FOUND; |
| 3925 | } |
| 3926 | } |
| 3927 | if ((defining == NULL) || (defining->type != EXP_COMMENT)) { |
| 3928 | error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'"); |
| 3929 | return DIRECTIVE_FOUND; |
| 3930 | } |
| 3931 | ed = defining; |
| 3932 | defining = ed->prev; |
| 3933 | nasm_free(ed); |
| 3934 | free_tlist(origline); |
| 3935 | return DIRECTIVE_FOUND; |
| 3936 | |
| 3937 | case PP_FINAL: |
| 3938 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3939 | if (in_final != false) { |
| 3940 | error(ERR_FATAL, "`%%final' cannot be used recursively"); |
| 3941 | } |
| 3942 | tline = tline->next; |
| 3943 | skip_white_(tline); |
| 3944 | if (tline == NULL) { |
| 3945 | error(ERR_NONFATAL, "`%%final' expects at least one parameter"); |
| 3946 | } else { |
| 3947 | l = new_Line(); |
| 3948 | l->first = copy_Token(tline); |
| 3949 | l->next = finals; |
| 3950 | finals = l; |
| 3951 | } |
| 3952 | free_tlist(origline); |
| 3953 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3954 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3955 | default: |
| 3956 | error(ERR_FATAL, |
| 3957 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3958 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3959 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3960 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3964 | * Ensure that a macro parameter contains a condition code and |
| 3965 | * nothing else. Return the condition code index if so, or -1 |
| 3966 | * otherwise. |
| 3967 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3968 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3969 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3970 | Token *tt; |
| 3971 | int i, j, k, m; |
| 3972 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3973 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3974 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3975 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3976 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3977 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3978 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3979 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3980 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3981 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3982 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3983 | |
| 3984 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 3985 | j = ARRAY_SIZE(conditions); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3986 | while (j - i > 1) { |
| 3987 | k = (j + i) / 2; |
| 3988 | m = nasm_stricmp(t->text, conditions[k]); |
| 3989 | if (m == 0) { |
| 3990 | i = k; |
| 3991 | j = -2; |
| 3992 | break; |
| 3993 | } else if (m < 0) { |
| 3994 | j = k; |
| 3995 | } else |
| 3996 | i = k; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3997 | } |
| 3998 | if (j != -2) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3999 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4000 | return i; |
| 4001 | } |
| 4002 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4003 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
| 4004 | int mnum, bool handle_paste_tokens) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4005 | { |
| 4006 | Token **tail, *t, *tt; |
| 4007 | Token **paste_head; |
| 4008 | bool did_paste = false; |
| 4009 | char *tmp; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4010 | int i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4011 | |
| 4012 | /* Now handle token pasting... */ |
| 4013 | paste_head = NULL; |
| 4014 | tail = head; |
| 4015 | while ((t = *tail) && (tt = t->next)) { |
| 4016 | switch (t->type) { |
| 4017 | case TOK_WHITESPACE: |
| 4018 | if (tt->type == TOK_WHITESPACE) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4019 | /* Zap adjacent whitespace tokens */ |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4020 | t->next = delete_Token(tt); |
| 4021 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4022 | /* Do not advance paste_head here */ |
| 4023 | tail = &t->next; |
| 4024 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4025 | break; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4026 | case TOK_PASTE: /* %+ */ |
| 4027 | if (handle_paste_tokens) { |
| 4028 | /* Zap %+ and whitespace tokens to the right */ |
| 4029 | while (t && (t->type == TOK_WHITESPACE || |
| 4030 | t->type == TOK_PASTE)) |
| 4031 | t = *tail = delete_Token(t); |
| 4032 | if (!paste_head || !t) |
| 4033 | break; /* Nothing to paste with */ |
| 4034 | tail = paste_head; |
| 4035 | t = *tail; |
| 4036 | tt = t->next; |
| 4037 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 4038 | tt = t->next = delete_Token(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4039 | if (tt) { |
| 4040 | tmp = nasm_strcat(t->text, tt->text); |
| 4041 | delete_Token(t); |
| 4042 | tt = delete_Token(tt); |
| 4043 | t = *tail = tokenize(tmp); |
| 4044 | nasm_free(tmp); |
| 4045 | while (t->next) { |
| 4046 | tail = &t->next; |
| 4047 | t = t->next; |
| 4048 | } |
| 4049 | t->next = tt; /* Attach the remaining token chain */ |
| 4050 | did_paste = true; |
| 4051 | } |
| 4052 | paste_head = tail; |
| 4053 | tail = &t->next; |
| 4054 | break; |
| 4055 | } |
| 4056 | /* else fall through */ |
| 4057 | default: |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4058 | /* |
| 4059 | * Concatenation of tokens might look nontrivial |
| 4060 | * but in real it's pretty simple -- the caller |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4061 | * prepares the masks of token types to be concatenated |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4062 | * and we simply find matched sequences and slip |
| 4063 | * them together |
| 4064 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4065 | for (i = 0; i < mnum; i++) { |
| 4066 | if (PP_CONCAT_MASK(t->type) & m[i].mask_head) { |
| 4067 | size_t len = 0; |
| 4068 | char *tmp, *p; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4069 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4070 | while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4071 | len += strlen(tt->text); |
| 4072 | tt = tt->next; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4073 | } |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4074 | |
Cyrill Gorcunov | fdd0ac5 | 2011-06-27 01:22:27 +0400 | [diff] [blame] | 4075 | nasm_dump_token(tt); |
| 4076 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4077 | /* |
| 4078 | * Now tt points to the first token after |
| 4079 | * the potential paste area... |
| 4080 | */ |
| 4081 | if (tt != t->next) { |
| 4082 | /* We have at least two tokens... */ |
| 4083 | len += strlen(t->text); |
| 4084 | p = tmp = nasm_malloc(len+1); |
| 4085 | while (t != tt) { |
| 4086 | strcpy(p, t->text); |
| 4087 | p = strchr(p, '\0'); |
| 4088 | t = delete_Token(t); |
| 4089 | } |
| 4090 | t = *tail = tokenize(tmp); |
| 4091 | nasm_free(tmp); |
| 4092 | while (t->next) { |
| 4093 | tail = &t->next; |
| 4094 | t = t->next; |
| 4095 | } |
| 4096 | t->next = tt; /* Attach the remaining token chain */ |
| 4097 | did_paste = true; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4098 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4099 | paste_head = tail; |
| 4100 | tail = &t->next; |
| 4101 | break; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4102 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4103 | } |
| 4104 | if (i >= mnum) { /* no match */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4105 | tail = &t->next; |
| 4106 | if (!tok_type_(t->next, TOK_WHITESPACE)) |
| 4107 | paste_head = tail; |
| 4108 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4109 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4110 | } |
| 4111 | } |
| 4112 | return did_paste; |
| 4113 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4114 | |
| 4115 | /* |
| 4116 | * expands to a list of tokens from %{x:y} |
| 4117 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4118 | static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4119 | { |
| 4120 | Token *t = tline, **tt, *tm, *head; |
| 4121 | char *pos; |
| 4122 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4123 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4124 | pos = strchr(tline->text, ':'); |
| 4125 | nasm_assert(pos); |
| 4126 | |
| 4127 | lst = atoi(pos + 1); |
| 4128 | fst = atoi(tline->text + 1); |
| 4129 | |
| 4130 | /* |
| 4131 | * only macros params are accounted so |
| 4132 | * if someone passes %0 -- we reject such |
| 4133 | * value(s) |
| 4134 | */ |
| 4135 | if (lst == 0 || fst == 0) |
| 4136 | goto err; |
| 4137 | |
| 4138 | /* the values should be sane */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4139 | if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) || |
| 4140 | (lst > (int)ei->nparam || lst < (-(int)ei->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4141 | goto err; |
| 4142 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4143 | fst = fst < 0 ? fst + (int)ei->nparam + 1: fst; |
| 4144 | lst = lst < 0 ? lst + (int)ei->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4145 | |
| 4146 | /* counted from zero */ |
| 4147 | fst--, lst--; |
| 4148 | |
| 4149 | /* |
| 4150 | * it will be at least one token |
| 4151 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4152 | tm = ei->params[(fst + ei->rotate) % ei->nparam]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4153 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4154 | head = t, tt = &t->next; |
| 4155 | if (fst < lst) { |
| 4156 | for (i = fst + 1; i <= lst; i++) { |
| 4157 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4158 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4159 | j = (i + ei->rotate) % ei->nparam; |
| 4160 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4161 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4162 | *tt = t, tt = &t->next; |
| 4163 | } |
| 4164 | } else { |
| 4165 | for (i = fst - 1; i >= lst; i--) { |
| 4166 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4167 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4168 | j = (i + ei->rotate) % ei->nparam; |
| 4169 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4170 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4171 | *tt = t, tt = &t->next; |
| 4172 | } |
| 4173 | } |
| 4174 | |
| 4175 | *last = tt; |
| 4176 | return head; |
| 4177 | |
| 4178 | err: |
| 4179 | error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
| 4180 | &tline->text[1]); |
| 4181 | return tline; |
| 4182 | } |
| 4183 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4184 | /* |
| 4185 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4186 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4187 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4188 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4189 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4190 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4191 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4192 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4193 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4194 | |
| 4195 | tail = &thead; |
| 4196 | thead = NULL; |
| 4197 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4198 | nasm_dump_stream(tline); |
| 4199 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4200 | while (tline) { |
| 4201 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4202 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4203 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4204 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4205 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4206 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4207 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4208 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4209 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4210 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4211 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4212 | t = tline; |
| 4213 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4214 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4215 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 4216 | if (ei->type == EXP_MMACRO) { |
| 4217 | break; |
| 4218 | } |
| 4219 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4220 | if (ei == NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4221 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4222 | } else { |
| 4223 | pos = strchr(t->text, ':'); |
| 4224 | if (!pos) { |
| 4225 | switch (t->text[1]) { |
| 4226 | /* |
| 4227 | * We have to make a substitution of one of the |
| 4228 | * forms %1, %-1, %+1, %%foo, %0. |
| 4229 | */ |
| 4230 | case '0': |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4231 | if ((strlen(t->text) > 2) && (t->text[2] == '0')) { |
| 4232 | type = TOK_ID; |
| 4233 | text = nasm_strdup(ei->label_text); |
| 4234 | } else { |
| 4235 | type = TOK_NUMBER; |
| 4236 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam); |
| 4237 | text = nasm_strdup(tmpbuf); |
| 4238 | } |
| 4239 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4240 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4241 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4242 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4243 | ei->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4244 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4245 | break; |
| 4246 | case '-': |
| 4247 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4248 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4249 | tt = NULL; |
| 4250 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4251 | if (ei->nparam > 1) |
| 4252 | n = (n + ei->rotate) % ei->nparam; |
| 4253 | tt = ei->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4254 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4255 | cc = find_cc(tt); |
| 4256 | if (cc == -1) { |
| 4257 | error(ERR_NONFATAL, |
| 4258 | "macro parameter %d is not a condition code", |
| 4259 | n + 1); |
| 4260 | text = NULL; |
| 4261 | } else { |
| 4262 | type = TOK_ID; |
| 4263 | if (inverse_ccs[cc] == -1) { |
| 4264 | error(ERR_NONFATAL, |
| 4265 | "condition code `%s' is not invertible", |
| 4266 | conditions[cc]); |
| 4267 | text = NULL; |
| 4268 | } else |
| 4269 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4270 | } |
| 4271 | break; |
| 4272 | case '+': |
| 4273 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4274 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4275 | tt = NULL; |
| 4276 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4277 | if (ei->nparam > 1) |
| 4278 | n = (n + ei->rotate) % ei->nparam; |
| 4279 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4280 | } |
| 4281 | cc = find_cc(tt); |
| 4282 | if (cc == -1) { |
| 4283 | error(ERR_NONFATAL, |
| 4284 | "macro parameter %d is not a condition code", |
| 4285 | n + 1); |
| 4286 | text = NULL; |
| 4287 | } else { |
| 4288 | type = TOK_ID; |
| 4289 | text = nasm_strdup(conditions[cc]); |
| 4290 | } |
| 4291 | break; |
| 4292 | default: |
| 4293 | n = atoi(t->text + 1) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4294 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4295 | tt = NULL; |
| 4296 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4297 | if (ei->nparam > 1) |
| 4298 | n = (n + ei->rotate) % ei->nparam; |
| 4299 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4300 | } |
| 4301 | if (tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4302 | for (i = 0; i < ei->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4303 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4304 | tail = &(*tail)->next; |
| 4305 | tt = tt->next; |
| 4306 | } |
| 4307 | } |
| 4308 | text = NULL; /* we've done it here */ |
| 4309 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4310 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4311 | } else { |
| 4312 | /* |
| 4313 | * seems we have a parameters range here |
| 4314 | */ |
| 4315 | Token *head, **last; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4316 | head = expand_mmac_params_range(ei, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4317 | if (head != t) { |
| 4318 | *tail = head; |
| 4319 | *last = tline; |
| 4320 | tline = head; |
| 4321 | text = NULL; |
| 4322 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4323 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4324 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4325 | if (!text) { |
| 4326 | delete_Token(t); |
| 4327 | } else { |
| 4328 | *tail = t; |
| 4329 | tail = &t->next; |
| 4330 | t->type = type; |
| 4331 | nasm_free(t->text); |
| 4332 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4333 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4334 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4335 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4336 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4337 | } else if (tline->type == TOK_INDIRECT) { |
| 4338 | t = tline; |
| 4339 | tline = tline->next; |
| 4340 | tt = tokenize(t->text); |
| 4341 | tt = expand_mmac_params(tt); |
| 4342 | tt = expand_smacro(tt); |
| 4343 | *tail = tt; |
| 4344 | while (tt) { |
| 4345 | tt->a.mac = NULL; /* Necessary? */ |
| 4346 | tail = &tt->next; |
| 4347 | tt = tt->next; |
| 4348 | } |
| 4349 | delete_Token(t); |
| 4350 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4351 | } else { |
| 4352 | t = *tail = tline; |
| 4353 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4354 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4355 | tail = &t->next; |
| 4356 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4357 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4358 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4359 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4360 | if (changed) { |
| 4361 | const struct tokseq_match t[] = { |
| 4362 | { |
| 4363 | PP_CONCAT_MASK(TOK_ID) | |
| 4364 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4365 | PP_CONCAT_MASK(TOK_ID) | |
| 4366 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4367 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4368 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4369 | }, |
| 4370 | { |
| 4371 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4372 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4373 | } |
| 4374 | }; |
| 4375 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4376 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4377 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4378 | nasm_dump_token(thead); |
| 4379 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4380 | return thead; |
| 4381 | } |
| 4382 | |
| 4383 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4384 | * Expand all single-line macro calls made in the given line. |
| 4385 | * Return the expanded version of the line. The original is deemed |
| 4386 | * to be destroyed in the process. (In reality we'll just move |
| 4387 | * Tokens from input to output a lot of the time, rather than |
| 4388 | * actually bothering to destroy and replicate.) |
| 4389 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4390 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4391 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4392 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4393 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4394 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4395 | Token **params; |
| 4396 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4397 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4398 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4399 | Token *org_tline = tline; |
| 4400 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4401 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4402 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4403 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4404 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4405 | /* |
| 4406 | * Trick: we should avoid changing the start token pointer since it can |
| 4407 | * be contained in "next" field of other token. Because of this |
| 4408 | * we allocate a copy of first token and work with it; at the end of |
| 4409 | * routine we copy it back |
| 4410 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4411 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4412 | tline = new_Token(org_tline->next, org_tline->type, |
| 4413 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4414 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4415 | nasm_free(org_tline->text); |
| 4416 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4419 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4420 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4421 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4422 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4423 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4424 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4425 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4426 | if (!--deadman) { |
| 4427 | error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4428 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4429 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4430 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4431 | if ((mname = tline->text)) { |
| 4432 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4433 | if (tline->type == TOK_ID) { |
| 4434 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4435 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 4436 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4437 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4438 | } else |
| 4439 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4440 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4441 | /* |
| 4442 | * We've hit an identifier. As in is_mmacro below, we first |
| 4443 | * check whether the identifier is a single-line macro at |
| 4444 | * all, then think about checking for parameters if |
| 4445 | * necessary. |
| 4446 | */ |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4447 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4448 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4449 | break; |
| 4450 | if (m) { |
| 4451 | mstart = tline; |
| 4452 | params = NULL; |
| 4453 | paramsize = NULL; |
| 4454 | if (m->nparam == 0) { |
| 4455 | /* |
| 4456 | * Simple case: the macro is parameterless. Discard the |
| 4457 | * one token that the macro call took, and push the |
| 4458 | * expansion back on the to-do stack. |
| 4459 | */ |
| 4460 | if (!m->expansion) { |
| 4461 | if (!strcmp("__FILE__", m->name)) { |
| 4462 | int32_t num = 0; |
| 4463 | char *file = NULL; |
| 4464 | src_get(&num, &file); |
| 4465 | tline->text = nasm_quote(file, strlen(file)); |
| 4466 | tline->type = TOK_STRING; |
| 4467 | nasm_free(file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4468 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4469 | } |
| 4470 | if (!strcmp("__LINE__", m->name)) { |
| 4471 | nasm_free(tline->text); |
| 4472 | make_tok_num(tline, src_get_linnum()); |
| 4473 | continue; |
| 4474 | } |
| 4475 | if (!strcmp("__BITS__", m->name)) { |
| 4476 | nasm_free(tline->text); |
| 4477 | make_tok_num(tline, globalbits); |
| 4478 | continue; |
| 4479 | } |
| 4480 | tline = delete_Token(tline); |
| 4481 | continue; |
| 4482 | } |
| 4483 | } else { |
| 4484 | /* |
| 4485 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4486 | * exists and takes parameters. We must find the |
| 4487 | * parameters in the call, count them, find the SMacro |
| 4488 | * that corresponds to that form of the macro call, and |
| 4489 | * substitute for the parameters when we expand. What a |
| 4490 | * pain. |
| 4491 | */ |
| 4492 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4493 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4494 | do { |
| 4495 | t = tline->next; |
| 4496 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4497 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4498 | t->text = NULL; |
| 4499 | t = tline->next = delete_Token(t); |
| 4500 | } |
| 4501 | tline = t; |
| 4502 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4503 | if (!tok_is_(tline, "(")) { |
| 4504 | /* |
| 4505 | * This macro wasn't called with parameters: ignore |
| 4506 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4507 | */ |
| 4508 | tline = mstart; |
| 4509 | m = NULL; |
| 4510 | } else { |
| 4511 | int paren = 0; |
| 4512 | int white = 0; |
| 4513 | brackets = 0; |
| 4514 | nparam = 0; |
| 4515 | sparam = PARAM_DELTA; |
| 4516 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4517 | params[0] = tline->next; |
| 4518 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4519 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4520 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4521 | /* |
| 4522 | * For some unusual expansions |
| 4523 | * which concatenates function call |
| 4524 | */ |
| 4525 | t = tline->next; |
| 4526 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4527 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4528 | t->text = NULL; |
| 4529 | t = tline->next = delete_Token(t); |
| 4530 | } |
| 4531 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4532 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4533 | if (!tline) { |
| 4534 | error(ERR_NONFATAL, |
| 4535 | "macro call expects terminating `)'"); |
| 4536 | break; |
| 4537 | } |
| 4538 | if (tline->type == TOK_WHITESPACE |
| 4539 | && brackets <= 0) { |
| 4540 | if (paramsize[nparam]) |
| 4541 | white++; |
| 4542 | else |
| 4543 | params[nparam] = tline->next; |
| 4544 | continue; /* parameter loop */ |
| 4545 | } |
| 4546 | if (tline->type == TOK_OTHER |
| 4547 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4548 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4549 | if (ch == ',' && !paren && brackets <= 0) { |
| 4550 | if (++nparam >= sparam) { |
| 4551 | sparam += PARAM_DELTA; |
| 4552 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4553 | sparam * sizeof(Token *)); |
| 4554 | paramsize = nasm_realloc(paramsize, |
| 4555 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4556 | } |
| 4557 | params[nparam] = tline->next; |
| 4558 | paramsize[nparam] = 0; |
| 4559 | white = 0; |
| 4560 | continue; /* parameter loop */ |
| 4561 | } |
| 4562 | if (ch == '{' && |
| 4563 | (brackets > 0 || (brackets == 0 && |
| 4564 | !paramsize[nparam]))) |
| 4565 | { |
| 4566 | if (!(brackets++)) { |
| 4567 | params[nparam] = tline->next; |
| 4568 | continue; /* parameter loop */ |
| 4569 | } |
| 4570 | } |
| 4571 | if (ch == '}' && brackets > 0) |
| 4572 | if (--brackets == 0) { |
| 4573 | brackets = -1; |
| 4574 | continue; /* parameter loop */ |
| 4575 | } |
| 4576 | if (ch == '(' && !brackets) |
| 4577 | paren++; |
| 4578 | if (ch == ')' && brackets <= 0) |
| 4579 | if (--paren < 0) |
| 4580 | break; |
| 4581 | } |
| 4582 | if (brackets < 0) { |
| 4583 | brackets = 0; |
| 4584 | error(ERR_NONFATAL, "braces do not " |
| 4585 | "enclose all of macro parameter"); |
| 4586 | } |
| 4587 | paramsize[nparam] += white + 1; |
| 4588 | white = 0; |
| 4589 | } /* parameter loop */ |
| 4590 | nparam++; |
| 4591 | while (m && (m->nparam != nparam || |
| 4592 | mstrcmp(m->name, mname, |
| 4593 | m->casesense))) |
| 4594 | m = m->next; |
| 4595 | if (!m) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4596 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4597 | "macro `%s' exists, " |
| 4598 | "but not taking %d parameters", |
| 4599 | mstart->text, nparam); |
| 4600 | } |
| 4601 | } |
| 4602 | if (m && m->in_progress) |
| 4603 | m = NULL; |
| 4604 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4605 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4606 | * Design question: should we handle !tline, which |
| 4607 | * indicates missing ')' here, or expand those |
| 4608 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4609 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4610 | */ |
| 4611 | nasm_free(params); |
| 4612 | nasm_free(paramsize); |
| 4613 | tline = mstart; |
| 4614 | } else { |
| 4615 | /* |
| 4616 | * Expand the macro: we are placed on the last token of the |
| 4617 | * call, so that we can easily split the call from the |
| 4618 | * following tokens. We also start by pushing an SMAC_END |
| 4619 | * token for the cycle removal. |
| 4620 | */ |
| 4621 | t = tline; |
| 4622 | if (t) { |
| 4623 | tline = t->next; |
| 4624 | t->next = NULL; |
| 4625 | } |
| 4626 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4627 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4628 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4629 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4630 | list_for_each(t, m->expansion) { |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4631 | if (is_smacro_param(t)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4632 | Token *pcopy = tline, **ptail = &pcopy; |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4633 | Token *ttt; |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4634 | int i, idx; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4635 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4636 | idx = smacro_get_param_idx(t); |
| 4637 | ttt = params[idx]; |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4638 | |
| 4639 | /* |
| 4640 | * We need smacro paramters appended. |
| 4641 | */ |
| 4642 | for (i = paramsize[idx]; i > 0; i--) { |
| 4643 | *ptail = new_Token(tline, ttt->type, ttt->text, 0); |
| 4644 | ptail = &(*ptail)->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4645 | ttt = ttt->next; |
| 4646 | } |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4647 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4648 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4649 | } else if (t->type == TOK_PREPROC_Q) { |
| 4650 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4651 | tline = tt; |
| 4652 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4653 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4654 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4655 | } else { |
| 4656 | tt = new_Token(tline, t->type, t->text, 0); |
| 4657 | tline = tt; |
| 4658 | } |
| 4659 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4660 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4661 | /* |
| 4662 | * Having done that, get rid of the macro call, and clean |
| 4663 | * up the parameters. |
| 4664 | */ |
| 4665 | nasm_free(params); |
| 4666 | nasm_free(paramsize); |
| 4667 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4668 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4669 | continue; /* main token loop */ |
| 4670 | } |
| 4671 | } |
| 4672 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4673 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4674 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4675 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4676 | tline = delete_Token(tline); |
| 4677 | } else { |
| 4678 | t = *tail = tline; |
| 4679 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4680 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4681 | t->next = NULL; |
| 4682 | tail = &t->next; |
| 4683 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4686 | /* |
| 4687 | * 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] | 4688 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4689 | * TOK_IDs should be concatenated. |
| 4690 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4691 | * them (without white spaces in between). |
| 4692 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4693 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4694 | const struct tokseq_match t[] = { |
| 4695 | { |
| 4696 | PP_CONCAT_MASK(TOK_ID) | |
| 4697 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4698 | PP_CONCAT_MASK(TOK_ID) | |
| 4699 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4700 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4701 | } |
| 4702 | }; |
| 4703 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4704 | /* |
| 4705 | * If we concatenated something, *and* we had previously expanded |
| 4706 | * an actual macro, scan the lines again for macros... |
| 4707 | */ |
| 4708 | tline = thead; |
| 4709 | expanded = false; |
| 4710 | goto again; |
| 4711 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4712 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4713 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4714 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4715 | if (org_tline) { |
| 4716 | if (thead) { |
| 4717 | *org_tline = *thead; |
| 4718 | /* since we just gave text to org_line, don't free it */ |
| 4719 | thead->text = NULL; |
| 4720 | delete_Token(thead); |
| 4721 | } else { |
| 4722 | /* the expression expanded to empty line; |
| 4723 | we can't return NULL for some reasons |
| 4724 | we just set the line to a single WHITESPACE token. */ |
| 4725 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4726 | org_tline->text = NULL; |
| 4727 | org_tline->type = TOK_WHITESPACE; |
| 4728 | } |
| 4729 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4732 | return thead; |
| 4733 | } |
| 4734 | |
| 4735 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4736 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4737 | * right before they are fetched in. The reason is that there can be |
| 4738 | * identifiers consisting of several subparts. We consider that if there |
| 4739 | * are more than one element forming the name, user wants a expansion, |
| 4740 | * otherwise it will be left as-is. Example: |
| 4741 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4742 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4743 | * |
| 4744 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4745 | * will suck it and define the corresponding value. Other case: |
| 4746 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4747 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4748 | * |
| 4749 | * In this case user wants name to be expanded *before* %define starts |
| 4750 | * working, so we'll expand %$abc into something (if it has a value; |
| 4751 | * otherwise it will be left as-is) then concatenate all successive |
| 4752 | * PP_IDs into one. |
| 4753 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4754 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4755 | { |
| 4756 | Token *cur, *oldnext = NULL; |
| 4757 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4758 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4759 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4760 | |
| 4761 | cur = tline; |
| 4762 | while (cur->next && |
Cyrill Gorcunov | 5b6c96b | 2011-06-30 00:22:53 +0400 | [diff] [blame] | 4763 | (cur->next->type == TOK_ID || |
| 4764 | cur->next->type == TOK_PREPROC_ID || |
| 4765 | cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4766 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4767 | |
| 4768 | /* If identifier consists of just one token, don't expand */ |
| 4769 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4770 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4771 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4772 | if (cur) { |
| 4773 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4774 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4777 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4778 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4779 | if (cur) { |
| 4780 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4781 | cur = tline; |
| 4782 | while (cur && cur->next) |
| 4783 | cur = cur->next; |
| 4784 | if (cur) |
| 4785 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | return tline; |
| 4789 | } |
| 4790 | |
| 4791 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4792 | * Determine whether the given line constitutes a multi-line macro |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4793 | * 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] | 4794 | * to check for an initial label - that's taken care of in |
| 4795 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4796 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4797 | * name is easy to find. |
| 4798 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4799 | static ExpDef *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4800 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4801 | ExpDef *head, *ed; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4802 | Token **params; |
| 4803 | int nparam; |
| 4804 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4805 | head = (ExpDef *) hash_findix(&expdefs, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4806 | |
| 4807 | /* |
| 4808 | * Efficiency: first we see if any macro exists with the given |
| 4809 | * name. If not, we can return NULL immediately. _Then_ we |
| 4810 | * count the parameters, and then we look further along the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4811 | * list if necessary to find the proper ExpDef. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4812 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4813 | list_for_each(ed, head) |
| 4814 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4815 | break; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4816 | if (!ed) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4817 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4818 | |
| 4819 | /* |
| 4820 | * OK, we have a potential macro. Count and demarcate the |
| 4821 | * parameters. |
| 4822 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4823 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4824 | |
| 4825 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4826 | * 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] | 4827 | * structure that handles this number. |
| 4828 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4829 | while (ed) { |
| 4830 | if (ed->nparam_min <= nparam |
| 4831 | && (ed->plus || nparam <= ed->nparam_max)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4832 | /* |
| 4833 | * It's right, and we can use it. Add its default |
| 4834 | * parameters to the end of our list if necessary. |
| 4835 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4836 | if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4837 | params = |
| 4838 | nasm_realloc(params, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4839 | ((ed->nparam_min + ed->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4840 | 1) * sizeof(*params))); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4841 | while (nparam < ed->nparam_min + ed->ndefs) { |
| 4842 | params[nparam] = ed->defaults[nparam - ed->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4843 | nparam++; |
| 4844 | } |
| 4845 | } |
| 4846 | /* |
| 4847 | * If we've gone over the maximum parameter count (and |
| 4848 | * we're in Plus mode), ignore parameters beyond |
| 4849 | * nparam_max. |
| 4850 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4851 | if (ed->plus && nparam > ed->nparam_max) |
| 4852 | nparam = ed->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4853 | /* |
| 4854 | * Then terminate the parameter list, and leave. |
| 4855 | */ |
| 4856 | if (!params) { /* need this special case */ |
| 4857 | params = nasm_malloc(sizeof(*params)); |
| 4858 | nparam = 0; |
| 4859 | } |
| 4860 | params[nparam] = NULL; |
| 4861 | *params_array = params; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4862 | return ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4863 | } |
| 4864 | /* |
| 4865 | * This one wasn't right: look for the next one with the |
| 4866 | * same name. |
| 4867 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4868 | list_for_each(ed, ed->next) |
| 4869 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4870 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | /* |
| 4874 | * After all that, we didn't find one with the right number of |
| 4875 | * parameters. Issue a warning, and fail to expand the macro. |
| 4876 | */ |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4877 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4878 | "macro `%s' exists, but not taking %d parameters", |
| 4879 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4880 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4881 | return NULL; |
| 4882 | } |
| 4883 | |
| 4884 | /* |
| 4885 | * Expand the multi-line macro call made by the given line, if |
| 4886 | * 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] | 4887 | * istk->expansion and return true. Otherwise return false. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4888 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4889 | static bool expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4890 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4891 | Token *label = NULL; |
| 4892 | int dont_prepend = 0; |
Cyrill Gorcunov | fb27fc2 | 2011-06-25 12:08:30 +0400 | [diff] [blame] | 4893 | Token **params, *t; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4894 | Line *l = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4895 | ExpDef *ed; |
| 4896 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4897 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4898 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4899 | |
| 4900 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4901 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4902 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4903 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4904 | return false; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4905 | ed = is_mmacro(t, ¶ms); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4906 | if (ed != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4907 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4908 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4909 | Token *last; |
| 4910 | /* |
| 4911 | * We have an id which isn't a macro call. We'll assume |
| 4912 | * it might be a label; we'll also check to see if a |
| 4913 | * colon follows it. Then, if there's another id after |
| 4914 | * that lot, we'll check it again for macro-hood. |
| 4915 | */ |
| 4916 | label = last = t; |
| 4917 | t = t->next; |
| 4918 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4919 | last = t, t = t->next; |
| 4920 | if (tok_is_(t, ":")) { |
| 4921 | dont_prepend = 1; |
| 4922 | last = t, t = t->next; |
| 4923 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4924 | last = t, t = t->next; |
| 4925 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4926 | if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, ¶ms))) |
| 4927 | return false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4928 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4929 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4930 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4931 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4932 | |
| 4933 | /* |
| 4934 | * Fix up the parameters: this involves stripping leading and |
| 4935 | * trailing whitespace, then stripping braces if they are |
| 4936 | * present. |
| 4937 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4938 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4939 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4940 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4941 | for (i = 0; params[i]; i++) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4942 | int brace = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4943 | int comma = (!ed->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4944 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4945 | t = params[i]; |
| 4946 | skip_white_(t); |
| 4947 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4948 | t = t->next, brace = true, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4949 | params[i] = t; |
| 4950 | paramlen[i] = 0; |
| 4951 | while (t) { |
| 4952 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4953 | break; /* ... because we have hit a comma */ |
| 4954 | if (comma && t->type == TOK_WHITESPACE |
| 4955 | && tok_is_(t->next, ",")) |
| 4956 | break; /* ... or a space then a comma */ |
| 4957 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
| 4958 | break; /* ... or a brace */ |
| 4959 | t = t->next; |
| 4960 | paramlen[i]++; |
| 4961 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4964 | if (ed->cur_depth >= ed->max_depth) { |
| 4965 | if (ed->max_depth > 1) { |
| 4966 | error(ERR_WARNING, |
| 4967 | "reached maximum macro recursion depth of %i for %s", |
| 4968 | ed->max_depth,ed->name); |
| 4969 | } |
| 4970 | return false; |
| 4971 | } else { |
| 4972 | ed->cur_depth ++; |
| 4973 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4974 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4975 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4976 | * OK, we have found a ExpDef structure representing a |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4977 | * previously defined mmacro. Create an expansion invocation |
| 4978 | * and point it back to the expansion definition. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4979 | * parameter tokens and macro-local tokens doesn't get done |
| 4980 | * until the single-line macro substitution process; this is |
| 4981 | * because delaying them allows us to change the semantics |
| 4982 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4983 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4984 | ei = new_ExpInv(EXP_MMACRO, ed); |
| 4985 | ei->name = nasm_strdup(mname); |
| 4986 | //ei->label = label; |
| 4987 | //ei->label_text = detoken(label, false); |
| 4988 | ei->current = ed->line; |
| 4989 | ei->emitting = true; |
| 4990 | //ei->iline = tline; |
| 4991 | ei->params = params; |
| 4992 | ei->nparam = nparam; |
| 4993 | ei->rotate = 0; |
| 4994 | ei->paramlen = paramlen; |
| 4995 | ei->lineno = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4996 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4997 | ei->prev = istk->expansion; |
| 4998 | istk->expansion = ei; |
| 4999 | |
| 5000 | /* |
| 5001 | * Special case: detect %00 on first invocation; if found, |
| 5002 | * avoid emitting any labels that precede the mmacro call. |
| 5003 | * ed->prepend is set to -1 when %00 is detected, else 1. |
| 5004 | */ |
| 5005 | if (ed->prepend == 0) { |
| 5006 | for (l = ed->line; l != NULL; l = l->next) { |
| 5007 | for (t = l->first; t != NULL; t = t->next) { |
| 5008 | if ((t->type == TOK_PREPROC_ID) && |
| 5009 | (strlen(t->text) == 3) && |
| 5010 | (t->text[1] == '0') && (t->text[2] == '0')) { |
| 5011 | dont_prepend = -1; |
| 5012 | break; |
| 5013 | } |
| 5014 | } |
| 5015 | if (dont_prepend < 0) { |
| 5016 | break; |
| 5017 | } |
| 5018 | } |
| 5019 | ed->prepend = ((dont_prepend < 0) ? -1 : 1); |
| 5020 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5021 | |
| 5022 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5023 | * If we had a label, push it on as the first line of |
| 5024 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5025 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5026 | if (label != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5027 | if (ed->prepend < 0) { |
| 5028 | ei->label_text = detoken(label, false); |
| 5029 | } else { |
| 5030 | if (dont_prepend == 0) { |
| 5031 | t = label; |
| 5032 | while (t->next != NULL) { |
| 5033 | t = t->next; |
| 5034 | } |
| 5035 | t->next = new_Token(NULL, TOK_OTHER, ":", 0); |
| 5036 | } |
| 5037 | l = new_Line(); |
| 5038 | l->first = copy_Token(label); |
| 5039 | l->next = ei->current; |
| 5040 | ei->current = l; |
| 5041 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5042 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5043 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5044 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5045 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5046 | istk->mmac_depth++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5047 | return true; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5050 | /* The function that actually does the error reporting */ |
| 5051 | static void verror(int severity, const char *fmt, va_list arg) |
| 5052 | { |
| 5053 | char buff[1024]; |
| 5054 | |
| 5055 | vsnprintf(buff, sizeof(buff), fmt, arg); |
| 5056 | |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5057 | if (istk && istk->mmac_depth > 0) { |
| 5058 | ExpInv *ei = istk->expansion; |
| 5059 | int lineno = ei->lineno; |
| 5060 | while (ei) { |
| 5061 | if (ei->type == EXP_MMACRO) |
| 5062 | break; |
| 5063 | lineno += ei->relno; |
| 5064 | ei = ei->prev; |
| 5065 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5066 | nasm_error(severity, "(%s:%d) %s", ei->def->name, |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5067 | lineno, buff); |
| 5068 | } else |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5069 | nasm_error(severity, "%s", buff); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5070 | } |
| 5071 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5072 | /* |
| 5073 | * Since preprocessor always operate only on the line that didn't |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5074 | * arrived yet, we should always use ERR_OFFBY1. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5075 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5076 | static void error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5077 | { |
| 5078 | va_list arg; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5079 | va_start(arg, fmt); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5080 | verror(severity, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5081 | va_end(arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5082 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5083 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5084 | /* |
| 5085 | * Because %else etc are evaluated in the state context |
| 5086 | * of the previous branch, errors might get lost with error(): |
| 5087 | * %if 0 ... %else trailing garbage ... %endif |
| 5088 | * So %else etc should report errors with this function. |
| 5089 | */ |
| 5090 | static void error_precond(int severity, const char *fmt, ...) |
| 5091 | { |
| 5092 | va_list arg; |
| 5093 | |
| 5094 | /* Only ignore the error if it's really in a dead branch */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5095 | if ((istk != NULL) && |
| 5096 | (istk->expansion != NULL) && |
| 5097 | (istk->expansion->type == EXP_IF) && |
| 5098 | (istk->expansion->def->state == COND_NEVER)) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5099 | return; |
| 5100 | |
| 5101 | va_start(arg, fmt); |
| 5102 | verror(severity, fmt, arg); |
| 5103 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5106 | static void |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5107 | pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5108 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5109 | Token *t; |
| 5110 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5111 | cstk = NULL; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5112 | istk = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5113 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5114 | src_set_fname(nasm_strdup(file)); |
| 5115 | src_set_linnum(0); |
| 5116 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5117 | if (!istk->fp) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5118 | error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5119 | file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5120 | defining = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5121 | finals = NULL; |
| 5122 | in_final = false; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5123 | nested_mac_count = 0; |
| 5124 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5125 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5126 | unique = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5127 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5128 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5129 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5130 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5131 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5132 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 5133 | do_predef = true; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5134 | list = listgen; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5135 | |
| 5136 | /* |
| 5137 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 5138 | * The caller, however, will also pass in 3 for preprocess-only so |
| 5139 | * we can set __PASS__ accordingly. |
| 5140 | */ |
| 5141 | pass = apass > 2 ? 2 : apass; |
| 5142 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5143 | dephead = deptail = deplist; |
| 5144 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5145 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 5146 | sl->next = NULL; |
| 5147 | strcpy(sl->str, file); |
| 5148 | *deptail = sl; |
| 5149 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5150 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5151 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5152 | /* |
| 5153 | * Define the __PASS__ macro. This is defined here unlike |
| 5154 | * all the other builtins, because it is special -- it varies between |
| 5155 | * passes. |
| 5156 | */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5157 | t = nasm_zalloc(sizeof(*t)); |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5158 | make_tok_num(t, apass); |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5159 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5162 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5163 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5164 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5165 | Token *tline; |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame] | 5166 | ExpDef *ed; |
| 5167 | ExpInv *ei; |
| 5168 | Line *l; |
| 5169 | int j; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5170 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5171 | while (1) { |
| 5172 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5173 | * Fetch a tokenized line, either from the expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5174 | * buffer or from the input file. |
| 5175 | */ |
| 5176 | tline = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5177 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5178 | while (1) { /* until we get a line we can use */ |
| 5179 | /* |
| 5180 | * Fetch a tokenized line from the expansion buffer |
| 5181 | */ |
| 5182 | if (istk->expansion != NULL) { |
| 5183 | ei = istk->expansion; |
| 5184 | if (ei->current != NULL) { |
| 5185 | if (ei->emitting == false) { |
| 5186 | ei->current = NULL; |
| 5187 | continue; |
| 5188 | } |
| 5189 | l = ei->current; |
| 5190 | ei->current = l->next; |
| 5191 | ei->lineno++; |
| 5192 | tline = copy_Token(l->first); |
| 5193 | if (((ei->type == EXP_REP) || |
| 5194 | (ei->type == EXP_MMACRO) || |
| 5195 | (ei->type == EXP_WHILE)) |
| 5196 | && (ei->def->nolist == false)) { |
| 5197 | char *p = detoken(tline, false); |
| 5198 | list->line(LIST_MACRO, p); |
| 5199 | nasm_free(p); |
| 5200 | } |
| 5201 | if (ei->linnum > -1) { |
| 5202 | src_set_linnum(src_get_linnum() + 1); |
| 5203 | } |
| 5204 | break; |
| 5205 | } else if ((ei->type == EXP_REP) && |
| 5206 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5207 | ei->def->cur_depth ++; |
| 5208 | ei->current = ei->def->line; |
| 5209 | ei->lineno = 0; |
| 5210 | continue; |
| 5211 | } else if ((ei->type == EXP_WHILE) && |
| 5212 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5213 | ei->current = ei->def->line; |
| 5214 | ei->lineno = 0; |
| 5215 | tline = copy_Token(ei->current->first); |
| 5216 | j = if_condition(tline, PP_WHILE); |
| 5217 | tline = NULL; |
| 5218 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 5219 | if (j == COND_IF_TRUE) { |
| 5220 | ei->current = ei->current->next; |
| 5221 | ei->def->cur_depth ++; |
| 5222 | } else { |
| 5223 | ei->emitting = false; |
| 5224 | ei->current = NULL; |
| 5225 | ei->def->cur_depth = ei->def->max_depth; |
| 5226 | } |
| 5227 | continue; |
| 5228 | } else { |
| 5229 | istk->expansion = ei->prev; |
| 5230 | ed = ei->def; |
| 5231 | if (ed != NULL) { |
| 5232 | if ((ei->emitting == true) && |
| 5233 | (ed->max_depth == DEADMAN_LIMIT) && |
| 5234 | (ed->cur_depth == DEADMAN_LIMIT) |
| 5235 | ) { |
| 5236 | error(ERR_FATAL, "runaway expansion detected, aborting"); |
| 5237 | } |
| 5238 | if (ed->cur_depth > 0) { |
| 5239 | ed->cur_depth --; |
Keith Kanios | 9412465 | 2010-12-18 11:47:28 -0600 | [diff] [blame] | 5240 | } else if (ed->type != EXP_MMACRO) { |
Keith Kanios | 6a7c3e9 | 2010-12-18 11:49:53 -0600 | [diff] [blame] | 5241 | expansions = ed->prev; |
| 5242 | free_expdef(ed); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5243 | } |
| 5244 | if ((ei->type == EXP_REP) || |
| 5245 | (ei->type == EXP_MMACRO) || |
| 5246 | (ei->type == EXP_WHILE)) { |
| 5247 | list->downlevel(LIST_MACRO); |
| 5248 | if (ei->type == EXP_MMACRO) { |
| 5249 | istk->mmac_depth--; |
| 5250 | } |
| 5251 | } |
| 5252 | } |
| 5253 | if (ei->linnum > -1) { |
| 5254 | src_set_linnum(ei->linnum); |
| 5255 | } |
| 5256 | free_expinv(ei); |
| 5257 | continue; |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | /* |
| 5262 | * Read in line from input and tokenize |
| 5263 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5264 | line = read_line(); |
| 5265 | if (line) { /* from the current input file */ |
| 5266 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5267 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5268 | nasm_free(line); |
| 5269 | break; |
| 5270 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5271 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5272 | /* |
| 5273 | * The current file has ended; work down the istk |
| 5274 | */ |
| 5275 | { |
| 5276 | Include *i = istk; |
| 5277 | fclose(i->fp); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5278 | if (i->expansion != NULL) { |
| 5279 | error(ERR_FATAL, |
| 5280 | "end of file while still in an expansion"); |
| 5281 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5282 | /* only set line and file name if there's a next node */ |
| 5283 | if (i->next) { |
| 5284 | src_set_linnum(i->lineno); |
Cyrill Gorcunov | d34a108 | 2011-03-07 11:23:08 +0300 | [diff] [blame] | 5285 | nasm_free(src_set_fname(nasm_strdup(i->fname))); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5286 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5287 | if ((i->next == NULL) && (finals != NULL)) { |
| 5288 | in_final = true; |
| 5289 | ei = new_ExpInv(EXP_FINAL, NULL); |
| 5290 | ei->emitting = true; |
| 5291 | ei->current = finals; |
| 5292 | istk->expansion = ei; |
| 5293 | finals = NULL; |
| 5294 | continue; |
| 5295 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5296 | istk = i->next; |
| 5297 | list->downlevel(LIST_INCLUDE); |
| 5298 | nasm_free(i); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5299 | if (istk == NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5300 | if (finals != NULL) { |
| 5301 | in_final = true; |
| 5302 | } else { |
| 5303 | return NULL; |
| 5304 | } |
| 5305 | } |
| 5306 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5307 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | if (defining == NULL) { |
| 5311 | tline = expand_mmac_params(tline); |
| 5312 | } |
| 5313 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5314 | /* |
| 5315 | * Check the line to see if it's a preprocessor directive. |
| 5316 | */ |
| 5317 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5318 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5319 | } else if (defining != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5320 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5321 | * We're defining an expansion. We emit nothing at all, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5322 | * and just shove the tokenized line on to the definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5323 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5324 | if (defining->ignoring == false) { |
| 5325 | Line *l = new_Line(); |
| 5326 | l->first = tline; |
| 5327 | if (defining->line == NULL) { |
| 5328 | defining->line = l; |
| 5329 | defining->last = l; |
| 5330 | } else { |
| 5331 | defining->last->next = l; |
| 5332 | defining->last = l; |
| 5333 | } |
| 5334 | } else { |
Keith Kanios | 104803d | 2010-12-18 11:05:46 -0600 | [diff] [blame] | 5335 | free_tlist(tline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5336 | } |
| 5337 | defining->linecount++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5338 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5339 | } else if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5340 | (istk->expansion->emitting != true)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5341 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5342 | * We're in a non-emitting branch of an expansion. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5343 | * Emit nothing at all, not even a blank line: when we |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5344 | * emerge from the expansion we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5345 | * directive so we keep our place correctly. |
| 5346 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5347 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5348 | continue; |
| 5349 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5350 | tline = expand_smacro(tline); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5351 | if (expand_mmacro(tline) != true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5352 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5353 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5354 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5355 | line = detoken(tline, true); |
| 5356 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5357 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5358 | } else { |
| 5359 | continue; |
| 5360 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5361 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5362 | } |
| 5363 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5366 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5367 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5368 | if (defining != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5369 | error(ERR_NONFATAL, "end of file while still defining an expansion"); |
| 5370 | while (defining != NULL) { |
| 5371 | ExpDef *ed = defining; |
| 5372 | defining = ed->prev; |
| 5373 | free_expdef(ed); |
| 5374 | } |
| 5375 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5376 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5377 | while (cstk != NULL) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5378 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5379 | free_macros(); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5380 | while (istk != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5381 | Include *i = istk; |
| 5382 | istk = istk->next; |
| 5383 | fclose(i->fp); |
| 5384 | nasm_free(i->fname); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5385 | while (i->expansion != NULL) { |
| 5386 | ExpInv *ei = i->expansion; |
| 5387 | i->expansion = ei->prev; |
| 5388 | free_expinv(ei); |
| 5389 | } |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5390 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5391 | } |
| 5392 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5393 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5394 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5395 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5396 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5397 | free_llist(predef); |
| 5398 | delete_Blocks(); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5399 | while ((i = ipath)) { |
| 5400 | ipath = i->next; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 5401 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5402 | nasm_free(i); |
| 5403 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5404 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5407 | void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5408 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5409 | IncPath *i = nasm_zalloc(sizeof(IncPath)); |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5410 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5411 | if (path) |
| 5412 | i->path = nasm_strdup(path); |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5413 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5414 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5415 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5416 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5417 | j = j->next; |
| 5418 | j->next = i; |
| 5419 | } else { |
| 5420 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5421 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5422 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5423 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5424 | void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5425 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5426 | Token *inc, *space, *name; |
| 5427 | Line *l; |
| 5428 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5429 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5430 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5431 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5432 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5433 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5434 | l->next = predef; |
| 5435 | l->first = inc; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5436 | predef = l; |
| 5437 | } |
| 5438 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5439 | void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5440 | { |
| 5441 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5442 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5443 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5444 | |
| 5445 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5446 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5447 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5448 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5449 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5450 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5451 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5452 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5453 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5454 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5455 | l->next = predef; |
| 5456 | l->first = def; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5457 | predef = l; |
| 5458 | } |
| 5459 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5460 | void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5461 | { |
| 5462 | Token *def, *space; |
| 5463 | Line *l; |
| 5464 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5465 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5466 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5467 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5468 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5469 | l = new_Line(); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5470 | l->next = predef; |
| 5471 | l->first = def; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5472 | predef = l; |
| 5473 | } |
| 5474 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5475 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5476 | * This function is used to assist with "runtime" preprocessor |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5477 | * directives, e.g. pp_runtime("%define __BITS__ 64"); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5478 | * |
| 5479 | * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU |
| 5480 | * PASS A VALID STRING TO THIS FUNCTION!!!!! |
| 5481 | */ |
| 5482 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5483 | void pp_runtime(char *definition) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5484 | { |
| 5485 | Token *def; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5486 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5487 | def = tokenize(definition); |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5488 | if (do_directive(def) == NO_DIRECTIVE_FOUND) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5489 | free_tlist(def); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5490 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 5493 | void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5494 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5495 | extrastdmac = macros; |
| 5496 | } |
| 5497 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5498 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5499 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5500 | char numbuf[20]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5501 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5502 | tok->text = nasm_strdup(numbuf); |
| 5503 | tok->type = TOK_NUMBER; |
| 5504 | } |
| 5505 | |
Cyrill Gorcunov | 86b2ad0 | 2011-07-01 10:38:25 +0400 | [diff] [blame] | 5506 | struct preproc_ops nasmpp = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5507 | pp_reset, |
| 5508 | pp_getline, |
| 5509 | pp_cleanup |
| 5510 | }; |