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 | }; |
| 327 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
| 328 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 329 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 330 | * These defines are used as the possible return values for do_directive |
| 331 | */ |
| 332 | #define NO_DIRECTIVE_FOUND 0 |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 333 | #define DIRECTIVE_FOUND 1 |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 334 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 335 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 336 | * This define sets the upper limit for smacro and expansions |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 337 | */ |
| 338 | #define DEADMAN_LIMIT (1 << 20) |
| 339 | |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 340 | /* max reps */ |
| 341 | #define REP_LIMIT ((INT64_C(1) << 62)) |
| 342 | |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 343 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 344 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 345 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 346 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 347 | * ones, so we need a different enum... |
| 348 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 349 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 350 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 351 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 352 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 353 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 354 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 355 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 356 | 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] | 357 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 358 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 359 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 360 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 361 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 362 | 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] | 363 | 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] | 364 | }; |
| 365 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 366 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 367 | * conditional compilation directives. Using the NASM pre-processor does |
| 368 | * not work, so we look for them specifically from the following list and |
| 369 | * then jam in the equivalent NASM directive into the input stream. |
| 370 | */ |
| 371 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 372 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 373 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 374 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 375 | }; |
| 376 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 377 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 378 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 379 | "ifndef", "include", "local" |
| 380 | }; |
| 381 | |
| 382 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 383 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 384 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 385 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 386 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 387 | static Context *cstk; |
| 388 | static Include *istk; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 389 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 390 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 391 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 392 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 393 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 394 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 395 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 396 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 397 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 398 | |
| 399 | static ListGen *list; |
| 400 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 401 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 402 | * The current set of expansion definitions we have defined. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 403 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 404 | static struct hash_table expdefs; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * The current set of single-line macros we have defined. |
| 408 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 409 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 410 | |
| 411 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 412 | * Linked List of all active expansion definitions |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 413 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 414 | struct ExpDef *expansions = NULL; |
| 415 | |
| 416 | /* |
| 417 | * The expansion we are currently defining |
| 418 | */ |
| 419 | static ExpDef *defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 420 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 421 | static uint64_t nested_mac_count; |
| 422 | static uint64_t nested_rep_count; |
| 423 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 424 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 425 | * Linked-list of lines to preprocess, prior to cleanup |
| 426 | */ |
| 427 | static Line *finals = NULL; |
| 428 | static bool in_final = false; |
| 429 | |
| 430 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 431 | * The number of macro parameters to allocate space for at a time. |
| 432 | */ |
| 433 | #define PARAM_DELTA 16 |
| 434 | |
| 435 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 436 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 437 | * 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] | 438 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 439 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 440 | |
| 441 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 442 | * The extra standard macros that come from the object format, if |
| 443 | * any. |
| 444 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 445 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 446 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 447 | |
| 448 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 449 | * Tokens are allocated in blocks to improve speed |
| 450 | */ |
| 451 | #define TOKEN_BLOCKSIZE 4096 |
| 452 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 453 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 454 | Blocks *next; |
| 455 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 459 | |
| 460 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 461 | * Forward declarations. |
| 462 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 463 | static Token *expand_mmac_params(Token * tline); |
| 464 | static Token *expand_smacro(Token * tline); |
| 465 | static Token *expand_id(Token * tline); |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 466 | static Context *get_ctx(const char *name, const char **namep); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 467 | static void make_tok_num(Token * tok, int64_t val); |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 468 | static void error(int severity, const char *fmt, ...); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 469 | static void error_precond(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 470 | static void *new_Block(size_t size); |
| 471 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 472 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 473 | const char *text, int txtlen); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 474 | static Token *copy_Token(Token * tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 475 | static Token *delete_Token(Token * t); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 476 | static Line *new_Line(void); |
| 477 | static ExpDef *new_ExpDef(int exp_type); |
| 478 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 482 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 483 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 484 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 485 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 486 | #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] | 487 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 488 | /* |
| 489 | * A few helpers for single macros |
| 490 | */ |
| 491 | |
| 492 | /* We might be not smacro parameter at all */ |
| 493 | static bool is_smacro_param(Token *t) |
| 494 | { |
| 495 | return t->type >= TOK_SMAC_PARAM; |
| 496 | } |
| 497 | |
| 498 | /* smacro parameters are counted in a special way */ |
| 499 | static int smacro_get_param_idx(Token *t) |
| 500 | { |
| 501 | return t->type - TOK_SMAC_PARAM; |
| 502 | } |
| 503 | |
| 504 | /* encode smacro parameter index */ |
| 505 | static int smacro_set_param_idx(Token *t, unsigned int index) |
| 506 | { |
| 507 | return t->type = TOK_SMAC_PARAM + index; |
| 508 | } |
| 509 | |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 510 | #ifdef NASM_TRACE |
| 511 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 512 | #define stringify(x) #x |
| 513 | |
Cyrill Gorcunov | 9d1141a | 2011-06-26 23:07:35 +0400 | [diff] [blame] | 514 | #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] | 515 | #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] | 516 | #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] | 517 | |
| 518 | /* FIXME: we really need some compound type here instead of inplace code */ |
| 519 | static const char *nasm_get_tok_type_str(enum pp_token_type type) |
| 520 | { |
| 521 | #define SWITCH_TOK_NAME(type) \ |
| 522 | case (type): \ |
| 523 | return stringify(type) |
| 524 | |
| 525 | switch (type) { |
| 526 | SWITCH_TOK_NAME(TOK_NONE); |
| 527 | SWITCH_TOK_NAME(TOK_WHITESPACE); |
| 528 | SWITCH_TOK_NAME(TOK_COMMENT); |
| 529 | SWITCH_TOK_NAME(TOK_ID); |
| 530 | SWITCH_TOK_NAME(TOK_PREPROC_ID); |
| 531 | SWITCH_TOK_NAME(TOK_STRING); |
| 532 | SWITCH_TOK_NAME(TOK_NUMBER); |
| 533 | SWITCH_TOK_NAME(TOK_FLOAT); |
| 534 | SWITCH_TOK_NAME(TOK_SMAC_END); |
| 535 | SWITCH_TOK_NAME(TOK_OTHER); |
| 536 | SWITCH_TOK_NAME(TOK_INTERNAL_STRING); |
| 537 | SWITCH_TOK_NAME(TOK_PREPROC_Q); |
| 538 | SWITCH_TOK_NAME(TOK_PREPROC_QQ); |
| 539 | SWITCH_TOK_NAME(TOK_PASTE); |
| 540 | SWITCH_TOK_NAME(TOK_INDIRECT); |
| 541 | SWITCH_TOK_NAME(TOK_SMAC_PARAM); |
| 542 | SWITCH_TOK_NAME(TOK_MAX); |
| 543 | } |
| 544 | |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | 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] | 549 | { |
| 550 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 551 | if (token) { |
| 552 | Token *t; |
| 553 | list_for_each(t, token) { |
| 554 | if (t->text) |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 555 | printf("'%s'(%s) ", t->text, |
| 556 | nasm_get_tok_type_str(t->type)); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 557 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 558 | printf("\n\n"); |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 562 | static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func) |
| 563 | { |
| 564 | printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token); |
| 565 | if (token) { |
| 566 | Token *t; |
| 567 | list_for_each(t, token) |
| 568 | printf("%s", t->text ? t->text : " "); |
| 569 | printf("\n\n"); |
| 570 | } |
| 571 | } |
| 572 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 573 | #else |
| 574 | #define nasm_trace(msg, ...) |
| 575 | #define nasm_dump_token(t) |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 576 | #define nasm_dump_stream(t) |
Cyrill Gorcunov | 49e8f69 | 2010-11-11 15:06:12 +0300 | [diff] [blame] | 577 | #endif |
| 578 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 579 | /* |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 580 | * nasm_unquote with error if the string contains NUL characters. |
| 581 | * If the string contains NUL characters, issue an error and return |
| 582 | * the C len, i.e. truncate at the NUL. |
| 583 | */ |
| 584 | static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive) |
| 585 | { |
| 586 | size_t len = nasm_unquote(qstr, NULL); |
| 587 | size_t clen = strlen(qstr); |
| 588 | |
| 589 | if (len != clen) |
| 590 | error(ERR_NONFATAL, "NUL character in `%s' directive", |
| 591 | pp_directives[directive]); |
| 592 | |
| 593 | return clen; |
| 594 | } |
| 595 | |
| 596 | /* |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 597 | * In-place reverse a list of tokens. |
| 598 | */ |
| 599 | static Token *reverse_tokens(Token *t) |
| 600 | { |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 601 | Token *prev, *next; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 602 | |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 603 | list_reverse(t, prev, next); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 604 | |
Cyrill Gorcunov | 3eba69a | 2011-04-13 13:15:02 +0400 | [diff] [blame] | 605 | return t; |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 609 | * Handle TASM specific directives, which do not contain a % in |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 610 | * front of them. We do it here because I could not find any other |
| 611 | * place to do it for the moment, and it is a hack (ideally it would |
| 612 | * be nice to be able to use the NASM pre-processor to do it). |
| 613 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 614 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 615 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 616 | int32_t i, j, k, m, len; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 617 | char *p, *q, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 618 | |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 619 | p = nasm_skip_spaces(line); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 620 | |
| 621 | /* Binary search for the directive name */ |
| 622 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 623 | j = ARRAY_SIZE(tasm_directives); |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 624 | q = nasm_skip_word(p); |
| 625 | len = q - p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 626 | if (len) { |
| 627 | oldchar = p[len]; |
| 628 | p[len] = 0; |
| 629 | while (j - i > 1) { |
| 630 | k = (j + i) / 2; |
| 631 | m = nasm_stricmp(p, tasm_directives[k]); |
| 632 | if (m == 0) { |
| 633 | /* We have found a directive, so jam a % in front of it |
| 634 | * so that NASM will then recognise it as one if it's own. |
| 635 | */ |
| 636 | p[len] = oldchar; |
| 637 | len = strlen(p); |
| 638 | oldline = line; |
| 639 | line = nasm_malloc(len + 2); |
| 640 | line[0] = '%'; |
| 641 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 642 | /* |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 643 | * NASM does not recognise IFDIFI, so we convert |
| 644 | * it to %if 0. This is not used in NASM |
| 645 | * compatible code, but does need to parse for the |
| 646 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 647 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 648 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 649 | } else { |
| 650 | memcpy(line + 1, p, len + 1); |
| 651 | } |
| 652 | nasm_free(oldline); |
| 653 | return line; |
| 654 | } else if (m < 0) { |
| 655 | j = k; |
| 656 | } else |
| 657 | i = k; |
| 658 | } |
| 659 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 660 | } |
| 661 | return line; |
| 662 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 663 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 664 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 665 | * The pre-preprocessing stage... This function translates line |
| 666 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 667 | * flags') into NASM preprocessor line number indications (`%line |
| 668 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 669 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 670 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 671 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 672 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 673 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 674 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 675 | if (line[0] == '#' && line[1] == ' ') { |
| 676 | oldline = line; |
| 677 | fname = oldline + 2; |
| 678 | lineno = atoi(fname); |
| 679 | fname += strspn(fname, "0123456789 "); |
| 680 | if (*fname == '"') |
| 681 | fname++; |
| 682 | fnlen = strcspn(fname, "\""); |
| 683 | line = nasm_malloc(20 + fnlen); |
| 684 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 685 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 686 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 687 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 688 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 689 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 693 | * Free a linked list of tokens. |
| 694 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 695 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 696 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 697 | while (list) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 698 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /* |
| 702 | * Free a linked list of lines. |
| 703 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 704 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 705 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 706 | Line *l, *tmp; |
| 707 | list_for_each_safe(l, tmp, list) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 708 | free_tlist(l->first); |
| 709 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
| 713 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 714 | * Free an ExpDef |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 715 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 716 | static void free_expdef(ExpDef * ed) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 717 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 718 | nasm_free(ed->name); |
| 719 | free_tlist(ed->dlist); |
| 720 | nasm_free(ed->defaults); |
| 721 | free_llist(ed->line); |
| 722 | nasm_free(ed); |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * Free an ExpInv |
| 727 | */ |
| 728 | static void free_expinv(ExpInv * ei) |
| 729 | { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 730 | nasm_free(ei->name); |
| 731 | nasm_free(ei->label_text); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 732 | nasm_free(ei); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 736 | * Free all currently defined macros, and free the hash tables |
| 737 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 738 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 739 | { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 740 | SMacro *s, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 741 | const char *key; |
| 742 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 743 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 744 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 745 | nasm_free((void *)key); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 746 | list_for_each_safe(s, tmp, s) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 747 | nasm_free(s->name); |
| 748 | free_tlist(s->expansion); |
| 749 | nasm_free(s); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 750 | } |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 751 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 752 | hash_free(smt); |
| 753 | } |
| 754 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 755 | static void free_expdef_table(struct hash_table *edt) |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 756 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 757 | ExpDef *ed, *tmp; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 758 | const char *key; |
| 759 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 760 | |
| 761 | it = NULL; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 762 | while ((ed = hash_iterate(edt, &it, &key)) != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 763 | nasm_free((void *)key); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 764 | list_for_each_safe(ed ,tmp, ed) |
| 765 | free_expdef(ed); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 766 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 767 | hash_free(edt); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void free_macros(void) |
| 771 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 772 | free_smacro_table(&smacros); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 773 | free_expdef_table(&expdefs); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Initialize the hash tables |
| 778 | */ |
| 779 | static void init_macros(void) |
| 780 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 781 | hash_init(&smacros, HASH_LARGE); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 782 | hash_init(&expdefs, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 786 | * Pop the context stack. |
| 787 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 788 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 789 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 790 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 791 | |
| 792 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 793 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 794 | nasm_free(c->name); |
| 795 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 796 | } |
| 797 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 798 | /* |
| 799 | * Search for a key in the hash index; adding it if necessary |
| 800 | * (in which case we initialize the data pointer to NULL.) |
| 801 | */ |
| 802 | static void ** |
| 803 | hash_findi_add(struct hash_table *hash, const char *str) |
| 804 | { |
| 805 | struct hash_insert hi; |
| 806 | void **r; |
| 807 | char *strx; |
| 808 | |
| 809 | r = hash_findi(hash, str, &hi); |
| 810 | if (r) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 811 | return r; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 812 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 813 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 814 | return hash_add(&hi, strx, NULL); |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Like hash_findi, but returns the data element rather than a pointer |
| 819 | * to it. Used only when not adding a new element, hence no third |
| 820 | * argument. |
| 821 | */ |
| 822 | static void * |
| 823 | hash_findix(struct hash_table *hash, const char *str) |
| 824 | { |
| 825 | void **p; |
| 826 | |
| 827 | p = hash_findi(hash, str, NULL); |
| 828 | return p ? *p : NULL; |
| 829 | } |
| 830 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 831 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 832 | * read line from standard macros set, |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 833 | * if there no more left -- return NULL |
| 834 | */ |
| 835 | static char *line_from_stdmac(void) |
| 836 | { |
| 837 | unsigned char c; |
| 838 | const unsigned char *p = stdmacpos; |
| 839 | char *line, *q; |
| 840 | size_t len = 0; |
| 841 | |
| 842 | if (!stdmacpos) |
| 843 | return NULL; |
| 844 | |
| 845 | while ((c = *p++)) { |
| 846 | if (c >= 0x80) |
| 847 | len += pp_directives_len[c - 0x80] + 1; |
| 848 | else |
| 849 | len++; |
| 850 | } |
| 851 | |
| 852 | line = nasm_malloc(len + 1); |
| 853 | q = line; |
| 854 | while ((c = *stdmacpos++)) { |
| 855 | if (c >= 0x80) { |
| 856 | memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]); |
| 857 | q += pp_directives_len[c - 0x80]; |
| 858 | *q++ = ' '; |
| 859 | } else { |
| 860 | *q++ = c; |
| 861 | } |
| 862 | } |
| 863 | stdmacpos = p; |
| 864 | *q = '\0'; |
| 865 | |
| 866 | if (!*stdmacpos) { |
| 867 | /* This was the last of the standard macro chain... */ |
| 868 | stdmacpos = NULL; |
| 869 | if (any_extrastdmac) { |
| 870 | stdmacpos = extrastdmac; |
| 871 | any_extrastdmac = false; |
| 872 | } else if (do_predef) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 873 | ExpInv *ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 874 | Line *pd, *l; |
| 875 | Token *head, **tail, *t; |
| 876 | |
| 877 | /* |
| 878 | * Nasty hack: here we push the contents of |
| 879 | * `predef' on to the top-level expansion stack, |
| 880 | * since this is the most convenient way to |
| 881 | * implement the pre-include and pre-define |
| 882 | * features. |
| 883 | */ |
| 884 | list_for_each(pd, predef) { |
| 885 | head = NULL; |
| 886 | tail = &head; |
| 887 | list_for_each(t, pd->first) { |
| 888 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 889 | tail = &(*tail)->next; |
| 890 | } |
| 891 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 892 | l = new_Line(); |
| 893 | l->first = head; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 894 | ei = new_ExpInv(EXP_PREDEF, NULL); |
| 895 | ei->current = l; |
| 896 | ei->emitting = true; |
| 897 | ei->prev = istk->expansion; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 898 | istk->expansion = ei; |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 899 | } |
| 900 | do_predef = false; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | return line; |
| 905 | } |
| 906 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 907 | #define BUF_DELTA 512 |
| 908 | /* |
| 909 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 910 | * at the end of the line read, and handling spurious ^Zs. Will |
| 911 | * return lines from the standard macro set if this has not already |
| 912 | * been done. |
| 913 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 914 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 915 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 916 | char *buffer, *p, *q; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 917 | int bufsize, continued_count; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 918 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 919 | /* |
| 920 | * standart macros set (predefined) goes first |
| 921 | */ |
| 922 | p = line_from_stdmac(); |
| 923 | if (p) |
| 924 | return p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 925 | |
Cyrill Gorcunov | 15bdc51 | 2010-07-13 11:27:41 +0400 | [diff] [blame] | 926 | /* |
| 927 | * regular read from a file |
| 928 | */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 929 | bufsize = BUF_DELTA; |
| 930 | buffer = nasm_malloc(BUF_DELTA); |
| 931 | p = buffer; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 932 | continued_count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 933 | while (1) { |
| 934 | q = fgets(p, bufsize - (p - buffer), istk->fp); |
| 935 | if (!q) |
| 936 | break; |
| 937 | p += strlen(p); |
| 938 | if (p > buffer && p[-1] == '\n') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 939 | /* |
| 940 | * Convert backslash-CRLF line continuation sequences into |
| 941 | * nothing at all (for DOS and Windows) |
| 942 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 943 | if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { |
| 944 | p -= 3; |
| 945 | *p = 0; |
| 946 | continued_count++; |
| 947 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 948 | /* |
| 949 | * Also convert backslash-LF line continuation sequences into |
| 950 | * nothing at all (for Unix) |
| 951 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 952 | else if (((p - 1) > buffer) && (p[-2] == '\\')) { |
| 953 | p -= 2; |
| 954 | *p = 0; |
| 955 | continued_count++; |
| 956 | } else { |
| 957 | break; |
| 958 | } |
| 959 | } |
| 960 | if (p - buffer > bufsize - 10) { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 961 | int32_t offset = p - buffer; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 962 | bufsize += BUF_DELTA; |
| 963 | buffer = nasm_realloc(buffer, bufsize); |
| 964 | p = buffer + offset; /* prevent stale-pointer problems */ |
| 965 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 966 | } |
| 967 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 968 | if (!q && p == buffer) { |
| 969 | nasm_free(buffer); |
| 970 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 973 | src_set_linnum(src_get_linnum() + istk->lineinc + |
| 974 | (continued_count * istk->lineinc)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 975 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 976 | /* |
| 977 | * Play safe: remove CRs as well as LFs, if any of either are |
| 978 | * present at the end of the line. |
| 979 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 980 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 981 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 982 | |
| 983 | /* |
| 984 | * Handle spurious ^Z, which may be inserted into source files |
| 985 | * by some file transfer utilities. |
| 986 | */ |
| 987 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 988 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 989 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 990 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 991 | return buffer; |
| 992 | } |
| 993 | |
| 994 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 995 | * 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] | 996 | * don't need to parse the value out of e.g. numeric tokens: we |
| 997 | * simply split one string into many. |
| 998 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 999 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1000 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 1001 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1002 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1003 | Token *list = NULL; |
| 1004 | Token *t, **tail = &list; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1005 | bool verbose = true; |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 1006 | |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1007 | nasm_trace("Tokenize for '%s'", line); |
| 1008 | |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1009 | if ((defining != NULL) && (defining->ignoring == true)) { |
| 1010 | verbose = false; |
| 1011 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1012 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1013 | while (*line) { |
| 1014 | p = line; |
| 1015 | if (*p == '%') { |
| 1016 | p++; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1017 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 1018 | p++; |
| 1019 | type = TOK_PASTE; |
| 1020 | } else if (nasm_isdigit(*p) || |
| 1021 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1022 | do { |
| 1023 | p++; |
| 1024 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1025 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1026 | type = TOK_PREPROC_ID; |
| 1027 | } else if (*p == '{') { |
| 1028 | p++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1029 | while (*p && *p != '}') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1030 | p[-1] = *p; |
| 1031 | p++; |
| 1032 | } |
| 1033 | p[-1] = '\0'; |
| 1034 | if (*p) |
| 1035 | p++; |
| 1036 | type = TOK_PREPROC_ID; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1037 | } else if (*p == '[') { |
| 1038 | int lvl = 1; |
| 1039 | line += 2; /* Skip the leading %[ */ |
| 1040 | p++; |
| 1041 | while (lvl && (c = *p++)) { |
| 1042 | switch (c) { |
| 1043 | case ']': |
| 1044 | lvl--; |
| 1045 | break; |
| 1046 | case '%': |
| 1047 | if (*p == '[') |
| 1048 | lvl++; |
| 1049 | break; |
| 1050 | case '\'': |
| 1051 | case '\"': |
| 1052 | case '`': |
Cyrill Gorcunov | c6360a7 | 2010-07-13 13:32:19 +0400 | [diff] [blame] | 1053 | p = nasm_skip_string(p - 1) + 1; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1054 | break; |
| 1055 | default: |
| 1056 | break; |
| 1057 | } |
| 1058 | } |
| 1059 | p--; |
| 1060 | if (*p) |
| 1061 | *p++ = '\0'; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1062 | if (lvl && verbose) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1063 | error(ERR_NONFATAL, "unterminated %[ construct"); |
| 1064 | type = TOK_INDIRECT; |
| 1065 | } else if (*p == '?') { |
| 1066 | type = TOK_PREPROC_Q; /* %? */ |
| 1067 | p++; |
| 1068 | if (*p == '?') { |
| 1069 | type = TOK_PREPROC_QQ; /* %?? */ |
| 1070 | p++; |
| 1071 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1072 | } else if (*p == '!') { |
| 1073 | type = TOK_PREPROC_ID; |
| 1074 | p++; |
| 1075 | if (isidchar(*p)) { |
| 1076 | do { |
| 1077 | p++; |
| 1078 | } while (isidchar(*p)); |
| 1079 | } else if (*p == '\'' || *p == '\"' || *p == '`') { |
| 1080 | p = nasm_skip_string(p); |
| 1081 | if (*p) |
| 1082 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1083 | else if(verbose) |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1084 | error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string"); |
| 1085 | } else { |
| 1086 | /* %! without string or identifier */ |
| 1087 | type = TOK_OTHER; /* Legacy behavior... */ |
| 1088 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1089 | } else if (isidchar(*p) || |
| 1090 | ((*p == '!' || *p == '%' || *p == '$') && |
| 1091 | isidchar(p[1]))) { |
| 1092 | do { |
| 1093 | p++; |
| 1094 | } |
| 1095 | while (isidchar(*p)); |
| 1096 | type = TOK_PREPROC_ID; |
| 1097 | } else { |
| 1098 | type = TOK_OTHER; |
| 1099 | if (*p == '%') |
| 1100 | p++; |
| 1101 | } |
| 1102 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 1103 | type = TOK_ID; |
| 1104 | p++; |
| 1105 | while (*p && isidchar(*p)) |
| 1106 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 1107 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1108 | /* |
| 1109 | * A string token. |
| 1110 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1111 | type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1112 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1113 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1114 | if (*p) { |
| 1115 | p++; |
Keith Kanios | 6faad4e | 2010-12-18 14:08:02 -0600 | [diff] [blame] | 1116 | } else if(verbose) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1117 | error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1118 | /* Handling unterminated strings by UNV */ |
| 1119 | /* type = -1; */ |
| 1120 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1121 | } else if (p[0] == '$' && p[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1122 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 1123 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1124 | } else if (isnumstart(*p)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1125 | bool is_hex = false; |
| 1126 | bool is_float = false; |
| 1127 | bool has_e = false; |
| 1128 | char c, *r; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1129 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1130 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1131 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1132 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1133 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1134 | if (*p == '$') { |
| 1135 | p++; |
| 1136 | is_hex = true; |
| 1137 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1138 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1139 | for (;;) { |
| 1140 | c = *p++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1141 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1142 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 1143 | has_e = true; |
| 1144 | if (*p == '+' || *p == '-') { |
| 1145 | /* |
| 1146 | * e can only be followed by +/- if it is either a |
| 1147 | * prefixed hex number or a floating-point number |
| 1148 | */ |
| 1149 | p++; |
| 1150 | is_float = true; |
| 1151 | } |
| 1152 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 1153 | is_hex = true; |
| 1154 | } else if (c == 'P' || c == 'p') { |
| 1155 | is_float = true; |
| 1156 | if (*p == '+' || *p == '-') |
| 1157 | p++; |
| 1158 | } else if (isnumchar(c) || c == '_') |
| 1159 | ; /* just advance */ |
| 1160 | else if (c == '.') { |
| 1161 | /* |
| 1162 | * we need to deal with consequences of the legacy |
| 1163 | * parser, like "1.nolist" being two tokens |
| 1164 | * (TOK_NUMBER, TOK_ID) here; at least give it |
| 1165 | * a shot for now. In the future, we probably need |
| 1166 | * a flex-based scanner with proper pattern matching |
| 1167 | * to do it as well as it can be done. Nothing in |
| 1168 | * the world is going to help the person who wants |
| 1169 | * 0x123.p16 interpreted as two tokens, though. |
| 1170 | */ |
| 1171 | r = p; |
| 1172 | while (*r == '_') |
| 1173 | r++; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1174 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1175 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
| 1176 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 1177 | (*r == 'p' || *r == 'P')) { |
| 1178 | p = r; |
| 1179 | is_float = true; |
| 1180 | } else |
| 1181 | break; /* Terminate the token */ |
| 1182 | } else |
| 1183 | break; |
| 1184 | } |
| 1185 | p--; /* Point to first character beyond number */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1186 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1187 | if (p == line+1 && *line == '$') { |
| 1188 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 1189 | } else { |
| 1190 | if (has_e && !is_hex) { |
| 1191 | /* 1e13 is floating-point, but 1e13h is not */ |
| 1192 | is_float = true; |
| 1193 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 1194 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1195 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
| 1196 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 1197 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1198 | type = TOK_WHITESPACE; |
Cyrill Gorcunov | f66ac7d | 2009-10-12 20:41:13 +0400 | [diff] [blame] | 1199 | p = nasm_skip_spaces(p); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1200 | /* |
| 1201 | * Whitespace just before end-of-line is discarded by |
| 1202 | * pretending it's a comment; whitespace just before a |
| 1203 | * comment gets lumped into the comment. |
| 1204 | */ |
| 1205 | if (!*p || *p == ';') { |
| 1206 | type = TOK_COMMENT; |
| 1207 | while (*p) |
| 1208 | p++; |
| 1209 | } |
| 1210 | } else if (*p == ';') { |
| 1211 | type = TOK_COMMENT; |
| 1212 | while (*p) |
| 1213 | p++; |
| 1214 | } else { |
| 1215 | /* |
| 1216 | * Anything else is an operator of some kind. We check |
| 1217 | * for all the double-character operators (>>, <<, //, |
| 1218 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1219 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1220 | */ |
| 1221 | type = TOK_OTHER; |
| 1222 | if ((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[0] == '^' && p[1] == '^')) { |
| 1233 | p++; |
| 1234 | } |
| 1235 | p++; |
| 1236 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1237 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1238 | /* Handling unterminated string by UNV */ |
| 1239 | /*if (type == -1) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1240 | { |
| 1241 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 1242 | t->text[p-line] = *line; |
| 1243 | tail = &t->next; |
| 1244 | } |
| 1245 | else */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1246 | if (type != TOK_COMMENT) { |
| 1247 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1248 | tail = &t->next; |
| 1249 | } |
| 1250 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1251 | } |
Cyrill Gorcunov | fc0c128 | 2011-06-25 19:51:44 +0400 | [diff] [blame] | 1252 | |
| 1253 | nasm_dump_token(list); |
| 1254 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1255 | return list; |
| 1256 | } |
| 1257 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1258 | /* |
| 1259 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1260 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1261 | * deleted only all at once by the delete_Blocks function. |
| 1262 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1263 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1264 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1265 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1266 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1267 | /* first, get to the end of the linked list */ |
| 1268 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1269 | b = b->next; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1270 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1271 | /* now allocate the requested chunk */ |
| 1272 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1273 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1274 | /* now allocate a new block for the next request */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1275 | b->next = nasm_zalloc(sizeof(Blocks)); |
| 1276 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1277 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * this function deletes all managed blocks of memory |
| 1282 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1283 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1284 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1285 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1286 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1287 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1288 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1289 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1290 | * free it. |
| 1291 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1292 | while (b) { |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 1293 | nasm_free(b->chunk); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1294 | a = b; |
| 1295 | b = b->next; |
| 1296 | if (a != &blocks) |
| 1297 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1298 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1299 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1300 | |
| 1301 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1302 | * 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] | 1303 | * 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] | 1304 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1305 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1306 | static Token *new_Token(Token * next, enum pp_token_type type, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1307 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1308 | { |
| 1309 | Token *t; |
| 1310 | int i; |
| 1311 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1312 | if (!freeTokens) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1313 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1314 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1315 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1316 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1317 | } |
| 1318 | t = freeTokens; |
| 1319 | freeTokens = t->next; |
| 1320 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1321 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1322 | t->type = type; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 1323 | if (type == TOK_WHITESPACE || !text) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1324 | t->text = NULL; |
| 1325 | } else { |
| 1326 | if (txtlen == 0) |
| 1327 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1328 | t->text = nasm_malloc(txtlen+1); |
| 1329 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1330 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1331 | } |
| 1332 | return t; |
| 1333 | } |
| 1334 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1335 | static Token *copy_Token(Token * tline) |
| 1336 | { |
| 1337 | Token *t, *tt, *first = NULL, *prev = NULL; |
| 1338 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1339 | for (tt = tline; tt != NULL; tt = tt->next) { |
| 1340 | if (!freeTokens) { |
| 1341 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1342 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1343 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1344 | freeTokens[i].next = NULL; |
| 1345 | } |
| 1346 | t = freeTokens; |
| 1347 | freeTokens = t->next; |
| 1348 | t->next = NULL; |
| 1349 | t->text = tt->text ? nasm_strdup(tt->text) : NULL; |
| 1350 | t->a.mac = tt->a.mac; |
| 1351 | t->a.len = tt->a.len; |
| 1352 | t->type = tt->type; |
| 1353 | if (prev != NULL) { |
| 1354 | prev->next = t; |
| 1355 | } else { |
| 1356 | first = t; |
| 1357 | } |
| 1358 | prev = t; |
| 1359 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1360 | return first; |
| 1361 | } |
| 1362 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1363 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1364 | { |
| 1365 | Token *next = t->next; |
| 1366 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1367 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1368 | freeTokens = t; |
| 1369 | return next; |
| 1370 | } |
| 1371 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1372 | /* |
| 1373 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1374 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1375 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1376 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1377 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1378 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1379 | Token *t; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1380 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1381 | const char *q; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1382 | int len = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1383 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1384 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1385 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1386 | char *v; |
| 1387 | char *q = t->text; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1388 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1389 | v = t->text + 2; |
| 1390 | if (*v == '\'' || *v == '\"' || *v == '`') { |
| 1391 | size_t len = nasm_unquote(v, NULL); |
| 1392 | size_t clen = strlen(v); |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1393 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1394 | if (len != clen) { |
| 1395 | error(ERR_NONFATAL | ERR_PASS1, |
| 1396 | "NUL character in %! string"); |
| 1397 | v = NULL; |
| 1398 | } |
| 1399 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1400 | |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1401 | if (v) { |
| 1402 | char *p = getenv(v); |
| 1403 | if (!p) { |
| 1404 | error(ERR_NONFATAL | ERR_PASS1, |
| 1405 | "nonexistent environment variable `%s'", v); |
| 1406 | p = ""; |
| 1407 | } |
| 1408 | t->text = nasm_strdup(p); |
| 1409 | } |
| 1410 | nasm_free(q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1411 | } |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1412 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1413 | /* Expand local macros here and not during preprocessing */ |
| 1414 | if (expand_locals && |
| 1415 | t->type == TOK_PREPROC_ID && t->text && |
| 1416 | t->text[0] == '%' && t->text[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1417 | const char *q; |
| 1418 | char *p; |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1419 | Context *ctx = get_ctx(t->text, &q); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1420 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1421 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1422 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1423 | p = nasm_strcat(buffer, q); |
| 1424 | nasm_free(t->text); |
| 1425 | t->text = p; |
| 1426 | } |
| 1427 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1428 | |
| 1429 | /* Expand %? and %?? directives */ |
Keith Kanios | 3136d48 | 2010-11-13 09:34:34 -0600 | [diff] [blame] | 1430 | if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1431 | ((t->type == TOK_PREPROC_Q) || |
| 1432 | (t->type == TOK_PREPROC_QQ))) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1433 | ExpInv *ei; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1434 | for (ei = istk->expansion; ei != NULL; ei = ei->prev){ |
| 1435 | if (ei->type == EXP_MMACRO) { |
| 1436 | nasm_free(t->text); |
| 1437 | if (t->type == TOK_PREPROC_Q) { |
| 1438 | t->text = nasm_strdup(ei->name); |
| 1439 | } else { |
| 1440 | t->text = nasm_strdup(ei->def->name); |
| 1441 | } |
| 1442 | break; |
| 1443 | } |
| 1444 | } |
| 1445 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1446 | |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1447 | if (t->type == TOK_WHITESPACE) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1448 | len++; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1449 | else if (t->text) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1450 | len += strlen(t->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1451 | } |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1452 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1453 | p = line = nasm_malloc(len + 1); |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1454 | |
| 1455 | list_for_each(t, tlist) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1456 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1457 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1458 | } else if (t->text) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1459 | q = t->text; |
| 1460 | while (*q) |
| 1461 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1462 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1463 | } |
| 1464 | *p = '\0'; |
Cyrill Gorcunov | f32ed14 | 2010-04-09 15:41:48 +0400 | [diff] [blame] | 1465 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1466 | return line; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1470 | * Initialize a new Line |
| 1471 | */ |
Cyrill Gorcunov | 29cb0bb | 2010-11-11 11:19:43 +0300 | [diff] [blame] | 1472 | static inline Line *new_Line(void) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1473 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 1474 | return (Line *)nasm_zalloc(sizeof(Line)); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | |
| 1478 | /* |
| 1479 | * Initialize a new Expansion Definition |
| 1480 | */ |
| 1481 | static ExpDef *new_ExpDef(int exp_type) |
| 1482 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1483 | ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef)); |
| 1484 | ed->type = exp_type; |
| 1485 | ed->casesense = true; |
| 1486 | ed->state = COND_NEVER; |
| 1487 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1488 | return ed; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | /* |
| 1493 | * Initialize a new Expansion Instance |
| 1494 | */ |
| 1495 | static ExpInv *new_ExpInv(int exp_type, ExpDef *ed) |
| 1496 | { |
Cyrill Gorcunov | c515774 | 2010-11-11 11:29:40 +0300 | [diff] [blame] | 1497 | ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv)); |
| 1498 | ei->type = exp_type; |
| 1499 | ei->def = ed; |
| 1500 | ei->unique = ++unique; |
| 1501 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 1502 | if ((istk->mmac_depth < 1) && |
| 1503 | (istk->expansion == NULL) && |
| 1504 | (ed != NULL) && |
| 1505 | (ed->type != EXP_MMACRO) && |
| 1506 | (ed->type != EXP_REP) && |
| 1507 | (ed->type != EXP_WHILE)) { |
| 1508 | ei->linnum = src_get_linnum(); |
| 1509 | src_set_linnum(ei->linnum - ed->linecount - 1); |
| 1510 | } else { |
| 1511 | ei->linnum = -1; |
| 1512 | } |
| 1513 | if ((istk->expansion == NULL) || |
| 1514 | (ei->type == EXP_MMACRO)) { |
| 1515 | ei->relno = 0; |
| 1516 | } else { |
| 1517 | ei->relno = istk->expansion->lineno; |
| 1518 | if (ed != NULL) { |
| 1519 | ei->relno -= (ed->linecount + 1); |
| 1520 | } |
| 1521 | } |
| 1522 | return ei; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1526 | * A scanner, suitable for use by the expression evaluator, which |
| 1527 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1528 | * the first token in the line to be passed in as its private_data |
| 1529 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1530 | * |
| 1531 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1532 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1533 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1534 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1535 | Token **tlineptr = private_data; |
| 1536 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1537 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1538 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1539 | do { |
| 1540 | tline = *tlineptr; |
| 1541 | *tlineptr = tline ? tline->next : NULL; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 1542 | } while (tline && (tline->type == TOK_WHITESPACE || |
| 1543 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1546 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1547 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1548 | tokval->t_charptr = tline->text; |
| 1549 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1550 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1551 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1552 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1553 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1554 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1555 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1556 | p = tokval->t_charptr = tline->text; |
| 1557 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1558 | tokval->t_charptr++; |
| 1559 | return tokval->t_type = TOKEN_ID; |
| 1560 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1561 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1562 | for (r = p, s = ourcopy; *r; r++) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1563 | if (r >= p+MAX_KEYWORD) |
| 1564 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1565 | *s++ = nasm_tolower(*r); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1566 | } |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1567 | *s = '\0'; |
| 1568 | /* right, so we have an identifier sitting in temp storage. now, |
| 1569 | * is it actually a register or instruction name, or what? */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1570 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1573 | if (tline->type == TOK_NUMBER) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1574 | bool rn_error; |
| 1575 | tokval->t_integer = readnum(tline->text, &rn_error); |
| 1576 | tokval->t_charptr = tline->text; |
| 1577 | if (rn_error) |
| 1578 | return tokval->t_type = TOKEN_ERRNUM; |
| 1579 | else |
| 1580 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1581 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1582 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1583 | if (tline->type == TOK_FLOAT) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1584 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1587 | if (tline->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1588 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1589 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1590 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1591 | tokval->t_charptr = tline->text; |
| 1592 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1593 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1594 | if (ep[0] != bq || ep[1] != '\0') |
| 1595 | return tokval->t_type = TOKEN_ERRSTR; |
| 1596 | else |
| 1597 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1600 | if (tline->type == TOK_OTHER) { |
| 1601 | if (!strcmp(tline->text, "<<")) |
| 1602 | return tokval->t_type = TOKEN_SHL; |
| 1603 | if (!strcmp(tline->text, ">>")) |
| 1604 | return tokval->t_type = TOKEN_SHR; |
| 1605 | if (!strcmp(tline->text, "//")) |
| 1606 | return tokval->t_type = TOKEN_SDIV; |
| 1607 | if (!strcmp(tline->text, "%%")) |
| 1608 | return tokval->t_type = TOKEN_SMOD; |
| 1609 | if (!strcmp(tline->text, "==")) |
| 1610 | return tokval->t_type = TOKEN_EQ; |
| 1611 | if (!strcmp(tline->text, "<>")) |
| 1612 | return tokval->t_type = TOKEN_NE; |
| 1613 | if (!strcmp(tline->text, "!=")) |
| 1614 | return tokval->t_type = TOKEN_NE; |
| 1615 | if (!strcmp(tline->text, "<=")) |
| 1616 | return tokval->t_type = TOKEN_LE; |
| 1617 | if (!strcmp(tline->text, ">=")) |
| 1618 | return tokval->t_type = TOKEN_GE; |
| 1619 | if (!strcmp(tline->text, "&&")) |
| 1620 | return tokval->t_type = TOKEN_DBL_AND; |
| 1621 | if (!strcmp(tline->text, "^^")) |
| 1622 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1623 | if (!strcmp(tline->text, "||")) |
| 1624 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * We have no other options: just return the first character of |
| 1629 | * the token text. |
| 1630 | */ |
| 1631 | return tokval->t_type = tline->text[0]; |
| 1632 | } |
| 1633 | |
| 1634 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1635 | * Compare a string to the name of an existing macro; this is a |
| 1636 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1637 | * depending on the value of the `casesense' parameter. |
| 1638 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1639 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1640 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1641 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1645 | * Compare a string to the name of an existing macro; this is a |
| 1646 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1647 | * depending on the value of the `casesense' parameter. |
| 1648 | */ |
| 1649 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1650 | { |
| 1651 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1652 | } |
| 1653 | |
| 1654 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1655 | * Return the Context structure associated with a %$ token. Return |
| 1656 | * NULL, having _already_ reported an error condition, if the |
| 1657 | * context stack isn't deep enough for the supplied number of $ |
| 1658 | * signs. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1659 | * |
| 1660 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1661 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1662 | */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1663 | static Context *get_ctx(const char *name, const char **namep) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1664 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1665 | Context *ctx; |
| 1666 | int i; |
| 1667 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1668 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1669 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1670 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1671 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1672 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1673 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1674 | if (!cstk) { |
| 1675 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
| 1676 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1679 | name += 2; |
| 1680 | ctx = cstk; |
| 1681 | i = 0; |
| 1682 | while (ctx && *name == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1683 | name++; |
| 1684 | i++; |
| 1685 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1686 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1687 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1688 | if (!ctx) { |
| 1689 | error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1690 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1691 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1692 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1693 | |
| 1694 | if (namep) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1695 | *namep = name; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1696 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1697 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1701 | * Check to see if a file is already in a string list |
| 1702 | */ |
| 1703 | static bool in_list(const StrList *list, const char *str) |
| 1704 | { |
| 1705 | while (list) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1706 | if (!strcmp(list->str, str)) |
| 1707 | return true; |
| 1708 | list = list->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1709 | } |
| 1710 | return false; |
| 1711 | } |
| 1712 | |
| 1713 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1714 | * Open an include file. This routine must always return a valid |
| 1715 | * file pointer if it returns - it's responsible for throwing an |
| 1716 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1717 | * the include path one by one until it finds the file or reaches |
| 1718 | * the end of the path. |
| 1719 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1720 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1721 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1722 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1723 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1724 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1725 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1726 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1727 | size_t prefix_len = 0; |
| 1728 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1729 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1730 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1731 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
Cyrill Gorcunov | 6f38fe6 | 2010-11-11 11:32:16 +0300 | [diff] [blame] | 1732 | sl->next = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1733 | memcpy(sl->str, prefix, prefix_len); |
| 1734 | memcpy(sl->str+prefix_len, file, len+1); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1735 | fp = fopen(sl->str, "r"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1736 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1737 | **dtail = sl; |
| 1738 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1739 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1740 | nasm_free(sl); |
| 1741 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1742 | if (fp) |
| 1743 | return fp; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1744 | if (!ip) { |
| 1745 | if (!missing_ok) |
| 1746 | break; |
| 1747 | prefix = NULL; |
| 1748 | } else { |
| 1749 | prefix = ip->path; |
| 1750 | ip = ip->next; |
| 1751 | } |
| 1752 | if (prefix) { |
| 1753 | prefix_len = strlen(prefix); |
| 1754 | } else { |
| 1755 | /* -MG given and file not found */ |
| 1756 | if (dhead && !in_list(*dhead, file)) { |
| 1757 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1758 | sl->next = NULL; |
| 1759 | strcpy(sl->str, file); |
| 1760 | **dtail = sl; |
| 1761 | *dtail = &sl->next; |
| 1762 | } |
| 1763 | return NULL; |
| 1764 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1765 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1766 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1767 | error(ERR_FATAL, "unable to open include file `%s'", file); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1768 | return NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1772 | * 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] | 1773 | * 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] | 1774 | * return true if _any_ single-line macro of that name is defined. |
| 1775 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1776 | * `nparam' or no parameters is defined. |
| 1777 | * |
| 1778 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1779 | * defined, or nparam is -1, the address of the definition structure |
| 1780 | * 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] | 1781 | * is NULL, no action will be taken regarding its contents, and no |
| 1782 | * error will occur. |
| 1783 | * |
| 1784 | * Note that this is also called with nparam zero to resolve |
| 1785 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1786 | * |
| 1787 | * If you already know which context macro belongs to, you can pass |
| 1788 | * the context pointer as first parameter; if you won't but name begins |
| 1789 | * with %$ the context will be automatically computed. If all_contexts |
| 1790 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1791 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1792 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1793 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1794 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1795 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1796 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1797 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1798 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1799 | if (ctx) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1800 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1801 | } else if (name[0] == '%' && name[1] == '$') { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1802 | if (cstk) |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 1803 | ctx = get_ctx(name, &name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1804 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1805 | return false; /* got to return _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1806 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1807 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1808 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1809 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1810 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1811 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1812 | while (m) { |
| 1813 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1814 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1815 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1816 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1817 | *defn = m; |
| 1818 | else |
| 1819 | *defn = NULL; |
| 1820 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1821 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1822 | } |
| 1823 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1824 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1825 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1826 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * Count and mark off the parameters in a multi-line macro call. |
| 1831 | * This is called both from within the multi-line macro expansion |
| 1832 | * code, and also to mark off the default parameters when provided |
| 1833 | * in a %macro definition line. |
| 1834 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1835 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1836 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1837 | int paramsize, brace; |
| 1838 | |
| 1839 | *nparam = paramsize = 0; |
| 1840 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1841 | while (t) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1842 | /* +1: we need space for the final NULL */ |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1843 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1844 | paramsize += PARAM_DELTA; |
| 1845 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1846 | } |
| 1847 | skip_white_(t); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1848 | brace = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1849 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1850 | brace = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1851 | (*params)[(*nparam)++] = t; |
| 1852 | while (tok_isnt_(t, brace ? "}" : ",")) |
| 1853 | t = t->next; |
| 1854 | if (t) { /* got a comma/brace */ |
| 1855 | t = t->next; |
| 1856 | if (brace) { |
| 1857 | /* |
| 1858 | * Now we've found the closing brace, look further |
| 1859 | * for the comma. |
| 1860 | */ |
| 1861 | skip_white_(t); |
| 1862 | if (tok_isnt_(t, ",")) { |
| 1863 | error(ERR_NONFATAL, |
| 1864 | "braces do not enclose all of macro parameter"); |
| 1865 | while (tok_isnt_(t, ",")) |
| 1866 | t = t->next; |
| 1867 | } |
| 1868 | if (t) |
| 1869 | t = t->next; /* eat the comma */ |
| 1870 | } |
| 1871 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1876 | * Determine whether one of the various `if' conditions is true or |
| 1877 | * not. |
| 1878 | * |
| 1879 | * We must free the tline we get passed. |
| 1880 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1881 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1882 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1883 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1884 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1885 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1886 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1887 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1888 | enum pp_token_type needtype; |
H. Peter Anvin | 077fb93 | 2010-07-20 14:56:30 -0700 | [diff] [blame] | 1889 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1890 | |
| 1891 | origline = tline; |
| 1892 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1893 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1894 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1895 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1896 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1897 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1898 | if (!tline) |
| 1899 | break; |
| 1900 | if (tline->type != TOK_ID) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1901 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1902 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1903 | free_tlist(origline); |
| 1904 | return -1; |
| 1905 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1906 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1907 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1908 | tline = tline->next; |
| 1909 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1910 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1911 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1912 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1913 | j = false; /* have we matched yet? */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1914 | while (tline) { |
| 1915 | skip_white_(tline); |
| 1916 | if (!tline || (tline->type != TOK_ID && |
| 1917 | (tline->type != TOK_PREPROC_ID || |
| 1918 | tline->text[1] != '$'))) { |
| 1919 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1920 | "`%s' expects macro identifiers", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1921 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1922 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1923 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1924 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1925 | tline = tline->next; |
| 1926 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1927 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1928 | |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1929 | case PPC_IFENV: |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1930 | tline = expand_smacro(tline); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1931 | j = false; /* have we matched yet? */ |
| 1932 | while (tline) { |
| 1933 | skip_white_(tline); |
| 1934 | if (!tline || (tline->type != TOK_ID && |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1935 | tline->type != TOK_STRING && |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1936 | (tline->type != TOK_PREPROC_ID || |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1937 | tline->text[1] != '!'))) { |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1938 | error(ERR_NONFATAL, |
| 1939 | "`%s' expects environment variable names", |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1940 | pp_directives[ct]); |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1941 | goto fail; |
| 1942 | } |
Cyrill Gorcunov | 84b4cba | 2010-09-12 21:39:40 +0400 | [diff] [blame] | 1943 | p = tline->text; |
| 1944 | if (tline->type == TOK_PREPROC_ID) |
| 1945 | p += 2; /* Skip leading %! */ |
| 1946 | if (*p == '\'' || *p == '\"' || *p == '`') |
| 1947 | nasm_unquote_cstr(p, ct); |
| 1948 | if (getenv(p)) |
| 1949 | j = true; |
H. Peter Anvin | 6d9b2b5 | 2010-07-13 12:00:58 -0700 | [diff] [blame] | 1950 | tline = tline->next; |
| 1951 | } |
| 1952 | break; |
| 1953 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1954 | case PPC_IFIDN: |
| 1955 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1956 | tline = expand_smacro(tline); |
| 1957 | t = tt = tline; |
| 1958 | while (tok_isnt_(tt, ",")) |
| 1959 | tt = tt->next; |
| 1960 | if (!tt) { |
| 1961 | error(ERR_NONFATAL, |
| 1962 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1963 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1964 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1965 | } |
| 1966 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1967 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1968 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1969 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1970 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1971 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1972 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1973 | } |
| 1974 | if (t->type == TOK_WHITESPACE) { |
| 1975 | t = t->next; |
| 1976 | continue; |
| 1977 | } |
| 1978 | if (tt->type == TOK_WHITESPACE) { |
| 1979 | tt = tt->next; |
| 1980 | continue; |
| 1981 | } |
| 1982 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1983 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1984 | break; |
| 1985 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1986 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1987 | if (t->type == TOK_STRING) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1988 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1989 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1990 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 1991 | if (l1 != l2) { |
| 1992 | j = false; |
| 1993 | break; |
| 1994 | } |
| 1995 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1996 | j = false; |
| 1997 | break; |
| 1998 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1999 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2000 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2001 | break; |
| 2002 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 2003 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2004 | t = t->next; |
| 2005 | tt = tt->next; |
| 2006 | } |
| 2007 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2008 | j = false; /* trailing gunk on one end or other */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2009 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2010 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2011 | case PPC_IFMACRO: |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2012 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2013 | bool found = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2014 | ExpDef searching, *ed; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2015 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2016 | skip_white_(tline); |
| 2017 | tline = expand_id(tline); |
| 2018 | if (!tok_type_(tline, TOK_ID)) { |
| 2019 | error(ERR_NONFATAL, |
| 2020 | "`%s' expects a macro name", pp_directives[ct]); |
| 2021 | goto fail; |
| 2022 | } |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2023 | memset(&searching, 0, sizeof(searching)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2024 | searching.name = nasm_strdup(tline->text); |
| 2025 | searching.casesense = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2026 | searching.nparam_max = INT_MAX; |
| 2027 | tline = expand_smacro(tline->next); |
| 2028 | skip_white_(tline); |
| 2029 | if (!tline) { |
| 2030 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2031 | error(ERR_NONFATAL, |
| 2032 | "`%s' expects a parameter count or nothing", |
| 2033 | pp_directives[ct]); |
| 2034 | } else { |
| 2035 | searching.nparam_min = searching.nparam_max = |
| 2036 | readnum(tline->text, &j); |
| 2037 | if (j) |
| 2038 | error(ERR_NONFATAL, |
| 2039 | "unable to parse parameter count `%s'", |
| 2040 | tline->text); |
| 2041 | } |
| 2042 | if (tline && tok_is_(tline->next, "-")) { |
| 2043 | tline = tline->next->next; |
| 2044 | if (tok_is_(tline, "*")) |
| 2045 | searching.nparam_max = INT_MAX; |
| 2046 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 2047 | error(ERR_NONFATAL, |
| 2048 | "`%s' expects a parameter count after `-'", |
| 2049 | pp_directives[ct]); |
| 2050 | else { |
| 2051 | searching.nparam_max = readnum(tline->text, &j); |
| 2052 | if (j) |
| 2053 | error(ERR_NONFATAL, |
| 2054 | "unable to parse parameter count `%s'", |
| 2055 | tline->text); |
| 2056 | if (searching.nparam_min > searching.nparam_max) |
| 2057 | error(ERR_NONFATAL, |
| 2058 | "minimum parameter count exceeds maximum"); |
| 2059 | } |
| 2060 | } |
| 2061 | if (tline && tok_is_(tline->next, "+")) { |
| 2062 | tline = tline->next; |
| 2063 | searching.plus = true; |
| 2064 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2065 | ed = (ExpDef *) hash_findix(&expdefs, searching.name); |
| 2066 | while (ed != NULL) { |
Cyrill Gorcunov | a22e7a9 | 2010-11-11 11:42:40 +0300 | [diff] [blame] | 2067 | if (!strcmp(ed->name, searching.name) && |
| 2068 | (ed->nparam_min <= searching.nparam_max || searching.plus) && |
| 2069 | (searching.nparam_min <= ed->nparam_max || ed->plus)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2070 | found = true; |
| 2071 | break; |
| 2072 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2073 | ed = ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2074 | } |
| 2075 | if (tline && tline->next) |
| 2076 | error(ERR_WARNING|ERR_PASS1, |
| 2077 | "trailing garbage after %%ifmacro ignored"); |
| 2078 | nasm_free(searching.name); |
| 2079 | j = found; |
| 2080 | break; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2081 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 2082 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2083 | case PPC_IFID: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2084 | needtype = TOK_ID; |
| 2085 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2086 | case PPC_IFNUM: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2087 | needtype = TOK_NUMBER; |
| 2088 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2089 | case PPC_IFSTR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2090 | needtype = TOK_STRING; |
| 2091 | goto iftype; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2092 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2093 | iftype: |
| 2094 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2095 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2096 | while (tok_type_(t, TOK_WHITESPACE) || |
| 2097 | (needtype == TOK_NUMBER && |
| 2098 | tok_type_(t, TOK_OTHER) && |
| 2099 | (t->text[0] == '-' || t->text[0] == '+') && |
| 2100 | !t->text[1])) |
| 2101 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 2102 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2103 | j = tok_type_(t, needtype); |
| 2104 | break; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2105 | |
| 2106 | case PPC_IFTOKEN: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2107 | t = tline = expand_smacro(tline); |
| 2108 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2109 | t = t->next; |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 2110 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2111 | j = false; |
| 2112 | if (t) { |
| 2113 | t = t->next; /* Skip the actual token */ |
| 2114 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2115 | t = t->next; |
| 2116 | j = !t; /* Should be nothing left */ |
| 2117 | } |
| 2118 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2119 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2120 | case PPC_IFEMPTY: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2121 | t = tline = expand_smacro(tline); |
| 2122 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2123 | t = t->next; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2124 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2125 | j = !t; /* Should be empty */ |
| 2126 | break; |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 2127 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2128 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2129 | t = tline = expand_smacro(tline); |
| 2130 | tptr = &t; |
| 2131 | tokval.t_type = TOKEN_INVALID; |
| 2132 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 2133 | NULL, pass | CRITICAL, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2134 | if (!evalresult) |
| 2135 | return -1; |
| 2136 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2137 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2138 | "trailing garbage after expression ignored"); |
| 2139 | if (!is_simple(evalresult)) { |
| 2140 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2141 | "non-constant value given to `%s'", pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2142 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2143 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 2144 | j = reloc_value(evalresult) != 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2145 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 2146 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2147 | default: |
| 2148 | error(ERR_FATAL, |
| 2149 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2150 | pp_directives[ct]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2151 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2152 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2153 | |
| 2154 | free_tlist(origline); |
| 2155 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2156 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2157 | fail: |
| 2158 | free_tlist(origline); |
| 2159 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2163 | * Common code for defining an smacro |
| 2164 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2165 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2166 | int nparam, Token *expansion) |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2167 | { |
| 2168 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2169 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2170 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2171 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2172 | if (!smac) { |
| 2173 | error(ERR_WARNING|ERR_PASS1, |
| 2174 | "single-line macro `%s' defined both with and" |
| 2175 | " without parameters", mname); |
| 2176 | /* |
| 2177 | * Some instances of the old code considered this a failure, |
| 2178 | * some others didn't. What is the right thing to do here? |
| 2179 | */ |
| 2180 | free_tlist(expansion); |
| 2181 | return false; /* Failure */ |
| 2182 | } else { |
| 2183 | /* |
| 2184 | * We're redefining, so we have to take over an |
| 2185 | * existing SMacro structure. This means freeing |
| 2186 | * what was already in it. |
| 2187 | */ |
| 2188 | nasm_free(smac->name); |
| 2189 | free_tlist(smac->expansion); |
| 2190 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2191 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2192 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2193 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2194 | smac = nasm_zalloc(sizeof(SMacro)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2195 | smac->next = *smhead; |
| 2196 | *smhead = smac; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2197 | } |
| 2198 | smac->name = nasm_strdup(mname); |
| 2199 | smac->casesense = casesense; |
| 2200 | smac->nparam = nparam; |
| 2201 | smac->expansion = expansion; |
| 2202 | smac->in_progress = false; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2203 | return true; /* Success */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /* |
| 2207 | * Undefine an smacro |
| 2208 | */ |
| 2209 | static void undef_smacro(Context *ctx, const char *mname) |
| 2210 | { |
| 2211 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2212 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2213 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2214 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 2215 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2216 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2217 | if (smhead) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2218 | /* |
| 2219 | * We now have a macro name... go hunt for it. |
| 2220 | */ |
| 2221 | sp = smhead; |
| 2222 | while ((s = *sp) != NULL) { |
| 2223 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 2224 | *sp = s->next; |
| 2225 | nasm_free(s->name); |
| 2226 | free_tlist(s->expansion); |
| 2227 | nasm_free(s); |
| 2228 | } else { |
| 2229 | sp = &s->next; |
| 2230 | } |
| 2231 | } |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2232 | } |
| 2233 | } |
| 2234 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2235 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2236 | * Parse a mmacro specification. |
| 2237 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2238 | 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] | 2239 | { |
| 2240 | bool err; |
| 2241 | |
| 2242 | tline = tline->next; |
| 2243 | skip_white_(tline); |
| 2244 | tline = expand_id(tline); |
| 2245 | if (!tok_type_(tline, TOK_ID)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2246 | error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
| 2247 | return false; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2248 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2249 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2250 | def->name = nasm_strdup(tline->text); |
| 2251 | def->plus = false; |
| 2252 | def->nolist = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2253 | // def->in_progress = 0; |
| 2254 | // def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2255 | def->nparam_min = 0; |
| 2256 | def->nparam_max = 0; |
| 2257 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2258 | tline = expand_smacro(tline->next); |
| 2259 | skip_white_(tline); |
| 2260 | if (!tok_type_(tline, TOK_NUMBER)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2261 | error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2262 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2263 | def->nparam_min = def->nparam_max = |
| 2264 | readnum(tline->text, &err); |
| 2265 | if (err) |
| 2266 | error(ERR_NONFATAL, |
| 2267 | "unable to parse parameter count `%s'", tline->text); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2268 | } |
| 2269 | if (tline && tok_is_(tline->next, "-")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2270 | tline = tline->next->next; |
| 2271 | if (tok_is_(tline, "*")) { |
| 2272 | def->nparam_max = INT_MAX; |
| 2273 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 2274 | error(ERR_NONFATAL, |
| 2275 | "`%s' expects a parameter count after `-'", directive); |
| 2276 | } else { |
| 2277 | def->nparam_max = readnum(tline->text, &err); |
| 2278 | if (err) { |
| 2279 | error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
| 2280 | tline->text); |
| 2281 | } |
| 2282 | if (def->nparam_min > def->nparam_max) { |
| 2283 | error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
| 2284 | } |
| 2285 | } |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2286 | } |
| 2287 | if (tline && tok_is_(tline->next, "+")) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2288 | tline = tline->next; |
| 2289 | def->plus = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2290 | } |
| 2291 | if (tline && tok_type_(tline->next, TOK_ID) && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2292 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 2293 | tline = tline->next; |
| 2294 | def->nolist = true; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2295 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2296 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2297 | /* |
| 2298 | * Handle default parameters. |
| 2299 | */ |
| 2300 | if (tline && tline->next) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2301 | def->dlist = tline->next; |
| 2302 | tline->next = NULL; |
| 2303 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2304 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2305 | def->dlist = NULL; |
| 2306 | def->defaults = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2307 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2308 | def->line = NULL; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2309 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2310 | if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2311 | !def->plus) |
| 2312 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
| 2313 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 2314 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2315 | return true; |
| 2316 | } |
| 2317 | |
| 2318 | |
| 2319 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2320 | * Decode a size directive |
| 2321 | */ |
| 2322 | static int parse_size(const char *str) { |
| 2323 | static const char *size_names[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2324 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2325 | static const int sizes[] = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2326 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2327 | |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 2328 | return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1]; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2329 | } |
| 2330 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2331 | /** |
| 2332 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2333 | * Find out if a line contains a preprocessor directive, and deal |
| 2334 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2335 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2336 | * If a directive _is_ found, it is the responsibility of this routine |
| 2337 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2338 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 2339 | * @param tline a pointer to the current tokeninzed line linked list |
| 2340 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2341 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2342 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2343 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2344 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2345 | enum preproc_token i; |
| 2346 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2347 | bool err; |
| 2348 | int nparam; |
| 2349 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2350 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 2351 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2352 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2353 | char *p, *pp; |
| 2354 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2355 | Include *inc; |
| 2356 | Context *ctx; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2357 | Line *l; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2358 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2359 | struct tokenval tokval; |
| 2360 | expr *evalresult; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2361 | ExpDef *ed, *eed, **edhead; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2362 | ExpInv *ei, *eei; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2363 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2364 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2365 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2366 | |
| 2367 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2368 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2369 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2370 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2371 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2372 | || tline->text[1] == '!')) |
| 2373 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2374 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2375 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2376 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2377 | switch (i) { |
| 2378 | case PP_INVALID: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2379 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2380 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2381 | tline->text); |
| 2382 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2383 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2384 | case PP_STACKSIZE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2385 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2386 | /* Directive to tell NASM what the default stack size is. The |
| 2387 | * default is for a 16-bit stack, and this can be overriden with |
| 2388 | * %stacksize large. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2389 | */ |
| 2390 | tline = tline->next; |
| 2391 | if (tline && tline->type == TOK_WHITESPACE) |
| 2392 | tline = tline->next; |
| 2393 | if (!tline || tline->type != TOK_ID) { |
| 2394 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 2395 | free_tlist(origline); |
| 2396 | return DIRECTIVE_FOUND; |
| 2397 | } |
| 2398 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2399 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2400 | StackSize = 4; |
| 2401 | StackPointer = "ebp"; |
| 2402 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2403 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2404 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2405 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2406 | StackSize = 8; |
| 2407 | StackPointer = "rbp"; |
Per Jessen | 53252e0 | 2010-02-11 00:16:59 +0300 | [diff] [blame] | 2408 | ArgOffset = 16; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2409 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2410 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2411 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2412 | * far function call. |
| 2413 | */ |
| 2414 | StackSize = 2; |
| 2415 | StackPointer = "bp"; |
| 2416 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2417 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2418 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2419 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2420 | * far function call. We don't support near functions. |
| 2421 | */ |
| 2422 | StackSize = 2; |
| 2423 | StackPointer = "bp"; |
| 2424 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2425 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2426 | } else { |
| 2427 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 2428 | free_tlist(origline); |
| 2429 | return DIRECTIVE_FOUND; |
| 2430 | } |
| 2431 | free_tlist(origline); |
| 2432 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2433 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2434 | case PP_ARG: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2435 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2436 | /* TASM like ARG directive to define arguments to functions, in |
| 2437 | * the following form: |
| 2438 | * |
| 2439 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2440 | */ |
| 2441 | offset = ArgOffset; |
| 2442 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2443 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2444 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2445 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2446 | /* Find the argument name */ |
| 2447 | tline = tline->next; |
| 2448 | if (tline && tline->type == TOK_WHITESPACE) |
| 2449 | tline = tline->next; |
| 2450 | if (!tline || tline->type != TOK_ID) { |
| 2451 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 2452 | free_tlist(origline); |
| 2453 | return DIRECTIVE_FOUND; |
| 2454 | } |
| 2455 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2456 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2457 | /* Find the argument size type */ |
| 2458 | tline = tline->next; |
| 2459 | if (!tline || tline->type != TOK_OTHER |
| 2460 | || tline->text[0] != ':') { |
| 2461 | error(ERR_NONFATAL, |
| 2462 | "Syntax error processing `%%arg' directive"); |
| 2463 | free_tlist(origline); |
| 2464 | return DIRECTIVE_FOUND; |
| 2465 | } |
| 2466 | tline = tline->next; |
| 2467 | if (!tline || tline->type != TOK_ID) { |
| 2468 | error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
| 2469 | free_tlist(origline); |
| 2470 | return DIRECTIVE_FOUND; |
| 2471 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2472 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2473 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2474 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2475 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2476 | size = parse_size(tt->text); |
| 2477 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2478 | error(ERR_NONFATAL, |
| 2479 | "Invalid size type for `%%arg' missing directive"); |
| 2480 | free_tlist(tt); |
| 2481 | free_tlist(origline); |
| 2482 | return DIRECTIVE_FOUND; |
| 2483 | } |
| 2484 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2485 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2486 | /* Round up to even stack slots */ |
| 2487 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2488 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2489 | /* Now define the macro for the argument */ |
| 2490 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2491 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2492 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2493 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2494 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2495 | /* Move to the next argument in the list */ |
| 2496 | tline = tline->next; |
| 2497 | if (tline && tline->type == TOK_WHITESPACE) |
| 2498 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2499 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2500 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2501 | free_tlist(origline); |
| 2502 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2503 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2504 | case PP_LOCAL: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2505 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2506 | /* TASM like LOCAL directive to define local variables for a |
| 2507 | * function, in the following form: |
| 2508 | * |
| 2509 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2510 | * |
| 2511 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2512 | * required by TASM to define the local parameter size (and used |
| 2513 | * by the TASM macro package). |
| 2514 | */ |
| 2515 | offset = LocalOffset; |
| 2516 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2517 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2518 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2519 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2520 | /* Find the argument name */ |
| 2521 | tline = tline->next; |
| 2522 | if (tline && tline->type == TOK_WHITESPACE) |
| 2523 | tline = tline->next; |
| 2524 | if (!tline || tline->type != TOK_ID) { |
| 2525 | error(ERR_NONFATAL, |
| 2526 | "`%%local' missing argument parameter"); |
| 2527 | free_tlist(origline); |
| 2528 | return DIRECTIVE_FOUND; |
| 2529 | } |
| 2530 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2531 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2532 | /* Find the argument size type */ |
| 2533 | tline = tline->next; |
| 2534 | if (!tline || tline->type != TOK_OTHER |
| 2535 | || tline->text[0] != ':') { |
| 2536 | error(ERR_NONFATAL, |
| 2537 | "Syntax error processing `%%local' directive"); |
| 2538 | free_tlist(origline); |
| 2539 | return DIRECTIVE_FOUND; |
| 2540 | } |
| 2541 | tline = tline->next; |
| 2542 | if (!tline || tline->type != TOK_ID) { |
| 2543 | error(ERR_NONFATAL, |
| 2544 | "`%%local' missing size type parameter"); |
| 2545 | free_tlist(origline); |
| 2546 | return DIRECTIVE_FOUND; |
| 2547 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2548 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2549 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2550 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2551 | tt = expand_smacro(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2552 | size = parse_size(tt->text); |
| 2553 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2554 | error(ERR_NONFATAL, |
| 2555 | "Invalid size type for `%%local' missing directive"); |
| 2556 | free_tlist(tt); |
| 2557 | free_tlist(origline); |
| 2558 | return DIRECTIVE_FOUND; |
| 2559 | } |
| 2560 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2561 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2562 | /* Round up to even stack slots */ |
| 2563 | size = ALIGN(size, StackSize); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2564 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2565 | offset += size; /* Negative offset, increment before */ |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2566 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2567 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2568 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2569 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2570 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2571 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2572 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2573 | snprintf(directive, sizeof(directive), |
| 2574 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2575 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2576 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2577 | /* Move to the next argument in the list */ |
| 2578 | tline = tline->next; |
| 2579 | if (tline && tline->type == TOK_WHITESPACE) |
| 2580 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2581 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2582 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2583 | free_tlist(origline); |
| 2584 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2585 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2586 | case PP_CLEAR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2587 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2588 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2589 | error(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2590 | "trailing garbage after `%%clear' ignored"); |
| 2591 | free_macros(); |
| 2592 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2593 | free_tlist(origline); |
| 2594 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2595 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2596 | case PP_DEPEND: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2597 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2598 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2599 | skip_white_(t); |
| 2600 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2601 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2602 | error(ERR_NONFATAL, "`%%depend' expects a file name"); |
| 2603 | free_tlist(origline); |
| 2604 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2605 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2606 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2607 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2608 | "trailing garbage after `%%depend' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2609 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2610 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2611 | nasm_unquote_cstr(p, i); |
| 2612 | if (dephead && !in_list(*dephead, p)) { |
| 2613 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2614 | sl->next = NULL; |
| 2615 | strcpy(sl->str, p); |
| 2616 | *deptail = sl; |
| 2617 | deptail = &sl->next; |
| 2618 | } |
| 2619 | free_tlist(origline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2620 | return DIRECTIVE_FOUND; |
| 2621 | |
| 2622 | case PP_INCLUDE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2623 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2624 | t = tline->next = expand_smacro(tline->next); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2625 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2626 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2627 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2628 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2629 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2630 | free_tlist(origline); |
| 2631 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2632 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2633 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2634 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2635 | "trailing garbage after `%%include' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2636 | p = t->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2637 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2638 | nasm_unquote_cstr(p, i); |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 2639 | inc = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2640 | inc->next = istk; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2641 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2642 | if (!inc->fp) { |
| 2643 | /* -MG given but file not found */ |
| 2644 | nasm_free(inc); |
| 2645 | } else { |
| 2646 | inc->fname = src_set_fname(nasm_strdup(p)); |
| 2647 | inc->lineno = src_set_linnum(0); |
| 2648 | inc->lineinc = 1; |
| 2649 | inc->expansion = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2650 | istk = inc; |
| 2651 | list->uplevel(LIST_INCLUDE); |
| 2652 | } |
| 2653 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2654 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2655 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2656 | case PP_USE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2657 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2658 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2659 | static macros_t *use_pkg; |
| 2660 | const char *pkg_macro = NULL; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2661 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2662 | tline = tline->next; |
| 2663 | skip_white_(tline); |
| 2664 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2665 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2666 | if (!tline || (tline->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2667 | tline->type != TOK_INTERNAL_STRING && |
| 2668 | tline->type != TOK_ID)) { |
H. Peter Anvin | 926fc40 | 2008-06-19 16:26:12 -0700 | [diff] [blame] | 2669 | error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2670 | free_tlist(origline); |
| 2671 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2672 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2673 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2674 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2675 | "trailing garbage after `%%use' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2676 | if (tline->type == TOK_STRING) |
| 2677 | nasm_unquote_cstr(tline->text, i); |
| 2678 | use_pkg = nasm_stdmac_find_package(tline->text); |
| 2679 | if (!use_pkg) |
| 2680 | error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
| 2681 | else |
| 2682 | 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] | 2683 | if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2684 | /* Not already included, go ahead and include it */ |
| 2685 | stdmacpos = use_pkg; |
| 2686 | } |
| 2687 | free_tlist(origline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2688 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2689 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2690 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2691 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2692 | case PP_POP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2693 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2694 | tline = tline->next; |
| 2695 | skip_white_(tline); |
| 2696 | tline = expand_id(tline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2697 | if (tline) { |
| 2698 | if (!tok_type_(tline, TOK_ID)) { |
| 2699 | error(ERR_NONFATAL, "`%s' expects a context identifier", |
| 2700 | pp_directives[i]); |
| 2701 | free_tlist(origline); |
| 2702 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2703 | } |
| 2704 | if (tline->next) |
| 2705 | error(ERR_WARNING|ERR_PASS1, |
| 2706 | "trailing garbage after `%s' ignored", |
| 2707 | pp_directives[i]); |
| 2708 | p = nasm_strdup(tline->text); |
| 2709 | } else { |
| 2710 | p = NULL; /* Anonymous */ |
| 2711 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2712 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2713 | if (i == PP_PUSH) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2714 | ctx = nasm_zalloc(sizeof(Context)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2715 | ctx->next = cstk; |
| 2716 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2717 | ctx->name = p; |
| 2718 | ctx->number = unique++; |
| 2719 | cstk = ctx; |
| 2720 | } else { |
| 2721 | /* %pop or %repl */ |
| 2722 | if (!cstk) { |
| 2723 | error(ERR_NONFATAL, "`%s': context stack is empty", |
| 2724 | pp_directives[i]); |
| 2725 | } else if (i == PP_POP) { |
| 2726 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
| 2727 | error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
| 2728 | "expected %s", |
| 2729 | cstk->name ? cstk->name : "anonymous", p); |
| 2730 | else |
| 2731 | ctx_pop(); |
| 2732 | } else { |
| 2733 | /* i == PP_REPL */ |
| 2734 | nasm_free(cstk->name); |
| 2735 | cstk->name = p; |
| 2736 | p = NULL; |
| 2737 | } |
| 2738 | nasm_free(p); |
| 2739 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2740 | free_tlist(origline); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2741 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2742 | case PP_FATAL: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2743 | severity = ERR_FATAL; |
| 2744 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2745 | case PP_ERROR: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2746 | severity = ERR_NONFATAL; |
| 2747 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2748 | case PP_WARNING: |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2749 | severity = ERR_WARNING|ERR_WARN_USER; |
| 2750 | goto issue_error; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2751 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2752 | issue_error: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2753 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2754 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2755 | /* Only error out if this is the final pass */ |
| 2756 | if (pass != 2 && i != PP_FATAL) |
| 2757 | return DIRECTIVE_FOUND; |
| 2758 | |
| 2759 | tline->next = expand_smacro(tline->next); |
| 2760 | tline = tline->next; |
| 2761 | skip_white_(tline); |
| 2762 | t = tline ? tline->next : NULL; |
| 2763 | skip_white_(t); |
| 2764 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2765 | /* The line contains only a quoted string */ |
| 2766 | p = tline->text; |
| 2767 | nasm_unquote(p, NULL); /* Ignore NUL character truncation */ |
| 2768 | error(severity, "%s", p); |
| 2769 | } else { |
| 2770 | /* Not a quoted string, or more than a quoted string */ |
| 2771 | p = detoken(tline, false); |
| 2772 | error(severity, "%s", p); |
| 2773 | nasm_free(p); |
| 2774 | } |
| 2775 | free_tlist(origline); |
| 2776 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2777 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2778 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2779 | CASE_PP_IF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2780 | if (defining != NULL) { |
| 2781 | if (defining->type == EXP_IF) { |
| 2782 | defining->def_depth ++; |
| 2783 | } |
| 2784 | return NO_DIRECTIVE_FOUND; |
| 2785 | } |
| 2786 | if ((istk->expansion != NULL) && |
| 2787 | (istk->expansion->emitting == false)) { |
| 2788 | j = COND_NEVER; |
| 2789 | } else { |
| 2790 | j = if_condition(tline->next, i); |
| 2791 | tline->next = NULL; /* it got freed */ |
| 2792 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 2793 | } |
| 2794 | ed = new_ExpDef(EXP_IF); |
| 2795 | ed->state = j; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2796 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 2797 | ed->prev = defining; |
| 2798 | defining = ed; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2799 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2800 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2801 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2802 | CASE_PP_ELIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2803 | if (defining != NULL) { |
| 2804 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2805 | return NO_DIRECTIVE_FOUND; |
| 2806 | } |
| 2807 | } |
Cyrill Gorcunov | 82667ff | 2011-06-25 19:34:19 +0400 | [diff] [blame] | 2808 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2809 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2810 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2811 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2812 | case COND_IF_TRUE: |
| 2813 | defining->state = COND_DONE; |
| 2814 | defining->ignoring = true; |
| 2815 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2816 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2817 | case COND_DONE: |
| 2818 | case COND_NEVER: |
| 2819 | defining->ignoring = true; |
| 2820 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2821 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2822 | case COND_ELSE_TRUE: |
| 2823 | case COND_ELSE_FALSE: |
| 2824 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2825 | "`%%elif' after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2826 | defining->state = COND_NEVER; |
| 2827 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2828 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2829 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2830 | case COND_IF_FALSE: |
| 2831 | /* |
| 2832 | * IMPORTANT: In the case of %if, we will already have |
| 2833 | * called expand_mmac_params(); however, if we're |
| 2834 | * processing an %elif we must have been in a |
| 2835 | * non-emitting mode, which would have inhibited |
| 2836 | * the normal invocation of expand_mmac_params(). |
| 2837 | * Therefore, we have to do it explicitly here. |
| 2838 | */ |
| 2839 | j = if_condition(expand_mmac_params(tline->next), i); |
| 2840 | tline->next = NULL; /* it got freed */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2841 | defining->state = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2842 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2843 | defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2844 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2845 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2846 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2847 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2848 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2849 | case PP_ELSE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2850 | if (defining != NULL) { |
| 2851 | if ((defining->type != EXP_IF) || (defining->def_depth > 0)) { |
| 2852 | return NO_DIRECTIVE_FOUND; |
| 2853 | } |
| 2854 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2855 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2856 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2857 | "trailing garbage after `%%else' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2858 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2859 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
| 2860 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2861 | switch (defining->state) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2862 | case COND_IF_TRUE: |
| 2863 | case COND_DONE: |
| 2864 | defining->state = COND_ELSE_FALSE; |
| 2865 | defining->ignoring = true; |
| 2866 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2867 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2868 | case COND_NEVER: |
| 2869 | defining->ignoring = true; |
| 2870 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2871 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2872 | case COND_IF_FALSE: |
| 2873 | defining->state = COND_ELSE_TRUE; |
| 2874 | defining->ignoring = false; |
| 2875 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2876 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2877 | case COND_ELSE_TRUE: |
| 2878 | case COND_ELSE_FALSE: |
| 2879 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2880 | "`%%else' after `%%else' ignored."); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2881 | defining->state = COND_NEVER; |
| 2882 | defining->ignoring = true; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2883 | break; |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2884 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2885 | free_tlist(origline); |
| 2886 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2887 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2888 | case PP_ENDIF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2889 | if (defining != NULL) { |
| 2890 | if (defining->type == EXP_IF) { |
| 2891 | if (defining->def_depth > 0) { |
| 2892 | defining->def_depth --; |
| 2893 | return NO_DIRECTIVE_FOUND; |
| 2894 | } |
| 2895 | } else { |
| 2896 | return NO_DIRECTIVE_FOUND; |
| 2897 | } |
| 2898 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2899 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2900 | error_precond(ERR_WARNING|ERR_PASS1, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2901 | "trailing garbage after `%%endif' ignored"); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2902 | if ((defining == NULL) || (defining->type != EXP_IF)) { |
| 2903 | error(ERR_NONFATAL, "`%%endif': no matching `%%if'"); |
| 2904 | return DIRECTIVE_FOUND; |
| 2905 | } |
| 2906 | ed = defining; |
| 2907 | defining = ed->prev; |
| 2908 | ed->prev = expansions; |
| 2909 | expansions = ed; |
| 2910 | ei = new_ExpInv(EXP_IF, ed); |
| 2911 | ei->current = ed->line; |
| 2912 | ei->emitting = true; |
| 2913 | ei->prev = istk->expansion; |
| 2914 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2915 | free_tlist(origline); |
| 2916 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2917 | |
H. Peter Anvin | db8f96e | 2009-07-15 09:07:29 -0400 | [diff] [blame] | 2918 | case PP_RMACRO: |
| 2919 | case PP_IRMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2920 | case PP_MACRO: |
| 2921 | case PP_IMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2922 | if (defining != NULL) { |
| 2923 | if (defining->type == EXP_MMACRO) { |
| 2924 | defining->def_depth ++; |
| 2925 | } |
| 2926 | return NO_DIRECTIVE_FOUND; |
| 2927 | } |
| 2928 | ed = new_ExpDef(EXP_MMACRO); |
| 2929 | ed->max_depth = |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2930 | (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2931 | ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 2932 | if (!parse_mmacro_spec(tline, ed, pp_directives[i])) { |
| 2933 | nasm_free(ed); |
| 2934 | ed = NULL; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 2935 | return DIRECTIVE_FOUND; |
| 2936 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2937 | ed->def_depth = 0; |
| 2938 | ed->cur_depth = 0; |
| 2939 | ed->max_depth = (ed->max_depth + 1); |
| 2940 | ed->ignoring = false; |
| 2941 | ed->prev = defining; |
| 2942 | defining = ed; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2943 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2944 | eed = (ExpDef *) hash_findix(&expdefs, ed->name); |
| 2945 | while (eed) { |
Cyrill Gorcunov | 574fbf1 | 2010-11-11 13:44:51 +0300 | [diff] [blame] | 2946 | if (!strcmp(eed->name, ed->name) && |
| 2947 | (eed->nparam_min <= ed->nparam_max || ed->plus) && |
| 2948 | (ed->nparam_min <= eed->nparam_max || eed->plus)) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2949 | error(ERR_WARNING|ERR_PASS1, |
| 2950 | "redefining multi-line macro `%s'", ed->name); |
| 2951 | return DIRECTIVE_FOUND; |
| 2952 | } |
| 2953 | eed = eed->next; |
| 2954 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2955 | free_tlist(origline); |
| 2956 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2957 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2958 | case PP_ENDM: |
| 2959 | case PP_ENDMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2960 | if (defining != NULL) { |
| 2961 | if (defining->type == EXP_MMACRO) { |
| 2962 | if (defining->def_depth > 0) { |
| 2963 | defining->def_depth --; |
| 2964 | return NO_DIRECTIVE_FOUND; |
| 2965 | } |
| 2966 | } else { |
| 2967 | return NO_DIRECTIVE_FOUND; |
| 2968 | } |
| 2969 | } |
| 2970 | if (!(defining) || (defining->type != EXP_MMACRO)) { |
| 2971 | error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
| 2972 | return DIRECTIVE_FOUND; |
| 2973 | } |
| 2974 | edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name); |
| 2975 | defining->next = *edhead; |
| 2976 | *edhead = defining; |
| 2977 | ed = defining; |
| 2978 | defining = ed->prev; |
| 2979 | ed->prev = expansions; |
| 2980 | expansions = ed; |
| 2981 | ed = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2982 | free_tlist(origline); |
| 2983 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2984 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 2985 | case PP_EXITMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 2986 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 2987 | /* |
| 2988 | * We must search along istk->expansion until we hit a |
| 2989 | * macro invocation. Then we disable the emitting state(s) |
| 2990 | * between exitmacro and endmacro. |
| 2991 | */ |
| 2992 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 2993 | if(ei->type == EXP_MMACRO) { |
| 2994 | break; |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | if (ei != NULL) { |
| 2999 | /* |
| 3000 | * Set all invocations leading back to the macro |
| 3001 | * invocation to a non-emitting state. |
| 3002 | */ |
| 3003 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3004 | eei->emitting = false; |
| 3005 | } |
| 3006 | eei->emitting = false; |
| 3007 | } else { |
| 3008 | error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block"); |
| 3009 | } |
Keith Kanios | 852f1ee | 2009-07-12 00:19:55 -0500 | [diff] [blame] | 3010 | free_tlist(origline); |
| 3011 | return DIRECTIVE_FOUND; |
| 3012 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3013 | case PP_UNMACRO: |
| 3014 | case PP_UNIMACRO: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3015 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3016 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3017 | ExpDef **ed_p; |
| 3018 | ExpDef spec; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3019 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3020 | spec.casesense = (i == PP_UNMACRO); |
| 3021 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 3022 | return DIRECTIVE_FOUND; |
| 3023 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3024 | ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL); |
| 3025 | while (ed_p && *ed_p) { |
| 3026 | ed = *ed_p; |
| 3027 | if (ed->casesense == spec.casesense && |
| 3028 | !mstrcmp(ed->name, spec.name, spec.casesense) && |
| 3029 | ed->nparam_min == spec.nparam_min && |
| 3030 | ed->nparam_max == spec.nparam_max && |
| 3031 | ed->plus == spec.plus) { |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3032 | if (ed->cur_depth > 0) { |
Keith Kanios | 21d885b | 2010-12-18 12:22:21 -0600 | [diff] [blame] | 3033 | error(ERR_NONFATAL, "`%s' ignored on active macro", |
Keith Kanios | c98a5b4 | 2010-12-18 12:17:31 -0600 | [diff] [blame] | 3034 | pp_directives[i]); |
| 3035 | break; |
| 3036 | } else { |
| 3037 | *ed_p = ed->next; |
| 3038 | free_expdef(ed); |
| 3039 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3040 | } else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3041 | ed_p = &ed->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3042 | } |
| 3043 | } |
| 3044 | free_tlist(origline); |
| 3045 | free_tlist(spec.dlist); |
| 3046 | return DIRECTIVE_FOUND; |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3049 | case PP_ROTATE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3050 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3051 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 3052 | tline = tline->next; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 3053 | if (!tline->next) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3054 | free_tlist(origline); |
| 3055 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 3056 | return DIRECTIVE_FOUND; |
| 3057 | } |
| 3058 | t = expand_smacro(tline->next); |
| 3059 | tline->next = NULL; |
| 3060 | free_tlist(origline); |
| 3061 | tline = t; |
| 3062 | tptr = &t; |
| 3063 | tokval.t_type = TOKEN_INVALID; |
| 3064 | evalresult = |
| 3065 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3066 | free_tlist(tline); |
| 3067 | if (!evalresult) |
| 3068 | return DIRECTIVE_FOUND; |
| 3069 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3070 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3071 | "trailing garbage after expression ignored"); |
| 3072 | if (!is_simple(evalresult)) { |
| 3073 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
| 3074 | return DIRECTIVE_FOUND; |
| 3075 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3076 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3077 | if (ei->type == EXP_MMACRO) { |
| 3078 | break; |
| 3079 | } |
| 3080 | } |
| 3081 | if (ei == NULL) { |
| 3082 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
| 3083 | } else if (ei->nparam == 0) { |
| 3084 | error(ERR_NONFATAL, |
| 3085 | "`%%rotate' invoked within macro without parameters"); |
| 3086 | } else { |
| 3087 | int rotate = ei->rotate + reloc_value(evalresult); |
| 3088 | |
| 3089 | rotate %= (int)ei->nparam; |
| 3090 | if (rotate < 0) |
| 3091 | rotate += ei->nparam; |
| 3092 | ei->rotate = rotate; |
| 3093 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3094 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3095 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3096 | case PP_REP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3097 | if (defining != NULL) { |
| 3098 | if (defining->type == EXP_REP) { |
| 3099 | defining->def_depth ++; |
| 3100 | } |
| 3101 | return NO_DIRECTIVE_FOUND; |
| 3102 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3103 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3104 | do { |
| 3105 | tline = tline->next; |
| 3106 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3107 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3108 | if (tok_type_(tline, TOK_ID) && |
| 3109 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3110 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3111 | do { |
| 3112 | tline = tline->next; |
| 3113 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3114 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3115 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3116 | if (tline) { |
| 3117 | t = expand_smacro(tline); |
| 3118 | tptr = &t; |
| 3119 | tokval.t_type = TOKEN_INVALID; |
| 3120 | evalresult = |
| 3121 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3122 | if (!evalresult) { |
| 3123 | free_tlist(origline); |
| 3124 | return DIRECTIVE_FOUND; |
| 3125 | } |
| 3126 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3127 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3128 | "trailing garbage after expression ignored"); |
| 3129 | if (!is_simple(evalresult)) { |
| 3130 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 3131 | return DIRECTIVE_FOUND; |
| 3132 | } |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3133 | count = reloc_value(evalresult); |
| 3134 | if (count >= REP_LIMIT) { |
Cyrill Gorcunov | 71f4f84 | 2010-08-09 20:17:17 +0400 | [diff] [blame] | 3135 | error(ERR_NONFATAL, "`%%rep' value exceeds limit"); |
Cyrill Gorcunov | e091d6e | 2010-08-09 13:58:22 +0400 | [diff] [blame] | 3136 | count = 0; |
| 3137 | } else |
| 3138 | count++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3139 | } else { |
| 3140 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3141 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3142 | } |
| 3143 | free_tlist(origline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3144 | ed = new_ExpDef(EXP_REP); |
| 3145 | ed->nolist = nolist; |
| 3146 | ed->def_depth = 0; |
| 3147 | ed->cur_depth = 1; |
| 3148 | ed->max_depth = (count - 1); |
| 3149 | ed->ignoring = false; |
| 3150 | ed->prev = defining; |
| 3151 | defining = ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3152 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3153 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3154 | case PP_ENDREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3155 | if (defining != NULL) { |
| 3156 | if (defining->type == EXP_REP) { |
| 3157 | if (defining->def_depth > 0) { |
| 3158 | defining->def_depth --; |
| 3159 | return NO_DIRECTIVE_FOUND; |
| 3160 | } |
| 3161 | } else { |
| 3162 | return NO_DIRECTIVE_FOUND; |
| 3163 | } |
| 3164 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3165 | if ((defining == NULL) || (defining->type != EXP_REP)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3166 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
| 3167 | return DIRECTIVE_FOUND; |
| 3168 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3169 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3170 | /* |
| 3171 | * Now we have a "macro" defined - although it has no name |
| 3172 | * and we won't be entering it in the hash tables - we must |
| 3173 | * push a macro-end marker for it on to istk->expansion. |
| 3174 | * After that, it will take care of propagating itself (a |
| 3175 | * macro-end marker line for a macro which is really a %rep |
| 3176 | * block will cause the macro to be re-expanded, complete |
| 3177 | * with another macro-end marker to ensure the process |
| 3178 | * continues) until the whole expansion is forcibly removed |
| 3179 | * from istk->expansion by a %exitrep. |
| 3180 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3181 | ed = defining; |
| 3182 | defining = ed->prev; |
| 3183 | ed->prev = expansions; |
| 3184 | expansions = ed; |
| 3185 | ei = new_ExpInv(EXP_REP, ed); |
| 3186 | ei->current = ed->line; |
| 3187 | ei->emitting = ((ed->max_depth > 0) ? true : false); |
| 3188 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 3189 | ei->prev = istk->expansion; |
| 3190 | istk->expansion = ei; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3191 | free_tlist(origline); |
| 3192 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3193 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3194 | case PP_EXITREP: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3195 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3196 | /* |
| 3197 | * We must search along istk->expansion until we hit a |
| 3198 | * rep invocation. Then we disable the emitting state(s) |
| 3199 | * between exitrep and endrep. |
| 3200 | */ |
| 3201 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3202 | if (ei->type == EXP_REP) { |
| 3203 | break; |
| 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | if (ei != NULL) { |
| 3208 | /* |
| 3209 | * Set all invocations leading back to the rep |
| 3210 | * invocation to a non-emitting state. |
| 3211 | */ |
| 3212 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3213 | eei->emitting = false; |
| 3214 | } |
| 3215 | eei->emitting = false; |
| 3216 | eei->current = NULL; |
| 3217 | eei->def->cur_depth = eei->def->max_depth; |
| 3218 | } else { |
| 3219 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 3220 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3221 | free_tlist(origline); |
| 3222 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3223 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3224 | case PP_XDEFINE: |
| 3225 | case PP_IXDEFINE: |
| 3226 | case PP_DEFINE: |
| 3227 | case PP_IDEFINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3228 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3229 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3230 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3231 | tline = tline->next; |
| 3232 | skip_white_(tline); |
| 3233 | tline = expand_id(tline); |
| 3234 | if (!tline || (tline->type != TOK_ID && |
| 3235 | (tline->type != TOK_PREPROC_ID || |
| 3236 | tline->text[1] != '$'))) { |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3237 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3238 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3239 | free_tlist(origline); |
| 3240 | return DIRECTIVE_FOUND; |
| 3241 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3242 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3243 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3244 | last = tline; |
| 3245 | param_start = tline = tline->next; |
| 3246 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3247 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3248 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 3249 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 3250 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3251 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3252 | if (tok_is_(tline, "(")) { |
| 3253 | /* |
| 3254 | * This macro has parameters. |
| 3255 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3256 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3257 | tline = tline->next; |
| 3258 | while (1) { |
| 3259 | skip_white_(tline); |
| 3260 | if (!tline) { |
| 3261 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 3262 | free_tlist(origline); |
| 3263 | return DIRECTIVE_FOUND; |
| 3264 | } |
| 3265 | if (tline->type != TOK_ID) { |
| 3266 | error(ERR_NONFATAL, |
| 3267 | "`%s': parameter identifier expected", |
| 3268 | tline->text); |
| 3269 | free_tlist(origline); |
| 3270 | return DIRECTIVE_FOUND; |
| 3271 | } |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 3272 | |
| 3273 | smacro_set_param_idx(tline, nparam); |
| 3274 | nparam++; |
| 3275 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3276 | tline = tline->next; |
| 3277 | skip_white_(tline); |
| 3278 | if (tok_is_(tline, ",")) { |
| 3279 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 3280 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3281 | if (!tok_is_(tline, ")")) { |
| 3282 | error(ERR_NONFATAL, |
| 3283 | "`)' expected to terminate macro template"); |
| 3284 | free_tlist(origline); |
| 3285 | return DIRECTIVE_FOUND; |
| 3286 | } |
| 3287 | break; |
| 3288 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3289 | } |
| 3290 | last = tline; |
| 3291 | tline = tline->next; |
| 3292 | } |
| 3293 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 3294 | last = tline, tline = tline->next; |
| 3295 | macro_start = NULL; |
| 3296 | last->next = NULL; |
| 3297 | t = tline; |
| 3298 | while (t) { |
| 3299 | if (t->type == TOK_ID) { |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3300 | list_for_each(tt, param_start) |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 3301 | if (is_smacro_param(tt) && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3302 | !strcmp(tt->text, t->text)) |
| 3303 | t->type = tt->type; |
| 3304 | } |
| 3305 | tt = t->next; |
| 3306 | t->next = macro_start; |
| 3307 | macro_start = t; |
| 3308 | t = tt; |
| 3309 | } |
| 3310 | /* |
| 3311 | * Good. We now have a macro name, a parameter count, and a |
| 3312 | * token list (in reverse order) for an expansion. We ought |
| 3313 | * to be OK just to create an SMacro, store it, and let |
| 3314 | * free_tlist have the rest of the line (which we have |
| 3315 | * carefully re-terminated after chopping off the expansion |
| 3316 | * from the end). |
| 3317 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3318 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3319 | free_tlist(origline); |
| 3320 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3321 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3322 | case PP_UNDEF: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3323 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3324 | tline = tline->next; |
| 3325 | skip_white_(tline); |
| 3326 | tline = expand_id(tline); |
| 3327 | if (!tline || (tline->type != TOK_ID && |
| 3328 | (tline->type != TOK_PREPROC_ID || |
| 3329 | tline->text[1] != '$'))) { |
| 3330 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 3331 | free_tlist(origline); |
| 3332 | return DIRECTIVE_FOUND; |
| 3333 | } |
| 3334 | if (tline->next) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3335 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3336 | "trailing garbage after macro name ignored"); |
| 3337 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3338 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3339 | /* Find the context that symbol belongs to */ |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3340 | ctx = get_ctx(tline->text, &mname); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3341 | undef_smacro(ctx, mname); |
| 3342 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3343 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3344 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3345 | case PP_DEFSTR: |
| 3346 | case PP_IDEFSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3347 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3348 | casesense = (i == PP_DEFSTR); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3349 | |
| 3350 | tline = tline->next; |
| 3351 | skip_white_(tline); |
| 3352 | tline = expand_id(tline); |
| 3353 | if (!tline || (tline->type != TOK_ID && |
| 3354 | (tline->type != TOK_PREPROC_ID || |
| 3355 | tline->text[1] != '$'))) { |
| 3356 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3357 | pp_directives[i]); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3358 | free_tlist(origline); |
| 3359 | return DIRECTIVE_FOUND; |
| 3360 | } |
| 3361 | |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3362 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3363 | last = tline; |
| 3364 | tline = expand_smacro(tline->next); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3365 | last->next = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3366 | |
| 3367 | while (tok_type_(tline, TOK_WHITESPACE)) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3368 | tline = delete_Token(tline); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3369 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3370 | p = detoken(tline, false); |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3371 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3372 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3373 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3374 | nasm_free(p); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3375 | |
| 3376 | /* |
| 3377 | * We now have a macro name, an implicit parameter count of |
| 3378 | * zero, and a string token to use as an expansion. Create |
| 3379 | * and store an SMacro. |
| 3380 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3381 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3382 | free_tlist(origline); |
| 3383 | return DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3384 | |
H. Peter Anvin | 2f55bda | 2009-07-14 15:04:04 -0400 | [diff] [blame] | 3385 | case PP_DEFTOK: |
| 3386 | case PP_IDEFTOK: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3387 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3388 | casesense = (i == PP_DEFTOK); |
| 3389 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3390 | tline = tline->next; |
| 3391 | skip_white_(tline); |
| 3392 | tline = expand_id(tline); |
| 3393 | if (!tline || (tline->type != TOK_ID && |
| 3394 | (tline->type != TOK_PREPROC_ID || |
| 3395 | tline->text[1] != '$'))) { |
| 3396 | error(ERR_NONFATAL, |
| 3397 | "`%s' expects a macro identifier as first parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3398 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3399 | free_tlist(origline); |
| 3400 | return DIRECTIVE_FOUND; |
| 3401 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3402 | ctx = get_ctx(tline->text, &mname); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3403 | last = tline; |
| 3404 | tline = expand_smacro(tline->next); |
| 3405 | last->next = NULL; |
| 3406 | |
| 3407 | t = tline; |
| 3408 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3409 | t = t->next; |
| 3410 | /* t should now point to the string */ |
Cyrill Gorcunov | 6908e58 | 2010-09-06 19:36:15 +0400 | [diff] [blame] | 3411 | if (!tok_type_(t, TOK_STRING)) { |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3412 | error(ERR_NONFATAL, |
| 3413 | "`%s` requires string as second parameter", |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3414 | pp_directives[i]); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3415 | free_tlist(tline); |
| 3416 | free_tlist(origline); |
| 3417 | return DIRECTIVE_FOUND; |
| 3418 | } |
| 3419 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3420 | /* |
| 3421 | * Convert the string to a token stream. Note that smacros |
| 3422 | * are stored with the token stream reversed, so we have to |
| 3423 | * reverse the output of tokenize(). |
| 3424 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3425 | nasm_unquote_cstr(t->text, i); |
H. Peter Anvin | b40992c | 2010-09-15 08:57:21 -0700 | [diff] [blame] | 3426 | macro_start = reverse_tokens(tokenize(t->text)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3427 | |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3428 | /* |
| 3429 | * We now have a macro name, an implicit parameter count of |
| 3430 | * zero, and a numeric token to use as an expansion. Create |
| 3431 | * and store an SMacro. |
| 3432 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3433 | define_smacro(ctx, mname, casesense, 0, macro_start); |
Keith Kanios | b83fd0b | 2009-07-14 01:04:12 -0500 | [diff] [blame] | 3434 | free_tlist(tline); |
| 3435 | free_tlist(origline); |
| 3436 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 3437 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3438 | case PP_PATHSEARCH: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3439 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3440 | { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3441 | FILE *fp; |
| 3442 | StrList *xsl = NULL; |
| 3443 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3444 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3445 | casesense = true; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3446 | |
| 3447 | tline = tline->next; |
| 3448 | skip_white_(tline); |
| 3449 | tline = expand_id(tline); |
| 3450 | if (!tline || (tline->type != TOK_ID && |
| 3451 | (tline->type != TOK_PREPROC_ID || |
| 3452 | tline->text[1] != '$'))) { |
| 3453 | error(ERR_NONFATAL, |
| 3454 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 3455 | free_tlist(origline); |
| 3456 | return DIRECTIVE_FOUND; |
| 3457 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3458 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3459 | last = tline; |
| 3460 | tline = expand_smacro(tline->next); |
| 3461 | last->next = NULL; |
| 3462 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3463 | t = tline; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3464 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3465 | t = t->next; |
| 3466 | |
| 3467 | if (!t || (t->type != TOK_STRING && |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3468 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3469 | error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3470 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3471 | free_tlist(origline); |
| 3472 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 3473 | } |
| 3474 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3475 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3476 | "trailing garbage after `%%pathsearch' ignored"); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3477 | p = t->text; |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 3478 | if (t->type != TOK_INTERNAL_STRING) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3479 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3480 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3481 | fp = inc_fopen(p, &xsl, &xst, true); |
| 3482 | if (fp) { |
| 3483 | p = xsl->str; |
| 3484 | fclose(fp); /* Don't actually care about the file */ |
| 3485 | } |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3486 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3487 | macro_start->text = nasm_quote(p, strlen(p)); |
| 3488 | macro_start->type = TOK_STRING; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 3489 | nasm_free(xsl); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3490 | |
| 3491 | /* |
| 3492 | * We now have a macro name, an implicit parameter count of |
| 3493 | * zero, and a string token to use as an expansion. Create |
| 3494 | * and store an SMacro. |
| 3495 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3496 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3497 | free_tlist(tline); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 3498 | free_tlist(origline); |
| 3499 | return DIRECTIVE_FOUND; |
| 3500 | } |
| 3501 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3502 | case PP_STRLEN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3503 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3504 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3505 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3506 | tline = tline->next; |
| 3507 | skip_white_(tline); |
| 3508 | tline = expand_id(tline); |
| 3509 | if (!tline || (tline->type != TOK_ID && |
| 3510 | (tline->type != TOK_PREPROC_ID || |
| 3511 | tline->text[1] != '$'))) { |
| 3512 | error(ERR_NONFATAL, |
| 3513 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3514 | free_tlist(origline); |
| 3515 | return DIRECTIVE_FOUND; |
| 3516 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3517 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3518 | last = tline; |
| 3519 | tline = expand_smacro(tline->next); |
| 3520 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3521 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3522 | t = tline; |
| 3523 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3524 | t = t->next; |
| 3525 | /* t should now point to the string */ |
Cyrill Gorcunov | 4e1d5ab | 2010-07-23 18:51:51 +0400 | [diff] [blame] | 3526 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3527 | error(ERR_NONFATAL, |
| 3528 | "`%%strlen` requires string as second parameter"); |
| 3529 | free_tlist(tline); |
| 3530 | free_tlist(origline); |
| 3531 | return DIRECTIVE_FOUND; |
| 3532 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3533 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3534 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3535 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3536 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3537 | /* |
| 3538 | * We now have a macro name, an implicit parameter count of |
| 3539 | * zero, and a numeric token to use as an expansion. Create |
| 3540 | * and store an SMacro. |
| 3541 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3542 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3543 | free_tlist(tline); |
| 3544 | free_tlist(origline); |
| 3545 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3546 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3547 | case PP_STRCAT: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3548 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3549 | casesense = true; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3550 | |
| 3551 | tline = tline->next; |
| 3552 | skip_white_(tline); |
| 3553 | tline = expand_id(tline); |
| 3554 | if (!tline || (tline->type != TOK_ID && |
| 3555 | (tline->type != TOK_PREPROC_ID || |
| 3556 | tline->text[1] != '$'))) { |
| 3557 | error(ERR_NONFATAL, |
| 3558 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3559 | free_tlist(origline); |
| 3560 | return DIRECTIVE_FOUND; |
| 3561 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3562 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3563 | last = tline; |
| 3564 | tline = expand_smacro(tline->next); |
| 3565 | last->next = NULL; |
| 3566 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3567 | len = 0; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3568 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3569 | switch (t->type) { |
| 3570 | case TOK_WHITESPACE: |
| 3571 | break; |
| 3572 | case TOK_STRING: |
| 3573 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3574 | break; |
| 3575 | case TOK_OTHER: |
| 3576 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3577 | break; |
| 3578 | /* else fall through */ |
| 3579 | default: |
| 3580 | error(ERR_NONFATAL, |
| 3581 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3582 | free_tlist(tline); |
| 3583 | free_tlist(origline); |
| 3584 | return DIRECTIVE_FOUND; |
| 3585 | } |
| 3586 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3587 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3588 | p = pp = nasm_malloc(len); |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 3589 | list_for_each(t, tline) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3590 | if (t->type == TOK_STRING) { |
| 3591 | memcpy(p, t->text, t->a.len); |
| 3592 | p += t->a.len; |
| 3593 | } |
| 3594 | } |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3595 | |
| 3596 | /* |
| 3597 | * We now have a macro name, an implicit parameter count of |
| 3598 | * zero, and a numeric token to use as an expansion. Create |
| 3599 | * and store an SMacro. |
| 3600 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3601 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3602 | macro_start->text = nasm_quote(pp, len); |
| 3603 | nasm_free(pp); |
| 3604 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3605 | free_tlist(tline); |
| 3606 | free_tlist(origline); |
| 3607 | return DIRECTIVE_FOUND; |
| 3608 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3609 | case PP_SUBSTR: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3610 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3611 | { |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3612 | int64_t start, count; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3613 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3614 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3615 | casesense = true; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3616 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3617 | tline = tline->next; |
| 3618 | skip_white_(tline); |
| 3619 | tline = expand_id(tline); |
| 3620 | if (!tline || (tline->type != TOK_ID && |
| 3621 | (tline->type != TOK_PREPROC_ID || |
| 3622 | tline->text[1] != '$'))) { |
| 3623 | error(ERR_NONFATAL, |
| 3624 | "`%%substr' expects a macro identifier as first parameter"); |
| 3625 | free_tlist(origline); |
| 3626 | return DIRECTIVE_FOUND; |
| 3627 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3628 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3629 | last = tline; |
| 3630 | tline = expand_smacro(tline->next); |
| 3631 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3632 | |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3633 | if (tline) /* skip expanded id */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3634 | t = tline->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3635 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3636 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3637 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3638 | /* t should now point to the string */ |
Cyrill Gorcunov | 35519d6 | 2010-09-06 23:49:52 +0400 | [diff] [blame] | 3639 | if (!tok_type_(t, TOK_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3640 | error(ERR_NONFATAL, |
| 3641 | "`%%substr` requires string as second parameter"); |
| 3642 | free_tlist(tline); |
| 3643 | free_tlist(origline); |
| 3644 | return DIRECTIVE_FOUND; |
| 3645 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3646 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3647 | tt = t->next; |
| 3648 | tptr = &tt; |
| 3649 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3650 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3651 | pass, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3652 | if (!evalresult) { |
| 3653 | free_tlist(tline); |
| 3654 | free_tlist(origline); |
| 3655 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3656 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3657 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3658 | free_tlist(tline); |
| 3659 | free_tlist(origline); |
| 3660 | return DIRECTIVE_FOUND; |
| 3661 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3662 | start = evalresult->value - 1; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3663 | |
| 3664 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3665 | tt = tt->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3666 | if (!tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3667 | count = 1; /* Backwards compatibility: one character */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3668 | } else { |
| 3669 | tokval.t_type = TOKEN_INVALID; |
| 3670 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3671 | pass, error, NULL); |
| 3672 | if (!evalresult) { |
| 3673 | free_tlist(tline); |
| 3674 | free_tlist(origline); |
| 3675 | return DIRECTIVE_FOUND; |
| 3676 | } else if (!is_simple(evalresult)) { |
| 3677 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3678 | free_tlist(tline); |
| 3679 | free_tlist(origline); |
| 3680 | return DIRECTIVE_FOUND; |
| 3681 | } |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3682 | count = evalresult->value; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3683 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3684 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3685 | len = nasm_unquote(t->text, NULL); |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3686 | /* make start and count being in range */ |
| 3687 | if (start < 0) |
| 3688 | start = 0; |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3689 | if (count < 0) |
| 3690 | count = len + count + 1 - start; |
| 3691 | if (start + count > (int64_t)len) |
Cyrill Gorcunov | cff031e | 2010-09-07 20:31:11 +0400 | [diff] [blame] | 3692 | count = len - start; |
| 3693 | if (!len || count < 0 || start >=(int64_t)len) |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3694 | start = -1, count = 0; /* empty string */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3695 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3696 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
Cyrill Gorcunov | ab12287 | 2010-09-07 10:42:02 +0400 | [diff] [blame] | 3697 | macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3698 | macro_start->type = TOK_STRING; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3699 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3700 | /* |
| 3701 | * We now have a macro name, an implicit parameter count of |
| 3702 | * zero, and a numeric token to use as an expansion. Create |
| 3703 | * and store an SMacro. |
| 3704 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3705 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3706 | free_tlist(tline); |
| 3707 | free_tlist(origline); |
| 3708 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3709 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3710 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3711 | case PP_ASSIGN: |
| 3712 | case PP_IASSIGN: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3713 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3714 | casesense = (i == PP_ASSIGN); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3715 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3716 | tline = tline->next; |
| 3717 | skip_white_(tline); |
| 3718 | tline = expand_id(tline); |
| 3719 | if (!tline || (tline->type != TOK_ID && |
| 3720 | (tline->type != TOK_PREPROC_ID || |
| 3721 | tline->text[1] != '$'))) { |
| 3722 | error(ERR_NONFATAL, |
| 3723 | "`%%%sassign' expects a macro identifier", |
| 3724 | (i == PP_IASSIGN ? "i" : "")); |
| 3725 | free_tlist(origline); |
| 3726 | return DIRECTIVE_FOUND; |
| 3727 | } |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 3728 | ctx = get_ctx(tline->text, &mname); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3729 | last = tline; |
| 3730 | tline = expand_smacro(tline->next); |
| 3731 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3732 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3733 | t = tline; |
| 3734 | tptr = &t; |
| 3735 | tokval.t_type = TOKEN_INVALID; |
| 3736 | evalresult = |
| 3737 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3738 | free_tlist(tline); |
| 3739 | if (!evalresult) { |
| 3740 | free_tlist(origline); |
| 3741 | return DIRECTIVE_FOUND; |
| 3742 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3743 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3744 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3745 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3746 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3747 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3748 | if (!is_simple(evalresult)) { |
| 3749 | error(ERR_NONFATAL, |
| 3750 | "non-constant value given to `%%%sassign'", |
| 3751 | (i == PP_IASSIGN ? "i" : "")); |
| 3752 | free_tlist(origline); |
| 3753 | return DIRECTIVE_FOUND; |
| 3754 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3755 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 3756 | macro_start = nasm_zalloc(sizeof(*macro_start)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3757 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3758 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3759 | /* |
| 3760 | * We now have a macro name, an implicit parameter count of |
| 3761 | * zero, and a numeric token to use as an expansion. Create |
| 3762 | * and store an SMacro. |
| 3763 | */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3764 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3765 | free_tlist(origline); |
| 3766 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3767 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3768 | case PP_LINE: |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3769 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3770 | /* |
| 3771 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3772 | */ |
| 3773 | tline = tline->next; |
| 3774 | skip_white_(tline); |
| 3775 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3776 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3777 | free_tlist(origline); |
| 3778 | return DIRECTIVE_FOUND; |
| 3779 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3780 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3781 | m = 1; |
| 3782 | tline = tline->next; |
| 3783 | if (tok_is_(tline, "+")) { |
| 3784 | tline = tline->next; |
| 3785 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3786 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3787 | free_tlist(origline); |
| 3788 | return DIRECTIVE_FOUND; |
| 3789 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3790 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3791 | tline = tline->next; |
| 3792 | } |
| 3793 | skip_white_(tline); |
| 3794 | src_set_linnum(k); |
| 3795 | istk->lineinc = m; |
| 3796 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3797 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3798 | } |
| 3799 | free_tlist(origline); |
| 3800 | return DIRECTIVE_FOUND; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 3801 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 3802 | case PP_WHILE: |
| 3803 | if (defining != NULL) { |
| 3804 | if (defining->type == EXP_WHILE) { |
| 3805 | defining->def_depth ++; |
| 3806 | } |
| 3807 | return NO_DIRECTIVE_FOUND; |
| 3808 | } |
| 3809 | l = NULL; |
| 3810 | if ((istk->expansion != NULL) && |
| 3811 | (istk->expansion->emitting == false)) { |
| 3812 | j = COND_NEVER; |
| 3813 | } else { |
| 3814 | l = new_Line(); |
| 3815 | l->first = copy_Token(tline->next); |
| 3816 | j = if_condition(tline->next, i); |
| 3817 | tline->next = NULL; /* it got freed */ |
| 3818 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 3819 | } |
| 3820 | ed = new_ExpDef(EXP_WHILE); |
| 3821 | ed->state = j; |
| 3822 | ed->cur_depth = 1; |
| 3823 | ed->max_depth = DEADMAN_LIMIT; |
| 3824 | ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true); |
| 3825 | if (ed->ignoring == false) { |
| 3826 | ed->line = l; |
| 3827 | ed->last = l; |
| 3828 | } else if (l != NULL) { |
| 3829 | delete_Token(l->first); |
| 3830 | nasm_free(l); |
| 3831 | l = NULL; |
| 3832 | } |
| 3833 | ed->prev = defining; |
| 3834 | defining = ed; |
| 3835 | free_tlist(origline); |
| 3836 | return DIRECTIVE_FOUND; |
| 3837 | |
| 3838 | case PP_ENDWHILE: |
| 3839 | if (defining != NULL) { |
| 3840 | if (defining->type == EXP_WHILE) { |
| 3841 | if (defining->def_depth > 0) { |
| 3842 | defining->def_depth --; |
| 3843 | return NO_DIRECTIVE_FOUND; |
| 3844 | } |
| 3845 | } else { |
| 3846 | return NO_DIRECTIVE_FOUND; |
| 3847 | } |
| 3848 | } |
| 3849 | if (tline->next != NULL) { |
| 3850 | error_precond(ERR_WARNING|ERR_PASS1, |
| 3851 | "trailing garbage after `%%endwhile' ignored"); |
| 3852 | } |
| 3853 | if ((defining == NULL) || (defining->type != EXP_WHILE)) { |
| 3854 | error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'"); |
| 3855 | return DIRECTIVE_FOUND; |
| 3856 | } |
| 3857 | ed = defining; |
| 3858 | defining = ed->prev; |
| 3859 | if (ed->ignoring == false) { |
| 3860 | ed->prev = expansions; |
| 3861 | expansions = ed; |
| 3862 | ei = new_ExpInv(EXP_WHILE, ed); |
| 3863 | ei->current = ed->line->next; |
| 3864 | ei->emitting = true; |
| 3865 | ei->prev = istk->expansion; |
| 3866 | istk->expansion = ei; |
| 3867 | } else { |
| 3868 | nasm_free(ed); |
| 3869 | } |
| 3870 | free_tlist(origline); |
| 3871 | return DIRECTIVE_FOUND; |
| 3872 | |
| 3873 | case PP_EXITWHILE: |
| 3874 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3875 | /* |
| 3876 | * We must search along istk->expansion until we hit a |
| 3877 | * while invocation. Then we disable the emitting state(s) |
| 3878 | * between exitwhile and endwhile. |
| 3879 | */ |
| 3880 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 3881 | if (ei->type == EXP_WHILE) { |
| 3882 | break; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | if (ei != NULL) { |
| 3887 | /* |
| 3888 | * Set all invocations leading back to the while |
| 3889 | * invocation to a non-emitting state. |
| 3890 | */ |
| 3891 | for (eei = istk->expansion; eei != ei; eei = eei->prev) { |
| 3892 | eei->emitting = false; |
| 3893 | } |
| 3894 | eei->emitting = false; |
| 3895 | eei->current = NULL; |
| 3896 | eei->def->cur_depth = eei->def->max_depth; |
| 3897 | } else { |
| 3898 | error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block"); |
| 3899 | } |
| 3900 | free_tlist(origline); |
| 3901 | return DIRECTIVE_FOUND; |
| 3902 | |
| 3903 | case PP_COMMENT: |
| 3904 | if (defining != NULL) { |
| 3905 | if (defining->type == EXP_COMMENT) { |
| 3906 | defining->def_depth ++; |
| 3907 | } |
| 3908 | return NO_DIRECTIVE_FOUND; |
| 3909 | } |
| 3910 | ed = new_ExpDef(EXP_COMMENT); |
| 3911 | ed->ignoring = true; |
| 3912 | ed->prev = defining; |
| 3913 | defining = ed; |
| 3914 | free_tlist(origline); |
| 3915 | return DIRECTIVE_FOUND; |
| 3916 | |
| 3917 | case PP_ENDCOMMENT: |
| 3918 | if (defining != NULL) { |
| 3919 | if (defining->type == EXP_COMMENT) { |
| 3920 | if (defining->def_depth > 0) { |
| 3921 | defining->def_depth --; |
| 3922 | return NO_DIRECTIVE_FOUND; |
| 3923 | } |
| 3924 | } else { |
| 3925 | return NO_DIRECTIVE_FOUND; |
| 3926 | } |
| 3927 | } |
| 3928 | if ((defining == NULL) || (defining->type != EXP_COMMENT)) { |
| 3929 | error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'"); |
| 3930 | return DIRECTIVE_FOUND; |
| 3931 | } |
| 3932 | ed = defining; |
| 3933 | defining = ed->prev; |
| 3934 | nasm_free(ed); |
| 3935 | free_tlist(origline); |
| 3936 | return DIRECTIVE_FOUND; |
| 3937 | |
| 3938 | case PP_FINAL: |
| 3939 | if (defining != NULL) return NO_DIRECTIVE_FOUND; |
| 3940 | if (in_final != false) { |
| 3941 | error(ERR_FATAL, "`%%final' cannot be used recursively"); |
| 3942 | } |
| 3943 | tline = tline->next; |
| 3944 | skip_white_(tline); |
| 3945 | if (tline == NULL) { |
| 3946 | error(ERR_NONFATAL, "`%%final' expects at least one parameter"); |
| 3947 | } else { |
| 3948 | l = new_Line(); |
| 3949 | l->first = copy_Token(tline); |
| 3950 | l->next = finals; |
| 3951 | finals = l; |
| 3952 | } |
| 3953 | free_tlist(origline); |
| 3954 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3955 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3956 | default: |
| 3957 | error(ERR_FATAL, |
| 3958 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3959 | pp_directives[i]); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3960 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3961 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3965 | * Ensure that a macro parameter contains a condition code and |
| 3966 | * nothing else. Return the condition code index if so, or -1 |
| 3967 | * otherwise. |
| 3968 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3969 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3970 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3971 | Token *tt; |
| 3972 | int i, j, k, m; |
| 3973 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3974 | if (!t) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 3975 | return -1; /* Probably a %+ without a space */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3976 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3977 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3978 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3979 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3980 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3981 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3982 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3983 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3984 | |
| 3985 | i = -1; |
Cyrill Gorcunov | a731924 | 2010-06-03 22:04:36 +0400 | [diff] [blame] | 3986 | j = ARRAY_SIZE(conditions); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3987 | while (j - i > 1) { |
| 3988 | k = (j + i) / 2; |
| 3989 | m = nasm_stricmp(t->text, conditions[k]); |
| 3990 | if (m == 0) { |
| 3991 | i = k; |
| 3992 | j = -2; |
| 3993 | break; |
| 3994 | } else if (m < 0) { |
| 3995 | j = k; |
| 3996 | } else |
| 3997 | i = k; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3998 | } |
| 3999 | if (j != -2) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4000 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4001 | return i; |
| 4002 | } |
| 4003 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4004 | static bool paste_tokens(Token **head, const struct tokseq_match *m, |
| 4005 | int mnum, bool handle_paste_tokens) |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4006 | { |
| 4007 | Token **tail, *t, *tt; |
| 4008 | Token **paste_head; |
| 4009 | bool did_paste = false; |
| 4010 | char *tmp; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4011 | int i; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4012 | |
| 4013 | /* Now handle token pasting... */ |
| 4014 | paste_head = NULL; |
| 4015 | tail = head; |
| 4016 | while ((t = *tail) && (tt = t->next)) { |
| 4017 | switch (t->type) { |
| 4018 | case TOK_WHITESPACE: |
| 4019 | if (tt->type == TOK_WHITESPACE) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4020 | /* Zap adjacent whitespace tokens */ |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4021 | t->next = delete_Token(tt); |
| 4022 | } else { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4023 | /* Do not advance paste_head here */ |
| 4024 | tail = &t->next; |
| 4025 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4026 | break; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4027 | case TOK_PASTE: /* %+ */ |
| 4028 | if (handle_paste_tokens) { |
| 4029 | /* Zap %+ and whitespace tokens to the right */ |
| 4030 | while (t && (t->type == TOK_WHITESPACE || |
| 4031 | t->type == TOK_PASTE)) |
| 4032 | t = *tail = delete_Token(t); |
| 4033 | if (!paste_head || !t) |
| 4034 | break; /* Nothing to paste with */ |
| 4035 | tail = paste_head; |
| 4036 | t = *tail; |
| 4037 | tt = t->next; |
| 4038 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 4039 | tt = t->next = delete_Token(tt); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4040 | if (tt) { |
| 4041 | tmp = nasm_strcat(t->text, tt->text); |
| 4042 | delete_Token(t); |
| 4043 | tt = delete_Token(tt); |
| 4044 | t = *tail = tokenize(tmp); |
| 4045 | nasm_free(tmp); |
| 4046 | while (t->next) { |
| 4047 | tail = &t->next; |
| 4048 | t = t->next; |
| 4049 | } |
| 4050 | t->next = tt; /* Attach the remaining token chain */ |
| 4051 | did_paste = true; |
| 4052 | } |
| 4053 | paste_head = tail; |
| 4054 | tail = &t->next; |
| 4055 | break; |
| 4056 | } |
| 4057 | /* else fall through */ |
| 4058 | default: |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4059 | /* |
| 4060 | * Concatenation of tokens might look nontrivial |
| 4061 | * but in real it's pretty simple -- the caller |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4062 | * prepares the masks of token types to be concatenated |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4063 | * and we simply find matched sequences and slip |
| 4064 | * them together |
| 4065 | */ |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4066 | for (i = 0; i < mnum; i++) { |
| 4067 | if (PP_CONCAT_MASK(t->type) & m[i].mask_head) { |
| 4068 | size_t len = 0; |
| 4069 | char *tmp, *p; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4070 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4071 | while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4072 | len += strlen(tt->text); |
| 4073 | tt = tt->next; |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4074 | } |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4075 | |
Cyrill Gorcunov | fdd0ac5 | 2011-06-27 01:22:27 +0400 | [diff] [blame] | 4076 | nasm_dump_token(tt); |
| 4077 | |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4078 | /* |
| 4079 | * Now tt points to the first token after |
| 4080 | * the potential paste area... |
| 4081 | */ |
| 4082 | if (tt != t->next) { |
| 4083 | /* We have at least two tokens... */ |
| 4084 | len += strlen(t->text); |
| 4085 | p = tmp = nasm_malloc(len+1); |
| 4086 | while (t != tt) { |
| 4087 | strcpy(p, t->text); |
| 4088 | p = strchr(p, '\0'); |
| 4089 | t = delete_Token(t); |
| 4090 | } |
| 4091 | t = *tail = tokenize(tmp); |
| 4092 | nasm_free(tmp); |
| 4093 | while (t->next) { |
| 4094 | tail = &t->next; |
| 4095 | t = t->next; |
| 4096 | } |
| 4097 | t->next = tt; /* Attach the remaining token chain */ |
| 4098 | did_paste = true; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4099 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4100 | paste_head = tail; |
| 4101 | tail = &t->next; |
| 4102 | break; |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4103 | } |
Cyrill Gorcunov | 575d428 | 2010-10-06 00:25:55 +0400 | [diff] [blame] | 4104 | } |
| 4105 | if (i >= mnum) { /* no match */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4106 | tail = &t->next; |
| 4107 | if (!tok_type_(t->next, TOK_WHITESPACE)) |
| 4108 | paste_head = tail; |
| 4109 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4110 | break; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4111 | } |
| 4112 | } |
| 4113 | return did_paste; |
| 4114 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4115 | |
| 4116 | /* |
| 4117 | * expands to a list of tokens from %{x:y} |
| 4118 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4119 | static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4120 | { |
| 4121 | Token *t = tline, **tt, *tm, *head; |
| 4122 | char *pos; |
| 4123 | int fst, lst, j, i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4124 | |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4125 | pos = strchr(tline->text, ':'); |
| 4126 | nasm_assert(pos); |
| 4127 | |
| 4128 | lst = atoi(pos + 1); |
| 4129 | fst = atoi(tline->text + 1); |
| 4130 | |
| 4131 | /* |
| 4132 | * only macros params are accounted so |
| 4133 | * if someone passes %0 -- we reject such |
| 4134 | * value(s) |
| 4135 | */ |
| 4136 | if (lst == 0 || fst == 0) |
| 4137 | goto err; |
| 4138 | |
| 4139 | /* the values should be sane */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4140 | if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) || |
| 4141 | (lst > (int)ei->nparam || lst < (-(int)ei->nparam))) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4142 | goto err; |
| 4143 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4144 | fst = fst < 0 ? fst + (int)ei->nparam + 1: fst; |
| 4145 | lst = lst < 0 ? lst + (int)ei->nparam + 1: lst; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4146 | |
| 4147 | /* counted from zero */ |
| 4148 | fst--, lst--; |
| 4149 | |
| 4150 | /* |
| 4151 | * it will be at least one token |
| 4152 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4153 | tm = ei->params[(fst + ei->rotate) % ei->nparam]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4154 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4155 | head = t, tt = &t->next; |
| 4156 | if (fst < lst) { |
| 4157 | for (i = fst + 1; i <= lst; i++) { |
| 4158 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4159 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4160 | j = (i + ei->rotate) % ei->nparam; |
| 4161 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4162 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4163 | *tt = t, tt = &t->next; |
| 4164 | } |
| 4165 | } else { |
| 4166 | for (i = fst - 1; i >= lst; i--) { |
| 4167 | t = new_Token(NULL, TOK_OTHER, ",", 0); |
| 4168 | *tt = t, tt = &t->next; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4169 | j = (i + ei->rotate) % ei->nparam; |
| 4170 | tm = ei->params[j]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4171 | t = new_Token(NULL, tm->type, tm->text, 0); |
| 4172 | *tt = t, tt = &t->next; |
| 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | *last = tt; |
| 4177 | return head; |
| 4178 | |
| 4179 | err: |
| 4180 | error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range", |
| 4181 | &tline->text[1]); |
| 4182 | return tline; |
| 4183 | } |
| 4184 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4185 | /* |
| 4186 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4187 | * %-n) and MMacro-local identifiers (%%foo) as well as |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4188 | * macro indirection (%[...]) and range (%{..:..}). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4189 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4190 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4191 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4192 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4193 | bool changed = false; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4194 | char *pos; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4195 | |
| 4196 | tail = &thead; |
| 4197 | thead = NULL; |
| 4198 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4199 | nasm_dump_stream(tline); |
| 4200 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4201 | while (tline) { |
| 4202 | if (tline->type == TOK_PREPROC_ID && |
Cyrill Gorcunov | ca61119 | 2010-06-04 09:22:12 +0400 | [diff] [blame] | 4203 | (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) || |
| 4204 | (tline->text[1] >= '0' && tline->text[1] <= '9') || |
| 4205 | tline->text[1] == '%')) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4206 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4207 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4208 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4209 | unsigned int n; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4210 | int i; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4211 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4212 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4213 | t = tline; |
| 4214 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4215 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4216 | for (ei = istk->expansion; ei != NULL; ei = ei->prev) { |
| 4217 | if (ei->type == EXP_MMACRO) { |
| 4218 | break; |
| 4219 | } |
| 4220 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4221 | if (ei == NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4222 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4223 | } else { |
| 4224 | pos = strchr(t->text, ':'); |
| 4225 | if (!pos) { |
| 4226 | switch (t->text[1]) { |
| 4227 | /* |
| 4228 | * We have to make a substitution of one of the |
| 4229 | * forms %1, %-1, %+1, %%foo, %0. |
| 4230 | */ |
| 4231 | case '0': |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4232 | if ((strlen(t->text) > 2) && (t->text[2] == '0')) { |
| 4233 | type = TOK_ID; |
| 4234 | text = nasm_strdup(ei->label_text); |
| 4235 | } else { |
| 4236 | type = TOK_NUMBER; |
| 4237 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam); |
| 4238 | text = nasm_strdup(tmpbuf); |
| 4239 | } |
| 4240 | break; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4241 | case '%': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4242 | type = TOK_ID; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4243 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4244 | ei->unique); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4245 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 4246 | break; |
| 4247 | case '-': |
| 4248 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4249 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4250 | tt = NULL; |
| 4251 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4252 | if (ei->nparam > 1) |
| 4253 | n = (n + ei->rotate) % ei->nparam; |
| 4254 | tt = ei->params[n]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4255 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4256 | cc = find_cc(tt); |
| 4257 | if (cc == -1) { |
| 4258 | error(ERR_NONFATAL, |
| 4259 | "macro parameter %d is not a condition code", |
| 4260 | n + 1); |
| 4261 | text = NULL; |
| 4262 | } else { |
| 4263 | type = TOK_ID; |
| 4264 | if (inverse_ccs[cc] == -1) { |
| 4265 | error(ERR_NONFATAL, |
| 4266 | "condition code `%s' is not invertible", |
| 4267 | conditions[cc]); |
| 4268 | text = NULL; |
| 4269 | } else |
| 4270 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
| 4271 | } |
| 4272 | break; |
| 4273 | case '+': |
| 4274 | n = atoi(t->text + 2) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4275 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4276 | tt = NULL; |
| 4277 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4278 | if (ei->nparam > 1) |
| 4279 | n = (n + ei->rotate) % ei->nparam; |
| 4280 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4281 | } |
| 4282 | cc = find_cc(tt); |
| 4283 | if (cc == -1) { |
| 4284 | error(ERR_NONFATAL, |
| 4285 | "macro parameter %d is not a condition code", |
| 4286 | n + 1); |
| 4287 | text = NULL; |
| 4288 | } else { |
| 4289 | type = TOK_ID; |
| 4290 | text = nasm_strdup(conditions[cc]); |
| 4291 | } |
| 4292 | break; |
| 4293 | default: |
| 4294 | n = atoi(t->text + 1) - 1; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4295 | if (n >= ei->nparam) |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4296 | tt = NULL; |
| 4297 | else { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4298 | if (ei->nparam > 1) |
| 4299 | n = (n + ei->rotate) % ei->nparam; |
| 4300 | tt = ei->params[n]; |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4301 | } |
| 4302 | if (tt) { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4303 | for (i = 0; i < ei->paramlen[n]; i++) { |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4304 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 4305 | tail = &(*tail)->next; |
| 4306 | tt = tt->next; |
| 4307 | } |
| 4308 | } |
| 4309 | text = NULL; /* we've done it here */ |
| 4310 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4311 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4312 | } else { |
| 4313 | /* |
| 4314 | * seems we have a parameters range here |
| 4315 | */ |
| 4316 | Token *head, **last; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4317 | head = expand_mmac_params_range(ei, t, &last); |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4318 | if (head != t) { |
| 4319 | *tail = head; |
| 4320 | *last = tline; |
| 4321 | tline = head; |
| 4322 | text = NULL; |
| 4323 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4324 | } |
Cyrill Gorcunov | c29404d | 2010-06-05 01:50:23 +0400 | [diff] [blame] | 4325 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4326 | if (!text) { |
| 4327 | delete_Token(t); |
| 4328 | } else { |
| 4329 | *tail = t; |
| 4330 | tail = &t->next; |
| 4331 | t->type = type; |
| 4332 | nasm_free(t->text); |
| 4333 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4334 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4335 | } |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4336 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4337 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4338 | } else if (tline->type == TOK_INDIRECT) { |
| 4339 | t = tline; |
| 4340 | tline = tline->next; |
| 4341 | tt = tokenize(t->text); |
| 4342 | tt = expand_mmac_params(tt); |
| 4343 | tt = expand_smacro(tt); |
| 4344 | *tail = tt; |
| 4345 | while (tt) { |
| 4346 | tt->a.mac = NULL; /* Necessary? */ |
| 4347 | tail = &tt->next; |
| 4348 | tt = tt->next; |
| 4349 | } |
| 4350 | delete_Token(t); |
| 4351 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4352 | } else { |
| 4353 | t = *tail = tline; |
| 4354 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4355 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4356 | tail = &t->next; |
| 4357 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4358 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4359 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 4360 | |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4361 | if (changed) { |
| 4362 | const struct tokseq_match t[] = { |
| 4363 | { |
| 4364 | PP_CONCAT_MASK(TOK_ID) | |
| 4365 | PP_CONCAT_MASK(TOK_FLOAT), /* head */ |
| 4366 | PP_CONCAT_MASK(TOK_ID) | |
| 4367 | PP_CONCAT_MASK(TOK_NUMBER) | |
| 4368 | PP_CONCAT_MASK(TOK_FLOAT) | |
| 4369 | PP_CONCAT_MASK(TOK_OTHER) /* tail */ |
| 4370 | }, |
| 4371 | { |
| 4372 | PP_CONCAT_MASK(TOK_NUMBER), /* head */ |
| 4373 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4374 | } |
| 4375 | }; |
| 4376 | paste_tokens(&thead, t, ARRAY_SIZE(t), false); |
| 4377 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 4378 | |
Cyrill Gorcunov | 2e04600 | 2011-06-26 23:33:56 +0400 | [diff] [blame] | 4379 | nasm_dump_token(thead); |
| 4380 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4381 | return thead; |
| 4382 | } |
| 4383 | |
| 4384 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4385 | * Expand all single-line macro calls made in the given line. |
| 4386 | * Return the expanded version of the line. The original is deemed |
| 4387 | * to be destroyed in the process. (In reality we'll just move |
| 4388 | * Tokens from input to output a lot of the time, rather than |
| 4389 | * actually bothering to destroy and replicate.) |
| 4390 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4391 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4392 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4393 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4394 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4395 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4396 | Token **params; |
| 4397 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 4398 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 4399 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4400 | Token *org_tline = tline; |
| 4401 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 4402 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 4403 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4404 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4405 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4406 | /* |
| 4407 | * Trick: we should avoid changing the start token pointer since it can |
| 4408 | * be contained in "next" field of other token. Because of this |
| 4409 | * we allocate a copy of first token and work with it; at the end of |
| 4410 | * routine we copy it back |
| 4411 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4412 | if (org_tline) { |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4413 | tline = new_Token(org_tline->next, org_tline->type, |
| 4414 | org_tline->text, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4415 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4416 | nasm_free(org_tline->text); |
| 4417 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4420 | expanded = true; /* Always expand %+ at least once */ |
H. Peter Anvin | 8287daf | 2009-07-07 16:00:58 -0700 | [diff] [blame] | 4421 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4422 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4423 | thead = NULL; |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4424 | tail = &thead; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4425 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4426 | while (tline) { /* main token loop */ |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4427 | if (!--deadman) { |
| 4428 | error(ERR_NONFATAL, "interminable macro recursion"); |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4429 | goto err; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4430 | } |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 4431 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4432 | if ((mname = tline->text)) { |
| 4433 | /* if this token is a local macro, look in local context */ |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4434 | if (tline->type == TOK_ID) { |
| 4435 | head = (SMacro *)hash_findix(&smacros, mname); |
| 4436 | } else if (tline->type == TOK_PREPROC_ID) { |
Cyrill Gorcunov | 290eac7 | 2011-06-28 01:59:05 +0400 | [diff] [blame] | 4437 | ctx = get_ctx(mname, &mname); |
Cyrill Gorcunov | c56d9ad | 2010-02-11 15:12:19 +0300 | [diff] [blame] | 4438 | head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL; |
| 4439 | } else |
| 4440 | head = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 4441 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4442 | /* |
| 4443 | * We've hit an identifier. As in is_mmacro below, we first |
| 4444 | * check whether the identifier is a single-line macro at |
| 4445 | * all, then think about checking for parameters if |
| 4446 | * necessary. |
| 4447 | */ |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4448 | list_for_each(m, head) |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4449 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 4450 | break; |
| 4451 | if (m) { |
| 4452 | mstart = tline; |
| 4453 | params = NULL; |
| 4454 | paramsize = NULL; |
| 4455 | if (m->nparam == 0) { |
| 4456 | /* |
| 4457 | * Simple case: the macro is parameterless. Discard the |
| 4458 | * one token that the macro call took, and push the |
| 4459 | * expansion back on the to-do stack. |
| 4460 | */ |
| 4461 | if (!m->expansion) { |
| 4462 | if (!strcmp("__FILE__", m->name)) { |
| 4463 | int32_t num = 0; |
| 4464 | char *file = NULL; |
| 4465 | src_get(&num, &file); |
| 4466 | tline->text = nasm_quote(file, strlen(file)); |
| 4467 | tline->type = TOK_STRING; |
| 4468 | nasm_free(file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4469 | continue; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4470 | } |
| 4471 | if (!strcmp("__LINE__", m->name)) { |
| 4472 | nasm_free(tline->text); |
| 4473 | make_tok_num(tline, src_get_linnum()); |
| 4474 | continue; |
| 4475 | } |
| 4476 | if (!strcmp("__BITS__", m->name)) { |
| 4477 | nasm_free(tline->text); |
| 4478 | make_tok_num(tline, globalbits); |
| 4479 | continue; |
| 4480 | } |
| 4481 | tline = delete_Token(tline); |
| 4482 | continue; |
| 4483 | } |
| 4484 | } else { |
| 4485 | /* |
| 4486 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4487 | * exists and takes parameters. We must find the |
| 4488 | * parameters in the call, count them, find the SMacro |
| 4489 | * that corresponds to that form of the macro call, and |
| 4490 | * substitute for the parameters when we expand. What a |
| 4491 | * pain. |
| 4492 | */ |
| 4493 | /*tline = tline->next; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4494 | skip_white_(tline); */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4495 | do { |
| 4496 | t = tline->next; |
| 4497 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4498 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4499 | t->text = NULL; |
| 4500 | t = tline->next = delete_Token(t); |
| 4501 | } |
| 4502 | tline = t; |
| 4503 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 4504 | if (!tok_is_(tline, "(")) { |
| 4505 | /* |
| 4506 | * This macro wasn't called with parameters: ignore |
| 4507 | * the call. (Behaviour borrowed from gnu cpp.) |
| 4508 | */ |
| 4509 | tline = mstart; |
| 4510 | m = NULL; |
| 4511 | } else { |
| 4512 | int paren = 0; |
| 4513 | int white = 0; |
| 4514 | brackets = 0; |
| 4515 | nparam = 0; |
| 4516 | sparam = PARAM_DELTA; |
| 4517 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 4518 | params[0] = tline->next; |
| 4519 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 4520 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4521 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4522 | /* |
| 4523 | * For some unusual expansions |
| 4524 | * which concatenates function call |
| 4525 | */ |
| 4526 | t = tline->next; |
| 4527 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4528 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4529 | t->text = NULL; |
| 4530 | t = tline->next = delete_Token(t); |
| 4531 | } |
| 4532 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 4533 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4534 | if (!tline) { |
| 4535 | error(ERR_NONFATAL, |
| 4536 | "macro call expects terminating `)'"); |
| 4537 | break; |
| 4538 | } |
| 4539 | if (tline->type == TOK_WHITESPACE |
| 4540 | && brackets <= 0) { |
| 4541 | if (paramsize[nparam]) |
| 4542 | white++; |
| 4543 | else |
| 4544 | params[nparam] = tline->next; |
| 4545 | continue; /* parameter loop */ |
| 4546 | } |
| 4547 | if (tline->type == TOK_OTHER |
| 4548 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4549 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4550 | if (ch == ',' && !paren && brackets <= 0) { |
| 4551 | if (++nparam >= sparam) { |
| 4552 | sparam += PARAM_DELTA; |
| 4553 | params = nasm_realloc(params, |
Cyrill Gorcunov | ed4a805 | 2010-04-09 15:40:35 +0400 | [diff] [blame] | 4554 | sparam * sizeof(Token *)); |
| 4555 | paramsize = nasm_realloc(paramsize, |
| 4556 | sparam * sizeof(int)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4557 | } |
| 4558 | params[nparam] = tline->next; |
| 4559 | paramsize[nparam] = 0; |
| 4560 | white = 0; |
| 4561 | continue; /* parameter loop */ |
| 4562 | } |
| 4563 | if (ch == '{' && |
| 4564 | (brackets > 0 || (brackets == 0 && |
| 4565 | !paramsize[nparam]))) |
| 4566 | { |
| 4567 | if (!(brackets++)) { |
| 4568 | params[nparam] = tline->next; |
| 4569 | continue; /* parameter loop */ |
| 4570 | } |
| 4571 | } |
| 4572 | if (ch == '}' && brackets > 0) |
| 4573 | if (--brackets == 0) { |
| 4574 | brackets = -1; |
| 4575 | continue; /* parameter loop */ |
| 4576 | } |
| 4577 | if (ch == '(' && !brackets) |
| 4578 | paren++; |
| 4579 | if (ch == ')' && brackets <= 0) |
| 4580 | if (--paren < 0) |
| 4581 | break; |
| 4582 | } |
| 4583 | if (brackets < 0) { |
| 4584 | brackets = 0; |
| 4585 | error(ERR_NONFATAL, "braces do not " |
| 4586 | "enclose all of macro parameter"); |
| 4587 | } |
| 4588 | paramsize[nparam] += white + 1; |
| 4589 | white = 0; |
| 4590 | } /* parameter loop */ |
| 4591 | nparam++; |
| 4592 | while (m && (m->nparam != nparam || |
| 4593 | mstrcmp(m->name, mname, |
| 4594 | m->casesense))) |
| 4595 | m = m->next; |
| 4596 | if (!m) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4597 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4598 | "macro `%s' exists, " |
| 4599 | "but not taking %d parameters", |
| 4600 | mstart->text, nparam); |
| 4601 | } |
| 4602 | } |
| 4603 | if (m && m->in_progress) |
| 4604 | m = NULL; |
| 4605 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4606 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4607 | * Design question: should we handle !tline, which |
| 4608 | * indicates missing ')' here, or expand those |
| 4609 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4610 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4611 | */ |
| 4612 | nasm_free(params); |
| 4613 | nasm_free(paramsize); |
| 4614 | tline = mstart; |
| 4615 | } else { |
| 4616 | /* |
| 4617 | * Expand the macro: we are placed on the last token of the |
| 4618 | * call, so that we can easily split the call from the |
| 4619 | * following tokens. We also start by pushing an SMAC_END |
| 4620 | * token for the cycle removal. |
| 4621 | */ |
| 4622 | t = tline; |
| 4623 | if (t) { |
| 4624 | tline = t->next; |
| 4625 | t->next = NULL; |
| 4626 | } |
| 4627 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4628 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4629 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4630 | tline = tt; |
Cyrill Gorcunov | 3b4e86b | 2010-06-02 15:57:51 +0400 | [diff] [blame] | 4631 | list_for_each(t, m->expansion) { |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4632 | if (is_smacro_param(t)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4633 | Token *pcopy = tline, **ptail = &pcopy; |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4634 | Token *ttt; |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4635 | int i, idx; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4636 | |
Cyrill Gorcunov | 194ba89 | 2011-06-30 01:16:35 +0400 | [diff] [blame] | 4637 | idx = smacro_get_param_idx(t); |
| 4638 | ttt = params[idx]; |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4639 | |
| 4640 | /* |
| 4641 | * We need smacro paramters appended. |
| 4642 | */ |
| 4643 | for (i = paramsize[idx]; i > 0; i--) { |
| 4644 | *ptail = new_Token(tline, ttt->type, ttt->text, 0); |
| 4645 | ptail = &(*ptail)->next; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4646 | ttt = ttt->next; |
| 4647 | } |
Cyrill Gorcunov | 0ad6a7b | 2011-06-30 01:36:45 +0400 | [diff] [blame] | 4648 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4649 | tline = pcopy; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4650 | } else if (t->type == TOK_PREPROC_Q) { |
| 4651 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 4652 | tline = tt; |
| 4653 | } else if (t->type == TOK_PREPROC_QQ) { |
| 4654 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 4655 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4656 | } else { |
| 4657 | tt = new_Token(tline, t->type, t->text, 0); |
| 4658 | tline = tt; |
| 4659 | } |
| 4660 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4661 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4662 | /* |
| 4663 | * Having done that, get rid of the macro call, and clean |
| 4664 | * up the parameters. |
| 4665 | */ |
| 4666 | nasm_free(params); |
| 4667 | nasm_free(paramsize); |
| 4668 | free_tlist(mstart); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4669 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4670 | continue; /* main token loop */ |
| 4671 | } |
| 4672 | } |
| 4673 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4674 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4675 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4676 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4677 | tline = delete_Token(tline); |
| 4678 | } else { |
| 4679 | t = *tail = tline; |
| 4680 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 4681 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4682 | t->next = NULL; |
| 4683 | tail = &t->next; |
| 4684 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4687 | /* |
| 4688 | * 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] | 4689 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4690 | * TOK_IDs should be concatenated. |
| 4691 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 4692 | * them (without white spaces in between). |
| 4693 | */ |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4694 | if (expanded) { |
Cyrill Gorcunov | c6a742c | 2011-06-27 01:23:09 +0400 | [diff] [blame] | 4695 | const struct tokseq_match t[] = { |
| 4696 | { |
| 4697 | PP_CONCAT_MASK(TOK_ID) | |
| 4698 | PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */ |
| 4699 | PP_CONCAT_MASK(TOK_ID) | |
| 4700 | PP_CONCAT_MASK(TOK_PREPROC_ID) | |
| 4701 | PP_CONCAT_MASK(TOK_NUMBER) /* tail */ |
| 4702 | } |
| 4703 | }; |
| 4704 | if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) { |
Cyrill Gorcunov | 8dcbbd7 | 2010-09-25 02:33:20 +0400 | [diff] [blame] | 4705 | /* |
| 4706 | * If we concatenated something, *and* we had previously expanded |
| 4707 | * an actual macro, scan the lines again for macros... |
| 4708 | */ |
| 4709 | tline = thead; |
| 4710 | expanded = false; |
| 4711 | goto again; |
| 4712 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4713 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4714 | |
Cyrill Gorcunov | bd38c8f | 2009-11-21 11:11:23 +0300 | [diff] [blame] | 4715 | err: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4716 | if (org_tline) { |
| 4717 | if (thead) { |
| 4718 | *org_tline = *thead; |
| 4719 | /* since we just gave text to org_line, don't free it */ |
| 4720 | thead->text = NULL; |
| 4721 | delete_Token(thead); |
| 4722 | } else { |
| 4723 | /* the expression expanded to empty line; |
| 4724 | we can't return NULL for some reasons |
| 4725 | we just set the line to a single WHITESPACE token. */ |
| 4726 | memset(org_tline, 0, sizeof(*org_tline)); |
| 4727 | org_tline->text = NULL; |
| 4728 | org_tline->type = TOK_WHITESPACE; |
| 4729 | } |
| 4730 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4733 | return thead; |
| 4734 | } |
| 4735 | |
| 4736 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4737 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 4738 | * right before they are fetched in. The reason is that there can be |
| 4739 | * identifiers consisting of several subparts. We consider that if there |
| 4740 | * are more than one element forming the name, user wants a expansion, |
| 4741 | * otherwise it will be left as-is. Example: |
| 4742 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4743 | * %define %$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4744 | * |
| 4745 | * the identifier %$abc will be left as-is so that the handler for %define |
| 4746 | * will suck it and define the corresponding value. Other case: |
| 4747 | * |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4748 | * %define _%$abc cde |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4749 | * |
| 4750 | * In this case user wants name to be expanded *before* %define starts |
| 4751 | * working, so we'll expand %$abc into something (if it has a value; |
| 4752 | * otherwise it will be left as-is) then concatenate all successive |
| 4753 | * PP_IDs into one. |
| 4754 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4755 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4756 | { |
| 4757 | Token *cur, *oldnext = NULL; |
| 4758 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4759 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4760 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4761 | |
| 4762 | cur = tline; |
| 4763 | while (cur->next && |
Cyrill Gorcunov | 5b6c96b | 2011-06-30 00:22:53 +0400 | [diff] [blame] | 4764 | (cur->next->type == TOK_ID || |
| 4765 | cur->next->type == TOK_PREPROC_ID || |
| 4766 | cur->next->type == TOK_NUMBER)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4767 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4768 | |
| 4769 | /* If identifier consists of just one token, don't expand */ |
| 4770 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4771 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4772 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4773 | if (cur) { |
| 4774 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 4775 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4778 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4779 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4780 | if (cur) { |
| 4781 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 4782 | cur = tline; |
| 4783 | while (cur && cur->next) |
| 4784 | cur = cur->next; |
| 4785 | if (cur) |
| 4786 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | return tline; |
| 4790 | } |
| 4791 | |
| 4792 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4793 | * Determine whether the given line constitutes a multi-line macro |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4794 | * 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] | 4795 | * to check for an initial label - that's taken care of in |
| 4796 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 4797 | * to be called with tline->type == TOK_ID, so the putative macro |
| 4798 | * name is easy to find. |
| 4799 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4800 | static ExpDef *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4801 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4802 | ExpDef *head, *ed; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4803 | Token **params; |
| 4804 | int nparam; |
| 4805 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4806 | head = (ExpDef *) hash_findix(&expdefs, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4807 | |
| 4808 | /* |
| 4809 | * Efficiency: first we see if any macro exists with the given |
| 4810 | * name. If not, we can return NULL immediately. _Then_ we |
| 4811 | * count the parameters, and then we look further along the |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4812 | * list if necessary to find the proper ExpDef. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4813 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4814 | list_for_each(ed, head) |
| 4815 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4816 | break; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4817 | if (!ed) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4818 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4819 | |
| 4820 | /* |
| 4821 | * OK, we have a potential macro. Count and demarcate the |
| 4822 | * parameters. |
| 4823 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4824 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4825 | |
| 4826 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4827 | * 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] | 4828 | * structure that handles this number. |
| 4829 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4830 | while (ed) { |
| 4831 | if (ed->nparam_min <= nparam |
| 4832 | && (ed->plus || nparam <= ed->nparam_max)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4833 | /* |
| 4834 | * It's right, and we can use it. Add its default |
| 4835 | * parameters to the end of our list if necessary. |
| 4836 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4837 | if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4838 | params = |
| 4839 | nasm_realloc(params, |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4840 | ((ed->nparam_min + ed->ndefs + |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4841 | 1) * sizeof(*params))); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4842 | while (nparam < ed->nparam_min + ed->ndefs) { |
| 4843 | params[nparam] = ed->defaults[nparam - ed->nparam_min]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4844 | nparam++; |
| 4845 | } |
| 4846 | } |
| 4847 | /* |
| 4848 | * If we've gone over the maximum parameter count (and |
| 4849 | * we're in Plus mode), ignore parameters beyond |
| 4850 | * nparam_max. |
| 4851 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4852 | if (ed->plus && nparam > ed->nparam_max) |
| 4853 | nparam = ed->nparam_max; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4854 | /* |
| 4855 | * Then terminate the parameter list, and leave. |
| 4856 | */ |
| 4857 | if (!params) { /* need this special case */ |
| 4858 | params = nasm_malloc(sizeof(*params)); |
| 4859 | nparam = 0; |
| 4860 | } |
| 4861 | params[nparam] = NULL; |
| 4862 | *params_array = params; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4863 | return ed; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4864 | } |
| 4865 | /* |
| 4866 | * This one wasn't right: look for the next one with the |
| 4867 | * same name. |
| 4868 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4869 | list_for_each(ed, ed->next) |
| 4870 | if (!mstrcmp(ed->name, tline->text, ed->casesense)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4871 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | /* |
| 4875 | * After all that, we didn't find one with the right number of |
| 4876 | * parameters. Issue a warning, and fail to expand the macro. |
| 4877 | */ |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4878 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4879 | "macro `%s' exists, but not taking %d parameters", |
| 4880 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4881 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4882 | return NULL; |
| 4883 | } |
| 4884 | |
| 4885 | /* |
| 4886 | * Expand the multi-line macro call made by the given line, if |
| 4887 | * 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] | 4888 | * istk->expansion and return true. Otherwise return false. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4889 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4890 | static bool expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4891 | { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4892 | Token *label = NULL; |
| 4893 | int dont_prepend = 0; |
Cyrill Gorcunov | fb27fc2 | 2011-06-25 12:08:30 +0400 | [diff] [blame] | 4894 | Token **params, *t; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4895 | Line *l = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4896 | ExpDef *ed; |
| 4897 | ExpInv *ei; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4898 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4899 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4900 | |
| 4901 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4902 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4903 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4904 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4905 | return false; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4906 | ed = is_mmacro(t, ¶ms); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4907 | if (ed != NULL) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4908 | mname = t->text; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4909 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4910 | Token *last; |
| 4911 | /* |
| 4912 | * We have an id which isn't a macro call. We'll assume |
| 4913 | * it might be a label; we'll also check to see if a |
| 4914 | * colon follows it. Then, if there's another id after |
| 4915 | * that lot, we'll check it again for macro-hood. |
| 4916 | */ |
| 4917 | label = last = t; |
| 4918 | t = t->next; |
| 4919 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4920 | last = t, t = t->next; |
| 4921 | if (tok_is_(t, ":")) { |
| 4922 | dont_prepend = 1; |
| 4923 | last = t, t = t->next; |
| 4924 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4925 | last = t, t = t->next; |
| 4926 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4927 | if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, ¶ms))) |
| 4928 | return false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4929 | last->next = NULL; |
Keith Kanios | 891775e | 2009-07-11 06:08:54 -0500 | [diff] [blame] | 4930 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4931 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4932 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4933 | |
| 4934 | /* |
| 4935 | * Fix up the parameters: this involves stripping leading and |
| 4936 | * trailing whitespace, then stripping braces if they are |
| 4937 | * present. |
| 4938 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4939 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4940 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4941 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4942 | for (i = 0; params[i]; i++) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4943 | int brace = false; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4944 | int comma = (!ed->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4945 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4946 | t = params[i]; |
| 4947 | skip_white_(t); |
| 4948 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4949 | t = t->next, brace = true, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4950 | params[i] = t; |
| 4951 | paramlen[i] = 0; |
| 4952 | while (t) { |
| 4953 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4954 | break; /* ... because we have hit a comma */ |
| 4955 | if (comma && t->type == TOK_WHITESPACE |
| 4956 | && tok_is_(t->next, ",")) |
| 4957 | break; /* ... or a space then a comma */ |
| 4958 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
| 4959 | break; /* ... or a brace */ |
| 4960 | t = t->next; |
| 4961 | paramlen[i]++; |
| 4962 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4965 | if (ed->cur_depth >= ed->max_depth) { |
| 4966 | if (ed->max_depth > 1) { |
| 4967 | error(ERR_WARNING, |
| 4968 | "reached maximum macro recursion depth of %i for %s", |
| 4969 | ed->max_depth,ed->name); |
| 4970 | } |
| 4971 | return false; |
| 4972 | } else { |
| 4973 | ed->cur_depth ++; |
| 4974 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4975 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4976 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 4977 | * OK, we have found a ExpDef structure representing a |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4978 | * previously defined mmacro. Create an expansion invocation |
| 4979 | * and point it back to the expansion definition. Substitution of |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4980 | * parameter tokens and macro-local tokens doesn't get done |
| 4981 | * until the single-line macro substitution process; this is |
| 4982 | * because delaying them allows us to change the semantics |
| 4983 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4984 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4985 | ei = new_ExpInv(EXP_MMACRO, ed); |
| 4986 | ei->name = nasm_strdup(mname); |
| 4987 | //ei->label = label; |
| 4988 | //ei->label_text = detoken(label, false); |
| 4989 | ei->current = ed->line; |
| 4990 | ei->emitting = true; |
| 4991 | //ei->iline = tline; |
| 4992 | ei->params = params; |
| 4993 | ei->nparam = nparam; |
| 4994 | ei->rotate = 0; |
| 4995 | ei->paramlen = paramlen; |
| 4996 | ei->lineno = 0; |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 4997 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 4998 | ei->prev = istk->expansion; |
| 4999 | istk->expansion = ei; |
| 5000 | |
| 5001 | /* |
| 5002 | * Special case: detect %00 on first invocation; if found, |
| 5003 | * avoid emitting any labels that precede the mmacro call. |
| 5004 | * ed->prepend is set to -1 when %00 is detected, else 1. |
| 5005 | */ |
| 5006 | if (ed->prepend == 0) { |
| 5007 | for (l = ed->line; l != NULL; l = l->next) { |
| 5008 | for (t = l->first; t != NULL; t = t->next) { |
| 5009 | if ((t->type == TOK_PREPROC_ID) && |
| 5010 | (strlen(t->text) == 3) && |
| 5011 | (t->text[1] == '0') && (t->text[2] == '0')) { |
| 5012 | dont_prepend = -1; |
| 5013 | break; |
| 5014 | } |
| 5015 | } |
| 5016 | if (dont_prepend < 0) { |
| 5017 | break; |
| 5018 | } |
| 5019 | } |
| 5020 | ed->prepend = ((dont_prepend < 0) ? -1 : 1); |
| 5021 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5022 | |
| 5023 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5024 | * If we had a label, push it on as the first line of |
| 5025 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5026 | */ |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5027 | if (label != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5028 | if (ed->prepend < 0) { |
| 5029 | ei->label_text = detoken(label, false); |
| 5030 | } else { |
| 5031 | if (dont_prepend == 0) { |
| 5032 | t = label; |
| 5033 | while (t->next != NULL) { |
| 5034 | t = t->next; |
| 5035 | } |
| 5036 | t->next = new_Token(NULL, TOK_OTHER, ":", 0); |
| 5037 | } |
| 5038 | l = new_Line(); |
| 5039 | l->first = copy_Token(label); |
| 5040 | l->next = ei->current; |
| 5041 | ei->current = l; |
| 5042 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5043 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5044 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5045 | list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5046 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5047 | istk->mmac_depth++; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5048 | return true; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5051 | /* The function that actually does the error reporting */ |
| 5052 | static void verror(int severity, const char *fmt, va_list arg) |
| 5053 | { |
| 5054 | char buff[1024]; |
| 5055 | |
| 5056 | vsnprintf(buff, sizeof(buff), fmt, arg); |
| 5057 | |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5058 | if (istk && istk->mmac_depth > 0) { |
| 5059 | ExpInv *ei = istk->expansion; |
| 5060 | int lineno = ei->lineno; |
| 5061 | while (ei) { |
| 5062 | if (ei->type == EXP_MMACRO) |
| 5063 | break; |
| 5064 | lineno += ei->relno; |
| 5065 | ei = ei->prev; |
| 5066 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5067 | nasm_error(severity, "(%s:%d) %s", ei->def->name, |
Cyrill Gorcunov | 55cc4d0 | 2010-11-10 23:12:06 +0300 | [diff] [blame] | 5068 | lineno, buff); |
| 5069 | } else |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5070 | nasm_error(severity, "%s", buff); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5071 | } |
| 5072 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5073 | /* |
| 5074 | * Since preprocessor always operate only on the line that didn't |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5075 | * arrived yet, we should always use ERR_OFFBY1. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5076 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5077 | static void error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5078 | { |
| 5079 | va_list arg; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5080 | va_start(arg, fmt); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5081 | verror(severity, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5082 | va_end(arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5083 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5084 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5085 | /* |
| 5086 | * Because %else etc are evaluated in the state context |
| 5087 | * of the previous branch, errors might get lost with error(): |
| 5088 | * %if 0 ... %else trailing garbage ... %endif |
| 5089 | * So %else etc should report errors with this function. |
| 5090 | */ |
| 5091 | static void error_precond(int severity, const char *fmt, ...) |
| 5092 | { |
| 5093 | va_list arg; |
| 5094 | |
| 5095 | /* Only ignore the error if it's really in a dead branch */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5096 | if ((istk != NULL) && |
| 5097 | (istk->expansion != NULL) && |
| 5098 | (istk->expansion->type == EXP_IF) && |
| 5099 | (istk->expansion->def->state == COND_NEVER)) |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 5100 | return; |
| 5101 | |
| 5102 | va_start(arg, fmt); |
| 5103 | verror(severity, fmt, arg); |
| 5104 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5107 | static void |
H. Peter Anvin | dbb640b | 2009-07-18 18:57:16 -0700 | [diff] [blame] | 5108 | pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5109 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5110 | Token *t; |
| 5111 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5112 | cstk = NULL; |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5113 | istk = nasm_zalloc(sizeof(Include)); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5114 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5115 | src_set_fname(nasm_strdup(file)); |
| 5116 | src_set_linnum(0); |
| 5117 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5118 | if (!istk->fp) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 5119 | error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5120 | file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5121 | defining = NULL; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5122 | finals = NULL; |
| 5123 | in_final = false; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 5124 | nested_mac_count = 0; |
| 5125 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5126 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5127 | unique = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5128 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5129 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5130 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 5131 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5132 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 5133 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 5134 | do_predef = true; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5135 | list = listgen; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5136 | |
| 5137 | /* |
| 5138 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 5139 | * The caller, however, will also pass in 3 for preprocess-only so |
| 5140 | * we can set __PASS__ accordingly. |
| 5141 | */ |
| 5142 | pass = apass > 2 ? 2 : apass; |
| 5143 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5144 | dephead = deptail = deplist; |
| 5145 | if (deplist) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5146 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 5147 | sl->next = NULL; |
| 5148 | strcpy(sl->str, file); |
| 5149 | *deptail = sl; |
| 5150 | deptail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 5151 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5152 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5153 | /* |
| 5154 | * Define the __PASS__ macro. This is defined here unlike |
| 5155 | * all the other builtins, because it is special -- it varies between |
| 5156 | * passes. |
| 5157 | */ |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5158 | t = nasm_zalloc(sizeof(*t)); |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 5159 | make_tok_num(t, apass); |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 5160 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5161 | } |
| 5162 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5163 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5164 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5165 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5166 | Token *tline; |
Keith Kanios | 9858cec | 2010-11-08 00:58:02 -0600 | [diff] [blame] | 5167 | ExpDef *ed; |
| 5168 | ExpInv *ei; |
| 5169 | Line *l; |
| 5170 | int j; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5171 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5172 | while (1) { |
| 5173 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5174 | * Fetch a tokenized line, either from the expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5175 | * buffer or from the input file. |
| 5176 | */ |
| 5177 | tline = NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5178 | |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5179 | while (1) { /* until we get a line we can use */ |
| 5180 | /* |
| 5181 | * Fetch a tokenized line from the expansion buffer |
| 5182 | */ |
| 5183 | if (istk->expansion != NULL) { |
| 5184 | ei = istk->expansion; |
| 5185 | if (ei->current != NULL) { |
| 5186 | if (ei->emitting == false) { |
| 5187 | ei->current = NULL; |
| 5188 | continue; |
| 5189 | } |
| 5190 | l = ei->current; |
| 5191 | ei->current = l->next; |
| 5192 | ei->lineno++; |
| 5193 | tline = copy_Token(l->first); |
| 5194 | if (((ei->type == EXP_REP) || |
| 5195 | (ei->type == EXP_MMACRO) || |
| 5196 | (ei->type == EXP_WHILE)) |
| 5197 | && (ei->def->nolist == false)) { |
| 5198 | char *p = detoken(tline, false); |
| 5199 | list->line(LIST_MACRO, p); |
| 5200 | nasm_free(p); |
| 5201 | } |
| 5202 | if (ei->linnum > -1) { |
| 5203 | src_set_linnum(src_get_linnum() + 1); |
| 5204 | } |
| 5205 | break; |
| 5206 | } else if ((ei->type == EXP_REP) && |
| 5207 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5208 | ei->def->cur_depth ++; |
| 5209 | ei->current = ei->def->line; |
| 5210 | ei->lineno = 0; |
| 5211 | continue; |
| 5212 | } else if ((ei->type == EXP_WHILE) && |
| 5213 | (ei->def->cur_depth < ei->def->max_depth)) { |
| 5214 | ei->current = ei->def->line; |
| 5215 | ei->lineno = 0; |
| 5216 | tline = copy_Token(ei->current->first); |
| 5217 | j = if_condition(tline, PP_WHILE); |
| 5218 | tline = NULL; |
| 5219 | j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE); |
| 5220 | if (j == COND_IF_TRUE) { |
| 5221 | ei->current = ei->current->next; |
| 5222 | ei->def->cur_depth ++; |
| 5223 | } else { |
| 5224 | ei->emitting = false; |
| 5225 | ei->current = NULL; |
| 5226 | ei->def->cur_depth = ei->def->max_depth; |
| 5227 | } |
| 5228 | continue; |
| 5229 | } else { |
| 5230 | istk->expansion = ei->prev; |
| 5231 | ed = ei->def; |
| 5232 | if (ed != NULL) { |
| 5233 | if ((ei->emitting == true) && |
| 5234 | (ed->max_depth == DEADMAN_LIMIT) && |
| 5235 | (ed->cur_depth == DEADMAN_LIMIT) |
| 5236 | ) { |
| 5237 | error(ERR_FATAL, "runaway expansion detected, aborting"); |
| 5238 | } |
| 5239 | if (ed->cur_depth > 0) { |
| 5240 | ed->cur_depth --; |
Keith Kanios | 9412465 | 2010-12-18 11:47:28 -0600 | [diff] [blame] | 5241 | } else if (ed->type != EXP_MMACRO) { |
Keith Kanios | 6a7c3e9 | 2010-12-18 11:49:53 -0600 | [diff] [blame] | 5242 | expansions = ed->prev; |
| 5243 | free_expdef(ed); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5244 | } |
| 5245 | if ((ei->type == EXP_REP) || |
| 5246 | (ei->type == EXP_MMACRO) || |
| 5247 | (ei->type == EXP_WHILE)) { |
| 5248 | list->downlevel(LIST_MACRO); |
| 5249 | if (ei->type == EXP_MMACRO) { |
| 5250 | istk->mmac_depth--; |
| 5251 | } |
| 5252 | } |
| 5253 | } |
| 5254 | if (ei->linnum > -1) { |
| 5255 | src_set_linnum(ei->linnum); |
| 5256 | } |
| 5257 | free_expinv(ei); |
| 5258 | continue; |
| 5259 | } |
| 5260 | } |
| 5261 | |
| 5262 | /* |
| 5263 | * Read in line from input and tokenize |
| 5264 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5265 | line = read_line(); |
| 5266 | if (line) { /* from the current input file */ |
| 5267 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5268 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5269 | nasm_free(line); |
| 5270 | break; |
| 5271 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5272 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5273 | /* |
| 5274 | * The current file has ended; work down the istk |
| 5275 | */ |
| 5276 | { |
| 5277 | Include *i = istk; |
| 5278 | fclose(i->fp); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5279 | if (i->expansion != NULL) { |
| 5280 | error(ERR_FATAL, |
| 5281 | "end of file while still in an expansion"); |
| 5282 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5283 | /* only set line and file name if there's a next node */ |
| 5284 | if (i->next) { |
| 5285 | src_set_linnum(i->lineno); |
Cyrill Gorcunov | d34a108 | 2011-03-07 11:23:08 +0300 | [diff] [blame] | 5286 | nasm_free(src_set_fname(nasm_strdup(i->fname))); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5287 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5288 | if ((i->next == NULL) && (finals != NULL)) { |
| 5289 | in_final = true; |
| 5290 | ei = new_ExpInv(EXP_FINAL, NULL); |
| 5291 | ei->emitting = true; |
| 5292 | ei->current = finals; |
| 5293 | istk->expansion = ei; |
| 5294 | finals = NULL; |
| 5295 | continue; |
| 5296 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5297 | istk = i->next; |
| 5298 | list->downlevel(LIST_INCLUDE); |
| 5299 | nasm_free(i); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5300 | if (istk == NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5301 | if (finals != NULL) { |
| 5302 | in_final = true; |
| 5303 | } else { |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | } |
| 5307 | continue; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5308 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | if (defining == NULL) { |
| 5312 | tline = expand_mmac_params(tline); |
| 5313 | } |
| 5314 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5315 | /* |
| 5316 | * Check the line to see if it's a preprocessor directive. |
| 5317 | */ |
| 5318 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 5319 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5320 | } else if (defining != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5321 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5322 | * We're defining an expansion. We emit nothing at all, |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5323 | * and just shove the tokenized line on to the definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5324 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5325 | if (defining->ignoring == false) { |
| 5326 | Line *l = new_Line(); |
| 5327 | l->first = tline; |
| 5328 | if (defining->line == NULL) { |
| 5329 | defining->line = l; |
| 5330 | defining->last = l; |
| 5331 | } else { |
| 5332 | defining->last->next = l; |
| 5333 | defining->last = l; |
| 5334 | } |
| 5335 | } else { |
Keith Kanios | 104803d | 2010-12-18 11:05:46 -0600 | [diff] [blame] | 5336 | free_tlist(tline); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5337 | } |
| 5338 | defining->linecount++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5339 | continue; |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5340 | } else if ((istk->expansion != NULL) && |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5341 | (istk->expansion->emitting != true)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5342 | /* |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5343 | * We're in a non-emitting branch of an expansion. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5344 | * Emit nothing at all, not even a blank line: when we |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5345 | * emerge from the expansion we'll give a line-number |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5346 | * directive so we keep our place correctly. |
| 5347 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5348 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5349 | continue; |
| 5350 | } else { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5351 | tline = expand_smacro(tline); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5352 | if (expand_mmacro(tline) != true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5353 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5354 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5355 | */ |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5356 | line = detoken(tline, true); |
| 5357 | free_tlist(tline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5358 | break; |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5359 | } else { |
| 5360 | continue; |
| 5361 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5362 | } |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5363 | } |
| 5364 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5367 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5368 | { |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5369 | if (defining != NULL) { |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5370 | error(ERR_NONFATAL, "end of file while still defining an expansion"); |
| 5371 | while (defining != NULL) { |
| 5372 | ExpDef *ed = defining; |
| 5373 | defining = ed->prev; |
| 5374 | free_expdef(ed); |
| 5375 | } |
| 5376 | defining = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5377 | } |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5378 | while (cstk != NULL) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5379 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 5380 | free_macros(); |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5381 | while (istk != NULL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5382 | Include *i = istk; |
| 5383 | istk = istk->next; |
| 5384 | fclose(i->fp); |
| 5385 | nasm_free(i->fname); |
Cyrill Gorcunov | a5aea57 | 2010-11-11 10:14:45 +0300 | [diff] [blame] | 5386 | while (i->expansion != NULL) { |
| 5387 | ExpInv *ei = i->expansion; |
| 5388 | i->expansion = ei->prev; |
| 5389 | free_expinv(ei); |
| 5390 | } |
Cyrill Gorcunov | 8dcfd88 | 2011-03-03 09:18:56 +0300 | [diff] [blame] | 5391 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5392 | } |
| 5393 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5394 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 5395 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5396 | if (pass == 0) { |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5397 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5398 | free_llist(predef); |
| 5399 | delete_Blocks(); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5400 | while ((i = ipath)) { |
| 5401 | ipath = i->next; |
Cyrill Gorcunov | b6c6ca9 | 2011-06-28 01:33:02 +0400 | [diff] [blame] | 5402 | nasm_free(i->path); |
Cyrill Gorcunov | accda19 | 2010-02-16 10:27:56 +0300 | [diff] [blame] | 5403 | nasm_free(i); |
| 5404 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 5405 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5408 | void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5409 | { |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5410 | IncPath *i = nasm_zalloc(sizeof(IncPath)); |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 5411 | |
Cyrill Gorcunov | 6b27129 | 2011-02-28 08:45:52 +0300 | [diff] [blame] | 5412 | if (path) |
| 5413 | i->path = nasm_strdup(path); |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5414 | |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5415 | if (ipath) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5416 | IncPath *j = ipath; |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5417 | while (j->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5418 | j = j->next; |
| 5419 | j->next = i; |
| 5420 | } else { |
| 5421 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5422 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5423 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 5424 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5425 | void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5426 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5427 | Token *inc, *space, *name; |
| 5428 | Line *l; |
| 5429 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5430 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 5431 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 5432 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5433 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5434 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5435 | l->next = predef; |
| 5436 | l->first = inc; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5437 | predef = l; |
| 5438 | } |
| 5439 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5440 | void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5441 | { |
| 5442 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5443 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5444 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5445 | |
| 5446 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5447 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5448 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5449 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5450 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5451 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5452 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 5453 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5454 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5455 | l = new_Line(); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5456 | l->next = predef; |
| 5457 | l->first = def; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 5458 | predef = l; |
| 5459 | } |
| 5460 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5461 | void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5462 | { |
| 5463 | Token *def, *space; |
| 5464 | Line *l; |
| 5465 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 5466 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 5467 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5468 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5469 | |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5470 | l = new_Line(); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5471 | l->next = predef; |
| 5472 | l->first = def; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 5473 | predef = l; |
| 5474 | } |
| 5475 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5476 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5477 | * This function is used to assist with "runtime" preprocessor |
Keith Kanios | b307a4f | 2010-11-06 17:41:51 -0500 | [diff] [blame] | 5478 | * directives, e.g. pp_runtime("%define __BITS__ 64"); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5479 | * |
| 5480 | * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU |
| 5481 | * PASS A VALID STRING TO THIS FUNCTION!!!!! |
| 5482 | */ |
| 5483 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5484 | void pp_runtime(char *definition) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5485 | { |
| 5486 | Token *def; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5487 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5488 | def = tokenize(definition); |
H. Peter Anvin | 89cee57 | 2009-07-15 09:16:54 -0400 | [diff] [blame] | 5489 | if (do_directive(def) == NO_DIRECTIVE_FOUND) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5490 | free_tlist(def); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 5491 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 5492 | } |
| 5493 | |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 5494 | void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5495 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 5496 | extrastdmac = macros; |
| 5497 | } |
| 5498 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5499 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5500 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 5501 | char numbuf[20]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 5502 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 5503 | tok->text = nasm_strdup(numbuf); |
| 5504 | tok->type = TOK_NUMBER; |
| 5505 | } |
| 5506 | |
Cyrill Gorcunov | 86b2ad0 | 2011-07-01 10:38:25 +0400 | [diff] [blame] | 5507 | struct preproc_ops nasmpp = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 5508 | pp_reset, |
| 5509 | pp_getline, |
| 5510 | pp_cleanup |
| 5511 | }; |