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