H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1 | /* preproc.c macro preprocessor for the Netwide Assembler |
| 2 | * |
| 3 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and |
| 4 | * Julian Hall. All rights reserved. The software is |
Beroset | 095e6a2 | 2007-12-29 09:44:23 -0500 | [diff] [blame] | 5 | * redistributable under the license given in the file "LICENSE" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 6 | * distributed in the NASM archive. |
| 7 | * |
| 8 | * initial version 18/iii/97 by Simon Tatham |
| 9 | */ |
| 10 | |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 11 | /* Typical flow of text through preproc |
| 12 | * |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 13 | * pp_getline gets tokenized lines, either |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 14 | * |
| 15 | * from a macro expansion |
| 16 | * |
| 17 | * or |
| 18 | * { |
| 19 | * 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] | 20 | * tokenize converts to tokens |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 21 | * } |
| 22 | * |
| 23 | * expand_mmac_params is used to expand %1 etc., unless a macro is being |
| 24 | * defined or a false conditional is being processed |
| 25 | * (%0, %1, %+1, %-1, %%foo |
| 26 | * |
| 27 | * do_directive checks for directives |
| 28 | * |
| 29 | * expand_smacro is used to expand single line macros |
| 30 | * |
| 31 | * expand_mmacro is used to expand multi-line macros |
| 32 | * |
| 33 | * detoken is used to convert the line back to text |
| 34 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 35 | |
H. Peter Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 36 | #include "compiler.h" |
| 37 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 39 | #include <stdarg.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
| 41 | #include <stddef.h> |
| 42 | #include <string.h> |
| 43 | #include <ctype.h> |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 44 | #include <limits.h> |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 45 | #include <inttypes.h> |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 46 | |
| 47 | #include "nasm.h" |
| 48 | #include "nasmlib.h" |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 49 | #include "preproc.h" |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 50 | #include "hashtbl.h" |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 51 | #include "quote.h" |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 52 | #include "stdscan.h" |
| 53 | #include "tokens.h" |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 54 | #include "tables.h" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 55 | |
| 56 | typedef struct SMacro SMacro; |
| 57 | typedef struct MMacro MMacro; |
| 58 | typedef struct Context Context; |
| 59 | typedef struct Token Token; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 60 | typedef struct Blocks Blocks; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 61 | typedef struct Line Line; |
| 62 | typedef struct Include Include; |
| 63 | typedef struct Cond Cond; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 64 | typedef struct IncPath IncPath; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 67 | * Note on the storage of both SMacro and MMacros: the hash table |
| 68 | * indexes them case-insensitively, and we then have to go through a |
| 69 | * linked list of potential case aliases (and, for MMacros, parameter |
| 70 | * ranges); this is to preserve the matching semantics of the earlier |
| 71 | * code. If the number of case aliases for a specific macro is a |
| 72 | * performance issue, you may want to reconsider your coding style. |
| 73 | */ |
| 74 | |
| 75 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 76 | * Store the definition of a single-line macro. |
| 77 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 78 | struct SMacro { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 79 | SMacro *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 80 | char *name; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 81 | bool casesense; |
H. Peter Anvin | 16ed438 | 2007-10-11 10:06:19 -0700 | [diff] [blame] | 82 | bool in_progress; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 83 | unsigned int nparam; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 84 | Token *expansion; |
| 85 | }; |
| 86 | |
| 87 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 88 | * Store the definition of a multi-line macro. This is also used to |
| 89 | * store the interiors of `%rep...%endrep' blocks, which are |
| 90 | * effectively self-re-invoking multi-line macros which simply |
| 91 | * don't have a name or bother to appear in the hash tables. %rep |
| 92 | * blocks are signified by having a NULL `name' field. |
| 93 | * |
| 94 | * In a MMacro describing a `%rep' block, the `in_progress' field |
| 95 | * isn't merely boolean, but gives the number of repeats left to |
| 96 | * run. |
| 97 | * |
| 98 | * The `next' field is used for storing MMacros in hash tables; the |
| 99 | * `next_active' field is for stacking them on istk entries. |
| 100 | * |
| 101 | * When a MMacro is being expanded, `params', `iline', `nparam', |
| 102 | * `paramlen', `rotate' and `unique' are local to the invocation. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 103 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 104 | struct MMacro { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 105 | MMacro *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 106 | char *name; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 107 | int nparam_min, nparam_max; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 108 | bool casesense; |
| 109 | bool plus; /* is the last parameter greedy? */ |
| 110 | bool nolist; /* is this macro listing-inhibited? */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 111 | int64_t in_progress; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 112 | Token *dlist; /* All defaults as one list */ |
| 113 | Token **defaults; /* Parameter default pointers */ |
| 114 | int ndefs; /* number of default parameters */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 115 | Line *expansion; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 116 | |
| 117 | MMacro *next_active; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 118 | MMacro *rep_nest; /* used for nesting %rep */ |
| 119 | Token **params; /* actual parameters */ |
| 120 | Token *iline; /* invocation line */ |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 121 | unsigned int nparam, rotate; |
| 122 | int *paramlen; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 123 | uint64_t unique; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 124 | int lineno; /* Current line number on expansion */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | /* |
| 128 | * The context stack is composed of a linked list of these. |
| 129 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 130 | struct Context { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 131 | Context *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 132 | char *name; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 133 | struct hash_table localmac; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 134 | uint32_t number; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /* |
| 138 | * This is the internal form which we break input lines up into. |
| 139 | * Typically stored in linked lists. |
| 140 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 141 | * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not |
| 142 | * necessarily used as-is, but is intended to denote the number of |
| 143 | * the substituted parameter. So in the definition |
| 144 | * |
| 145 | * %define a(x,y) ( (x) & ~(y) ) |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 146 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 147 | * the token representing `x' will have its type changed to |
| 148 | * TOK_SMAC_PARAM, but the one representing `y' will be |
| 149 | * TOK_SMAC_PARAM+1. |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 150 | * |
| 151 | * TOK_INTERNAL_STRING is a dirty hack: it's a single string token |
| 152 | * which doesn't need quotes around it. Used in the pre-include |
| 153 | * mechanism as an alternative to trying to find a sensible type of |
| 154 | * quote to use on the filename we were passed. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 155 | */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 156 | enum pp_token_type { |
| 157 | TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID, |
| 158 | TOK_PREPROC_ID, TOK_STRING, |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 159 | TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, |
| 160 | TOK_INTERNAL_STRING, |
| 161 | TOK_PREPROC_Q, TOK_PREPROC_QQ, |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 162 | TOK_PASTE, /* %+ */ |
H. Peter Anvin | 9bb46df | 2009-04-07 21:59:24 -0700 | [diff] [blame] | 163 | TOK_INDIRECT, /* %[...] */ |
H. Peter Anvin | 5b76fa2 | 2008-05-26 11:14:38 -0700 | [diff] [blame] | 164 | TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */ |
| 165 | TOK_MAX = INT_MAX /* Keep compiler from reducing the range */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 168 | struct Token { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 169 | Token *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 170 | char *text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 171 | union { |
| 172 | SMacro *mac; /* associated macro for TOK_SMAC_END */ |
| 173 | size_t len; /* scratch length field */ |
| 174 | } a; /* Auxiliary data */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 175 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | /* |
| 179 | * Multi-line macro definitions are stored as a linked list of |
| 180 | * these, which is essentially a container to allow several linked |
| 181 | * lists of Tokens. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 182 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 183 | * Note that in this module, linked lists are treated as stacks |
| 184 | * wherever possible. For this reason, Lines are _pushed_ on to the |
| 185 | * `expansion' field in MMacro structures, so that the linked list, |
| 186 | * if walked, would give the macro lines in reverse order; this |
| 187 | * means that we can walk the list when expanding a macro, and thus |
| 188 | * push the lines on to the `expansion' field in _istk_ in reverse |
| 189 | * order (so that when popped back off they are in the right |
| 190 | * order). It may seem cockeyed, and it relies on my design having |
| 191 | * an even number of steps in, but it works... |
| 192 | * |
| 193 | * Some of these structures, rather than being actual lines, are |
| 194 | * markers delimiting the end of the expansion of a given macro. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 195 | * This is for use in the cycle-tracking and %rep-handling code. |
| 196 | * Such structures have `finishes' non-NULL, and `first' NULL. All |
| 197 | * others have `finishes' NULL, but `first' may still be NULL if |
| 198 | * the line is blank. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 199 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 200 | struct Line { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 201 | Line *next; |
| 202 | MMacro *finishes; |
| 203 | Token *first; |
| 204 | }; |
| 205 | |
| 206 | /* |
| 207 | * To handle an arbitrary level of file inclusion, we maintain a |
| 208 | * stack (ie linked list) of these things. |
| 209 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 210 | struct Include { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 211 | Include *next; |
| 212 | FILE *fp; |
| 213 | Cond *conds; |
| 214 | Line *expansion; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 215 | char *fname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 216 | int lineno, lineinc; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 217 | MMacro *mstk; /* stack of active macros/reps */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 221 | * Include search path. This is simply a list of strings which get |
| 222 | * prepended, in turn, to the name of an include file, in an |
| 223 | * attempt to find the file if it's not in the current directory. |
| 224 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 225 | struct IncPath { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 226 | IncPath *next; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 227 | char *path; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 231 | * Conditional assembly: we maintain a separate stack of these for |
| 232 | * each level of file inclusion. (The only reason we keep the |
| 233 | * stacks separate is to ensure that a stray `%endif' in a file |
| 234 | * included from within the true branch of a `%if' won't terminate |
| 235 | * it and cause confusion: instead, rightly, it'll cause an error.) |
| 236 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 237 | struct Cond { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 238 | Cond *next; |
| 239 | int state; |
| 240 | }; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 241 | enum { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 242 | /* |
| 243 | * These states are for use just after %if or %elif: IF_TRUE |
| 244 | * means the condition has evaluated to truth so we are |
| 245 | * currently emitting, whereas IF_FALSE means we are not |
| 246 | * currently emitting but will start doing so if a %else comes |
| 247 | * up. In these states, all directives are admissible: %elif, |
| 248 | * %else and %endif. (And of course %if.) |
| 249 | */ |
| 250 | COND_IF_TRUE, COND_IF_FALSE, |
| 251 | /* |
| 252 | * These states come up after a %else: ELSE_TRUE means we're |
| 253 | * emitting, and ELSE_FALSE means we're not. In ELSE_* states, |
| 254 | * any %elif or %else will cause an error. |
| 255 | */ |
| 256 | COND_ELSE_TRUE, COND_ELSE_FALSE, |
| 257 | /* |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 258 | * These states mean that we're not emitting now, and also that |
| 259 | * nothing until %endif will be emitted at all. COND_DONE is |
| 260 | * used when we've had our moment of emission |
| 261 | * and have now started seeing %elifs. COND_NEVER is used when |
| 262 | * the condition construct in question is contained within a |
| 263 | * non-emitting branch of a larger condition construct, |
| 264 | * or if there is an error. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 265 | */ |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 266 | COND_DONE, COND_NEVER |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 267 | }; |
| 268 | #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE ) |
| 269 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 270 | /* |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 271 | * These defines are used as the possible return values for do_directive |
| 272 | */ |
| 273 | #define NO_DIRECTIVE_FOUND 0 |
| 274 | #define DIRECTIVE_FOUND 1 |
| 275 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 276 | /* |
| 277 | * Condition codes. Note that we use c_ prefix not C_ because C_ is |
| 278 | * used in nasm.h for the "real" condition codes. At _this_ level, |
| 279 | * we treat CXZ and ECXZ as condition codes, albeit non-invertible |
| 280 | * ones, so we need a different enum... |
| 281 | */ |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 282 | static const char * const conditions[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 283 | "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le", |
| 284 | "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no", |
H. Peter Anvin | ce9be34 | 2007-09-12 00:22:29 +0000 | [diff] [blame] | 285 | "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z" |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 286 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 287 | enum pp_conds { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 288 | c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE, |
| 289 | 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] | 290 | c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z, |
| 291 | c_none = -1 |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 292 | }; |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 293 | static const enum pp_conds inverse_ccs[] = { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 294 | c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE, |
| 295 | 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] | 296 | 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] | 297 | }; |
| 298 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 299 | /* |
| 300 | * Directive names. |
| 301 | */ |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 302 | /* If this is a an IF, ELIF, ELSE or ENDIF keyword */ |
H. Peter Anvin | 9bf0aa7 | 2007-09-12 02:12:07 +0000 | [diff] [blame] | 303 | static int is_condition(enum preproc_token arg) |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 304 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 305 | return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF); |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 306 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 307 | |
| 308 | /* For TASM compatibility we need to be able to recognise TASM compatible |
| 309 | * conditional compilation directives. Using the NASM pre-processor does |
| 310 | * not work, so we look for them specifically from the following list and |
| 311 | * then jam in the equivalent NASM directive into the input stream. |
| 312 | */ |
| 313 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 314 | enum { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 315 | TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI, |
| 316 | TM_IFNDEF, TM_INCLUDE, TM_LOCAL |
| 317 | }; |
| 318 | |
H. Peter Anvin | 476d286 | 2007-10-02 22:04:15 -0700 | [diff] [blame] | 319 | static const char * const tasm_directives[] = { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 320 | "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi", |
| 321 | "ifndef", "include", "local" |
| 322 | }; |
| 323 | |
| 324 | static int StackSize = 4; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 325 | static char *StackPointer = "ebp"; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 326 | static int ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 327 | static int LocalOffset = 0; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 328 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 329 | static Context *cstk; |
| 330 | static Include *istk; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 331 | static IncPath *ipath = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 332 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 333 | static efunc _error; /* Pointer to client-provided error reporting function */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 334 | static evalfunc evaluate; |
| 335 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 336 | static int pass; /* HACK: pass 0 = generate dependencies only */ |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 337 | static StrList **dephead, **deptail; /* Dependency list */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 338 | |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 339 | static uint64_t unique; /* unique identifier numbers */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 340 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 341 | static Line *predef = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 342 | static bool do_predef; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 343 | |
| 344 | static ListGen *list; |
| 345 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 346 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 347 | * The current set of multi-line macros we have defined. |
| 348 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 349 | static struct hash_table mmacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * The current set of single-line macros we have defined. |
| 353 | */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 354 | static struct hash_table smacros; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 355 | |
| 356 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 357 | * The multi-line macro we are currently defining, or the %rep |
| 358 | * block we are currently reading, if any. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 359 | */ |
| 360 | static MMacro *defining; |
| 361 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 362 | static uint64_t nested_mac_count; |
| 363 | static uint64_t nested_rep_count; |
| 364 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 365 | /* |
| 366 | * The number of macro parameters to allocate space for at a time. |
| 367 | */ |
| 368 | #define PARAM_DELTA 16 |
| 369 | |
| 370 | /* |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 371 | * The standard macro set: defined in macros.c in the array nasm_stdmac. |
| 372 | * 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] | 373 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 374 | static macros_t *stdmacpos; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 375 | |
| 376 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 377 | * The extra standard macros that come from the object format, if |
| 378 | * any. |
| 379 | */ |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 380 | static macros_t *extrastdmac = NULL; |
H. Peter Anvin | cfb7176 | 2008-06-20 15:20:16 -0700 | [diff] [blame] | 381 | static bool any_extrastdmac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 382 | |
| 383 | /* |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 384 | * Tokens are allocated in blocks to improve speed |
| 385 | */ |
| 386 | #define TOKEN_BLOCKSIZE 4096 |
| 387 | static Token *freeTokens = NULL; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 388 | struct Blocks { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 389 | Blocks *next; |
| 390 | void *chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | static Blocks blocks = { NULL, NULL }; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 394 | |
| 395 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 396 | * Forward declarations. |
| 397 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 398 | static Token *expand_mmac_params(Token * tline); |
| 399 | static Token *expand_smacro(Token * tline); |
| 400 | static Token *expand_id(Token * tline); |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 401 | static Context *get_ctx(const char *name, const char **namep, |
| 402 | bool all_contexts); |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 403 | static void make_tok_num(Token * tok, int64_t val); |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 404 | static void error(int severity, const char *fmt, ...); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 405 | static void error_precond(int severity, const char *fmt, ...); |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 406 | static void *new_Block(size_t size); |
| 407 | static void delete_Blocks(void); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 408 | static Token *new_Token(Token * next, enum pp_token_type type, |
| 409 | const char *text, int txtlen); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 410 | static Token *delete_Token(Token * t); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * Macros for safe checking of token pointers, avoid *(NULL) |
| 414 | */ |
| 415 | #define tok_type_(x,t) ((x) && (x)->type == (t)) |
| 416 | #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next |
| 417 | #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v))) |
| 418 | #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] | 419 | |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 420 | /* Handle TASM specific directives, which do not contain a % in |
| 421 | * front of them. We do it here because I could not find any other |
| 422 | * place to do it for the moment, and it is a hack (ideally it would |
| 423 | * be nice to be able to use the NASM pre-processor to do it). |
| 424 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 425 | static char *check_tasm_directive(char *line) |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 426 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 427 | int32_t i, j, k, m, len; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 428 | char *p = line, *oldline, oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 429 | |
| 430 | /* Skip whitespace */ |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 431 | while (nasm_isspace(*p) && *p != 0) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 432 | p++; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 433 | |
| 434 | /* Binary search for the directive name */ |
| 435 | i = -1; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 436 | j = elements(tasm_directives); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 437 | len = 0; |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 438 | while (!nasm_isspace(p[len]) && p[len] != 0) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 439 | len++; |
| 440 | if (len) { |
| 441 | oldchar = p[len]; |
| 442 | p[len] = 0; |
| 443 | while (j - i > 1) { |
| 444 | k = (j + i) / 2; |
| 445 | m = nasm_stricmp(p, tasm_directives[k]); |
| 446 | if (m == 0) { |
| 447 | /* We have found a directive, so jam a % in front of it |
| 448 | * so that NASM will then recognise it as one if it's own. |
| 449 | */ |
| 450 | p[len] = oldchar; |
| 451 | len = strlen(p); |
| 452 | oldline = line; |
| 453 | line = nasm_malloc(len + 2); |
| 454 | line[0] = '%'; |
| 455 | if (k == TM_IFDIFI) { |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 456 | /* |
| 457 | * NASM does not recognise IFDIFI, so we convert |
| 458 | * it to %if 0. This is not used in NASM |
| 459 | * compatible code, but does need to parse for the |
| 460 | * TASM macro package. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 461 | */ |
H. Peter Anvin | 18f4879 | 2009-06-27 15:56:27 -0700 | [diff] [blame] | 462 | strcpy(line + 1, "if 0"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 463 | } else { |
| 464 | memcpy(line + 1, p, len + 1); |
| 465 | } |
| 466 | nasm_free(oldline); |
| 467 | return line; |
| 468 | } else if (m < 0) { |
| 469 | j = k; |
| 470 | } else |
| 471 | i = k; |
| 472 | } |
| 473 | p[len] = oldchar; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 474 | } |
| 475 | return line; |
| 476 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 477 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 478 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 479 | * The pre-preprocessing stage... This function translates line |
| 480 | * number indications as they emerge from GNU cpp (`# lineno "file" |
| 481 | * flags') into NASM preprocessor line number indications (`%line |
| 482 | * lineno file'). |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 483 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 484 | static char *prepreproc(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 485 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 486 | int lineno, fnlen; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 487 | char *fname, *oldline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 488 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 489 | if (line[0] == '#' && line[1] == ' ') { |
| 490 | oldline = line; |
| 491 | fname = oldline + 2; |
| 492 | lineno = atoi(fname); |
| 493 | fname += strspn(fname, "0123456789 "); |
| 494 | if (*fname == '"') |
| 495 | fname++; |
| 496 | fnlen = strcspn(fname, "\""); |
| 497 | line = nasm_malloc(20 + fnlen); |
| 498 | snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname); |
| 499 | nasm_free(oldline); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 500 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 501 | if (tasm_compatible_mode) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 502 | return check_tasm_directive(line); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 503 | return line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 507 | * Free a linked list of tokens. |
| 508 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 509 | static void free_tlist(Token * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 510 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 511 | while (list) { |
| 512 | list = delete_Token(list); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Free a linked list of lines. |
| 518 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 519 | static void free_llist(Line * list) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 520 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 521 | Line *l; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 522 | while (list) { |
| 523 | l = list; |
| 524 | list = list->next; |
| 525 | free_tlist(l->first); |
| 526 | nasm_free(l); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 531 | * Free an MMacro |
| 532 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 533 | static void free_mmacro(MMacro * m) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 534 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 535 | nasm_free(m->name); |
| 536 | free_tlist(m->dlist); |
| 537 | nasm_free(m->defaults); |
| 538 | free_llist(m->expansion); |
| 539 | nasm_free(m); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 543 | * Free all currently defined macros, and free the hash tables |
| 544 | */ |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 545 | static void free_smacro_table(struct hash_table *smt) |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 546 | { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 547 | SMacro *s; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 548 | const char *key; |
| 549 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 550 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 551 | while ((s = hash_iterate(smt, &it, &key)) != NULL) { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 552 | nasm_free((void *)key); |
| 553 | while (s) { |
| 554 | SMacro *ns = s->next; |
| 555 | nasm_free(s->name); |
| 556 | free_tlist(s->expansion); |
| 557 | nasm_free(s); |
| 558 | s = ns; |
| 559 | } |
| 560 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 561 | hash_free(smt); |
| 562 | } |
| 563 | |
| 564 | static void free_mmacro_table(struct hash_table *mmt) |
| 565 | { |
| 566 | MMacro *m; |
| 567 | const char *key; |
| 568 | struct hash_tbl_node *it = NULL; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 569 | |
| 570 | it = NULL; |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 571 | while ((m = hash_iterate(mmt, &it, &key)) != NULL) { |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 572 | nasm_free((void *)key); |
| 573 | while (m) { |
| 574 | MMacro *nm = m->next; |
| 575 | free_mmacro(m); |
| 576 | m = nm; |
| 577 | } |
| 578 | } |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 579 | hash_free(mmt); |
| 580 | } |
| 581 | |
| 582 | static void free_macros(void) |
| 583 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 584 | free_smacro_table(&smacros); |
| 585 | free_mmacro_table(&mmacros); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Initialize the hash tables |
| 590 | */ |
| 591 | static void init_macros(void) |
| 592 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 593 | hash_init(&smacros, HASH_LARGE); |
| 594 | hash_init(&mmacros, HASH_LARGE); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 598 | * Pop the context stack. |
| 599 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 600 | static void ctx_pop(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 601 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 602 | Context *c = cstk; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 603 | |
| 604 | cstk = cstk->next; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 605 | free_smacro_table(&c->localmac); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 606 | nasm_free(c->name); |
| 607 | nasm_free(c); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 608 | } |
| 609 | |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 610 | /* |
| 611 | * Search for a key in the hash index; adding it if necessary |
| 612 | * (in which case we initialize the data pointer to NULL.) |
| 613 | */ |
| 614 | static void ** |
| 615 | hash_findi_add(struct hash_table *hash, const char *str) |
| 616 | { |
| 617 | struct hash_insert hi; |
| 618 | void **r; |
| 619 | char *strx; |
| 620 | |
| 621 | r = hash_findi(hash, str, &hi); |
| 622 | if (r) |
| 623 | return r; |
| 624 | |
| 625 | strx = nasm_strdup(str); /* Use a more efficient allocator here? */ |
| 626 | return hash_add(&hi, strx, NULL); |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Like hash_findi, but returns the data element rather than a pointer |
| 631 | * to it. Used only when not adding a new element, hence no third |
| 632 | * argument. |
| 633 | */ |
| 634 | static void * |
| 635 | hash_findix(struct hash_table *hash, const char *str) |
| 636 | { |
| 637 | void **p; |
| 638 | |
| 639 | p = hash_findi(hash, str, NULL); |
| 640 | return p ? *p : NULL; |
| 641 | } |
| 642 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 643 | #define BUF_DELTA 512 |
| 644 | /* |
| 645 | * Read a line from the top file in istk, handling multiple CR/LFs |
| 646 | * at the end of the line read, and handling spurious ^Zs. Will |
| 647 | * return lines from the standard macro set if this has not already |
| 648 | * been done. |
| 649 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 650 | static char *read_line(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 651 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 652 | char *buffer, *p, *q; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 653 | int bufsize, continued_count; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 654 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 655 | if (stdmacpos) { |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 656 | unsigned char c; |
H. Peter Anvin | 7e50d23 | 2008-06-25 14:54:14 -0700 | [diff] [blame] | 657 | const unsigned char *p = stdmacpos; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 658 | char *ret, *q; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 659 | size_t len = 0; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 660 | while ((c = *p++)) { |
| 661 | if (c >= 0x80) |
| 662 | len += pp_directives_len[c-0x80]+1; |
| 663 | else |
| 664 | len++; |
| 665 | } |
| 666 | ret = nasm_malloc(len+1); |
H. Peter Anvin | cda8163 | 2008-06-21 15:15:40 -0700 | [diff] [blame] | 667 | q = ret; |
| 668 | while ((c = *stdmacpos++)) { |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 669 | if (c >= 0x80) { |
| 670 | memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]); |
| 671 | q += pp_directives_len[c-0x80]; |
| 672 | *q++ = ' '; |
| 673 | } else { |
| 674 | *q++ = c; |
| 675 | } |
| 676 | } |
H. Peter Anvin | cda8163 | 2008-06-21 15:15:40 -0700 | [diff] [blame] | 677 | stdmacpos = p; |
H. Peter Anvin | 72edbb8 | 2008-06-19 16:00:04 -0700 | [diff] [blame] | 678 | *q = '\0'; |
| 679 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 680 | if (!*stdmacpos) { |
| 681 | /* This was the last of the standard macro chain... */ |
| 682 | stdmacpos = NULL; |
| 683 | if (any_extrastdmac) { |
| 684 | stdmacpos = extrastdmac; |
| 685 | any_extrastdmac = false; |
| 686 | } else if (do_predef) { |
| 687 | Line *pd, *l; |
| 688 | Token *head, **tail, *t; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 689 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 690 | /* |
| 691 | * Nasty hack: here we push the contents of |
| 692 | * `predef' on to the top-level expansion stack, |
| 693 | * since this is the most convenient way to |
| 694 | * implement the pre-include and pre-define |
| 695 | * features. |
| 696 | */ |
| 697 | for (pd = predef; pd; pd = pd->next) { |
| 698 | head = NULL; |
| 699 | tail = &head; |
| 700 | for (t = pd->first; t; t = t->next) { |
| 701 | *tail = new_Token(NULL, t->type, t->text, 0); |
| 702 | tail = &(*tail)->next; |
| 703 | } |
| 704 | l = nasm_malloc(sizeof(Line)); |
| 705 | l->next = istk->expansion; |
| 706 | l->first = head; |
H. Peter Anvin | 538002d | 2008-06-28 18:30:27 -0700 | [diff] [blame] | 707 | l->finishes = NULL; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 708 | istk->expansion = l; |
| 709 | } |
| 710 | do_predef = false; |
| 711 | } |
| 712 | } |
| 713 | return ret; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | bufsize = BUF_DELTA; |
| 717 | buffer = nasm_malloc(BUF_DELTA); |
| 718 | p = buffer; |
H. Peter Anvin | 9f39464 | 2002-04-30 21:07:51 +0000 | [diff] [blame] | 719 | continued_count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 720 | while (1) { |
| 721 | q = fgets(p, bufsize - (p - buffer), istk->fp); |
| 722 | if (!q) |
| 723 | break; |
| 724 | p += strlen(p); |
| 725 | if (p > buffer && p[-1] == '\n') { |
| 726 | /* Convert backslash-CRLF line continuation sequences into |
| 727 | nothing at all (for DOS and Windows) */ |
| 728 | if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { |
| 729 | p -= 3; |
| 730 | *p = 0; |
| 731 | continued_count++; |
| 732 | } |
| 733 | /* Also convert backslash-LF line continuation sequences into |
| 734 | nothing at all (for Unix) */ |
| 735 | else if (((p - 1) > buffer) && (p[-2] == '\\')) { |
| 736 | p -= 2; |
| 737 | *p = 0; |
| 738 | continued_count++; |
| 739 | } else { |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | if (p - buffer > bufsize - 10) { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 744 | int32_t offset = p - buffer; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 745 | bufsize += BUF_DELTA; |
| 746 | buffer = nasm_realloc(buffer, bufsize); |
| 747 | p = buffer + offset; /* prevent stale-pointer problems */ |
| 748 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 749 | } |
| 750 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 751 | if (!q && p == buffer) { |
| 752 | nasm_free(buffer); |
| 753 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 754 | } |
| 755 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 756 | src_set_linnum(src_get_linnum() + istk->lineinc + |
| 757 | (continued_count * istk->lineinc)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 758 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 759 | /* |
| 760 | * Play safe: remove CRs as well as LFs, if any of either are |
| 761 | * present at the end of the line. |
| 762 | */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 763 | while (--p >= buffer && (*p == '\n' || *p == '\r')) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 764 | *p = '\0'; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Handle spurious ^Z, which may be inserted into source files |
| 768 | * by some file transfer utilities. |
| 769 | */ |
| 770 | buffer[strcspn(buffer, "\032")] = '\0'; |
| 771 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 772 | list->line(LIST_READ, buffer); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 773 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 774 | return buffer; |
| 775 | } |
| 776 | |
| 777 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 778 | * 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] | 779 | * don't need to parse the value out of e.g. numeric tokens: we |
| 780 | * simply split one string into many. |
| 781 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 782 | static Token *tokenize(char *line) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 783 | { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 784 | char c, *p = line; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 785 | enum pp_token_type type; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 786 | Token *list = NULL; |
| 787 | Token *t, **tail = &list; |
| 788 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 789 | while (*line) { |
| 790 | p = line; |
| 791 | if (*p == '%') { |
| 792 | p++; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 793 | if (*p == '+' && !nasm_isdigit(p[1])) { |
| 794 | p++; |
| 795 | type = TOK_PASTE; |
| 796 | } else if (nasm_isdigit(*p) || |
| 797 | ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 798 | do { |
| 799 | p++; |
| 800 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 801 | while (nasm_isdigit(*p)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 802 | type = TOK_PREPROC_ID; |
| 803 | } else if (*p == '{') { |
| 804 | p++; |
| 805 | while (*p && *p != '}') { |
| 806 | p[-1] = *p; |
| 807 | p++; |
| 808 | } |
| 809 | p[-1] = '\0'; |
| 810 | if (*p) |
| 811 | p++; |
| 812 | type = TOK_PREPROC_ID; |
H. Peter Anvin | 992fe75 | 2008-10-19 15:45:05 -0700 | [diff] [blame] | 813 | } else if (*p == '[') { |
| 814 | int lvl = 1; |
| 815 | line += 2; /* Skip the leading %[ */ |
| 816 | p++; |
H. Peter Anvin | ec03301 | 2008-10-19 22:12:06 -0700 | [diff] [blame] | 817 | while (lvl && (c = *p++)) { |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 818 | switch (c) { |
| 819 | case ']': |
| 820 | lvl--; |
| 821 | break; |
| 822 | case '%': |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 823 | if (*p == '[') |
| 824 | lvl++; |
| 825 | break; |
| 826 | case '\'': |
| 827 | case '\"': |
| 828 | case '`': |
H. Peter Anvin | ec03301 | 2008-10-19 22:12:06 -0700 | [diff] [blame] | 829 | p = nasm_skip_string(p)+1; |
H. Peter Anvin | ca544db | 2008-10-19 19:30:11 -0700 | [diff] [blame] | 830 | break; |
| 831 | default: |
| 832 | break; |
H. Peter Anvin | 992fe75 | 2008-10-19 15:45:05 -0700 | [diff] [blame] | 833 | } |
H. Peter Anvin | 992fe75 | 2008-10-19 15:45:05 -0700 | [diff] [blame] | 834 | } |
H. Peter Anvin | ec03301 | 2008-10-19 22:12:06 -0700 | [diff] [blame] | 835 | p--; |
H. Peter Anvin | 992fe75 | 2008-10-19 15:45:05 -0700 | [diff] [blame] | 836 | if (*p) |
| 837 | *p++ = '\0'; |
H. Peter Anvin | e126581 | 2008-10-19 22:14:30 -0700 | [diff] [blame] | 838 | if (lvl) |
| 839 | error(ERR_NONFATAL, "unterminated %[ construct"); |
H. Peter Anvin | 992fe75 | 2008-10-19 15:45:05 -0700 | [diff] [blame] | 840 | type = TOK_INDIRECT; |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 841 | } else if (*p == '?') { |
| 842 | type = TOK_PREPROC_Q; /* %? */ |
| 843 | p++; |
| 844 | if (*p == '?') { |
| 845 | type = TOK_PREPROC_QQ; /* %?? */ |
| 846 | p++; |
| 847 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 848 | } else if (isidchar(*p) || |
| 849 | ((*p == '!' || *p == '%' || *p == '$') && |
| 850 | isidchar(p[1]))) { |
| 851 | do { |
| 852 | p++; |
| 853 | } |
| 854 | while (isidchar(*p)); |
| 855 | type = TOK_PREPROC_ID; |
| 856 | } else { |
| 857 | type = TOK_OTHER; |
| 858 | if (*p == '%') |
| 859 | p++; |
| 860 | } |
| 861 | } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) { |
| 862 | type = TOK_ID; |
| 863 | p++; |
| 864 | while (*p && isidchar(*p)) |
| 865 | p++; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 866 | } else if (*p == '\'' || *p == '"' || *p == '`') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 867 | /* |
| 868 | * A string token. |
| 869 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 870 | type = TOK_STRING; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 871 | p = nasm_skip_string(p); |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 872 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 873 | if (*p) { |
| 874 | p++; |
| 875 | } else { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 876 | error(ERR_WARNING|ERR_PASS1, "unterminated string"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 877 | /* Handling unterminated strings by UNV */ |
| 878 | /* type = -1; */ |
| 879 | } |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 880 | } else if (p[0] == '$' && p[1] == '$') { |
H. Peter Anvin | 8e1f811 | 2009-04-17 14:20:44 -0700 | [diff] [blame] | 881 | type = TOK_OTHER; /* TOKEN_BASE */ |
Victor van den Elzen | fb5f251 | 2009-04-17 16:17:59 +0200 | [diff] [blame] | 882 | p += 2; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 883 | } else if (isnumstart(*p)) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 884 | bool is_hex = false; |
| 885 | bool is_float = false; |
| 886 | bool has_e = false; |
| 887 | char c, *r; |
| 888 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 889 | /* |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 890 | * A numeric token. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 891 | */ |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 892 | |
| 893 | if (*p == '$') { |
| 894 | p++; |
| 895 | is_hex = true; |
| 896 | } |
| 897 | |
| 898 | for (;;) { |
| 899 | c = *p++; |
| 900 | |
| 901 | if (!is_hex && (c == 'e' || c == 'E')) { |
| 902 | has_e = true; |
| 903 | if (*p == '+' || *p == '-') { |
| 904 | /* e can only be followed by +/- if it is either a |
| 905 | prefixed hex number or a floating-point number */ |
| 906 | p++; |
| 907 | is_float = true; |
| 908 | } |
| 909 | } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') { |
| 910 | is_hex = true; |
| 911 | } else if (c == 'P' || c == 'p') { |
| 912 | is_float = true; |
| 913 | if (*p == '+' || *p == '-') |
| 914 | p++; |
| 915 | } else if (isnumchar(c) || c == '_') |
| 916 | ; /* just advance */ |
| 917 | else if (c == '.') { |
| 918 | /* we need to deal with consequences of the legacy |
| 919 | parser, like "1.nolist" being two tokens |
| 920 | (TOK_NUMBER, TOK_ID) here; at least give it |
| 921 | a shot for now. In the future, we probably need |
| 922 | a flex-based scanner with proper pattern matching |
| 923 | to do it as well as it can be done. Nothing in |
| 924 | the world is going to help the person who wants |
| 925 | 0x123.p16 interpreted as two tokens, though. */ |
| 926 | r = p; |
| 927 | while (*r == '_') |
| 928 | r++; |
| 929 | |
H. Peter Anvin | f221b9e | 2008-06-21 11:03:51 -0700 | [diff] [blame] | 930 | if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) || |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 931 | (!is_hex && (*r == 'e' || *r == 'E')) || |
| 932 | (*r == 'p' || *r == 'P')) { |
| 933 | p = r; |
| 934 | is_float = true; |
| 935 | } else |
| 936 | break; /* Terminate the token */ |
| 937 | } else |
| 938 | break; |
| 939 | } |
| 940 | p--; /* Point to first character beyond number */ |
| 941 | |
H. Peter Anvin | 8e1f811 | 2009-04-17 14:20:44 -0700 | [diff] [blame] | 942 | if (p == line+1 && *line == '$') { |
| 943 | type = TOK_OTHER; /* TOKEN_HERE */ |
| 944 | } else { |
| 945 | if (has_e && !is_hex) { |
| 946 | /* 1e13 is floating-point, but 1e13h is not */ |
| 947 | is_float = true; |
| 948 | } |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 949 | |
H. Peter Anvin | 8e1f811 | 2009-04-17 14:20:44 -0700 | [diff] [blame] | 950 | type = is_float ? TOK_FLOAT : TOK_NUMBER; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 951 | } |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 952 | } else if (nasm_isspace(*p)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 953 | type = TOK_WHITESPACE; |
| 954 | p++; |
H. Peter Anvin | bda7a6e | 2008-06-21 10:23:17 -0700 | [diff] [blame] | 955 | while (*p && nasm_isspace(*p)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 956 | p++; |
| 957 | /* |
| 958 | * Whitespace just before end-of-line is discarded by |
| 959 | * pretending it's a comment; whitespace just before a |
| 960 | * comment gets lumped into the comment. |
| 961 | */ |
| 962 | if (!*p || *p == ';') { |
| 963 | type = TOK_COMMENT; |
| 964 | while (*p) |
| 965 | p++; |
| 966 | } |
| 967 | } else if (*p == ';') { |
| 968 | type = TOK_COMMENT; |
| 969 | while (*p) |
| 970 | p++; |
| 971 | } else { |
| 972 | /* |
| 973 | * Anything else is an operator of some kind. We check |
| 974 | * for all the double-character operators (>>, <<, //, |
| 975 | * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 976 | * else is a single-character operator. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 977 | */ |
| 978 | type = TOK_OTHER; |
| 979 | if ((p[0] == '>' && p[1] == '>') || |
| 980 | (p[0] == '<' && p[1] == '<') || |
| 981 | (p[0] == '/' && p[1] == '/') || |
| 982 | (p[0] == '<' && p[1] == '=') || |
| 983 | (p[0] == '>' && p[1] == '=') || |
| 984 | (p[0] == '=' && p[1] == '=') || |
| 985 | (p[0] == '!' && p[1] == '=') || |
| 986 | (p[0] == '<' && p[1] == '>') || |
| 987 | (p[0] == '&' && p[1] == '&') || |
| 988 | (p[0] == '|' && p[1] == '|') || |
| 989 | (p[0] == '^' && p[1] == '^')) { |
| 990 | p++; |
| 991 | } |
| 992 | p++; |
| 993 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 994 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 995 | /* Handling unterminated string by UNV */ |
| 996 | /*if (type == -1) |
| 997 | { |
| 998 | *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1); |
| 999 | t->text[p-line] = *line; |
| 1000 | tail = &t->next; |
| 1001 | } |
| 1002 | else */ |
| 1003 | if (type != TOK_COMMENT) { |
| 1004 | *tail = t = new_Token(NULL, type, line, p - line); |
| 1005 | tail = &t->next; |
| 1006 | } |
| 1007 | line = p; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1008 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1009 | return list; |
| 1010 | } |
| 1011 | |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1012 | /* |
| 1013 | * this function allocates a new managed block of memory and |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1014 | * returns a pointer to the block. The managed blocks are |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1015 | * deleted only all at once by the delete_Blocks function. |
| 1016 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1017 | static void *new_Block(size_t size) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1018 | { |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1019 | Blocks *b = &blocks; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1020 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1021 | /* first, get to the end of the linked list */ |
| 1022 | while (b->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1023 | b = b->next; |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1024 | /* now allocate the requested chunk */ |
| 1025 | b->chunk = nasm_malloc(size); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1026 | |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1027 | /* now allocate a new block for the next request */ |
| 1028 | b->next = nasm_malloc(sizeof(Blocks)); |
| 1029 | /* and initialize the contents of the new block */ |
| 1030 | b->next->next = NULL; |
| 1031 | b->next->chunk = NULL; |
| 1032 | return b->chunk; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * this function deletes all managed blocks of memory |
| 1037 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1038 | static void delete_Blocks(void) |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1039 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1040 | Blocks *a, *b = &blocks; |
H. Peter Anvin | ce61607 | 2002-04-30 21:02:23 +0000 | [diff] [blame] | 1041 | |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1042 | /* |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1043 | * keep in mind that the first block, pointed to by blocks |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1044 | * is a static and not dynamically allocated, so we don't |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1045 | * free it. |
| 1046 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1047 | while (b) { |
| 1048 | if (b->chunk) |
| 1049 | nasm_free(b->chunk); |
| 1050 | a = b; |
| 1051 | b = b->next; |
| 1052 | if (a != &blocks) |
| 1053 | nasm_free(a); |
Nickolay Yurchenko | f7956c4 | 2003-09-26 19:03:40 +0000 | [diff] [blame] | 1054 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1055 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1058 | * 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] | 1059 | * 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] | 1060 | * also the a.mac and next elements to NULL. |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1061 | */ |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1062 | static Token *new_Token(Token * next, enum pp_token_type type, |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 1063 | const char *text, int txtlen) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1064 | { |
| 1065 | Token *t; |
| 1066 | int i; |
| 1067 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1068 | if (freeTokens == NULL) { |
| 1069 | freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); |
| 1070 | for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) |
| 1071 | freeTokens[i].next = &freeTokens[i + 1]; |
| 1072 | freeTokens[i].next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1073 | } |
| 1074 | t = freeTokens; |
| 1075 | freeTokens = t->next; |
| 1076 | t->next = next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1077 | t->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1078 | t->type = type; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1079 | if (type == TOK_WHITESPACE || text == NULL) { |
| 1080 | t->text = NULL; |
| 1081 | } else { |
| 1082 | if (txtlen == 0) |
| 1083 | txtlen = strlen(text); |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1084 | t->text = nasm_malloc(txtlen+1); |
| 1085 | memcpy(t->text, text, txtlen); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1086 | t->text[txtlen] = '\0'; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1087 | } |
| 1088 | return t; |
| 1089 | } |
| 1090 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1091 | static Token *delete_Token(Token * t) |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1092 | { |
| 1093 | Token *next = t->next; |
| 1094 | nasm_free(t->text); |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame] | 1095 | t->next = freeTokens; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1096 | freeTokens = t; |
| 1097 | return next; |
| 1098 | } |
| 1099 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1100 | /* |
| 1101 | * Convert a line of tokens back into text. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1102 | * If expand_locals is not zero, identifiers of the form "%$*xxx" |
| 1103 | * will be transformed into ..@ctxnum.xxx |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1104 | */ |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 1105 | static char *detoken(Token * tlist, bool expand_locals) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1106 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1107 | Token *t; |
| 1108 | int len; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1109 | char *line, *p; |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1110 | const char *q; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1111 | |
| 1112 | len = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1113 | for (t = tlist; t; t = t->next) { |
| 1114 | if (t->type == TOK_PREPROC_ID && t->text[1] == '!') { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1115 | char *p = getenv(t->text + 2); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1116 | nasm_free(t->text); |
| 1117 | if (p) |
| 1118 | t->text = nasm_strdup(p); |
| 1119 | else |
| 1120 | t->text = NULL; |
| 1121 | } |
| 1122 | /* Expand local macros here and not during preprocessing */ |
| 1123 | if (expand_locals && |
| 1124 | t->type == TOK_PREPROC_ID && t->text && |
| 1125 | t->text[0] == '%' && t->text[1] == '$') { |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1126 | const char *q; |
| 1127 | char *p; |
| 1128 | Context *ctx = get_ctx(t->text, &q, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1129 | if (ctx) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 1130 | char buffer[40]; |
Keith Kanios | 93f2e9a | 2007-04-14 00:10:59 +0000 | [diff] [blame] | 1131 | snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1132 | p = nasm_strcat(buffer, q); |
| 1133 | nasm_free(t->text); |
| 1134 | t->text = p; |
| 1135 | } |
| 1136 | } |
| 1137 | if (t->type == TOK_WHITESPACE) { |
| 1138 | len++; |
| 1139 | } else if (t->text) { |
| 1140 | len += strlen(t->text); |
| 1141 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1142 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1143 | p = line = nasm_malloc(len + 1); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1144 | for (t = tlist; t; t = t->next) { |
| 1145 | if (t->type == TOK_WHITESPACE) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1146 | *p++ = ' '; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1147 | } else if (t->text) { |
H. Peter Anvin | b4daadc | 2008-01-21 16:31:57 -0800 | [diff] [blame] | 1148 | q = t->text; |
| 1149 | while (*q) |
| 1150 | *p++ = *q++; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1151 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1152 | } |
| 1153 | *p = '\0'; |
| 1154 | return line; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1158 | * A scanner, suitable for use by the expression evaluator, which |
| 1159 | * operates on a line of Tokens. Expects a pointer to a pointer to |
| 1160 | * the first token in the line to be passed in as its private_data |
| 1161 | * field. |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1162 | * |
| 1163 | * FIX: This really needs to be unified with stdscan. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1164 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1165 | static int ppscan(void *private_data, struct tokenval *tokval) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1166 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1167 | Token **tlineptr = private_data; |
| 1168 | Token *tline; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1169 | char ourcopy[MAX_KEYWORD+1], *p, *r, *s; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1170 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1171 | do { |
| 1172 | tline = *tlineptr; |
| 1173 | *tlineptr = tline ? tline->next : NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1174 | } |
| 1175 | while (tline && (tline->type == TOK_WHITESPACE || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1176 | tline->type == TOK_COMMENT)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1177 | |
| 1178 | if (!tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1179 | return tokval->t_type = TOKEN_EOS; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1180 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1181 | tokval->t_charptr = tline->text; |
| 1182 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1183 | if (tline->text[0] == '$' && !tline->text[1]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1184 | return tokval->t_type = TOKEN_HERE; |
H. Peter Anvin | 7cf897e | 2002-05-30 21:30:33 +0000 | [diff] [blame] | 1185 | if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1186 | return tokval->t_type = TOKEN_BASE; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1187 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1188 | if (tline->type == TOK_ID) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1189 | p = tokval->t_charptr = tline->text; |
| 1190 | if (p[0] == '$') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1191 | tokval->t_charptr++; |
| 1192 | return tokval->t_type = TOKEN_ID; |
| 1193 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1194 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1195 | for (r = p, s = ourcopy; *r; r++) { |
Philipp Thomas | 76ec8e7 | 2008-05-21 08:53:21 -0700 | [diff] [blame] | 1196 | if (r >= p+MAX_KEYWORD) |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1197 | return tokval->t_type = TOKEN_ID; /* Not a keyword */ |
H. Peter Anvin | ac8f8fc | 2008-06-11 15:49:41 -0700 | [diff] [blame] | 1198 | *s++ = nasm_tolower(*r); |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1199 | } |
| 1200 | *s = '\0'; |
| 1201 | /* right, so we have an identifier sitting in temp storage. now, |
| 1202 | * is it actually a register or instruction name, or what? */ |
| 1203 | return nasm_token_hash(ourcopy, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1206 | if (tline->type == TOK_NUMBER) { |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1207 | bool rn_error; |
| 1208 | tokval->t_integer = readnum(tline->text, &rn_error); |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1209 | tokval->t_charptr = tline->text; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1210 | if (rn_error) |
| 1211 | return tokval->t_type = TOKEN_ERRNUM; |
| 1212 | else |
| 1213 | return tokval->t_type = TOKEN_NUM; |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1214 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1215 | |
H. Peter Anvin | c2df282 | 2007-10-24 15:29:28 -0700 | [diff] [blame] | 1216 | if (tline->type == TOK_FLOAT) { |
| 1217 | return tokval->t_type = TOKEN_FLOAT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1220 | if (tline->type == TOK_STRING) { |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 1221 | char bq, *ep; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1222 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 1223 | bq = tline->text[0]; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1224 | tokval->t_charptr = tline->text; |
| 1225 | tokval->t_inttwo = nasm_unquote(tline->text, &ep); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1226 | |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 1227 | if (ep[0] != bq || ep[1] != '\0') |
| 1228 | return tokval->t_type = TOKEN_ERRSTR; |
| 1229 | else |
| 1230 | return tokval->t_type = TOKEN_STR; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1233 | if (tline->type == TOK_OTHER) { |
| 1234 | if (!strcmp(tline->text, "<<")) |
| 1235 | return tokval->t_type = TOKEN_SHL; |
| 1236 | if (!strcmp(tline->text, ">>")) |
| 1237 | return tokval->t_type = TOKEN_SHR; |
| 1238 | if (!strcmp(tline->text, "//")) |
| 1239 | return tokval->t_type = TOKEN_SDIV; |
| 1240 | if (!strcmp(tline->text, "%%")) |
| 1241 | return tokval->t_type = TOKEN_SMOD; |
| 1242 | if (!strcmp(tline->text, "==")) |
| 1243 | return tokval->t_type = TOKEN_EQ; |
| 1244 | if (!strcmp(tline->text, "<>")) |
| 1245 | return tokval->t_type = TOKEN_NE; |
| 1246 | if (!strcmp(tline->text, "!=")) |
| 1247 | return tokval->t_type = TOKEN_NE; |
| 1248 | if (!strcmp(tline->text, "<=")) |
| 1249 | return tokval->t_type = TOKEN_LE; |
| 1250 | if (!strcmp(tline->text, ">=")) |
| 1251 | return tokval->t_type = TOKEN_GE; |
| 1252 | if (!strcmp(tline->text, "&&")) |
| 1253 | return tokval->t_type = TOKEN_DBL_AND; |
| 1254 | if (!strcmp(tline->text, "^^")) |
| 1255 | return tokval->t_type = TOKEN_DBL_XOR; |
| 1256 | if (!strcmp(tline->text, "||")) |
| 1257 | return tokval->t_type = TOKEN_DBL_OR; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /* |
| 1261 | * We have no other options: just return the first character of |
| 1262 | * the token text. |
| 1263 | */ |
| 1264 | return tokval->t_type = tline->text[0]; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1268 | * Compare a string to the name of an existing macro; this is a |
| 1269 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1270 | * depending on the value of the `casesense' parameter. |
| 1271 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1272 | static int mstrcmp(const char *p, const char *q, bool casesense) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1273 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1274 | return casesense ? strcmp(p, q) : nasm_stricmp(p, q); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | /* |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1278 | * Compare a string to the name of an existing macro; this is a |
| 1279 | * simple wrapper which calls either strcmp or nasm_stricmp |
| 1280 | * depending on the value of the `casesense' parameter. |
| 1281 | */ |
| 1282 | static int mmemcmp(const char *p, const char *q, size_t l, bool casesense) |
| 1283 | { |
| 1284 | return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l); |
| 1285 | } |
| 1286 | |
| 1287 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1288 | * Return the Context structure associated with a %$ token. Return |
| 1289 | * NULL, having _already_ reported an error condition, if the |
| 1290 | * context stack isn't deep enough for the supplied number of $ |
| 1291 | * signs. |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1292 | * If all_contexts == true, contexts that enclose current are |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1293 | * also scanned for such smacro, until it is found; if not - |
| 1294 | * only the context that directly results from the number of $'s |
| 1295 | * in variable's name. |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1296 | * |
| 1297 | * If "namep" is non-NULL, set it to the pointer to the macro name |
| 1298 | * tail, i.e. the part beyond %$... |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1299 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1300 | static Context *get_ctx(const char *name, const char **namep, |
| 1301 | bool all_contexts) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1302 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1303 | Context *ctx; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1304 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1305 | int i; |
| 1306 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1307 | if (namep) |
| 1308 | *namep = name; |
| 1309 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1310 | if (!name || name[0] != '%' || name[1] != '$') |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1311 | return NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1312 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1313 | if (!cstk) { |
| 1314 | error(ERR_NONFATAL, "`%s': context stack is empty", name); |
| 1315 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1318 | name += 2; |
| 1319 | ctx = cstk; |
| 1320 | i = 0; |
| 1321 | while (ctx && *name == '$') { |
| 1322 | name++; |
| 1323 | i++; |
| 1324 | ctx = ctx->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1325 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1326 | if (!ctx) { |
| 1327 | error(ERR_NONFATAL, "`%s': context stack is only" |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1328 | " %d level%s deep", name, i, (i == 1 ? "" : "s")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1329 | return NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1330 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1331 | |
| 1332 | if (namep) |
| 1333 | *namep = name; |
| 1334 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1335 | if (!all_contexts) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1336 | return ctx; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1337 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1338 | do { |
| 1339 | /* Search for this smacro in found context */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1340 | m = hash_findix(&ctx->localmac, name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1341 | while (m) { |
| 1342 | if (!mstrcmp(m->name, name, m->casesense)) |
| 1343 | return ctx; |
| 1344 | m = m->next; |
| 1345 | } |
| 1346 | ctx = ctx->next; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1347 | } |
| 1348 | while (ctx); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1349 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | /* |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1353 | * Check to see if a file is already in a string list |
| 1354 | */ |
| 1355 | static bool in_list(const StrList *list, const char *str) |
| 1356 | { |
| 1357 | while (list) { |
| 1358 | if (!strcmp(list->str, str)) |
| 1359 | return true; |
| 1360 | list = list->next; |
| 1361 | } |
| 1362 | return false; |
| 1363 | } |
| 1364 | |
| 1365 | /* |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1366 | * Open an include file. This routine must always return a valid |
| 1367 | * file pointer if it returns - it's responsible for throwing an |
| 1368 | * ERR_FATAL and bombing out completely if not. It should also try |
| 1369 | * the include path one by one until it finds the file or reaches |
| 1370 | * the end of the path. |
| 1371 | */ |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1372 | static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 1373 | bool missing_ok) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1374 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1375 | FILE *fp; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1376 | char *prefix = ""; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1377 | IncPath *ip = ipath; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1378 | int len = strlen(file); |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1379 | size_t prefix_len = 0; |
| 1380 | StrList *sl; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1381 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1382 | while (1) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1383 | sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); |
| 1384 | memcpy(sl->str, prefix, prefix_len); |
| 1385 | memcpy(sl->str+prefix_len, file, len+1); |
| 1386 | fp = fopen(sl->str, "r"); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 1387 | if (fp && dhead && !in_list(*dhead, sl->str)) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1388 | sl->next = NULL; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1389 | **dtail = sl; |
| 1390 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1391 | } else { |
| 1392 | nasm_free(sl); |
| 1393 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1394 | if (fp) |
| 1395 | return fp; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 1396 | if (!ip) { |
| 1397 | if (!missing_ok) |
| 1398 | break; |
| 1399 | prefix = NULL; |
| 1400 | } else { |
| 1401 | prefix = ip->path; |
| 1402 | ip = ip->next; |
| 1403 | } |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1404 | if (prefix) { |
| 1405 | prefix_len = strlen(prefix); |
| 1406 | } else { |
| 1407 | /* -MG given and file not found */ |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 1408 | if (dhead && !in_list(*dhead, file)) { |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1409 | sl = nasm_malloc(len+1+sizeof sl->next); |
| 1410 | sl->next = NULL; |
| 1411 | strcpy(sl->str, file); |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 1412 | **dtail = sl; |
| 1413 | *dtail = &sl->next; |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 1414 | } |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 1415 | return NULL; |
| 1416 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1417 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1418 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1419 | error(ERR_FATAL, "unable to open include file `%s'", file); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1420 | return NULL; /* never reached - placate compilers */ |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1424 | * 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] | 1425 | * 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] | 1426 | * return true if _any_ single-line macro of that name is defined. |
| 1427 | * Otherwise, will return true if a single-line macro with either |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1428 | * `nparam' or no parameters is defined. |
| 1429 | * |
| 1430 | * If a macro with precisely the right number of parameters is |
H. Peter Anvin | ef7468f | 2002-04-30 20:57:59 +0000 | [diff] [blame] | 1431 | * defined, or nparam is -1, the address of the definition structure |
| 1432 | * 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] | 1433 | * is NULL, no action will be taken regarding its contents, and no |
| 1434 | * error will occur. |
| 1435 | * |
| 1436 | * Note that this is also called with nparam zero to resolve |
| 1437 | * `ifdef'. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1438 | * |
| 1439 | * If you already know which context macro belongs to, you can pass |
| 1440 | * the context pointer as first parameter; if you won't but name begins |
| 1441 | * with %$ the context will be automatically computed. If all_contexts |
| 1442 | * is true, macro will be searched in outer contexts as well. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1443 | */ |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1444 | static bool |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 1445 | smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn, |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1446 | bool nocase) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1447 | { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1448 | struct hash_table *smtbl; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1449 | SMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1450 | |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1451 | if (ctx) { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1452 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1453 | } else if (name[0] == '%' && name[1] == '$') { |
| 1454 | if (cstk) |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1455 | ctx = get_ctx(name, &name, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1456 | if (!ctx) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1457 | return false; /* got to return _something_ */ |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1458 | smtbl = &ctx->localmac; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1459 | } else { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1460 | smtbl = &smacros; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1461 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1462 | m = (SMacro *) hash_findix(smtbl, name); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1463 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1464 | while (m) { |
| 1465 | if (!mstrcmp(m->name, name, m->casesense && nocase) && |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1466 | (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1467 | if (defn) { |
Charles Crayne | 192d5b5 | 2007-10-18 19:02:42 -0700 | [diff] [blame] | 1468 | if (nparam == (int) m->nparam || nparam == -1) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1469 | *defn = m; |
| 1470 | else |
| 1471 | *defn = NULL; |
| 1472 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1473 | return true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1474 | } |
| 1475 | m = m->next; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1476 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 1477 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1478 | return false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Count and mark off the parameters in a multi-line macro call. |
| 1483 | * This is called both from within the multi-line macro expansion |
| 1484 | * code, and also to mark off the default parameters when provided |
| 1485 | * in a %macro definition line. |
| 1486 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1487 | static void count_mmac_params(Token * t, int *nparam, Token *** params) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1488 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1489 | int paramsize, brace; |
| 1490 | |
| 1491 | *nparam = paramsize = 0; |
| 1492 | *params = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1493 | while (t) { |
H. Peter Anvin | 91fb6f1 | 2008-09-01 10:56:33 -0700 | [diff] [blame] | 1494 | /* +1: we need space for the final NULL */ |
| 1495 | if (*nparam+1 >= paramsize) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1496 | paramsize += PARAM_DELTA; |
| 1497 | *params = nasm_realloc(*params, sizeof(**params) * paramsize); |
| 1498 | } |
| 1499 | skip_white_(t); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1500 | brace = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1501 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1502 | brace = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1503 | (*params)[(*nparam)++] = t; |
| 1504 | while (tok_isnt_(t, brace ? "}" : ",")) |
| 1505 | t = t->next; |
| 1506 | if (t) { /* got a comma/brace */ |
| 1507 | t = t->next; |
| 1508 | if (brace) { |
| 1509 | /* |
| 1510 | * Now we've found the closing brace, look further |
| 1511 | * for the comma. |
| 1512 | */ |
| 1513 | skip_white_(t); |
| 1514 | if (tok_isnt_(t, ",")) { |
| 1515 | error(ERR_NONFATAL, |
| 1516 | "braces do not enclose all of macro parameter"); |
| 1517 | while (tok_isnt_(t, ",")) |
| 1518 | t = t->next; |
| 1519 | } |
| 1520 | if (t) |
| 1521 | t = t->next; /* eat the comma */ |
| 1522 | } |
| 1523 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1528 | * Determine whether one of the various `if' conditions is true or |
| 1529 | * not. |
| 1530 | * |
| 1531 | * We must free the tline we get passed. |
| 1532 | */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1533 | static bool if_condition(Token * tline, enum preproc_token ct) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1534 | { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1535 | enum pp_conditional i = PP_COND(ct); |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1536 | bool j; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1537 | Token *t, *tt, **tptr, *origline; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1538 | struct tokenval tokval; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1539 | expr *evalresult; |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1540 | enum pp_token_type needtype; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1541 | |
| 1542 | origline = tline; |
| 1543 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1544 | switch (i) { |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1545 | case PPC_IFCTX: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1546 | j = false; /* have we matched yet? */ |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1547 | while (true) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1548 | skip_white_(tline); |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1549 | if (!tline) |
| 1550 | break; |
| 1551 | if (tline->type != TOK_ID) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1552 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1553 | "`%s' expects context identifiers", pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1554 | free_tlist(origline); |
| 1555 | return -1; |
| 1556 | } |
Victor van den Elzen | 0e857f1 | 2008-07-23 13:21:29 +0200 | [diff] [blame] | 1557 | if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1558 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1559 | tline = tline->next; |
| 1560 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1561 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1562 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1563 | case PPC_IFDEF: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1564 | j = false; /* have we matched yet? */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1565 | while (tline) { |
| 1566 | skip_white_(tline); |
| 1567 | if (!tline || (tline->type != TOK_ID && |
| 1568 | (tline->type != TOK_PREPROC_ID || |
| 1569 | tline->text[1] != '$'))) { |
| 1570 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1571 | "`%s' expects macro identifiers", pp_directives[ct]); |
| 1572 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1573 | } |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1574 | if (smacro_defined(NULL, tline->text, 0, NULL, true)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1575 | j = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1576 | tline = tline->next; |
| 1577 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1578 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 1579 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1580 | case PPC_IFIDN: |
| 1581 | case PPC_IFIDNI: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1582 | tline = expand_smacro(tline); |
| 1583 | t = tt = tline; |
| 1584 | while (tok_isnt_(tt, ",")) |
| 1585 | tt = tt->next; |
| 1586 | if (!tt) { |
| 1587 | error(ERR_NONFATAL, |
| 1588 | "`%s' expects two comma-separated arguments", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1589 | pp_directives[ct]); |
| 1590 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1591 | } |
| 1592 | tt = tt->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1593 | j = true; /* assume equality unless proved not */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1594 | while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) { |
| 1595 | if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) { |
| 1596 | error(ERR_NONFATAL, "`%s': more than one comma on line", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1597 | pp_directives[ct]); |
| 1598 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1599 | } |
| 1600 | if (t->type == TOK_WHITESPACE) { |
| 1601 | t = t->next; |
| 1602 | continue; |
| 1603 | } |
| 1604 | if (tt->type == TOK_WHITESPACE) { |
| 1605 | tt = tt->next; |
| 1606 | continue; |
| 1607 | } |
| 1608 | if (tt->type != t->type) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1609 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1610 | break; |
| 1611 | } |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1612 | /* When comparing strings, need to unquote them first */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1613 | if (t->type == TOK_STRING) { |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 1614 | size_t l1 = nasm_unquote(t->text, NULL); |
| 1615 | size_t l2 = nasm_unquote(tt->text, NULL); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 1616 | |
H. Peter Anvin | 6ecc159 | 2008-06-01 21:34:49 -0700 | [diff] [blame] | 1617 | if (l1 != l2) { |
| 1618 | j = false; |
| 1619 | break; |
| 1620 | } |
| 1621 | if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) { |
| 1622 | j = false; |
| 1623 | break; |
| 1624 | } |
| 1625 | } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1626 | j = false; /* found mismatching tokens */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | } |
Nickolay Yurchenko | f3b3ce2 | 2003-09-21 20:38:43 +0000 | [diff] [blame] | 1629 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1630 | t = t->next; |
| 1631 | tt = tt->next; |
| 1632 | } |
| 1633 | if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1634 | j = false; /* trailing gunk on one end or other */ |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1635 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1636 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1637 | case PPC_IFMACRO: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1638 | { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1639 | bool found = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1640 | MMacro searching, *mmac; |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1641 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1642 | skip_white_(tline); |
| 1643 | tline = expand_id(tline); |
| 1644 | if (!tok_type_(tline, TOK_ID)) { |
| 1645 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1646 | "`%s' expects a macro name", pp_directives[ct]); |
| 1647 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1648 | } |
| 1649 | searching.name = nasm_strdup(tline->text); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1650 | searching.casesense = true; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1651 | searching.plus = false; |
| 1652 | searching.nolist = false; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 1653 | searching.in_progress = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1654 | searching.rep_nest = NULL; |
| 1655 | searching.nparam_min = 0; |
| 1656 | searching.nparam_max = INT_MAX; |
| 1657 | tline = expand_smacro(tline->next); |
| 1658 | skip_white_(tline); |
| 1659 | if (!tline) { |
| 1660 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 1661 | error(ERR_NONFATAL, |
| 1662 | "`%s' expects a parameter count or nothing", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1663 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1664 | } else { |
| 1665 | searching.nparam_min = searching.nparam_max = |
| 1666 | readnum(tline->text, &j); |
| 1667 | if (j) |
| 1668 | error(ERR_NONFATAL, |
| 1669 | "unable to parse parameter count `%s'", |
| 1670 | tline->text); |
| 1671 | } |
| 1672 | if (tline && tok_is_(tline->next, "-")) { |
| 1673 | tline = tline->next->next; |
| 1674 | if (tok_is_(tline, "*")) |
| 1675 | searching.nparam_max = INT_MAX; |
| 1676 | else if (!tok_type_(tline, TOK_NUMBER)) |
| 1677 | error(ERR_NONFATAL, |
| 1678 | "`%s' expects a parameter count after `-'", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1679 | pp_directives[ct]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1680 | else { |
| 1681 | searching.nparam_max = readnum(tline->text, &j); |
| 1682 | if (j) |
| 1683 | error(ERR_NONFATAL, |
| 1684 | "unable to parse parameter count `%s'", |
| 1685 | tline->text); |
| 1686 | if (searching.nparam_min > searching.nparam_max) |
| 1687 | error(ERR_NONFATAL, |
| 1688 | "minimum parameter count exceeds maximum"); |
| 1689 | } |
| 1690 | } |
| 1691 | if (tline && tok_is_(tline->next, "+")) { |
| 1692 | tline = tline->next; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1693 | searching.plus = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1694 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1695 | mmac = (MMacro *) hash_findix(&mmacros, searching.name); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1696 | while (mmac) { |
| 1697 | if (!strcmp(mmac->name, searching.name) && |
| 1698 | (mmac->nparam_min <= searching.nparam_max |
| 1699 | || searching.plus) |
| 1700 | && (searching.nparam_min <= mmac->nparam_max |
| 1701 | || mmac->plus)) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1702 | found = true; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1703 | break; |
| 1704 | } |
| 1705 | mmac = mmac->next; |
| 1706 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 1707 | if(tline && tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1708 | error(ERR_WARNING|ERR_PASS1, |
| 1709 | "trailing garbage after %%ifmacro ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1710 | nasm_free(searching.name); |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1711 | j = found; |
| 1712 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1713 | } |
H. Peter Anvin | 6574726 | 2002-05-07 00:10:05 +0000 | [diff] [blame] | 1714 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1715 | case PPC_IFID: |
| 1716 | needtype = TOK_ID; |
| 1717 | goto iftype; |
| 1718 | case PPC_IFNUM: |
| 1719 | needtype = TOK_NUMBER; |
| 1720 | goto iftype; |
| 1721 | case PPC_IFSTR: |
| 1722 | needtype = TOK_STRING; |
| 1723 | goto iftype; |
| 1724 | |
| 1725 | iftype: |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1726 | t = tline = expand_smacro(tline); |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1727 | |
H. Peter Anvin | 927c92b | 2008-02-16 13:44:52 -0800 | [diff] [blame] | 1728 | while (tok_type_(t, TOK_WHITESPACE) || |
| 1729 | (needtype == TOK_NUMBER && |
| 1730 | tok_type_(t, TOK_OTHER) && |
| 1731 | (t->text[0] == '-' || t->text[0] == '+') && |
| 1732 | !t->text[1])) |
| 1733 | t = t->next; |
H. Peter Anvin | d85d250 | 2008-05-04 17:53:31 -0700 | [diff] [blame] | 1734 | |
H. Peter Anvin | cbf768d | 2008-02-16 16:41:25 -0800 | [diff] [blame] | 1735 | j = tok_type_(t, needtype); |
| 1736 | break; |
| 1737 | |
| 1738 | case PPC_IFTOKEN: |
| 1739 | t = tline = expand_smacro(tline); |
| 1740 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1741 | t = t->next; |
| 1742 | |
| 1743 | j = false; |
| 1744 | if (t) { |
| 1745 | t = t->next; /* Skip the actual token */ |
| 1746 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1747 | t = t->next; |
| 1748 | j = !t; /* Should be nothing left */ |
| 1749 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1750 | break; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1751 | |
H. Peter Anvin | 134b946 | 2008-02-16 17:01:40 -0800 | [diff] [blame] | 1752 | case PPC_IFEMPTY: |
| 1753 | t = tline = expand_smacro(tline); |
| 1754 | while (tok_type_(t, TOK_WHITESPACE)) |
| 1755 | t = t->next; |
| 1756 | |
| 1757 | j = !t; /* Should be empty */ |
| 1758 | break; |
| 1759 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1760 | case PPC_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1761 | t = tline = expand_smacro(tline); |
| 1762 | tptr = &t; |
| 1763 | tokval.t_type = TOKEN_INVALID; |
| 1764 | evalresult = evaluate(ppscan, tptr, &tokval, |
| 1765 | NULL, pass | CRITICAL, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1766 | if (!evalresult) |
| 1767 | return -1; |
| 1768 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1769 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1770 | "trailing garbage after expression ignored"); |
| 1771 | if (!is_simple(evalresult)) { |
| 1772 | error(ERR_NONFATAL, |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1773 | "non-constant value given to `%s'", pp_directives[ct]); |
| 1774 | goto fail; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1775 | } |
Chuck Crayne | 60ae75d | 2007-05-02 01:59:16 +0000 | [diff] [blame] | 1776 | j = reloc_value(evalresult) != 0; |
H. Peter Anvin | 90e6e81 | 2008-07-16 14:38:24 -0700 | [diff] [blame] | 1777 | break; |
H. Peter Anvin | 95e2882 | 2007-09-12 04:20:08 +0000 | [diff] [blame] | 1778 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1779 | default: |
| 1780 | error(ERR_FATAL, |
| 1781 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1782 | pp_directives[ct]); |
| 1783 | goto fail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1784 | } |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1785 | |
| 1786 | free_tlist(origline); |
| 1787 | return j ^ PP_NEGATIVE(ct); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1788 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 1789 | fail: |
| 1790 | free_tlist(origline); |
| 1791 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /* |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1795 | * Common code for defining an smacro |
| 1796 | */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1797 | static bool define_smacro(Context *ctx, const char *mname, bool casesense, |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1798 | int nparam, Token *expansion) |
| 1799 | { |
| 1800 | SMacro *smac, **smhead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1801 | struct hash_table *smtbl; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1802 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1803 | if (smacro_defined(ctx, mname, nparam, &smac, casesense)) { |
| 1804 | if (!smac) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1805 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1806 | "single-line macro `%s' defined both with and" |
| 1807 | " without parameters", mname); |
| 1808 | |
| 1809 | /* Some instances of the old code considered this a failure, |
| 1810 | some others didn't. What is the right thing to do here? */ |
| 1811 | free_tlist(expansion); |
| 1812 | return false; /* Failure */ |
| 1813 | } else { |
| 1814 | /* |
| 1815 | * We're redefining, so we have to take over an |
| 1816 | * existing SMacro structure. This means freeing |
| 1817 | * what was already in it. |
| 1818 | */ |
| 1819 | nasm_free(smac->name); |
| 1820 | free_tlist(smac->expansion); |
| 1821 | } |
| 1822 | } else { |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1823 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 1824 | smhead = (SMacro **) hash_findi_add(smtbl, mname); |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1825 | smac = nasm_malloc(sizeof(SMacro)); |
| 1826 | smac->next = *smhead; |
| 1827 | *smhead = smac; |
| 1828 | } |
| 1829 | smac->name = nasm_strdup(mname); |
| 1830 | smac->casesense = casesense; |
| 1831 | smac->nparam = nparam; |
| 1832 | smac->expansion = expansion; |
| 1833 | smac->in_progress = false; |
| 1834 | return true; /* Success */ |
| 1835 | } |
| 1836 | |
| 1837 | /* |
| 1838 | * Undefine an smacro |
| 1839 | */ |
| 1840 | static void undef_smacro(Context *ctx, const char *mname) |
| 1841 | { |
| 1842 | SMacro **smhead, *s, **sp; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1843 | struct hash_table *smtbl; |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1844 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 1845 | smtbl = ctx ? &ctx->localmac : &smacros; |
| 1846 | smhead = (SMacro **)hash_findi(smtbl, mname, NULL); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1847 | |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 1848 | if (smhead) { |
| 1849 | /* |
| 1850 | * We now have a macro name... go hunt for it. |
| 1851 | */ |
| 1852 | sp = smhead; |
| 1853 | while ((s = *sp) != NULL) { |
| 1854 | if (!mstrcmp(s->name, mname, s->casesense)) { |
| 1855 | *sp = s->next; |
| 1856 | nasm_free(s->name); |
| 1857 | free_tlist(s->expansion); |
| 1858 | nasm_free(s); |
| 1859 | } else { |
| 1860 | sp = &s->next; |
| 1861 | } |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 1866 | /* |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 1867 | * Parse a mmacro specification. |
| 1868 | */ |
| 1869 | static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive) |
| 1870 | { |
| 1871 | bool err; |
| 1872 | |
| 1873 | tline = tline->next; |
| 1874 | skip_white_(tline); |
| 1875 | tline = expand_id(tline); |
| 1876 | if (!tok_type_(tline, TOK_ID)) { |
| 1877 | error(ERR_NONFATAL, "`%s' expects a macro name", directive); |
| 1878 | return false; |
| 1879 | } |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 1880 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 1881 | def->name = nasm_strdup(tline->text); |
| 1882 | def->plus = false; |
| 1883 | def->nolist = false; |
| 1884 | def->in_progress = 0; |
| 1885 | def->rep_nest = NULL; |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 1886 | def->nparam_min = 0; |
| 1887 | def->nparam_max = 0; |
| 1888 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 1889 | tline = expand_smacro(tline->next); |
| 1890 | skip_white_(tline); |
| 1891 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 1892 | error(ERR_NONFATAL, "`%s' expects a parameter count", directive); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 1893 | } else { |
| 1894 | def->nparam_min = def->nparam_max = |
| 1895 | readnum(tline->text, &err); |
| 1896 | if (err) |
| 1897 | error(ERR_NONFATAL, |
| 1898 | "unable to parse parameter count `%s'", tline->text); |
| 1899 | } |
| 1900 | if (tline && tok_is_(tline->next, "-")) { |
| 1901 | tline = tline->next->next; |
| 1902 | if (tok_is_(tline, "*")) { |
| 1903 | def->nparam_max = INT_MAX; |
| 1904 | } else if (!tok_type_(tline, TOK_NUMBER)) { |
| 1905 | error(ERR_NONFATAL, |
| 1906 | "`%s' expects a parameter count after `-'", directive); |
| 1907 | } else { |
| 1908 | def->nparam_max = readnum(tline->text, &err); |
| 1909 | if (err) { |
| 1910 | error(ERR_NONFATAL, "unable to parse parameter count `%s'", |
| 1911 | tline->text); |
| 1912 | } |
| 1913 | if (def->nparam_min > def->nparam_max) { |
| 1914 | error(ERR_NONFATAL, "minimum parameter count exceeds maximum"); |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | if (tline && tok_is_(tline->next, "+")) { |
| 1919 | tline = tline->next; |
| 1920 | def->plus = true; |
| 1921 | } |
| 1922 | if (tline && tok_type_(tline->next, TOK_ID) && |
| 1923 | !nasm_stricmp(tline->next->text, ".nolist")) { |
| 1924 | tline = tline->next; |
| 1925 | def->nolist = true; |
| 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * Handle default parameters. |
| 1930 | */ |
| 1931 | if (tline && tline->next) { |
| 1932 | def->dlist = tline->next; |
| 1933 | tline->next = NULL; |
| 1934 | count_mmac_params(def->dlist, &def->ndefs, &def->defaults); |
| 1935 | } else { |
| 1936 | def->dlist = NULL; |
| 1937 | def->defaults = NULL; |
| 1938 | } |
| 1939 | def->expansion = NULL; |
| 1940 | |
Victor van den Elzen | 22343c2 | 2008-08-06 14:47:54 +0200 | [diff] [blame] | 1941 | if(def->defaults && |
| 1942 | def->ndefs > def->nparam_max - def->nparam_min && |
| 1943 | !def->plus) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 1944 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP, |
| 1945 | "too many default macro parameters"); |
Victor van den Elzen | b916ede | 2008-07-23 15:14:22 +0200 | [diff] [blame] | 1946 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 1947 | return true; |
| 1948 | } |
| 1949 | |
| 1950 | |
| 1951 | /* |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 1952 | * Decode a size directive |
| 1953 | */ |
| 1954 | static int parse_size(const char *str) { |
| 1955 | static const char *size_names[] = |
H. Peter Anvin | dfb9180 | 2008-05-20 11:43:53 -0700 | [diff] [blame] | 1956 | { "byte", "dword", "oword", "qword", "tword", "word", "yword" }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 1957 | static const int sizes[] = |
H. Peter Anvin | dfb9180 | 2008-05-20 11:43:53 -0700 | [diff] [blame] | 1958 | { 0, 1, 4, 16, 8, 10, 2, 32 }; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 1959 | |
| 1960 | return sizes[bsii(str, size_names, elements(size_names))+1]; |
| 1961 | } |
| 1962 | |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1963 | /** |
| 1964 | * find and process preprocessor directive in passed line |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1965 | * Find out if a line contains a preprocessor directive, and deal |
| 1966 | * with it if so. |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1967 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1968 | * If a directive _is_ found, it is the responsibility of this routine |
| 1969 | * (and not the caller) to free_tlist() the line. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1970 | * |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 1971 | * @param tline a pointer to the current tokeninzed line linked list |
| 1972 | * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 1973 | * |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1974 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1975 | static int do_directive(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 1976 | { |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 1977 | enum preproc_token i; |
| 1978 | int j; |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 1979 | bool err; |
| 1980 | int nparam; |
| 1981 | bool nolist; |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 1982 | bool casesense; |
H. Peter Anvin | 8cfdb9d | 2007-09-14 18:36:01 -0700 | [diff] [blame] | 1983 | int k, m; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 1984 | int offset; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 1985 | char *p, *pp; |
| 1986 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 1987 | Include *inc; |
| 1988 | Context *ctx; |
| 1989 | Cond *cond; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 1990 | MMacro *mmac, **mmhead; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1991 | Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline; |
| 1992 | Line *l; |
| 1993 | struct tokenval tokval; |
| 1994 | expr *evalresult; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1995 | MMacro *tmp_defining; /* Used when manipulating rep_nest */ |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 1996 | int64_t count; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 1997 | size_t len; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 1998 | int severity; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1999 | |
| 2000 | origline = tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2001 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2002 | skip_white_(tline); |
H. Peter Anvin | f2936d7 | 2008-06-04 15:11:23 -0700 | [diff] [blame] | 2003 | if (!tline || !tok_type_(tline, TOK_PREPROC_ID) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2004 | (tline->text[1] == '%' || tline->text[1] == '$' |
| 2005 | || tline->text[1] == '!')) |
| 2006 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2007 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2008 | i = pp_token_hash(tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2009 | |
| 2010 | /* |
| 2011 | * If we're in a non-emitting branch of a condition construct, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2012 | * or walking to the end of an already terminated %rep block, |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2013 | * we should ignore all directives except for condition |
| 2014 | * directives. |
| 2015 | */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2016 | if (((istk->conds && !emitting(istk->conds->state)) || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2017 | (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) { |
| 2018 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2019 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2020 | |
| 2021 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2022 | * If we're defining a macro or reading a %rep block, we should |
Victor van den Elzen | 8f1120f | 2008-07-16 13:41:37 +0200 | [diff] [blame] | 2023 | * ignore all directives except for %macro/%imacro (which nest), |
| 2024 | * %endm/%endmacro, and (only if we're in a %rep block) %endrep. |
| 2025 | * If we're in a %rep block, another %rep nests, so should be let through. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2026 | */ |
| 2027 | if (defining && i != PP_MACRO && i != PP_IMACRO && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2028 | i != PP_ENDMACRO && i != PP_ENDM && |
| 2029 | (defining->name || (i != PP_ENDREP && i != PP_REP))) { |
| 2030 | return NO_DIRECTIVE_FOUND; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 2031 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2032 | |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 2033 | if (defining) { |
| 2034 | if (i == PP_MACRO || i == PP_IMACRO) { |
| 2035 | nested_mac_count++; |
| 2036 | return NO_DIRECTIVE_FOUND; |
| 2037 | } else if (nested_mac_count > 0) { |
| 2038 | if (i == PP_ENDMACRO) { |
| 2039 | nested_mac_count--; |
| 2040 | return NO_DIRECTIVE_FOUND; |
| 2041 | } |
| 2042 | } |
| 2043 | if (!defining->name) { |
| 2044 | if (i == PP_REP) { |
| 2045 | nested_rep_count++; |
| 2046 | return NO_DIRECTIVE_FOUND; |
| 2047 | } else if (nested_rep_count > 0) { |
| 2048 | if (i == PP_ENDREP) { |
| 2049 | nested_rep_count--; |
| 2050 | return NO_DIRECTIVE_FOUND; |
| 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2056 | switch (i) { |
| 2057 | case PP_INVALID: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2058 | error(ERR_NONFATAL, "unknown preprocessor directive `%s'", |
| 2059 | tline->text); |
| 2060 | return NO_DIRECTIVE_FOUND; /* didn't get it */ |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 2061 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2062 | case PP_STACKSIZE: |
| 2063 | /* Directive to tell NASM what the default stack size is. The |
| 2064 | * default is for a 16-bit stack, and this can be overriden with |
| 2065 | * %stacksize large. |
| 2066 | * the following form: |
| 2067 | * |
| 2068 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2069 | */ |
| 2070 | tline = tline->next; |
| 2071 | if (tline && tline->type == TOK_WHITESPACE) |
| 2072 | tline = tline->next; |
| 2073 | if (!tline || tline->type != TOK_ID) { |
| 2074 | error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); |
| 2075 | free_tlist(origline); |
| 2076 | return DIRECTIVE_FOUND; |
| 2077 | } |
| 2078 | if (nasm_stricmp(tline->text, "flat") == 0) { |
| 2079 | /* All subsequent ARG directives are for a 32-bit stack */ |
| 2080 | StackSize = 4; |
| 2081 | StackPointer = "ebp"; |
| 2082 | ArgOffset = 8; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2083 | LocalOffset = 0; |
Charles Crayne | 7eaf919 | 2007-11-08 22:11:14 -0800 | [diff] [blame] | 2084 | } else if (nasm_stricmp(tline->text, "flat64") == 0) { |
| 2085 | /* All subsequent ARG directives are for a 64-bit stack */ |
| 2086 | StackSize = 8; |
| 2087 | StackPointer = "rbp"; |
| 2088 | ArgOffset = 8; |
| 2089 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2090 | } else if (nasm_stricmp(tline->text, "large") == 0) { |
| 2091 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2092 | * far function call. |
| 2093 | */ |
| 2094 | StackSize = 2; |
| 2095 | StackPointer = "bp"; |
| 2096 | ArgOffset = 4; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2097 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2098 | } else if (nasm_stricmp(tline->text, "small") == 0) { |
| 2099 | /* All subsequent ARG directives are for a 16-bit stack, |
| 2100 | * far function call. We don't support near functions. |
| 2101 | */ |
| 2102 | StackSize = 2; |
| 2103 | StackPointer = "bp"; |
| 2104 | ArgOffset = 6; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2105 | LocalOffset = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2106 | } else { |
| 2107 | error(ERR_NONFATAL, "`%%stacksize' invalid size type"); |
| 2108 | free_tlist(origline); |
| 2109 | return DIRECTIVE_FOUND; |
| 2110 | } |
| 2111 | free_tlist(origline); |
| 2112 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2113 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2114 | case PP_ARG: |
| 2115 | /* TASM like ARG directive to define arguments to functions, in |
| 2116 | * the following form: |
| 2117 | * |
| 2118 | * ARG arg1:WORD, arg2:DWORD, arg4:QWORD |
| 2119 | */ |
| 2120 | offset = ArgOffset; |
| 2121 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2122 | char *arg, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2123 | int size = StackSize; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2124 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2125 | /* Find the argument name */ |
| 2126 | tline = tline->next; |
| 2127 | if (tline && tline->type == TOK_WHITESPACE) |
| 2128 | tline = tline->next; |
| 2129 | if (!tline || tline->type != TOK_ID) { |
| 2130 | error(ERR_NONFATAL, "`%%arg' missing argument parameter"); |
| 2131 | free_tlist(origline); |
| 2132 | return DIRECTIVE_FOUND; |
| 2133 | } |
| 2134 | arg = tline->text; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2135 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2136 | /* Find the argument size type */ |
| 2137 | tline = tline->next; |
| 2138 | if (!tline || tline->type != TOK_OTHER |
| 2139 | || tline->text[0] != ':') { |
| 2140 | error(ERR_NONFATAL, |
| 2141 | "Syntax error processing `%%arg' directive"); |
| 2142 | free_tlist(origline); |
| 2143 | return DIRECTIVE_FOUND; |
| 2144 | } |
| 2145 | tline = tline->next; |
| 2146 | if (!tline || tline->type != TOK_ID) { |
| 2147 | error(ERR_NONFATAL, "`%%arg' missing size type parameter"); |
| 2148 | free_tlist(origline); |
| 2149 | return DIRECTIVE_FOUND; |
| 2150 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2151 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2152 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2153 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2154 | tt = expand_smacro(tt); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2155 | size = parse_size(tt->text); |
| 2156 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2157 | error(ERR_NONFATAL, |
| 2158 | "Invalid size type for `%%arg' missing directive"); |
| 2159 | free_tlist(tt); |
| 2160 | free_tlist(origline); |
| 2161 | return DIRECTIVE_FOUND; |
| 2162 | } |
| 2163 | free_tlist(tt); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2164 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2165 | /* Round up to even stack slots */ |
| 2166 | size = (size+StackSize-1) & ~(StackSize-1); |
| 2167 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2168 | /* Now define the macro for the argument */ |
| 2169 | snprintf(directive, sizeof(directive), "%%define %s (%s+%d)", |
| 2170 | arg, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2171 | do_directive(tokenize(directive)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2172 | offset += size; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2173 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2174 | /* Move to the next argument in the list */ |
| 2175 | tline = tline->next; |
| 2176 | if (tline && tline->type == TOK_WHITESPACE) |
| 2177 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2178 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
| 2179 | ArgOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2180 | free_tlist(origline); |
| 2181 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2182 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2183 | case PP_LOCAL: |
| 2184 | /* TASM like LOCAL directive to define local variables for a |
| 2185 | * function, in the following form: |
| 2186 | * |
| 2187 | * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize |
| 2188 | * |
| 2189 | * The '= LocalSize' at the end is ignored by NASM, but is |
| 2190 | * required by TASM to define the local parameter size (and used |
| 2191 | * by the TASM macro package). |
| 2192 | */ |
| 2193 | offset = LocalOffset; |
| 2194 | do { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 2195 | char *local, directive[256]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2196 | int size = StackSize; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2197 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2198 | /* Find the argument name */ |
| 2199 | tline = tline->next; |
| 2200 | if (tline && tline->type == TOK_WHITESPACE) |
| 2201 | tline = tline->next; |
| 2202 | if (!tline || tline->type != TOK_ID) { |
| 2203 | error(ERR_NONFATAL, |
| 2204 | "`%%local' missing argument parameter"); |
| 2205 | free_tlist(origline); |
| 2206 | return DIRECTIVE_FOUND; |
| 2207 | } |
| 2208 | local = tline->text; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2209 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2210 | /* Find the argument size type */ |
| 2211 | tline = tline->next; |
| 2212 | if (!tline || tline->type != TOK_OTHER |
| 2213 | || tline->text[0] != ':') { |
| 2214 | error(ERR_NONFATAL, |
| 2215 | "Syntax error processing `%%local' directive"); |
| 2216 | free_tlist(origline); |
| 2217 | return DIRECTIVE_FOUND; |
| 2218 | } |
| 2219 | tline = tline->next; |
| 2220 | if (!tline || tline->type != TOK_ID) { |
| 2221 | error(ERR_NONFATAL, |
| 2222 | "`%%local' missing size type parameter"); |
| 2223 | free_tlist(origline); |
| 2224 | return DIRECTIVE_FOUND; |
| 2225 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2226 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2227 | /* Allow macro expansion of type parameter */ |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2228 | tt = tokenize(tline->text); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2229 | tt = expand_smacro(tt); |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2230 | size = parse_size(tt->text); |
| 2231 | if (!size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2232 | error(ERR_NONFATAL, |
| 2233 | "Invalid size type for `%%local' missing directive"); |
| 2234 | free_tlist(tt); |
| 2235 | free_tlist(origline); |
| 2236 | return DIRECTIVE_FOUND; |
| 2237 | } |
| 2238 | free_tlist(tt); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2239 | |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2240 | /* Round up to even stack slots */ |
| 2241 | size = (size+StackSize-1) & ~(StackSize-1); |
| 2242 | |
| 2243 | offset += size; /* Negative offset, increment before */ |
| 2244 | |
| 2245 | /* Now define the macro for the argument */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2246 | snprintf(directive, sizeof(directive), "%%define %s (%s-%d)", |
| 2247 | local, StackPointer, offset); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2248 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2249 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2250 | /* Now define the assign to setup the enter_c macro correctly */ |
| 2251 | snprintf(directive, sizeof(directive), |
| 2252 | "%%assign %%$localsize %%$localsize+%d", size); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 2253 | do_directive(tokenize(directive)); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2254 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2255 | /* Move to the next argument in the list */ |
| 2256 | tline = tline->next; |
| 2257 | if (tline && tline->type == TOK_WHITESPACE) |
| 2258 | tline = tline->next; |
H. Peter Anvin | 8781cb0 | 2007-11-08 20:01:11 -0800 | [diff] [blame] | 2259 | } while (tline && tline->type == TOK_OTHER && tline->text[0] == ','); |
| 2260 | LocalOffset = offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2261 | free_tlist(origline); |
| 2262 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2263 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2264 | case PP_CLEAR: |
| 2265 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2266 | error(ERR_WARNING|ERR_PASS1, |
| 2267 | "trailing garbage after `%%clear' ignored"); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 2268 | free_macros(); |
| 2269 | init_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2270 | free_tlist(origline); |
| 2271 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2272 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2273 | case PP_DEPEND: |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2274 | t = tline->next = expand_smacro(tline->next); |
| 2275 | skip_white_(t); |
| 2276 | if (!t || (t->type != TOK_STRING && |
| 2277 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2278 | error(ERR_NONFATAL, "`%%depend' expects a file name"); |
| 2279 | free_tlist(origline); |
| 2280 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2281 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2282 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2283 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2284 | "trailing garbage after `%%depend' ignored"); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2285 | p = t->text; |
| 2286 | if (t->type != TOK_INTERNAL_STRING) |
| 2287 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2288 | if (dephead && !in_list(*dephead, p)) { |
| 2289 | StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next); |
| 2290 | sl->next = NULL; |
| 2291 | strcpy(sl->str, p); |
| 2292 | *deptail = sl; |
| 2293 | deptail = &sl->next; |
| 2294 | } |
| 2295 | free_tlist(origline); |
| 2296 | return DIRECTIVE_FOUND; |
| 2297 | |
| 2298 | case PP_INCLUDE: |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2299 | t = tline->next = expand_smacro(tline->next); |
| 2300 | skip_white_(t); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2301 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2302 | if (!t || (t->type != TOK_STRING && |
| 2303 | t->type != TOK_INTERNAL_STRING)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2304 | error(ERR_NONFATAL, "`%%include' expects a file name"); |
| 2305 | free_tlist(origline); |
| 2306 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2307 | } |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2308 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2309 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2310 | "trailing garbage after `%%include' ignored"); |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2311 | p = t->text; |
| 2312 | if (t->type != TOK_INTERNAL_STRING) |
| 2313 | nasm_unquote(p, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2314 | inc = nasm_malloc(sizeof(Include)); |
| 2315 | inc->next = istk; |
| 2316 | inc->conds = NULL; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2317 | inc->fp = inc_fopen(p, dephead, &deptail, pass == 0); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2318 | if (!inc->fp) { |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 2319 | /* -MG given but file not found */ |
| 2320 | nasm_free(inc); |
| 2321 | } else { |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2322 | inc->fname = src_set_fname(nasm_strdup(p)); |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 2323 | inc->lineno = src_set_linnum(0); |
| 2324 | inc->lineinc = 1; |
| 2325 | inc->expansion = NULL; |
| 2326 | inc->mstk = NULL; |
| 2327 | istk = inc; |
| 2328 | list->uplevel(LIST_INCLUDE); |
| 2329 | } |
| 2330 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2331 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2332 | |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2333 | case PP_USE: |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2334 | { |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 2335 | static macros_t *use_pkg; |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 2336 | const char *pkg_macro; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2337 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2338 | tline = tline->next; |
| 2339 | skip_white_(tline); |
| 2340 | tline = expand_id(tline); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2341 | |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2342 | if (!tline || (tline->type != TOK_STRING && |
| 2343 | tline->type != TOK_INTERNAL_STRING && |
| 2344 | tline->type != TOK_ID)) { |
H. Peter Anvin | 926fc40 | 2008-06-19 16:26:12 -0700 | [diff] [blame] | 2345 | error(ERR_NONFATAL, "`%%use' expects a package name"); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2346 | free_tlist(origline); |
| 2347 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2348 | } |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2349 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2350 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2351 | "trailing garbage after `%%use' ignored"); |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2352 | if (tline->type == TOK_STRING) |
| 2353 | nasm_unquote(tline->text, NULL); |
| 2354 | use_pkg = nasm_stdmac_find_package(tline->text); |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2355 | if (!use_pkg) |
H. Peter Anvin | 264b7b9 | 2008-10-24 16:38:17 -0700 | [diff] [blame] | 2356 | error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text); |
H. Peter Anvin | b2a5fda | 2008-06-19 21:42:42 -0700 | [diff] [blame] | 2357 | /* The first string will be <%define>__USE_*__ */ |
H. Peter Anvin | 7e50d23 | 2008-06-25 14:54:14 -0700 | [diff] [blame] | 2358 | pkg_macro = (char *)use_pkg + 1; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2359 | if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) { |
| 2360 | /* Not already included, go ahead and include it */ |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2361 | stdmacpos = use_pkg; |
| 2362 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2363 | free_tlist(origline); |
| 2364 | return DIRECTIVE_FOUND; |
H. Peter Anvin | f4ae5ad | 2008-06-19 18:39:24 -0700 | [diff] [blame] | 2365 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2366 | case PP_PUSH: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2367 | case PP_REPL: |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2368 | case PP_POP: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2369 | tline = tline->next; |
| 2370 | skip_white_(tline); |
| 2371 | tline = expand_id(tline); |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 2372 | if (tline) { |
| 2373 | if (!tok_type_(tline, TOK_ID)) { |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2374 | error(ERR_NONFATAL, "`%s' expects a context identifier", |
| 2375 | pp_directives[i]); |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 2376 | free_tlist(origline); |
| 2377 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2378 | } |
| 2379 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2380 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2381 | "trailing garbage after `%s' ignored", |
| 2382 | pp_directives[i]); |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 2383 | p = nasm_strdup(tline->text); |
| 2384 | } else { |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2385 | p = NULL; /* Anonymous */ |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 2386 | } |
H. Peter Anvin | 42b5639 | 2008-10-24 16:24:21 -0700 | [diff] [blame] | 2387 | |
| 2388 | if (i == PP_PUSH) { |
| 2389 | ctx = nasm_malloc(sizeof(Context)); |
| 2390 | ctx->next = cstk; |
| 2391 | hash_init(&ctx->localmac, HASH_SMALL); |
| 2392 | ctx->name = p; |
| 2393 | ctx->number = unique++; |
| 2394 | cstk = ctx; |
| 2395 | } else { |
| 2396 | /* %pop or %repl */ |
| 2397 | if (!cstk) { |
| 2398 | error(ERR_NONFATAL, "`%s': context stack is empty", |
| 2399 | pp_directives[i]); |
| 2400 | } else if (i == PP_POP) { |
| 2401 | if (p && (!cstk->name || nasm_stricmp(p, cstk->name))) |
| 2402 | error(ERR_NONFATAL, "`%%pop' in wrong context: %s, " |
| 2403 | "expected %s", |
| 2404 | cstk->name ? cstk->name : "anonymous", p); |
| 2405 | else |
| 2406 | ctx_pop(); |
| 2407 | } else { |
| 2408 | /* i == PP_REPL */ |
| 2409 | nasm_free(cstk->name); |
| 2410 | cstk->name = p; |
| 2411 | p = NULL; |
| 2412 | } |
| 2413 | nasm_free(p); |
| 2414 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2415 | free_tlist(origline); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2416 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2417 | case PP_FATAL: |
| 2418 | severity = ERR_FATAL|ERR_NO_SEVERITY; |
| 2419 | goto issue_error; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2420 | case PP_ERROR: |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2421 | severity = ERR_NONFATAL|ERR_NO_SEVERITY; |
| 2422 | goto issue_error; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2423 | case PP_WARNING: |
H. Peter Anvin | 2f16043 | 2008-09-30 16:39:17 -0700 | [diff] [blame] | 2424 | severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER; |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2425 | goto issue_error; |
| 2426 | |
| 2427 | issue_error: |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2428 | { |
H. Peter Anvin | 8e3f75e | 2008-09-24 00:21:58 -0700 | [diff] [blame] | 2429 | /* Only error out if this is the final pass */ |
| 2430 | if (pass != 2 && i != PP_FATAL) |
| 2431 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2432 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2433 | tline->next = expand_smacro(tline->next); |
| 2434 | tline = tline->next; |
| 2435 | skip_white_(tline); |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2436 | t = tline ? tline->next : NULL; |
| 2437 | skip_white_(t); |
| 2438 | if (tok_type_(tline, TOK_STRING) && !t) { |
| 2439 | /* The line contains only a quoted string */ |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 2440 | p = tline->text; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2441 | nasm_unquote(p, NULL); |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2442 | error(severity, "%s: %s", pp_directives[i], p); |
| 2443 | } else { |
| 2444 | /* Not a quoted string, or more than a quoted string */ |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2445 | p = detoken(tline, false); |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2446 | error(severity, "%s: %s", pp_directives[i], p); |
| 2447 | nasm_free(p); |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 2448 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2449 | free_tlist(origline); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2450 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 7df0417 | 2008-06-10 18:27:38 -0700 | [diff] [blame] | 2451 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2452 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2453 | CASE_PP_IF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2454 | if (istk->conds && !emitting(istk->conds->state)) |
| 2455 | j = COND_NEVER; |
| 2456 | else { |
| 2457 | j = if_condition(tline->next, i); |
| 2458 | tline->next = NULL; /* it got freed */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2459 | j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2460 | } |
| 2461 | cond = nasm_malloc(sizeof(Cond)); |
| 2462 | cond->next = istk->conds; |
| 2463 | cond->state = j; |
| 2464 | istk->conds = cond; |
H. Peter Anvin | 7061ad7 | 2007-11-26 22:02:21 -0800 | [diff] [blame] | 2465 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2466 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2467 | |
H. Peter Anvin | da10e7b | 2007-09-12 04:18:37 +0000 | [diff] [blame] | 2468 | CASE_PP_ELIF: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2469 | if (!istk->conds) |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 2470 | error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2471 | switch(istk->conds->state) { |
| 2472 | case COND_IF_TRUE: |
| 2473 | istk->conds->state = COND_DONE; |
| 2474 | break; |
| 2475 | |
| 2476 | case COND_DONE: |
| 2477 | case COND_NEVER: |
| 2478 | break; |
| 2479 | |
| 2480 | case COND_ELSE_TRUE: |
| 2481 | case COND_ELSE_FALSE: |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2482 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2483 | "`%%elif' after `%%else' ignored"); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2484 | istk->conds->state = COND_NEVER; |
| 2485 | break; |
| 2486 | |
| 2487 | case COND_IF_FALSE: |
| 2488 | /* |
| 2489 | * IMPORTANT: In the case of %if, we will already have |
| 2490 | * called expand_mmac_params(); however, if we're |
| 2491 | * processing an %elif we must have been in a |
| 2492 | * non-emitting mode, which would have inhibited |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 2493 | * the normal invocation of expand_mmac_params(). |
| 2494 | * Therefore, we have to do it explicitly here. |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2495 | */ |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 2496 | j = if_condition(expand_mmac_params(tline->next), i); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2497 | tline->next = NULL; /* it got freed */ |
| 2498 | istk->conds->state = |
| 2499 | j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; |
| 2500 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2501 | } |
H. Peter Anvin | 7061ad7 | 2007-11-26 22:02:21 -0800 | [diff] [blame] | 2502 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2503 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2504 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2505 | case PP_ELSE: |
| 2506 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2507 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2508 | "trailing garbage after `%%else' ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2509 | if (!istk->conds) |
| 2510 | error(ERR_FATAL, "`%%else': no matching `%%if'"); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2511 | switch(istk->conds->state) { |
| 2512 | case COND_IF_TRUE: |
| 2513 | case COND_DONE: |
| 2514 | istk->conds->state = COND_ELSE_FALSE; |
| 2515 | break; |
| 2516 | |
| 2517 | case COND_NEVER: |
| 2518 | break; |
| 2519 | |
| 2520 | case COND_IF_FALSE: |
| 2521 | istk->conds->state = COND_ELSE_TRUE; |
| 2522 | break; |
| 2523 | |
| 2524 | case COND_ELSE_TRUE: |
| 2525 | case COND_ELSE_FALSE: |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2526 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2527 | "`%%else' after `%%else' ignored."); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 2528 | istk->conds->state = COND_NEVER; |
| 2529 | break; |
| 2530 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2531 | free_tlist(origline); |
| 2532 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2533 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2534 | case PP_ENDIF: |
| 2535 | if (tline->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2536 | error_precond(ERR_WARNING|ERR_PASS1, |
| 2537 | "trailing garbage after `%%endif' ignored"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2538 | if (!istk->conds) |
| 2539 | error(ERR_FATAL, "`%%endif': no matching `%%if'"); |
| 2540 | cond = istk->conds; |
| 2541 | istk->conds = cond->next; |
| 2542 | nasm_free(cond); |
| 2543 | free_tlist(origline); |
| 2544 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2545 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2546 | case PP_MACRO: |
| 2547 | case PP_IMACRO: |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2548 | if (defining) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2549 | error(ERR_FATAL, |
| 2550 | "`%%%smacro': already defining a macro", |
| 2551 | (i == PP_IMACRO ? "i" : "")); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2552 | return DIRECTIVE_FOUND; |
| 2553 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2554 | defining = nasm_malloc(sizeof(MMacro)); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2555 | defining->casesense = (i == PP_MACRO); |
| 2556 | if (!parse_mmacro_spec(tline, defining, pp_directives[i])) { |
| 2557 | nasm_free(defining); |
| 2558 | defining = NULL; |
| 2559 | return DIRECTIVE_FOUND; |
| 2560 | } |
| 2561 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2562 | mmac = (MMacro *) hash_findix(&mmacros, defining->name); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2563 | while (mmac) { |
| 2564 | if (!strcmp(mmac->name, defining->name) && |
| 2565 | (mmac->nparam_min <= defining->nparam_max |
| 2566 | || defining->plus) |
| 2567 | && (defining->nparam_min <= mmac->nparam_max |
| 2568 | || mmac->plus)) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2569 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2570 | "redefining multi-line macro `%s'", defining->name); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2571 | return DIRECTIVE_FOUND; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2572 | } |
| 2573 | mmac = mmac->next; |
| 2574 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2575 | free_tlist(origline); |
| 2576 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2577 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2578 | case PP_ENDM: |
| 2579 | case PP_ENDMACRO: |
Victor van den Elzen | 8f1120f | 2008-07-16 13:41:37 +0200 | [diff] [blame] | 2580 | if (! (defining && defining->name)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2581 | error(ERR_NONFATAL, "`%s': not defining a macro", tline->text); |
| 2582 | return DIRECTIVE_FOUND; |
| 2583 | } |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 2584 | mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 2585 | defining->next = *mmhead; |
| 2586 | *mmhead = defining; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2587 | defining = NULL; |
| 2588 | free_tlist(origline); |
| 2589 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2590 | |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 2591 | case PP_UNMACRO: |
| 2592 | case PP_UNIMACRO: |
| 2593 | { |
| 2594 | MMacro **mmac_p; |
| 2595 | MMacro spec; |
| 2596 | |
| 2597 | spec.casesense = (i == PP_UNMACRO); |
| 2598 | if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) { |
| 2599 | return DIRECTIVE_FOUND; |
| 2600 | } |
| 2601 | mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL); |
| 2602 | while (mmac_p && *mmac_p) { |
| 2603 | mmac = *mmac_p; |
| 2604 | if (mmac->casesense == spec.casesense && |
| 2605 | !mstrcmp(mmac->name, spec.name, spec.casesense) && |
| 2606 | mmac->nparam_min == spec.nparam_min && |
| 2607 | mmac->nparam_max == spec.nparam_max && |
| 2608 | mmac->plus == spec.plus) { |
| 2609 | *mmac_p = mmac->next; |
| 2610 | free_mmacro(mmac); |
| 2611 | } else { |
| 2612 | mmac_p = &mmac->next; |
| 2613 | } |
| 2614 | } |
| 2615 | free_tlist(origline); |
| 2616 | free_tlist(spec.dlist); |
| 2617 | return DIRECTIVE_FOUND; |
| 2618 | } |
| 2619 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2620 | case PP_ROTATE: |
| 2621 | if (tline->next && tline->next->type == TOK_WHITESPACE) |
| 2622 | tline = tline->next; |
| 2623 | if (tline->next == NULL) { |
| 2624 | free_tlist(origline); |
| 2625 | error(ERR_NONFATAL, "`%%rotate' missing rotate count"); |
| 2626 | return DIRECTIVE_FOUND; |
| 2627 | } |
| 2628 | t = expand_smacro(tline->next); |
| 2629 | tline->next = NULL; |
| 2630 | free_tlist(origline); |
| 2631 | tline = t; |
| 2632 | tptr = &t; |
| 2633 | tokval.t_type = TOKEN_INVALID; |
| 2634 | evalresult = |
| 2635 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2636 | free_tlist(tline); |
| 2637 | if (!evalresult) |
| 2638 | return DIRECTIVE_FOUND; |
| 2639 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2640 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2641 | "trailing garbage after expression ignored"); |
| 2642 | if (!is_simple(evalresult)) { |
| 2643 | error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); |
| 2644 | return DIRECTIVE_FOUND; |
| 2645 | } |
| 2646 | mmac = istk->mstk; |
| 2647 | while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ |
| 2648 | mmac = mmac->next_active; |
| 2649 | if (!mmac) { |
| 2650 | error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call"); |
| 2651 | } else if (mmac->nparam == 0) { |
| 2652 | error(ERR_NONFATAL, |
| 2653 | "`%%rotate' invoked within macro without parameters"); |
| 2654 | } else { |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 2655 | int rotate = mmac->rotate + reloc_value(evalresult); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2656 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 2657 | rotate %= (int)mmac->nparam; |
| 2658 | if (rotate < 0) |
| 2659 | rotate += mmac->nparam; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2660 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 2661 | mmac->rotate = rotate; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2662 | } |
| 2663 | return DIRECTIVE_FOUND; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2664 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2665 | case PP_REP: |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2666 | nolist = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2667 | do { |
| 2668 | tline = tline->next; |
| 2669 | } while (tok_type_(tline, TOK_WHITESPACE)); |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2670 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2671 | if (tok_type_(tline, TOK_ID) && |
| 2672 | nasm_stricmp(tline->text, ".nolist") == 0) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2673 | nolist = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2674 | do { |
| 2675 | tline = tline->next; |
| 2676 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 2677 | } |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 2678 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2679 | if (tline) { |
| 2680 | t = expand_smacro(tline); |
| 2681 | tptr = &t; |
| 2682 | tokval.t_type = TOKEN_INVALID; |
| 2683 | evalresult = |
| 2684 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 2685 | if (!evalresult) { |
| 2686 | free_tlist(origline); |
| 2687 | return DIRECTIVE_FOUND; |
| 2688 | } |
| 2689 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2690 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2691 | "trailing garbage after expression ignored"); |
| 2692 | if (!is_simple(evalresult)) { |
| 2693 | error(ERR_NONFATAL, "non-constant value given to `%%rep'"); |
| 2694 | return DIRECTIVE_FOUND; |
| 2695 | } |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2696 | count = reloc_value(evalresult) + 1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2697 | } else { |
| 2698 | error(ERR_NONFATAL, "`%%rep' expects a repeat count"); |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2699 | count = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2700 | } |
| 2701 | free_tlist(origline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2702 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2703 | tmp_defining = defining; |
| 2704 | defining = nasm_malloc(sizeof(MMacro)); |
| 2705 | defining->name = NULL; /* flags this macro as a %rep block */ |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 2706 | defining->casesense = false; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 2707 | defining->plus = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2708 | defining->nolist = nolist; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 2709 | defining->in_progress = count; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2710 | defining->nparam_min = defining->nparam_max = 0; |
| 2711 | defining->defaults = NULL; |
| 2712 | defining->dlist = NULL; |
| 2713 | defining->expansion = NULL; |
| 2714 | defining->next_active = istk->mstk; |
| 2715 | defining->rep_nest = tmp_defining; |
| 2716 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2717 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2718 | case PP_ENDREP: |
| 2719 | if (!defining || defining->name) { |
| 2720 | error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); |
| 2721 | return DIRECTIVE_FOUND; |
| 2722 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2723 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2724 | /* |
| 2725 | * Now we have a "macro" defined - although it has no name |
| 2726 | * and we won't be entering it in the hash tables - we must |
| 2727 | * push a macro-end marker for it on to istk->expansion. |
| 2728 | * After that, it will take care of propagating itself (a |
| 2729 | * macro-end marker line for a macro which is really a %rep |
| 2730 | * block will cause the macro to be re-expanded, complete |
| 2731 | * with another macro-end marker to ensure the process |
| 2732 | * continues) until the whole expansion is forcibly removed |
| 2733 | * from istk->expansion by a %exitrep. |
| 2734 | */ |
| 2735 | l = nasm_malloc(sizeof(Line)); |
| 2736 | l->next = istk->expansion; |
| 2737 | l->finishes = defining; |
| 2738 | l->first = NULL; |
| 2739 | istk->expansion = l; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2740 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2741 | istk->mstk = defining; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2742 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2743 | list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
| 2744 | tmp_defining = defining; |
| 2745 | defining = defining->rep_nest; |
| 2746 | free_tlist(origline); |
| 2747 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2748 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2749 | case PP_EXITREP: |
| 2750 | /* |
| 2751 | * We must search along istk->expansion until we hit a |
| 2752 | * macro-end marker for a macro with no name. Then we set |
| 2753 | * its `in_progress' flag to 0. |
| 2754 | */ |
| 2755 | for (l = istk->expansion; l; l = l->next) |
| 2756 | if (l->finishes && !l->finishes->name) |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 2757 | break; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2758 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2759 | if (l) |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 2760 | l->finishes->in_progress = 1; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2761 | else |
| 2762 | error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); |
| 2763 | free_tlist(origline); |
| 2764 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2765 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2766 | case PP_XDEFINE: |
| 2767 | case PP_IXDEFINE: |
| 2768 | case PP_DEFINE: |
| 2769 | case PP_IDEFINE: |
H. Peter Anvin | 95e7f95 | 2007-10-11 13:38:38 -0700 | [diff] [blame] | 2770 | casesense = (i == PP_DEFINE || i == PP_XDEFINE); |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2771 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2772 | tline = tline->next; |
| 2773 | skip_white_(tline); |
| 2774 | tline = expand_id(tline); |
| 2775 | if (!tline || (tline->type != TOK_ID && |
| 2776 | (tline->type != TOK_PREPROC_ID || |
| 2777 | tline->text[1] != '$'))) { |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2778 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
| 2779 | pp_directives[i]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2780 | free_tlist(origline); |
| 2781 | return DIRECTIVE_FOUND; |
| 2782 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2783 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2784 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2785 | last = tline; |
| 2786 | param_start = tline = tline->next; |
| 2787 | nparam = 0; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2788 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2789 | /* Expand the macro definition now for %xdefine and %ixdefine */ |
| 2790 | if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) |
| 2791 | tline = expand_smacro(tline); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2792 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2793 | if (tok_is_(tline, "(")) { |
| 2794 | /* |
| 2795 | * This macro has parameters. |
| 2796 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2797 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2798 | tline = tline->next; |
| 2799 | while (1) { |
| 2800 | skip_white_(tline); |
| 2801 | if (!tline) { |
| 2802 | error(ERR_NONFATAL, "parameter identifier expected"); |
| 2803 | free_tlist(origline); |
| 2804 | return DIRECTIVE_FOUND; |
| 2805 | } |
| 2806 | if (tline->type != TOK_ID) { |
| 2807 | error(ERR_NONFATAL, |
| 2808 | "`%s': parameter identifier expected", |
| 2809 | tline->text); |
| 2810 | free_tlist(origline); |
| 2811 | return DIRECTIVE_FOUND; |
| 2812 | } |
| 2813 | tline->type = TOK_SMAC_PARAM + nparam++; |
| 2814 | tline = tline->next; |
| 2815 | skip_white_(tline); |
| 2816 | if (tok_is_(tline, ",")) { |
| 2817 | tline = tline->next; |
H. Peter Anvin | ca348b6 | 2008-07-23 10:49:26 -0400 | [diff] [blame] | 2818 | } else { |
| 2819 | if (!tok_is_(tline, ")")) { |
| 2820 | error(ERR_NONFATAL, |
| 2821 | "`)' expected to terminate macro template"); |
| 2822 | free_tlist(origline); |
| 2823 | return DIRECTIVE_FOUND; |
| 2824 | } |
| 2825 | break; |
| 2826 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2827 | } |
| 2828 | last = tline; |
| 2829 | tline = tline->next; |
| 2830 | } |
| 2831 | if (tok_type_(tline, TOK_WHITESPACE)) |
| 2832 | last = tline, tline = tline->next; |
| 2833 | macro_start = NULL; |
| 2834 | last->next = NULL; |
| 2835 | t = tline; |
| 2836 | while (t) { |
| 2837 | if (t->type == TOK_ID) { |
| 2838 | for (tt = param_start; tt; tt = tt->next) |
| 2839 | if (tt->type >= TOK_SMAC_PARAM && |
| 2840 | !strcmp(tt->text, t->text)) |
| 2841 | t->type = tt->type; |
| 2842 | } |
| 2843 | tt = t->next; |
| 2844 | t->next = macro_start; |
| 2845 | macro_start = t; |
| 2846 | t = tt; |
| 2847 | } |
| 2848 | /* |
| 2849 | * Good. We now have a macro name, a parameter count, and a |
| 2850 | * token list (in reverse order) for an expansion. We ought |
| 2851 | * to be OK just to create an SMacro, store it, and let |
| 2852 | * free_tlist have the rest of the line (which we have |
| 2853 | * carefully re-terminated after chopping off the expansion |
| 2854 | * from the end). |
| 2855 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2856 | define_smacro(ctx, mname, casesense, nparam, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2857 | free_tlist(origline); |
| 2858 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 2859 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2860 | case PP_UNDEF: |
| 2861 | tline = tline->next; |
| 2862 | skip_white_(tline); |
| 2863 | tline = expand_id(tline); |
| 2864 | if (!tline || (tline->type != TOK_ID && |
| 2865 | (tline->type != TOK_PREPROC_ID || |
| 2866 | tline->text[1] != '$'))) { |
| 2867 | error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); |
| 2868 | free_tlist(origline); |
| 2869 | return DIRECTIVE_FOUND; |
| 2870 | } |
| 2871 | if (tline->next) { |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2872 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2873 | "trailing garbage after macro name ignored"); |
| 2874 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 2875 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2876 | /* Find the context that symbol belongs to */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2877 | ctx = get_ctx(tline->text, &mname, false); |
| 2878 | undef_smacro(ctx, mname); |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 2879 | free_tlist(origline); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2880 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 2881 | |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 2882 | case PP_DEFSTR: |
| 2883 | case PP_IDEFSTR: |
| 2884 | casesense = (i == PP_DEFSTR); |
| 2885 | |
| 2886 | tline = tline->next; |
| 2887 | skip_white_(tline); |
| 2888 | tline = expand_id(tline); |
| 2889 | if (!tline || (tline->type != TOK_ID && |
| 2890 | (tline->type != TOK_PREPROC_ID || |
| 2891 | tline->text[1] != '$'))) { |
| 2892 | error(ERR_NONFATAL, "`%s' expects a macro identifier", |
| 2893 | pp_directives[i]); |
| 2894 | free_tlist(origline); |
| 2895 | return DIRECTIVE_FOUND; |
| 2896 | } |
| 2897 | |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2898 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 2899 | last = tline; |
| 2900 | tline = expand_smacro(tline->next); |
| 2901 | last->next = NULL; |
| 2902 | |
| 2903 | while (tok_type_(tline, TOK_WHITESPACE)) |
| 2904 | tline = delete_Token(tline); |
| 2905 | |
| 2906 | p = detoken(tline, false); |
| 2907 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 2908 | macro_start->next = NULL; |
| 2909 | macro_start->text = nasm_quote(p, strlen(p)); |
| 2910 | macro_start->type = TOK_STRING; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2911 | macro_start->a.mac = NULL; |
H. Peter Anvin | 9e20016 | 2008-06-04 17:23:14 -0700 | [diff] [blame] | 2912 | nasm_free(p); |
| 2913 | |
| 2914 | /* |
| 2915 | * We now have a macro name, an implicit parameter count of |
| 2916 | * zero, and a string token to use as an expansion. Create |
| 2917 | * and store an SMacro. |
| 2918 | */ |
| 2919 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 2920 | free_tlist(origline); |
| 2921 | return DIRECTIVE_FOUND; |
| 2922 | |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2923 | case PP_PATHSEARCH: |
| 2924 | { |
| 2925 | FILE *fp; |
| 2926 | StrList *xsl = NULL; |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2927 | StrList **xst = &xsl; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2928 | |
| 2929 | casesense = true; |
| 2930 | |
| 2931 | tline = tline->next; |
| 2932 | skip_white_(tline); |
| 2933 | tline = expand_id(tline); |
| 2934 | if (!tline || (tline->type != TOK_ID && |
| 2935 | (tline->type != TOK_PREPROC_ID || |
| 2936 | tline->text[1] != '$'))) { |
| 2937 | error(ERR_NONFATAL, |
| 2938 | "`%%pathsearch' expects a macro identifier as first parameter"); |
| 2939 | free_tlist(origline); |
| 2940 | return DIRECTIVE_FOUND; |
| 2941 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 2942 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2943 | last = tline; |
| 2944 | tline = expand_smacro(tline->next); |
| 2945 | last->next = NULL; |
| 2946 | |
| 2947 | t = tline; |
| 2948 | while (tok_type_(t, TOK_WHITESPACE)) |
| 2949 | t = t->next; |
| 2950 | |
| 2951 | if (!t || (t->type != TOK_STRING && |
| 2952 | t->type != TOK_INTERNAL_STRING)) { |
| 2953 | error(ERR_NONFATAL, "`%%pathsearch' expects a file name"); |
| 2954 | free_tlist(tline); |
| 2955 | free_tlist(origline); |
| 2956 | return DIRECTIVE_FOUND; /* but we did _something_ */ |
| 2957 | } |
| 2958 | if (t->next) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 2959 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2960 | "trailing garbage after `%%pathsearch' ignored"); |
H. Peter Anvin | 427cc91 | 2008-06-01 21:43:03 -0700 | [diff] [blame] | 2961 | p = t->text; |
| 2962 | if (t->type != TOK_INTERNAL_STRING) |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 2963 | nasm_unquote(p, NULL); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2964 | |
H. Peter Anvin | 2b1c3b9 | 2008-06-06 10:38:46 -0700 | [diff] [blame] | 2965 | fp = inc_fopen(p, &xsl, &xst, true); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2966 | if (fp) { |
| 2967 | p = xsl->str; |
| 2968 | fclose(fp); /* Don't actually care about the file */ |
| 2969 | } |
| 2970 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 2971 | macro_start->next = NULL; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 2972 | macro_start->text = nasm_quote(p, strlen(p)); |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2973 | macro_start->type = TOK_STRING; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 2974 | macro_start->a.mac = NULL; |
H. Peter Anvin | 418ca70 | 2008-05-30 10:42:30 -0700 | [diff] [blame] | 2975 | if (xsl) |
| 2976 | nasm_free(xsl); |
| 2977 | |
| 2978 | /* |
| 2979 | * We now have a macro name, an implicit parameter count of |
| 2980 | * zero, and a string token to use as an expansion. Create |
| 2981 | * and store an SMacro. |
| 2982 | */ |
| 2983 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 2984 | free_tlist(tline); |
| 2985 | free_tlist(origline); |
| 2986 | return DIRECTIVE_FOUND; |
| 2987 | } |
| 2988 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2989 | case PP_STRLEN: |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 2990 | casesense = true; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 2991 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 2992 | tline = tline->next; |
| 2993 | skip_white_(tline); |
| 2994 | tline = expand_id(tline); |
| 2995 | if (!tline || (tline->type != TOK_ID && |
| 2996 | (tline->type != TOK_PREPROC_ID || |
| 2997 | tline->text[1] != '$'))) { |
| 2998 | error(ERR_NONFATAL, |
| 2999 | "`%%strlen' expects a macro identifier as first parameter"); |
| 3000 | free_tlist(origline); |
| 3001 | return DIRECTIVE_FOUND; |
| 3002 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3003 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3004 | last = tline; |
| 3005 | tline = expand_smacro(tline->next); |
| 3006 | last->next = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3007 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3008 | t = tline; |
| 3009 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3010 | t = t->next; |
| 3011 | /* t should now point to the string */ |
| 3012 | if (t->type != TOK_STRING) { |
| 3013 | error(ERR_NONFATAL, |
| 3014 | "`%%strlen` requires string as second parameter"); |
| 3015 | free_tlist(tline); |
| 3016 | free_tlist(origline); |
| 3017 | return DIRECTIVE_FOUND; |
| 3018 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3019 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3020 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3021 | macro_start->next = NULL; |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3022 | make_tok_num(macro_start, nasm_unquote(t->text, NULL)); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3023 | macro_start->a.mac = NULL; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3024 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3025 | /* |
| 3026 | * We now have a macro name, an implicit parameter count of |
| 3027 | * zero, and a numeric token to use as an expansion. Create |
| 3028 | * and store an SMacro. |
| 3029 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3030 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3031 | free_tlist(tline); |
| 3032 | free_tlist(origline); |
| 3033 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3034 | |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3035 | case PP_STRCAT: |
| 3036 | casesense = true; |
| 3037 | |
| 3038 | tline = tline->next; |
| 3039 | skip_white_(tline); |
| 3040 | tline = expand_id(tline); |
| 3041 | if (!tline || (tline->type != TOK_ID && |
| 3042 | (tline->type != TOK_PREPROC_ID || |
| 3043 | tline->text[1] != '$'))) { |
| 3044 | error(ERR_NONFATAL, |
| 3045 | "`%%strcat' expects a macro identifier as first parameter"); |
| 3046 | free_tlist(origline); |
| 3047 | return DIRECTIVE_FOUND; |
| 3048 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3049 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3050 | last = tline; |
| 3051 | tline = expand_smacro(tline->next); |
| 3052 | last->next = NULL; |
| 3053 | |
| 3054 | len = 0; |
| 3055 | for (t = tline; t; t = t->next) { |
| 3056 | switch (t->type) { |
| 3057 | case TOK_WHITESPACE: |
| 3058 | break; |
| 3059 | case TOK_STRING: |
| 3060 | len += t->a.len = nasm_unquote(t->text, NULL); |
| 3061 | break; |
H. Peter Anvin | accf433 | 2008-07-01 21:42:08 -0700 | [diff] [blame] | 3062 | case TOK_OTHER: |
| 3063 | if (!strcmp(t->text, ",")) /* permit comma separators */ |
| 3064 | break; |
| 3065 | /* else fall through */ |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3066 | default: |
| 3067 | error(ERR_NONFATAL, |
| 3068 | "non-string passed to `%%strcat' (%d)", t->type); |
| 3069 | free_tlist(tline); |
| 3070 | free_tlist(origline); |
| 3071 | return DIRECTIVE_FOUND; |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | p = pp = nasm_malloc(len); |
| 3076 | t = tline; |
| 3077 | for (t = tline; t; t = t->next) { |
| 3078 | if (t->type == TOK_STRING) { |
| 3079 | memcpy(p, t->text, t->a.len); |
| 3080 | p += t->a.len; |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * We now have a macro name, an implicit parameter count of |
| 3086 | * zero, and a numeric token to use as an expansion. Create |
| 3087 | * and store an SMacro. |
| 3088 | */ |
| 3089 | macro_start = new_Token(NULL, TOK_STRING, NULL, 0); |
| 3090 | macro_start->text = nasm_quote(pp, len); |
| 3091 | nasm_free(pp); |
| 3092 | define_smacro(ctx, mname, casesense, 0, macro_start); |
| 3093 | free_tlist(tline); |
| 3094 | free_tlist(origline); |
| 3095 | return DIRECTIVE_FOUND; |
| 3096 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3097 | case PP_SUBSTR: |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3098 | { |
| 3099 | int64_t a1, a2; |
| 3100 | size_t len; |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 3101 | |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3102 | casesense = true; |
| 3103 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3104 | tline = tline->next; |
| 3105 | skip_white_(tline); |
| 3106 | tline = expand_id(tline); |
| 3107 | if (!tline || (tline->type != TOK_ID && |
| 3108 | (tline->type != TOK_PREPROC_ID || |
| 3109 | tline->text[1] != '$'))) { |
| 3110 | error(ERR_NONFATAL, |
| 3111 | "`%%substr' expects a macro identifier as first parameter"); |
| 3112 | free_tlist(origline); |
| 3113 | return DIRECTIVE_FOUND; |
| 3114 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3115 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3116 | last = tline; |
| 3117 | tline = expand_smacro(tline->next); |
| 3118 | last->next = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3119 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3120 | t = tline->next; |
| 3121 | while (tok_type_(t, TOK_WHITESPACE)) |
| 3122 | t = t->next; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3123 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3124 | /* t should now point to the string */ |
| 3125 | if (t->type != TOK_STRING) { |
| 3126 | error(ERR_NONFATAL, |
| 3127 | "`%%substr` requires string as second parameter"); |
| 3128 | free_tlist(tline); |
| 3129 | free_tlist(origline); |
| 3130 | return DIRECTIVE_FOUND; |
| 3131 | } |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3132 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3133 | tt = t->next; |
| 3134 | tptr = &tt; |
| 3135 | tokval.t_type = TOKEN_INVALID; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3136 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3137 | pass, error, NULL); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3138 | if (!evalresult) { |
| 3139 | free_tlist(tline); |
| 3140 | free_tlist(origline); |
| 3141 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3142 | } else if (!is_simple(evalresult)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3143 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3144 | free_tlist(tline); |
| 3145 | free_tlist(origline); |
| 3146 | return DIRECTIVE_FOUND; |
| 3147 | } |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3148 | a1 = evalresult->value-1; |
| 3149 | |
| 3150 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3151 | tt = tt->next; |
| 3152 | if (!tt) { |
| 3153 | a2 = 1; /* Backwards compatibility: one character */ |
| 3154 | } else { |
| 3155 | tokval.t_type = TOKEN_INVALID; |
| 3156 | evalresult = evaluate(ppscan, tptr, &tokval, NULL, |
| 3157 | pass, error, NULL); |
| 3158 | if (!evalresult) { |
| 3159 | free_tlist(tline); |
| 3160 | free_tlist(origline); |
| 3161 | return DIRECTIVE_FOUND; |
| 3162 | } else if (!is_simple(evalresult)) { |
| 3163 | error(ERR_NONFATAL, "non-constant value given to `%%substr`"); |
| 3164 | free_tlist(tline); |
| 3165 | free_tlist(origline); |
| 3166 | return DIRECTIVE_FOUND; |
| 3167 | } |
| 3168 | a2 = evalresult->value; |
| 3169 | } |
| 3170 | |
H. Peter Anvin | 88c9e1f | 2008-06-04 11:26:59 -0700 | [diff] [blame] | 3171 | len = nasm_unquote(t->text, NULL); |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3172 | if (a2 < 0) |
| 3173 | a2 = a2+1+len-a1; |
| 3174 | if (a1+a2 > (int64_t)len) |
| 3175 | a2 = len-a1; |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 3176 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3177 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3178 | macro_start->next = NULL; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3179 | macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3180 | macro_start->type = TOK_STRING; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3181 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3182 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3183 | /* |
| 3184 | * We now have a macro name, an implicit parameter count of |
| 3185 | * zero, and a numeric token to use as an expansion. Create |
| 3186 | * and store an SMacro. |
| 3187 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3188 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3189 | free_tlist(tline); |
| 3190 | free_tlist(origline); |
| 3191 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3192 | } |
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_ASSIGN: |
| 3195 | case PP_IASSIGN: |
H. Peter Anvin | 4bc9f1d | 2007-10-11 12:52:03 -0700 | [diff] [blame] | 3196 | casesense = (i == PP_ASSIGN); |
| 3197 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3198 | tline = tline->next; |
| 3199 | skip_white_(tline); |
| 3200 | tline = expand_id(tline); |
| 3201 | if (!tline || (tline->type != TOK_ID && |
| 3202 | (tline->type != TOK_PREPROC_ID || |
| 3203 | tline->text[1] != '$'))) { |
| 3204 | error(ERR_NONFATAL, |
| 3205 | "`%%%sassign' expects a macro identifier", |
| 3206 | (i == PP_IASSIGN ? "i" : "")); |
| 3207 | free_tlist(origline); |
| 3208 | return DIRECTIVE_FOUND; |
| 3209 | } |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3210 | ctx = get_ctx(tline->text, &mname, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3211 | last = tline; |
| 3212 | tline = expand_smacro(tline->next); |
| 3213 | last->next = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3214 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3215 | t = tline; |
| 3216 | tptr = &t; |
| 3217 | tokval.t_type = TOKEN_INVALID; |
| 3218 | evalresult = |
| 3219 | evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL); |
| 3220 | free_tlist(tline); |
| 3221 | if (!evalresult) { |
| 3222 | free_tlist(origline); |
| 3223 | return DIRECTIVE_FOUND; |
| 3224 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3225 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3226 | if (tokval.t_type) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3227 | error(ERR_WARNING|ERR_PASS1, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3228 | "trailing garbage after expression ignored"); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3229 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3230 | if (!is_simple(evalresult)) { |
| 3231 | error(ERR_NONFATAL, |
| 3232 | "non-constant value given to `%%%sassign'", |
| 3233 | (i == PP_IASSIGN ? "i" : "")); |
| 3234 | free_tlist(origline); |
| 3235 | return DIRECTIVE_FOUND; |
| 3236 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3237 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3238 | macro_start = nasm_malloc(sizeof(*macro_start)); |
| 3239 | macro_start->next = NULL; |
| 3240 | make_tok_num(macro_start, reloc_value(evalresult)); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3241 | macro_start->a.mac = NULL; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3242 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3243 | /* |
| 3244 | * We now have a macro name, an implicit parameter count of |
| 3245 | * zero, and a numeric token to use as an expansion. Create |
| 3246 | * and store an SMacro. |
| 3247 | */ |
H. Peter Anvin | 4db5a16 | 2007-10-11 13:42:09 -0700 | [diff] [blame] | 3248 | define_smacro(ctx, mname, casesense, 0, macro_start); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3249 | free_tlist(origline); |
| 3250 | return DIRECTIVE_FOUND; |
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 | case PP_LINE: |
| 3253 | /* |
| 3254 | * Syntax is `%line nnn[+mmm] [filename]' |
| 3255 | */ |
| 3256 | tline = tline->next; |
| 3257 | skip_white_(tline); |
| 3258 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3259 | error(ERR_NONFATAL, "`%%line' expects line number"); |
| 3260 | free_tlist(origline); |
| 3261 | return DIRECTIVE_FOUND; |
| 3262 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3263 | k = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3264 | m = 1; |
| 3265 | tline = tline->next; |
| 3266 | if (tok_is_(tline, "+")) { |
| 3267 | tline = tline->next; |
| 3268 | if (!tok_type_(tline, TOK_NUMBER)) { |
| 3269 | error(ERR_NONFATAL, "`%%line' expects line increment"); |
| 3270 | free_tlist(origline); |
| 3271 | return DIRECTIVE_FOUND; |
| 3272 | } |
H. Peter Anvin | 7005596 | 2007-10-11 00:05:31 -0700 | [diff] [blame] | 3273 | m = readnum(tline->text, &err); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3274 | tline = tline->next; |
| 3275 | } |
| 3276 | skip_white_(tline); |
| 3277 | src_set_linnum(k); |
| 3278 | istk->lineinc = m; |
| 3279 | if (tline) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3280 | nasm_free(src_set_fname(detoken(tline, false))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3281 | } |
| 3282 | free_tlist(origline); |
| 3283 | return DIRECTIVE_FOUND; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3284 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3285 | default: |
| 3286 | error(ERR_FATAL, |
| 3287 | "preprocessor directive `%s' not yet implemented", |
H. Peter Anvin | 4169a47 | 2007-09-12 01:29:43 +0000 | [diff] [blame] | 3288 | pp_directives[i]); |
H. Peter Anvin | a26433d | 2008-07-16 14:40:01 -0700 | [diff] [blame] | 3289 | return DIRECTIVE_FOUND; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3290 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3294 | * Ensure that a macro parameter contains a condition code and |
| 3295 | * nothing else. Return the condition code index if so, or -1 |
| 3296 | * otherwise. |
| 3297 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3298 | static int find_cc(Token * t) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3299 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3300 | Token *tt; |
| 3301 | int i, j, k, m; |
| 3302 | |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3303 | if (!t) |
| 3304 | return -1; /* Probably a %+ without a space */ |
| 3305 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3306 | skip_white_(t); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3307 | if (t->type != TOK_ID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3308 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3309 | tt = t->next; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3310 | skip_white_(tt); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3311 | if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3312 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3313 | |
| 3314 | i = -1; |
Ed Beroset | 3ab3f41 | 2002-06-11 03:31:49 +0000 | [diff] [blame] | 3315 | j = elements(conditions); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3316 | while (j - i > 1) { |
| 3317 | k = (j + i) / 2; |
| 3318 | m = nasm_stricmp(t->text, conditions[k]); |
| 3319 | if (m == 0) { |
| 3320 | i = k; |
| 3321 | j = -2; |
| 3322 | break; |
| 3323 | } else if (m < 0) { |
| 3324 | j = k; |
| 3325 | } else |
| 3326 | i = k; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3327 | } |
| 3328 | if (j != -2) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3329 | return -1; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3330 | return i; |
| 3331 | } |
| 3332 | |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3333 | static bool paste_tokens(Token **head, bool handle_paste_tokens) |
| 3334 | { |
| 3335 | Token **tail, *t, *tt; |
| 3336 | Token **paste_head; |
| 3337 | bool did_paste = false; |
| 3338 | char *tmp; |
| 3339 | |
| 3340 | /* Now handle token pasting... */ |
| 3341 | paste_head = NULL; |
| 3342 | tail = head; |
| 3343 | while ((t = *tail) && (tt = t->next)) { |
| 3344 | switch (t->type) { |
| 3345 | case TOK_WHITESPACE: |
| 3346 | if (tt->type == TOK_WHITESPACE) { |
| 3347 | /* Zap adjacent whitespace tokens */ |
| 3348 | t->next = delete_Token(tt); |
| 3349 | } else { |
| 3350 | /* Do not advance paste_head here */ |
| 3351 | tail = &t->next; |
| 3352 | } |
| 3353 | break; |
| 3354 | case TOK_ID: |
| 3355 | case TOK_PREPROC_ID: |
| 3356 | case TOK_NUMBER: |
| 3357 | case TOK_FLOAT: |
| 3358 | { |
| 3359 | size_t len = 0; |
| 3360 | char *tmp, *p; |
| 3361 | |
| 3362 | while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID || |
| 3363 | tt->type == TOK_NUMBER || tt->type == TOK_FLOAT || |
| 3364 | tt->type == TOK_OTHER)) { |
| 3365 | len += strlen(tt->text); |
| 3366 | tt = tt->next; |
| 3367 | } |
| 3368 | |
| 3369 | /* Now tt points to the first token after the potential |
| 3370 | paste area... */ |
| 3371 | if (tt != t->next) { |
| 3372 | /* We have at least two tokens... */ |
| 3373 | len += strlen(t->text); |
| 3374 | p = tmp = nasm_malloc(len+1); |
| 3375 | |
| 3376 | while (t != tt) { |
| 3377 | strcpy(p, t->text); |
| 3378 | p = strchr(p, '\0'); |
| 3379 | t = delete_Token(t); |
| 3380 | } |
| 3381 | |
| 3382 | t = *tail = tokenize(tmp); |
| 3383 | nasm_free(tmp); |
| 3384 | |
| 3385 | while (t->next) { |
| 3386 | tail = &t->next; |
| 3387 | t = t->next; |
| 3388 | } |
| 3389 | t->next = tt; /* Attach the remaining token chain */ |
| 3390 | |
| 3391 | did_paste = true; |
| 3392 | } |
| 3393 | paste_head = tail; |
| 3394 | tail = &t->next; |
| 3395 | break; |
| 3396 | } |
| 3397 | case TOK_PASTE: /* %+ */ |
| 3398 | if (handle_paste_tokens) { |
| 3399 | /* Zap %+ and whitespace tokens to the right */ |
| 3400 | while (t && (t->type == TOK_WHITESPACE || |
| 3401 | t->type == TOK_PASTE)) |
| 3402 | t = *tail = delete_Token(t); |
| 3403 | if (!paste_head || !t) |
| 3404 | break; /* Nothing to paste with */ |
| 3405 | tail = paste_head; |
| 3406 | t = *tail; |
| 3407 | tt = t->next; |
| 3408 | while (tok_type_(tt, TOK_WHITESPACE)) |
| 3409 | tt = t->next = delete_Token(tt); |
| 3410 | |
| 3411 | if (tt) { |
| 3412 | tmp = nasm_strcat(t->text, tt->text); |
| 3413 | delete_Token(t); |
| 3414 | tt = delete_Token(tt); |
| 3415 | t = *tail = tokenize(tmp); |
| 3416 | nasm_free(tmp); |
| 3417 | while (t->next) { |
| 3418 | tail = &t->next; |
| 3419 | t = t->next; |
| 3420 | } |
| 3421 | t->next = tt; /* Attach the remaining token chain */ |
| 3422 | did_paste = true; |
| 3423 | } |
| 3424 | paste_head = tail; |
| 3425 | tail = &t->next; |
| 3426 | break; |
| 3427 | } |
| 3428 | /* else fall through */ |
| 3429 | default: |
| 3430 | tail = paste_head = &t->next; |
| 3431 | break; |
| 3432 | } |
| 3433 | } |
| 3434 | return did_paste; |
| 3435 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3436 | /* |
| 3437 | * Expand MMacro-local things: parameter references (%0, %n, %+n, |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3438 | * %-n) and MMacro-local identifiers (%%foo) as well as |
| 3439 | * macro indirection (%[...]). |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3440 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3441 | static Token *expand_mmac_params(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3442 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3443 | Token *t, *tt, **tail, *thead; |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3444 | bool changed = false; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3445 | |
| 3446 | tail = &thead; |
| 3447 | thead = NULL; |
| 3448 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3449 | while (tline) { |
| 3450 | if (tline->type == TOK_PREPROC_ID && |
| 3451 | (((tline->text[1] == '+' || tline->text[1] == '-') |
| 3452 | && tline->text[2]) || tline->text[1] == '%' |
| 3453 | || (tline->text[1] >= '0' && tline->text[1] <= '9'))) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3454 | char *text = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3455 | int type = 0, cc; /* type = 0 to placate optimisers */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3456 | char tmpbuf[30]; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3457 | unsigned int n; |
| 3458 | int i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3459 | MMacro *mac; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3460 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3461 | t = tline; |
| 3462 | tline = tline->next; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3463 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3464 | mac = istk->mstk; |
| 3465 | while (mac && !mac->name) /* avoid mistaking %reps for macros */ |
| 3466 | mac = mac->next_active; |
| 3467 | if (!mac) |
| 3468 | error(ERR_NONFATAL, "`%s': not in a macro call", t->text); |
| 3469 | else |
| 3470 | switch (t->text[1]) { |
| 3471 | /* |
| 3472 | * We have to make a substitution of one of the |
| 3473 | * forms %1, %-1, %+1, %%foo, %0. |
| 3474 | */ |
| 3475 | case '0': |
| 3476 | type = TOK_NUMBER; |
| 3477 | snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam); |
| 3478 | text = nasm_strdup(tmpbuf); |
| 3479 | break; |
| 3480 | case '%': |
| 3481 | type = TOK_ID; |
H. Peter Anvin | f8ba53e | 2007-10-11 10:11:57 -0700 | [diff] [blame] | 3482 | snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3483 | mac->unique); |
| 3484 | text = nasm_strcat(tmpbuf, t->text + 2); |
| 3485 | break; |
| 3486 | case '-': |
| 3487 | n = atoi(t->text + 2) - 1; |
| 3488 | if (n >= mac->nparam) |
| 3489 | tt = NULL; |
| 3490 | else { |
| 3491 | if (mac->nparam > 1) |
| 3492 | n = (n + mac->rotate) % mac->nparam; |
| 3493 | tt = mac->params[n]; |
| 3494 | } |
| 3495 | cc = find_cc(tt); |
| 3496 | if (cc == -1) { |
| 3497 | error(ERR_NONFATAL, |
| 3498 | "macro parameter %d is not a condition code", |
| 3499 | n + 1); |
| 3500 | text = NULL; |
| 3501 | } else { |
| 3502 | type = TOK_ID; |
| 3503 | if (inverse_ccs[cc] == -1) { |
| 3504 | error(ERR_NONFATAL, |
| 3505 | "condition code `%s' is not invertible", |
| 3506 | conditions[cc]); |
| 3507 | text = NULL; |
| 3508 | } else |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3509 | text = nasm_strdup(conditions[inverse_ccs[cc]]); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3510 | } |
| 3511 | break; |
| 3512 | case '+': |
| 3513 | n = atoi(t->text + 2) - 1; |
| 3514 | if (n >= mac->nparam) |
| 3515 | tt = NULL; |
| 3516 | else { |
| 3517 | if (mac->nparam > 1) |
| 3518 | n = (n + mac->rotate) % mac->nparam; |
| 3519 | tt = mac->params[n]; |
| 3520 | } |
| 3521 | cc = find_cc(tt); |
| 3522 | if (cc == -1) { |
| 3523 | error(ERR_NONFATAL, |
| 3524 | "macro parameter %d is not a condition code", |
| 3525 | n + 1); |
| 3526 | text = NULL; |
| 3527 | } else { |
| 3528 | type = TOK_ID; |
| 3529 | text = nasm_strdup(conditions[cc]); |
| 3530 | } |
| 3531 | break; |
| 3532 | default: |
| 3533 | n = atoi(t->text + 1) - 1; |
| 3534 | if (n >= mac->nparam) |
| 3535 | tt = NULL; |
| 3536 | else { |
| 3537 | if (mac->nparam > 1) |
| 3538 | n = (n + mac->rotate) % mac->nparam; |
| 3539 | tt = mac->params[n]; |
| 3540 | } |
| 3541 | if (tt) { |
| 3542 | for (i = 0; i < mac->paramlen[n]; i++) { |
| 3543 | *tail = new_Token(NULL, tt->type, tt->text, 0); |
| 3544 | tail = &(*tail)->next; |
| 3545 | tt = tt->next; |
| 3546 | } |
| 3547 | } |
| 3548 | text = NULL; /* we've done it here */ |
| 3549 | break; |
| 3550 | } |
| 3551 | if (!text) { |
| 3552 | delete_Token(t); |
| 3553 | } else { |
| 3554 | *tail = t; |
| 3555 | tail = &t->next; |
| 3556 | t->type = type; |
| 3557 | nasm_free(t->text); |
| 3558 | t->text = text; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3559 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3560 | } |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3561 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3562 | continue; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3563 | } else if (tline->type == TOK_INDIRECT) { |
| 3564 | t = tline; |
| 3565 | tline = tline->next; |
| 3566 | tt = tokenize(t->text); |
| 3567 | tt = expand_mmac_params(tt); |
| 3568 | tt = expand_smacro(tt); |
| 3569 | *tail = tt; |
| 3570 | while (tt) { |
| 3571 | tt->a.mac = NULL; /* Necessary? */ |
| 3572 | tail = &tt->next; |
| 3573 | tt = tt->next; |
| 3574 | } |
| 3575 | delete_Token(t); |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3576 | changed = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3577 | } else { |
| 3578 | t = *tail = tline; |
| 3579 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3580 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3581 | tail = &t->next; |
| 3582 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3583 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3584 | *tail = NULL; |
H. Peter Anvin | 67c6372 | 2008-10-26 23:49:00 -0700 | [diff] [blame] | 3585 | |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3586 | if (changed) |
H. Peter Anvin | ba7a0d0 | 2009-05-04 10:11:22 -0700 | [diff] [blame] | 3587 | paste_tokens(&thead, true); |
H. Peter Anvin | 6125b62 | 2009-04-08 14:02:25 -0700 | [diff] [blame] | 3588 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 3589 | return thead; |
| 3590 | } |
| 3591 | |
| 3592 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3593 | * Expand all single-line macro calls made in the given line. |
| 3594 | * Return the expanded version of the line. The original is deemed |
| 3595 | * to be destroyed in the process. (In reality we'll just move |
| 3596 | * Tokens from input to output a lot of the time, rather than |
| 3597 | * actually bothering to destroy and replicate.) |
| 3598 | */ |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 3599 | #define DEADMAN_LIMIT (1 << 20) |
| 3600 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3601 | static Token *expand_smacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3602 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3603 | Token *t, *tt, *mstart, **tail, *thead; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 3604 | struct hash_table *smtbl; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3605 | SMacro *head = NULL, *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3606 | Token **params; |
| 3607 | int *paramsize; |
H. Peter Anvin | 25a9934 | 2007-09-22 17:45:45 -0700 | [diff] [blame] | 3608 | unsigned int nparam, sparam; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3609 | int brackets; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3610 | Token *org_tline = tline; |
| 3611 | Context *ctx; |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3612 | const char *mname; |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 3613 | int deadman = DEADMAN_LIMIT; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3614 | bool expanded; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3615 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3616 | /* |
| 3617 | * Trick: we should avoid changing the start token pointer since it can |
| 3618 | * be contained in "next" field of other token. Because of this |
| 3619 | * we allocate a copy of first token and work with it; at the end of |
| 3620 | * routine we copy it back |
| 3621 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3622 | if (org_tline) { |
| 3623 | tline = |
| 3624 | new_Token(org_tline->next, org_tline->type, org_tline->text, |
| 3625 | 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3626 | tline->a.mac = org_tline->a.mac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3627 | nasm_free(org_tline->text); |
| 3628 | org_tline->text = NULL; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 3631 | again: |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3632 | tail = &thead; |
| 3633 | thead = NULL; |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3634 | expanded = false; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3635 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3636 | while (tline) { /* main token loop */ |
H. Peter Anvin | 2a15e69 | 2007-11-19 13:14:59 -0800 | [diff] [blame] | 3637 | if (!--deadman) { |
H. Peter Anvin | cb1cf59 | 2007-11-19 12:26:50 -0800 | [diff] [blame] | 3638 | error(ERR_NONFATAL, "interminable macro recursion"); |
| 3639 | break; |
| 3640 | } |
| 3641 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3642 | if ((mname = tline->text)) { |
| 3643 | /* if this token is a local macro, look in local context */ |
H. Peter Anvin | f8ad532 | 2009-02-21 17:55:08 -0800 | [diff] [blame] | 3644 | if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) |
| 3645 | ctx = get_ctx(mname, &mname, true); |
| 3646 | else |
| 3647 | ctx = NULL; |
| 3648 | smtbl = ctx ? &ctx->localmac : &smacros; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 3649 | head = (SMacro *) hash_findix(smtbl, mname); |
H. Peter Anvin | 072771e | 2008-05-22 13:17:51 -0700 | [diff] [blame] | 3650 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3651 | /* |
| 3652 | * We've hit an identifier. As in is_mmacro below, we first |
| 3653 | * check whether the identifier is a single-line macro at |
| 3654 | * all, then think about checking for parameters if |
| 3655 | * necessary. |
| 3656 | */ |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 3657 | for (m = head; m; m = m->next) |
| 3658 | if (!mstrcmp(m->name, mname, m->casesense)) |
| 3659 | break; |
| 3660 | if (m) { |
| 3661 | mstart = tline; |
| 3662 | params = NULL; |
| 3663 | paramsize = NULL; |
| 3664 | if (m->nparam == 0) { |
| 3665 | /* |
| 3666 | * Simple case: the macro is parameterless. Discard the |
| 3667 | * one token that the macro call took, and push the |
| 3668 | * expansion back on the to-do stack. |
| 3669 | */ |
| 3670 | if (!m->expansion) { |
| 3671 | if (!strcmp("__FILE__", m->name)) { |
| 3672 | int32_t num = 0; |
H. Peter Anvin | 932de6c | 2008-07-31 18:46:11 -0700 | [diff] [blame] | 3673 | char *file = NULL; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3674 | src_get(&num, &file); |
| 3675 | tline->text = nasm_quote(file, strlen(file)); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 3676 | tline->type = TOK_STRING; |
H. Peter Anvin | 8cad14b | 2008-06-01 17:23:51 -0700 | [diff] [blame] | 3677 | nasm_free(file); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 3678 | continue; |
| 3679 | } |
| 3680 | if (!strcmp("__LINE__", m->name)) { |
| 3681 | nasm_free(tline->text); |
| 3682 | make_tok_num(tline, src_get_linnum()); |
| 3683 | continue; |
| 3684 | } |
| 3685 | if (!strcmp("__BITS__", m->name)) { |
| 3686 | nasm_free(tline->text); |
| 3687 | make_tok_num(tline, globalbits); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3688 | continue; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 3689 | } |
| 3690 | tline = delete_Token(tline); |
| 3691 | continue; |
| 3692 | } |
| 3693 | } else { |
| 3694 | /* |
| 3695 | * Complicated case: at least one macro with this name |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3696 | * exists and takes parameters. We must find the |
| 3697 | * parameters in the call, count them, find the SMacro |
| 3698 | * that corresponds to that form of the macro call, and |
| 3699 | * substitute for the parameters when we expand. What a |
| 3700 | * pain. |
| 3701 | */ |
| 3702 | /*tline = tline->next; |
| 3703 | skip_white_(tline); */ |
| 3704 | do { |
| 3705 | t = tline->next; |
| 3706 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3707 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3708 | t->text = NULL; |
| 3709 | t = tline->next = delete_Token(t); |
| 3710 | } |
| 3711 | tline = t; |
| 3712 | } while (tok_type_(tline, TOK_WHITESPACE)); |
| 3713 | if (!tok_is_(tline, "(")) { |
| 3714 | /* |
| 3715 | * This macro wasn't called with parameters: ignore |
| 3716 | * the call. (Behaviour borrowed from gnu cpp.) |
| 3717 | */ |
| 3718 | tline = mstart; |
| 3719 | m = NULL; |
| 3720 | } else { |
| 3721 | int paren = 0; |
| 3722 | int white = 0; |
| 3723 | brackets = 0; |
| 3724 | nparam = 0; |
| 3725 | sparam = PARAM_DELTA; |
| 3726 | params = nasm_malloc(sparam * sizeof(Token *)); |
| 3727 | params[0] = tline->next; |
| 3728 | paramsize = nasm_malloc(sparam * sizeof(int)); |
| 3729 | paramsize[0] = 0; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3730 | while (true) { /* parameter loop */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3731 | /* |
| 3732 | * For some unusual expansions |
| 3733 | * which concatenates function call |
| 3734 | */ |
| 3735 | t = tline->next; |
| 3736 | while (tok_type_(t, TOK_SMAC_END)) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3737 | t->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3738 | t->text = NULL; |
| 3739 | t = tline->next = delete_Token(t); |
| 3740 | } |
| 3741 | tline = t; |
Nickolay Yurchenko | 9aea715 | 2003-09-07 22:46:26 +0000 | [diff] [blame] | 3742 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3743 | if (!tline) { |
| 3744 | error(ERR_NONFATAL, |
| 3745 | "macro call expects terminating `)'"); |
| 3746 | break; |
| 3747 | } |
| 3748 | if (tline->type == TOK_WHITESPACE |
| 3749 | && brackets <= 0) { |
| 3750 | if (paramsize[nparam]) |
| 3751 | white++; |
| 3752 | else |
| 3753 | params[nparam] = tline->next; |
| 3754 | continue; /* parameter loop */ |
| 3755 | } |
| 3756 | if (tline->type == TOK_OTHER |
| 3757 | && tline->text[1] == 0) { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 3758 | char ch = tline->text[0]; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3759 | if (ch == ',' && !paren && brackets <= 0) { |
| 3760 | if (++nparam >= sparam) { |
| 3761 | sparam += PARAM_DELTA; |
| 3762 | params = nasm_realloc(params, |
| 3763 | sparam * |
| 3764 | sizeof(Token |
| 3765 | *)); |
| 3766 | paramsize = |
| 3767 | nasm_realloc(paramsize, |
| 3768 | sparam * |
| 3769 | sizeof(int)); |
| 3770 | } |
| 3771 | params[nparam] = tline->next; |
| 3772 | paramsize[nparam] = 0; |
| 3773 | white = 0; |
| 3774 | continue; /* parameter loop */ |
| 3775 | } |
| 3776 | if (ch == '{' && |
| 3777 | (brackets > 0 || (brackets == 0 && |
| 3778 | !paramsize[nparam]))) |
| 3779 | { |
| 3780 | if (!(brackets++)) { |
| 3781 | params[nparam] = tline->next; |
| 3782 | continue; /* parameter loop */ |
| 3783 | } |
| 3784 | } |
| 3785 | if (ch == '}' && brackets > 0) |
| 3786 | if (--brackets == 0) { |
| 3787 | brackets = -1; |
| 3788 | continue; /* parameter loop */ |
| 3789 | } |
| 3790 | if (ch == '(' && !brackets) |
| 3791 | paren++; |
| 3792 | if (ch == ')' && brackets <= 0) |
| 3793 | if (--paren < 0) |
| 3794 | break; |
| 3795 | } |
| 3796 | if (brackets < 0) { |
| 3797 | brackets = 0; |
| 3798 | error(ERR_NONFATAL, "braces do not " |
| 3799 | "enclose all of macro parameter"); |
| 3800 | } |
| 3801 | paramsize[nparam] += white + 1; |
| 3802 | white = 0; |
| 3803 | } /* parameter loop */ |
| 3804 | nparam++; |
| 3805 | while (m && (m->nparam != nparam || |
| 3806 | mstrcmp(m->name, mname, |
| 3807 | m->casesense))) |
| 3808 | m = m->next; |
| 3809 | if (!m) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 3810 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3811 | "macro `%s' exists, " |
| 3812 | "but not taking %d parameters", |
| 3813 | mstart->text, nparam); |
| 3814 | } |
| 3815 | } |
| 3816 | if (m && m->in_progress) |
| 3817 | m = NULL; |
| 3818 | if (!m) { /* in progess or didn't find '(' or wrong nparam */ |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3819 | /* |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3820 | * Design question: should we handle !tline, which |
| 3821 | * indicates missing ')' here, or expand those |
| 3822 | * macros anyway, which requires the (t) test a few |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 3823 | * lines down? |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3824 | */ |
| 3825 | nasm_free(params); |
| 3826 | nasm_free(paramsize); |
| 3827 | tline = mstart; |
| 3828 | } else { |
| 3829 | /* |
| 3830 | * Expand the macro: we are placed on the last token of the |
| 3831 | * call, so that we can easily split the call from the |
| 3832 | * following tokens. We also start by pushing an SMAC_END |
| 3833 | * token for the cycle removal. |
| 3834 | */ |
| 3835 | t = tline; |
| 3836 | if (t) { |
| 3837 | tline = t->next; |
| 3838 | t->next = NULL; |
| 3839 | } |
| 3840 | tt = new_Token(tline, TOK_SMAC_END, NULL, 0); |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3841 | tt->a.mac = m; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 3842 | m->in_progress = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3843 | tline = tt; |
| 3844 | for (t = m->expansion; t; t = t->next) { |
| 3845 | if (t->type >= TOK_SMAC_PARAM) { |
| 3846 | Token *pcopy = tline, **ptail = &pcopy; |
| 3847 | Token *ttt, *pt; |
| 3848 | int i; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3849 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3850 | ttt = params[t->type - TOK_SMAC_PARAM]; |
| 3851 | for (i = paramsize[t->type - TOK_SMAC_PARAM]; |
| 3852 | --i >= 0;) { |
| 3853 | pt = *ptail = |
| 3854 | new_Token(tline, ttt->type, ttt->text, |
| 3855 | 0); |
| 3856 | ptail = &pt->next; |
| 3857 | ttt = ttt->next; |
| 3858 | } |
| 3859 | tline = pcopy; |
H. Peter Anvin | 6c81f0a | 2008-05-25 21:46:17 -0700 | [diff] [blame] | 3860 | } else if (t->type == TOK_PREPROC_Q) { |
| 3861 | tt = new_Token(tline, TOK_ID, mname, 0); |
| 3862 | tline = tt; |
| 3863 | } else if (t->type == TOK_PREPROC_QQ) { |
| 3864 | tt = new_Token(tline, TOK_ID, m->name, 0); |
| 3865 | tline = tt; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3866 | } else { |
| 3867 | tt = new_Token(tline, t->type, t->text, 0); |
| 3868 | tline = tt; |
| 3869 | } |
| 3870 | } |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3871 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3872 | /* |
| 3873 | * Having done that, get rid of the macro call, and clean |
| 3874 | * up the parameters. |
| 3875 | */ |
| 3876 | nasm_free(params); |
| 3877 | nasm_free(paramsize); |
| 3878 | free_tlist(mstart); |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3879 | expanded = true; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3880 | continue; /* main token loop */ |
| 3881 | } |
| 3882 | } |
| 3883 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3884 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3885 | if (tline->type == TOK_SMAC_END) { |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3886 | tline->a.mac->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3887 | tline = delete_Token(tline); |
| 3888 | } else { |
| 3889 | t = *tail = tline; |
| 3890 | tline = tline->next; |
H. Peter Anvin | f26e097 | 2008-07-01 21:26:27 -0700 | [diff] [blame] | 3891 | t->a.mac = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3892 | t->next = NULL; |
| 3893 | tail = &t->next; |
| 3894 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3897 | /* |
| 3898 | * 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] | 3899 | * after expansion (they can't be produced by tokenize()). The successive |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3900 | * TOK_IDs should be concatenated. |
| 3901 | * Also we look for %+ tokens and concatenate the tokens before and after |
| 3902 | * them (without white spaces in between). |
| 3903 | */ |
H. Peter Anvin | d784a08 | 2009-04-20 14:01:18 -0700 | [diff] [blame] | 3904 | if (expanded && paste_tokens(&thead, true)) { |
| 3905 | /* If we concatenated something, re-scan the line for macros */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3906 | tline = thead; |
| 3907 | goto again; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3908 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3909 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3910 | if (org_tline) { |
| 3911 | if (thead) { |
| 3912 | *org_tline = *thead; |
| 3913 | /* since we just gave text to org_line, don't free it */ |
| 3914 | thead->text = NULL; |
| 3915 | delete_Token(thead); |
| 3916 | } else { |
| 3917 | /* the expression expanded to empty line; |
| 3918 | we can't return NULL for some reasons |
| 3919 | we just set the line to a single WHITESPACE token. */ |
| 3920 | memset(org_tline, 0, sizeof(*org_tline)); |
| 3921 | org_tline->text = NULL; |
| 3922 | org_tline->type = TOK_WHITESPACE; |
| 3923 | } |
| 3924 | thead = org_tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3927 | return thead; |
| 3928 | } |
| 3929 | |
| 3930 | /* |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3931 | * Similar to expand_smacro but used exclusively with macro identifiers |
| 3932 | * right before they are fetched in. The reason is that there can be |
| 3933 | * identifiers consisting of several subparts. We consider that if there |
| 3934 | * are more than one element forming the name, user wants a expansion, |
| 3935 | * otherwise it will be left as-is. Example: |
| 3936 | * |
| 3937 | * %define %$abc cde |
| 3938 | * |
| 3939 | * the identifier %$abc will be left as-is so that the handler for %define |
| 3940 | * will suck it and define the corresponding value. Other case: |
| 3941 | * |
| 3942 | * %define _%$abc cde |
| 3943 | * |
| 3944 | * In this case user wants name to be expanded *before* %define starts |
| 3945 | * working, so we'll expand %$abc into something (if it has a value; |
| 3946 | * otherwise it will be left as-is) then concatenate all successive |
| 3947 | * PP_IDs into one. |
| 3948 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3949 | static Token *expand_id(Token * tline) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3950 | { |
| 3951 | Token *cur, *oldnext = NULL; |
| 3952 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3953 | if (!tline || !tline->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3954 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3955 | |
| 3956 | cur = tline; |
| 3957 | while (cur->next && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3958 | (cur->next->type == TOK_ID || |
| 3959 | cur->next->type == TOK_PREPROC_ID |
| 3960 | || cur->next->type == TOK_NUMBER)) |
| 3961 | cur = cur->next; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3962 | |
| 3963 | /* If identifier consists of just one token, don't expand */ |
| 3964 | if (cur == tline) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3965 | return tline; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3966 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3967 | if (cur) { |
| 3968 | oldnext = cur->next; /* Detach the tail past identifier */ |
| 3969 | cur->next = NULL; /* so that expand_smacro stops here */ |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 3972 | tline = expand_smacro(tline); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3973 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3974 | if (cur) { |
| 3975 | /* expand_smacro possibly changhed tline; re-scan for EOL */ |
| 3976 | cur = tline; |
| 3977 | while (cur && cur->next) |
| 3978 | cur = cur->next; |
| 3979 | if (cur) |
| 3980 | cur->next = oldnext; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | return tline; |
| 3984 | } |
| 3985 | |
| 3986 | /* |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3987 | * Determine whether the given line constitutes a multi-line macro |
| 3988 | * call, and return the MMacro structure called if so. Doesn't have |
| 3989 | * to check for an initial label - that's taken care of in |
| 3990 | * expand_mmacro - but must check numbers of parameters. Guaranteed |
| 3991 | * to be called with tline->type == TOK_ID, so the putative macro |
| 3992 | * name is easy to find. |
| 3993 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 3994 | static MMacro *is_mmacro(Token * tline, Token *** params_array) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 3995 | { |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 3996 | MMacro *head, *m; |
| 3997 | Token **params; |
| 3998 | int nparam; |
| 3999 | |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 4000 | head = (MMacro *) hash_findix(&mmacros, tline->text); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4001 | |
| 4002 | /* |
| 4003 | * Efficiency: first we see if any macro exists with the given |
| 4004 | * name. If not, we can return NULL immediately. _Then_ we |
| 4005 | * count the parameters, and then we look further along the |
| 4006 | * list if necessary to find the proper MMacro. |
| 4007 | */ |
| 4008 | for (m = head; m; m = m->next) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4009 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 4010 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4011 | if (!m) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4012 | return NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4013 | |
| 4014 | /* |
| 4015 | * OK, we have a potential macro. Count and demarcate the |
| 4016 | * parameters. |
| 4017 | */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4018 | count_mmac_params(tline->next, &nparam, ¶ms); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4019 | |
| 4020 | /* |
| 4021 | * So we know how many parameters we've got. Find the MMacro |
| 4022 | * structure that handles this number. |
| 4023 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4024 | while (m) { |
| 4025 | if (m->nparam_min <= nparam |
| 4026 | && (m->plus || nparam <= m->nparam_max)) { |
| 4027 | /* |
| 4028 | * This one is right. Just check if cycle removal |
| 4029 | * prohibits us using it before we actually celebrate... |
| 4030 | */ |
| 4031 | if (m->in_progress) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4032 | #if 0 |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4033 | error(ERR_NONFATAL, |
| 4034 | "self-reference in multi-line macro `%s'", m->name); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4035 | #endif |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4036 | nasm_free(params); |
| 4037 | return NULL; |
| 4038 | } |
| 4039 | /* |
| 4040 | * It's right, and we can use it. Add its default |
| 4041 | * parameters to the end of our list if necessary. |
| 4042 | */ |
| 4043 | if (m->defaults && nparam < m->nparam_min + m->ndefs) { |
| 4044 | params = |
| 4045 | nasm_realloc(params, |
| 4046 | ((m->nparam_min + m->ndefs + |
| 4047 | 1) * sizeof(*params))); |
| 4048 | while (nparam < m->nparam_min + m->ndefs) { |
| 4049 | params[nparam] = m->defaults[nparam - m->nparam_min]; |
| 4050 | nparam++; |
| 4051 | } |
| 4052 | } |
| 4053 | /* |
| 4054 | * If we've gone over the maximum parameter count (and |
| 4055 | * we're in Plus mode), ignore parameters beyond |
| 4056 | * nparam_max. |
| 4057 | */ |
| 4058 | if (m->plus && nparam > m->nparam_max) |
| 4059 | nparam = m->nparam_max; |
| 4060 | /* |
| 4061 | * Then terminate the parameter list, and leave. |
| 4062 | */ |
| 4063 | if (!params) { /* need this special case */ |
| 4064 | params = nasm_malloc(sizeof(*params)); |
| 4065 | nparam = 0; |
| 4066 | } |
| 4067 | params[nparam] = NULL; |
| 4068 | *params_array = params; |
| 4069 | return m; |
| 4070 | } |
| 4071 | /* |
| 4072 | * This one wasn't right: look for the next one with the |
| 4073 | * same name. |
| 4074 | */ |
| 4075 | for (m = m->next; m; m = m->next) |
| 4076 | if (!mstrcmp(m->name, tline->text, m->casesense)) |
| 4077 | break; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | /* |
| 4081 | * After all that, we didn't find one with the right number of |
| 4082 | * parameters. Issue a warning, and fail to expand the macro. |
| 4083 | */ |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4084 | error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4085 | "macro `%s' exists, but not taking %d parameters", |
| 4086 | tline->text, nparam); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4087 | nasm_free(params); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4088 | return NULL; |
| 4089 | } |
| 4090 | |
| 4091 | /* |
| 4092 | * Expand the multi-line macro call made by the given line, if |
| 4093 | * there is one to be expanded. If there is, push the expansion on |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4094 | * istk->expansion and return 1. Otherwise return 0. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4095 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4096 | static int expand_mmacro(Token * tline) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4097 | { |
| 4098 | Token *startline = tline; |
| 4099 | Token *label = NULL; |
| 4100 | int dont_prepend = 0; |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4101 | Token **params, *t, *mtok, *tt; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4102 | MMacro *m; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4103 | Line *l, *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4104 | int i, nparam, *paramlen; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4105 | const char *mname; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4106 | |
| 4107 | t = tline; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4108 | skip_white_(t); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4109 | /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ |
H. Peter Anvin | dce1e2f | 2002-04-30 21:06:37 +0000 | [diff] [blame] | 4110 | if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4111 | return 0; |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4112 | mtok = t; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4113 | m = is_mmacro(t, ¶ms); |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4114 | if (m) { |
| 4115 | mname = t->text; |
| 4116 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4117 | Token *last; |
| 4118 | /* |
| 4119 | * We have an id which isn't a macro call. We'll assume |
| 4120 | * it might be a label; we'll also check to see if a |
| 4121 | * colon follows it. Then, if there's another id after |
| 4122 | * that lot, we'll check it again for macro-hood. |
| 4123 | */ |
| 4124 | label = last = t; |
| 4125 | t = t->next; |
| 4126 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4127 | last = t, t = t->next; |
| 4128 | if (tok_is_(t, ":")) { |
| 4129 | dont_prepend = 1; |
| 4130 | last = t, t = t->next; |
| 4131 | if (tok_type_(t, TOK_WHITESPACE)) |
| 4132 | last = t, t = t->next; |
| 4133 | } |
| 4134 | if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, ¶ms)) == NULL) |
| 4135 | return 0; |
| 4136 | last->next = NULL; |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4137 | mname = t->text; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4138 | tline = t; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4139 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4140 | |
| 4141 | /* |
| 4142 | * Fix up the parameters: this involves stripping leading and |
| 4143 | * trailing whitespace, then stripping braces if they are |
| 4144 | * present. |
| 4145 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4146 | for (nparam = 0; params[nparam]; nparam++) ; |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4147 | paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4148 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4149 | for (i = 0; params[i]; i++) { |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4150 | int brace = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4151 | int comma = (!m->plus || i < nparam - 1); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4152 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4153 | t = params[i]; |
| 4154 | skip_white_(t); |
| 4155 | if (tok_is_(t, "{")) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4156 | t = t->next, brace = true, comma = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4157 | params[i] = t; |
| 4158 | paramlen[i] = 0; |
| 4159 | while (t) { |
| 4160 | if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) |
| 4161 | break; /* ... because we have hit a comma */ |
| 4162 | if (comma && t->type == TOK_WHITESPACE |
| 4163 | && tok_is_(t->next, ",")) |
| 4164 | break; /* ... or a space then a comma */ |
| 4165 | if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) |
| 4166 | break; /* ... or a brace */ |
| 4167 | t = t->next; |
| 4168 | paramlen[i]++; |
| 4169 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | /* |
| 4173 | * OK, we have a MMacro structure together with a set of |
| 4174 | * parameters. We must now go through the expansion and push |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4175 | * copies of each Line on to istk->expansion. Substitution of |
| 4176 | * parameter tokens and macro-local tokens doesn't get done |
| 4177 | * until the single-line macro substitution process; this is |
| 4178 | * because delaying them allows us to change the semantics |
| 4179 | * later through %rotate. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4180 | * |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4181 | * First, push an end marker on to istk->expansion, mark this |
| 4182 | * macro as in progress, and set up its invocation-specific |
| 4183 | * variables. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4184 | */ |
| 4185 | ll = nasm_malloc(sizeof(Line)); |
| 4186 | ll->next = istk->expansion; |
| 4187 | ll->finishes = m; |
| 4188 | ll->first = NULL; |
| 4189 | istk->expansion = ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4190 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4191 | m->in_progress = true; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4192 | m->params = params; |
| 4193 | m->iline = tline; |
| 4194 | m->nparam = nparam; |
| 4195 | m->rotate = 0; |
| 4196 | m->paramlen = paramlen; |
| 4197 | m->unique = unique++; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4198 | m->lineno = 0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4199 | |
| 4200 | m->next_active = istk->mstk; |
| 4201 | istk->mstk = m; |
| 4202 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4203 | for (l = m->expansion; l; l = l->next) { |
| 4204 | Token **tail; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4205 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4206 | ll = nasm_malloc(sizeof(Line)); |
| 4207 | ll->finishes = NULL; |
| 4208 | ll->next = istk->expansion; |
| 4209 | istk->expansion = ll; |
| 4210 | tail = &ll->first; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4211 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4212 | for (t = l->first; t; t = t->next) { |
| 4213 | Token *x = t; |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4214 | switch (t->type) { |
| 4215 | case TOK_PREPROC_Q: |
H. Peter Anvin | c751e86 | 2008-06-09 10:18:45 -0700 | [diff] [blame] | 4216 | tt = *tail = new_Token(NULL, TOK_ID, mname, 0); |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4217 | break; |
| 4218 | case TOK_PREPROC_QQ: |
| 4219 | tt = *tail = new_Token(NULL, TOK_ID, m->name, 0); |
| 4220 | break; |
| 4221 | case TOK_PREPROC_ID: |
| 4222 | if (t->text[1] == '0' && t->text[2] == '0') { |
| 4223 | dont_prepend = -1; |
| 4224 | x = label; |
| 4225 | if (!x) |
| 4226 | continue; |
| 4227 | } |
| 4228 | /* fall through */ |
| 4229 | default: |
| 4230 | tt = *tail = new_Token(NULL, x->type, x->text, 0); |
| 4231 | break; |
H. Peter Anvin | 166c247 | 2008-05-28 12:28:58 -0700 | [diff] [blame] | 4232 | } |
H. Peter Anvin | ce2233b | 2008-05-25 21:57:00 -0700 | [diff] [blame] | 4233 | tail = &tt->next; |
| 4234 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4235 | *tail = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4239 | * If we had a label, push it on as the first line of |
| 4240 | * the macro expansion. |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4241 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4242 | if (label) { |
| 4243 | if (dont_prepend < 0) |
| 4244 | free_tlist(startline); |
| 4245 | else { |
| 4246 | ll = nasm_malloc(sizeof(Line)); |
| 4247 | ll->finishes = NULL; |
| 4248 | ll->next = istk->expansion; |
| 4249 | istk->expansion = ll; |
| 4250 | ll->first = startline; |
| 4251 | if (!dont_prepend) { |
| 4252 | while (label->next) |
| 4253 | label = label->next; |
| 4254 | label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); |
| 4255 | } |
| 4256 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4257 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4258 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4259 | list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4260 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4261 | return 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4264 | /* The function that actually does the error reporting */ |
| 4265 | static void verror(int severity, const char *fmt, va_list arg) |
| 4266 | { |
| 4267 | char buff[1024]; |
| 4268 | |
| 4269 | vsnprintf(buff, sizeof(buff), fmt, arg); |
| 4270 | |
| 4271 | if (istk && istk->mstk && istk->mstk->name) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4272 | _error(severity, "(%s:%d) %s", istk->mstk->name, |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4273 | istk->mstk->lineno, buff); |
| 4274 | else |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4275 | _error(severity, "%s", buff); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4276 | } |
| 4277 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4278 | /* |
| 4279 | * Since preprocessor always operate only on the line that didn't |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4280 | * arrived yet, we should always use ERR_OFFBY1. |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4281 | */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4282 | static void error(int severity, const char *fmt, ...) |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4283 | { |
| 4284 | va_list arg; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4285 | |
| 4286 | /* If we're in a dead branch of IF or something like it, ignore the error */ |
H. Peter Anvin | 77ba0c6 | 2002-05-14 03:18:53 +0000 | [diff] [blame] | 4287 | if (istk && istk->conds && !emitting(istk->conds->state)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4288 | return; |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4289 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4290 | va_start(arg, fmt); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4291 | verror(severity, fmt, arg); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4292 | va_end(arg); |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4293 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4294 | |
Victor van den Elzen | 3b404c0 | 2008-09-18 13:51:36 +0200 | [diff] [blame] | 4295 | /* |
| 4296 | * Because %else etc are evaluated in the state context |
| 4297 | * of the previous branch, errors might get lost with error(): |
| 4298 | * %if 0 ... %else trailing garbage ... %endif |
| 4299 | * So %else etc should report errors with this function. |
| 4300 | */ |
| 4301 | static void error_precond(int severity, const char *fmt, ...) |
| 4302 | { |
| 4303 | va_list arg; |
| 4304 | |
| 4305 | /* Only ignore the error if it's really in a dead branch */ |
| 4306 | if (istk && istk->conds && istk->conds->state == COND_NEVER) |
| 4307 | return; |
| 4308 | |
| 4309 | va_start(arg, fmt); |
| 4310 | verror(severity, fmt, arg); |
| 4311 | va_end(arg); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4314 | static void |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4315 | pp_reset(char *file, int apass, efunc errfunc, evalfunc eval, |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4316 | ListGen * listgen, StrList **deplist) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4317 | { |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4318 | Token *t; |
| 4319 | |
H. Peter Anvin | 99941bf | 2002-05-14 17:44:03 +0000 | [diff] [blame] | 4320 | _error = errfunc; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4321 | cstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4322 | istk = nasm_malloc(sizeof(Include)); |
| 4323 | istk->next = NULL; |
| 4324 | istk->conds = NULL; |
| 4325 | istk->expansion = NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4326 | istk->mstk = NULL; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4327 | istk->fp = fopen(file, "r"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4328 | istk->fname = NULL; |
| 4329 | src_set_fname(nasm_strdup(file)); |
| 4330 | src_set_linnum(0); |
| 4331 | istk->lineinc = 1; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4332 | if (!istk->fp) |
H. Peter Anvin | 917a349 | 2008-09-24 09:14:49 -0700 | [diff] [blame] | 4333 | error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'", |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4334 | file); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4335 | defining = NULL; |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4336 | nested_mac_count = 0; |
| 4337 | nested_rep_count = 0; |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4338 | init_macros(); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4339 | unique = 0; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4340 | if (tasm_compatible_mode) { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4341 | stdmacpos = nasm_stdmac; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4342 | } else { |
H. Peter Anvin | a4835d4 | 2008-05-20 14:21:29 -0700 | [diff] [blame] | 4343 | stdmacpos = nasm_stdmac_after_tasm; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4344 | } |
H. Peter Anvin | d245659 | 2008-06-19 15:04:18 -0700 | [diff] [blame] | 4345 | any_extrastdmac = extrastdmac && *extrastdmac; |
| 4346 | do_predef = true; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4347 | list = listgen; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4348 | evaluate = eval; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4349 | |
| 4350 | /* |
| 4351 | * 0 for dependencies, 1 for preparatory passes, 2 for final pass. |
| 4352 | * The caller, however, will also pass in 3 for preprocess-only so |
| 4353 | * we can set __PASS__ accordingly. |
| 4354 | */ |
| 4355 | pass = apass > 2 ? 2 : apass; |
| 4356 | |
H. Peter Anvin | 9e1f528 | 2008-05-29 21:38:00 -0700 | [diff] [blame] | 4357 | dephead = deptail = deplist; |
| 4358 | if (deplist) { |
| 4359 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 4360 | sl->next = NULL; |
| 4361 | strcpy(sl->str, file); |
| 4362 | *deptail = sl; |
| 4363 | deptail = &sl->next; |
| 4364 | } |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4365 | |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4366 | /* |
| 4367 | * Define the __PASS__ macro. This is defined here unlike |
| 4368 | * all the other builtins, because it is special -- it varies between |
| 4369 | * passes. |
| 4370 | */ |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4371 | t = nasm_malloc(sizeof(*t)); |
| 4372 | t->next = NULL; |
H. Peter Anvin | 61f130f | 2008-09-25 15:45:06 -0700 | [diff] [blame] | 4373 | make_tok_num(t, apass); |
H. Peter Anvin | 7383b40 | 2008-09-24 10:20:40 -0700 | [diff] [blame] | 4374 | t->a.mac = NULL; |
| 4375 | define_smacro(NULL, "__PASS__", true, 0, t); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4378 | static char *pp_getline(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4379 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4380 | char *line; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4381 | Token *tline; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4382 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4383 | while (1) { |
| 4384 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4385 | * Fetch a tokenized line, either from the macro-expansion |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4386 | * buffer or from the input file. |
| 4387 | */ |
| 4388 | tline = NULL; |
| 4389 | while (istk->expansion && istk->expansion->finishes) { |
| 4390 | Line *l = istk->expansion; |
| 4391 | if (!l->finishes->name && l->finishes->in_progress > 1) { |
| 4392 | Line *ll; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4393 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4394 | /* |
| 4395 | * This is a macro-end marker for a macro with no |
| 4396 | * name, which means it's not really a macro at all |
| 4397 | * but a %rep block, and the `in_progress' field is |
| 4398 | * more than 1, meaning that we still need to |
| 4399 | * repeat. (1 means the natural last repetition; 0 |
| 4400 | * means termination by %exitrep.) We have |
| 4401 | * therefore expanded up to the %endrep, and must |
| 4402 | * push the whole block on to the expansion buffer |
| 4403 | * again. We don't bother to remove the macro-end |
| 4404 | * marker: we'd only have to generate another one |
| 4405 | * if we did. |
| 4406 | */ |
| 4407 | l->finishes->in_progress--; |
| 4408 | for (l = l->finishes->expansion; l; l = l->next) { |
| 4409 | Token *t, *tt, **tail; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4410 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4411 | ll = nasm_malloc(sizeof(Line)); |
| 4412 | ll->next = istk->expansion; |
| 4413 | ll->finishes = NULL; |
| 4414 | ll->first = NULL; |
| 4415 | tail = &ll->first; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4416 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4417 | for (t = l->first; t; t = t->next) { |
| 4418 | if (t->text || t->type == TOK_WHITESPACE) { |
| 4419 | tt = *tail = |
| 4420 | new_Token(NULL, t->type, t->text, 0); |
| 4421 | tail = &tt->next; |
| 4422 | } |
| 4423 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4424 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4425 | istk->expansion = ll; |
| 4426 | } |
| 4427 | } else { |
| 4428 | /* |
| 4429 | * Check whether a `%rep' was started and not ended |
| 4430 | * within this macro expansion. This can happen and |
| 4431 | * should be detected. It's a fatal error because |
| 4432 | * I'm too confused to work out how to recover |
| 4433 | * sensibly from it. |
| 4434 | */ |
| 4435 | if (defining) { |
| 4436 | if (defining->name) |
| 4437 | error(ERR_PANIC, |
| 4438 | "defining with name in expansion"); |
| 4439 | else if (istk->mstk->name) |
| 4440 | error(ERR_FATAL, |
| 4441 | "`%%rep' without `%%endrep' within" |
| 4442 | " expansion of macro `%s'", |
| 4443 | istk->mstk->name); |
| 4444 | } |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 4445 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4446 | /* |
| 4447 | * FIXME: investigate the relationship at this point between |
| 4448 | * istk->mstk and l->finishes |
| 4449 | */ |
| 4450 | { |
| 4451 | MMacro *m = istk->mstk; |
| 4452 | istk->mstk = m->next_active; |
| 4453 | if (m->name) { |
| 4454 | /* |
| 4455 | * This was a real macro call, not a %rep, and |
| 4456 | * therefore the parameter information needs to |
| 4457 | * be freed. |
| 4458 | */ |
| 4459 | nasm_free(m->params); |
| 4460 | free_tlist(m->iline); |
| 4461 | nasm_free(m->paramlen); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4462 | l->finishes->in_progress = false; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4463 | } else |
| 4464 | free_mmacro(m); |
| 4465 | } |
| 4466 | istk->expansion = l->next; |
| 4467 | nasm_free(l); |
| 4468 | list->downlevel(LIST_MACRO); |
| 4469 | } |
| 4470 | } |
| 4471 | while (1) { /* until we get a line we can use */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4472 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4473 | if (istk->expansion) { /* from a macro expansion */ |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4474 | char *p; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4475 | Line *l = istk->expansion; |
| 4476 | if (istk->mstk) |
| 4477 | istk->mstk->lineno++; |
| 4478 | tline = l->first; |
| 4479 | istk->expansion = l->next; |
| 4480 | nasm_free(l); |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4481 | p = detoken(tline, false); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4482 | list->line(LIST_MACRO, p); |
| 4483 | nasm_free(p); |
| 4484 | break; |
| 4485 | } |
| 4486 | line = read_line(); |
| 4487 | if (line) { /* from the current input file */ |
| 4488 | line = prepreproc(line); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4489 | tline = tokenize(line); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4490 | nasm_free(line); |
| 4491 | break; |
| 4492 | } |
| 4493 | /* |
| 4494 | * The current file has ended; work down the istk |
| 4495 | */ |
| 4496 | { |
| 4497 | Include *i = istk; |
| 4498 | fclose(i->fp); |
| 4499 | if (i->conds) |
| 4500 | error(ERR_FATAL, |
| 4501 | "expected `%%endif' before end of file"); |
| 4502 | /* only set line and file name if there's a next node */ |
| 4503 | if (i->next) { |
| 4504 | src_set_linnum(i->lineno); |
| 4505 | nasm_free(src_set_fname(i->fname)); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 4506 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4507 | istk = i->next; |
| 4508 | list->downlevel(LIST_INCLUDE); |
| 4509 | nasm_free(i); |
| 4510 | if (!istk) |
| 4511 | return NULL; |
Victor van den Elzen | 4c9d622 | 2008-10-01 13:08:50 +0200 | [diff] [blame] | 4512 | if (istk->expansion && istk->expansion->finishes) |
| 4513 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4514 | } |
| 4515 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4516 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4517 | /* |
| 4518 | * We must expand MMacro parameters and MMacro-local labels |
| 4519 | * _before_ we plunge into directive processing, to cope |
| 4520 | * with things like `%define something %1' such as STRUC |
| 4521 | * uses. Unless we're _defining_ a MMacro, in which case |
| 4522 | * those tokens should be left alone to go into the |
| 4523 | * definition; and unless we're in a non-emitting |
| 4524 | * condition, in which case we don't want to meddle with |
| 4525 | * anything. |
| 4526 | */ |
Charles Crayne | d4200be | 2008-07-12 16:42:33 -0700 | [diff] [blame] | 4527 | if (!defining && !(istk->conds && !emitting(istk->conds->state)) |
H. Peter Anvin | 992fe75 | 2008-10-19 15:45:05 -0700 | [diff] [blame] | 4528 | && !(istk->mstk && !istk->mstk->in_progress)) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4529 | tline = expand_mmac_params(tline); |
H. Peter Anvin | 992fe75 | 2008-10-19 15:45:05 -0700 | [diff] [blame] | 4530 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4531 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4532 | /* |
| 4533 | * Check the line to see if it's a preprocessor directive. |
| 4534 | */ |
| 4535 | if (do_directive(tline) == DIRECTIVE_FOUND) { |
| 4536 | continue; |
| 4537 | } else if (defining) { |
| 4538 | /* |
| 4539 | * We're defining a multi-line macro. We emit nothing |
| 4540 | * at all, and just |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4541 | * shove the tokenized line on to the macro definition. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4542 | */ |
| 4543 | Line *l = nasm_malloc(sizeof(Line)); |
| 4544 | l->next = defining->expansion; |
| 4545 | l->first = tline; |
H. Peter Anvin | 538002d | 2008-06-28 18:30:27 -0700 | [diff] [blame] | 4546 | l->finishes = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4547 | defining->expansion = l; |
| 4548 | continue; |
| 4549 | } else if (istk->conds && !emitting(istk->conds->state)) { |
| 4550 | /* |
| 4551 | * We're in a non-emitting branch of a condition block. |
| 4552 | * Emit nothing at all, not even a blank line: when we |
| 4553 | * emerge from the condition we'll give a line-number |
| 4554 | * directive so we keep our place correctly. |
| 4555 | */ |
| 4556 | free_tlist(tline); |
| 4557 | continue; |
| 4558 | } else if (istk->mstk && !istk->mstk->in_progress) { |
| 4559 | /* |
| 4560 | * We're in a %rep block which has been terminated, so |
| 4561 | * we're walking through to the %endrep without |
| 4562 | * emitting anything. Emit nothing at all, not even a |
| 4563 | * blank line: when we emerge from the %rep block we'll |
| 4564 | * give a line-number directive so we keep our place |
| 4565 | * correctly. |
| 4566 | */ |
| 4567 | free_tlist(tline); |
| 4568 | continue; |
| 4569 | } else { |
| 4570 | tline = expand_smacro(tline); |
| 4571 | if (!expand_mmacro(tline)) { |
| 4572 | /* |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4573 | * De-tokenize the line again, and emit it. |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4574 | */ |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 4575 | line = detoken(tline, true); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4576 | free_tlist(tline); |
| 4577 | break; |
| 4578 | } else { |
| 4579 | continue; /* expand_mmacro calls free_tlist */ |
| 4580 | } |
| 4581 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4584 | return line; |
| 4585 | } |
| 4586 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4587 | static void pp_cleanup(int pass) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4588 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4589 | if (defining) { |
Victor van den Elzen | 8f1120f | 2008-07-16 13:41:37 +0200 | [diff] [blame] | 4590 | if(defining->name) { |
| 4591 | error(ERR_NONFATAL, |
| 4592 | "end of file while still defining macro `%s'", |
| 4593 | defining->name); |
| 4594 | } else { |
| 4595 | error(ERR_NONFATAL, "end of file while still in %%rep"); |
| 4596 | } |
| 4597 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4598 | free_mmacro(defining); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4599 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4600 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4601 | ctx_pop(); |
H. Peter Anvin | 97a2347 | 2007-09-16 17:57:25 -0700 | [diff] [blame] | 4602 | free_macros(); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4603 | while (istk) { |
| 4604 | Include *i = istk; |
| 4605 | istk = istk->next; |
| 4606 | fclose(i->fp); |
| 4607 | nasm_free(i->fname); |
| 4608 | nasm_free(i); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4609 | } |
| 4610 | while (cstk) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4611 | ctx_pop(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 4612 | nasm_free(src_set_fname(NULL)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4613 | if (pass == 0) { |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 4614 | IncPath *i; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4615 | free_llist(predef); |
| 4616 | delete_Blocks(); |
H. Peter Anvin | 86877b2 | 2008-06-20 15:55:45 -0700 | [diff] [blame] | 4617 | while ((i = ipath)) { |
| 4618 | ipath = i->next; |
| 4619 | if (i->path) |
| 4620 | nasm_free(i->path); |
| 4621 | nasm_free(i); |
| 4622 | } |
H. Peter Anvin | 11dfa1a | 2008-07-02 18:11:04 -0700 | [diff] [blame] | 4623 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4626 | void pp_include_path(char *path) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4627 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4628 | IncPath *i; |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 4629 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4630 | i = nasm_malloc(sizeof(IncPath)); |
H. Peter Anvin | 37a321f | 2007-09-24 13:41:58 -0700 | [diff] [blame] | 4631 | i->path = path ? nasm_strdup(path) : NULL; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 4632 | i->next = NULL; |
| 4633 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4634 | if (ipath != NULL) { |
| 4635 | IncPath *j = ipath; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 4636 | while (j->next != NULL) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4637 | j = j->next; |
| 4638 | j->next = i; |
| 4639 | } else { |
| 4640 | ipath = i; |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 4641 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4642 | } |
Frank Kotler | d0ed6fd | 2003-08-27 11:33:56 +0000 | [diff] [blame] | 4643 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4644 | void pp_pre_include(char *fname) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4645 | { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4646 | Token *inc, *space, *name; |
| 4647 | Line *l; |
| 4648 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4649 | name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0); |
| 4650 | space = new_Token(name, TOK_WHITESPACE, NULL, 0); |
| 4651 | inc = new_Token(space, TOK_PREPROC_ID, "%include", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4652 | |
| 4653 | l = nasm_malloc(sizeof(Line)); |
| 4654 | l->next = predef; |
| 4655 | l->first = inc; |
H. Peter Anvin | 538002d | 2008-06-28 18:30:27 -0700 | [diff] [blame] | 4656 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4657 | predef = l; |
| 4658 | } |
| 4659 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4660 | void pp_pre_define(char *definition) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4661 | { |
| 4662 | Token *def, *space; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4663 | Line *l; |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4664 | char *equals; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4665 | |
| 4666 | equals = strchr(definition, '='); |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4667 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 4668 | def = new_Token(space, TOK_PREPROC_ID, "%define", 0); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4669 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4670 | *equals = ' '; |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4671 | space->next = tokenize(definition); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4672 | if (equals) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 4673 | *equals = '='; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4674 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4675 | l = nasm_malloc(sizeof(Line)); |
| 4676 | l->next = predef; |
| 4677 | l->first = def; |
H. Peter Anvin | 538002d | 2008-06-28 18:30:27 -0700 | [diff] [blame] | 4678 | l->finishes = NULL; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 4679 | predef = l; |
| 4680 | } |
| 4681 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4682 | void pp_pre_undefine(char *definition) |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 4683 | { |
| 4684 | Token *def, *space; |
| 4685 | Line *l; |
| 4686 | |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 4687 | space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); |
| 4688 | def = new_Token(space, TOK_PREPROC_ID, "%undef", 0); |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4689 | space->next = tokenize(definition); |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 4690 | |
| 4691 | l = nasm_malloc(sizeof(Line)); |
| 4692 | l->next = predef; |
| 4693 | l->first = def; |
H. Peter Anvin | 538002d | 2008-06-28 18:30:27 -0700 | [diff] [blame] | 4694 | l->finishes = NULL; |
H. Peter Anvin | 620515a | 2002-04-30 20:57:38 +0000 | [diff] [blame] | 4695 | predef = l; |
| 4696 | } |
| 4697 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4698 | /* |
| 4699 | * Added by Keith Kanios: |
| 4700 | * |
| 4701 | * This function is used to assist with "runtime" preprocessor |
| 4702 | * directives. (e.g. pp_runtime("%define __BITS__ 64");) |
| 4703 | * |
| 4704 | * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU |
| 4705 | * PASS A VALID STRING TO THIS FUNCTION!!!!! |
| 4706 | */ |
| 4707 | |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4708 | void pp_runtime(char *definition) |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4709 | { |
| 4710 | Token *def; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4711 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4712 | def = tokenize(definition); |
| 4713 | if(do_directive(def) == NO_DIRECTIVE_FOUND) |
| 4714 | free_tlist(def); |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 4715 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
H. Peter Anvin | a70547f | 2008-07-19 21:44:26 -0700 | [diff] [blame] | 4718 | void pp_extra_stdmac(macros_t *macros) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4719 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 4720 | extrastdmac = macros; |
| 4721 | } |
| 4722 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 4723 | static void make_tok_num(Token * tok, int64_t val) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4724 | { |
Keith Kanios | a6dfa78 | 2007-04-13 16:47:53 +0000 | [diff] [blame] | 4725 | char numbuf[20]; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 4726 | snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 4727 | tok->text = nasm_strdup(numbuf); |
| 4728 | tok->type = TOK_NUMBER; |
| 4729 | } |
| 4730 | |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 4731 | Preproc nasmpp = { |
| 4732 | pp_reset, |
| 4733 | pp_getline, |
| 4734 | pp_cleanup |
| 4735 | }; |