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