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