H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 1 | /* parser.c source line parser for the Netwide Assembler |
| 2 | * |
| 3 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and |
| 4 | * Julian Hall. All rights reserved. The software is |
| 5 | * redistributable under the licence given in the file "Licence" |
| 6 | * distributed in the NASM archive. |
| 7 | * |
| 8 | * initial version 27/iii/95 by Simon Tatham |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <stddef.h> |
| 14 | #include <string.h> |
| 15 | #include <ctype.h> |
| 16 | |
| 17 | #include "nasm.h" |
| 18 | #include "nasmlib.h" |
| 19 | #include "parser.h" |
| 20 | #include "float.h" |
| 21 | |
H. Peter Anvin | d0e365d | 2002-05-26 18:19:19 +0000 | [diff] [blame] | 22 | extern int in_abs_seg; /* ABSOLUTE segment flag */ |
| 23 | extern long abs_seg; /* ABSOLUTE segment */ |
| 24 | extern long abs_offset; /* ABSOLUTE segment offset */ |
| 25 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 26 | static long reg_flags[] = { /* sizes and special flags */ |
| 27 | 0, REG8, REG_AL, REG_AX, REG8, REG8, REG16, REG16, REG8, REG_CL, |
| 28 | REG_CREG, REG_CREG, REG_CREG, REG_CR4, REG_CS, REG_CX, REG8, |
| 29 | REG16, REG8, REG_DREG, REG_DREG, REG_DREG, REG_DREG, REG_DREG, |
| 30 | REG_DREG, REG_DESS, REG_DX, REG_EAX, REG32, REG32, REG_ECX, |
| 31 | REG32, REG32, REG_DESS, REG32, REG32, REG_FSGS, REG_FSGS, |
| 32 | MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, |
| 33 | REG16, REG16, REG_DESS, FPU0, FPUREG, FPUREG, FPUREG, FPUREG, |
| 34 | FPUREG, FPUREG, FPUREG, REG_TREG, REG_TREG, REG_TREG, REG_TREG, |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 35 | REG_TREG, |
| 36 | XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | enum { /* special tokens */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 40 | S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD, |
H. Peter Anvin | 01377d8 | 2002-05-21 03:16:33 +0000 | [diff] [blame] | 41 | S_SHORT, S_STRICT, S_TO, S_TWORD, S_WORD |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 44 | static int is_comma_next (void); |
| 45 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 46 | static int i; |
| 47 | static struct tokenval tokval; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 48 | static efunc error; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 49 | static struct ofmt *outfmt; /* Structure of addresses of output routines */ |
| 50 | static loc_t *location; /* Pointer to current line's segment,offset */ |
| 51 | |
| 52 | void parser_global_info (struct ofmt *output, loc_t *locp) |
| 53 | { |
| 54 | outfmt = output; |
| 55 | location = locp; |
| 56 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 57 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 58 | insn *parse_line (int pass, char *buffer, insn *result, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 59 | efunc errfunc, evalfunc evaluate, ldfunc ldef) |
| 60 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 61 | int operand; |
| 62 | int critical; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 63 | struct eval_hints hints; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 64 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 65 | result->forw_ref = FALSE; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 66 | error = errfunc; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 67 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 68 | stdscan_reset(); |
| 69 | stdscan_bufptr = buffer; |
| 70 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 71 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 72 | result->label = NULL; /* Assume no label */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 73 | result->eops = NULL; /* must do this, whatever happens */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 74 | result->operands = 0; /* must initialise this */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 75 | |
| 76 | if (i==0) { /* blank line - ignore */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 77 | result->opcode = -1; /* and no instruction either */ |
| 78 | return result; |
| 79 | } |
| 80 | if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX && |
| 81 | (i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) { |
| 82 | error (ERR_NONFATAL, "label or instruction expected" |
| 83 | " at start of line"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 84 | result->opcode = -1; |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | if (i == TOKEN_ID) { /* there's a label here */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 89 | result->label = tokval.t_charptr; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 90 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 91 | if (i == ':') { /* skip over the optional colon */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 92 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 93 | } else if (i == 0) { |
| 94 | error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 95 | "label alone on a line without a colon might be in error"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 96 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 97 | if (i != TOKEN_INSN || tokval.t_integer != I_EQU) |
| 98 | { |
| 99 | /* |
| 100 | * FIXME: location->segment could be NO_SEG, in which case |
| 101 | * it is possible we should be passing 'abs_seg'. Look into this. |
| 102 | * Work out whether that is *really* what we should be doing. |
| 103 | * Generally fix things. I think this is right as it is, but |
| 104 | * am still not certain. |
| 105 | */ |
H. Peter Anvin | d0e365d | 2002-05-26 18:19:19 +0000 | [diff] [blame] | 106 | ldef (result->label, in_abs_seg?abs_seg:location->segment, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 107 | location->offset, NULL, TRUE, FALSE, outfmt, errfunc); |
| 108 | } |
| 109 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 110 | |
| 111 | if (i==0) { |
| 112 | result->opcode = -1; /* this line contains just a label */ |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | result->nprefix = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 117 | result->times = 1L; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 118 | |
| 119 | while (i == TOKEN_PREFIX || |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 120 | (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer]))) |
| 121 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 122 | /* |
| 123 | * Handle special case: the TIMES prefix. |
| 124 | */ |
| 125 | if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) { |
| 126 | expr *value; |
| 127 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 128 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | 8ac3641 | 2002-04-30 21:09:12 +0000 | [diff] [blame] | 129 | value = evaluate (stdscan, NULL, &tokval, NULL, pass0, error, NULL); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 130 | i = tokval.t_type; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 131 | if (!value) { /* but, error in evaluator */ |
| 132 | result->opcode = -1; /* unrecoverable parse error: */ |
| 133 | return result; /* ignore this instruction */ |
| 134 | } |
| 135 | if (!is_simple (value)) { |
| 136 | error (ERR_NONFATAL, |
| 137 | "non-constant argument supplied to TIMES"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 138 | result->times = 1L; |
| 139 | } else { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 140 | result->times = value->value; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 141 | if (value->value < 0) { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 142 | error(ERR_NONFATAL, "TIMES value %d is negative", |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 143 | value->value); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 144 | result->times = 0; |
| 145 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 146 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 147 | } else { |
| 148 | if (result->nprefix == MAXPREFIX) |
| 149 | error (ERR_NONFATAL, |
| 150 | "instruction has more than %d prefixes", MAXPREFIX); |
| 151 | else |
| 152 | result->prefixes[result->nprefix++] = tokval.t_integer; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 153 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | if (i != TOKEN_INSN) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 158 | if (result->nprefix > 0 && i == 0) { |
| 159 | /* |
| 160 | * Instruction prefixes are present, but no actual |
| 161 | * instruction. This is allowed: at this point we |
| 162 | * invent a notional instruction of RESB 0. |
| 163 | */ |
| 164 | result->opcode = I_RESB; |
| 165 | result->operands = 1; |
| 166 | result->oprs[0].type = IMMEDIATE; |
| 167 | result->oprs[0].offset = 0L; |
| 168 | result->oprs[0].segment = result->oprs[0].wrt = NO_SEG; |
| 169 | return result; |
| 170 | } else { |
| 171 | error (ERR_NONFATAL, "parser: instruction expected"); |
| 172 | result->opcode = -1; |
| 173 | return result; |
| 174 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | result->opcode = tokval.t_integer; |
| 178 | result->condition = tokval.t_inttwo; |
| 179 | |
| 180 | /* |
| 181 | * RESB, RESW and RESD cannot be satisfied with incorrectly |
| 182 | * evaluated operands, since the correct values _must_ be known |
| 183 | * on the first pass. Hence, even in pass one, we set the |
| 184 | * `critical' flag on calling evaluate(), so that it will bomb |
| 185 | * out on undefined symbols. Nasty, but there's nothing we can |
| 186 | * do about it. |
| 187 | * |
| 188 | * For the moment, EQU has the same difficulty, so we'll |
| 189 | * include that. |
| 190 | */ |
| 191 | if (result->opcode == I_RESB || |
| 192 | result->opcode == I_RESW || |
| 193 | result->opcode == I_RESD || |
| 194 | result->opcode == I_RESQ || |
| 195 | result->opcode == I_REST || |
H. Peter Anvin | 8ac3641 | 2002-04-30 21:09:12 +0000 | [diff] [blame] | 196 | result->opcode == I_EQU || |
| 197 | result->opcode == I_INCBIN) /* fbk */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 198 | { |
H. Peter Anvin | 8ac3641 | 2002-04-30 21:09:12 +0000 | [diff] [blame] | 199 | critical = pass0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 200 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 201 | else |
| 202 | critical = (pass==2 ? 2 : 0); |
| 203 | |
| 204 | if (result->opcode == I_DB || |
| 205 | result->opcode == I_DW || |
| 206 | result->opcode == I_DD || |
| 207 | result->opcode == I_DQ || |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 208 | result->opcode == I_DT || |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 209 | result->opcode == I_INCBIN) |
| 210 | { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 211 | extop *eop, **tail = &result->eops, **fixptr; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 212 | int oper_num = 0; |
| 213 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 214 | result->eops_float = FALSE; |
| 215 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 216 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 217 | * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands. |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 218 | */ |
| 219 | while (1) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 220 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 221 | if (i == 0) |
| 222 | break; |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 223 | fixptr = tail; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 224 | eop = *tail = nasm_malloc(sizeof(extop)); |
| 225 | tail = &eop->next; |
| 226 | eop->next = NULL; |
| 227 | eop->type = EOT_NOTHING; |
| 228 | oper_num++; |
| 229 | |
| 230 | if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) { |
| 231 | eop->type = EOT_DB_STRING; |
| 232 | eop->stringval = tokval.t_charptr; |
| 233 | eop->stringlen = tokval.t_inttwo; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 234 | i = stdscan(NULL, &tokval); /* eat the comma */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 235 | continue; |
| 236 | } |
| 237 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 238 | if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 239 | long sign = +1L; |
| 240 | |
| 241 | if (i == '-') { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 242 | char *save = stdscan_bufptr; |
| 243 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 244 | sign = -1L; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 245 | if (i != TOKEN_FLOAT || !is_comma_next()) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 246 | stdscan_bufptr = save; |
| 247 | i = tokval.t_type = '-'; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | if (i == TOKEN_FLOAT) { |
| 252 | eop->type = EOT_DB_STRING; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 253 | result->eops_float = TRUE; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 254 | if (result->opcode == I_DD) |
| 255 | eop->stringlen = 4; |
| 256 | else if (result->opcode == I_DQ) |
| 257 | eop->stringlen = 8; |
| 258 | else if (result->opcode == I_DT) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 259 | eop->stringlen = 10; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 260 | else { |
| 261 | error(ERR_NONFATAL, "floating-point constant" |
| 262 | " encountered in `D%c' instruction", |
| 263 | result->opcode == I_DW ? 'W' : 'B'); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 264 | /* |
| 265 | * fix suggested by Pedro Gimeno... original line |
| 266 | * was: |
| 267 | * eop->type = EOT_NOTHING; |
| 268 | */ |
| 269 | eop->stringlen = 0; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 270 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 271 | eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen); |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 272 | tail = &eop->next; |
| 273 | *fixptr = eop; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 274 | eop->stringval = (char *)eop + sizeof(extop); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 275 | if (eop->stringlen < 4 || |
| 276 | !float_const (tokval.t_charptr, sign, |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 277 | (unsigned char *)eop->stringval, |
| 278 | eop->stringlen, error)) |
| 279 | eop->type = EOT_NOTHING; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 280 | i = stdscan(NULL, &tokval); /* eat the comma */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 281 | continue; |
| 282 | } |
| 283 | } |
| 284 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 285 | /* anything else */ |
| 286 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 287 | expr *value; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 288 | value = evaluate (stdscan, NULL, &tokval, NULL, |
| 289 | critical, error, NULL); |
| 290 | i = tokval.t_type; |
| 291 | if (!value) { /* error in evaluator */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 292 | result->opcode = -1;/* unrecoverable parse error: */ |
| 293 | return result; /* ignore this instruction */ |
| 294 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 295 | if (is_unknown(value)) { |
| 296 | eop->type = EOT_DB_NUMBER; |
| 297 | eop->offset = 0; /* doesn't matter what we put */ |
| 298 | eop->segment = eop->wrt = NO_SEG; /* likewise */ |
| 299 | } else if (is_reloc(value)) { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 300 | eop->type = EOT_DB_NUMBER; |
| 301 | eop->offset = reloc_value(value); |
| 302 | eop->segment = reloc_seg(value); |
| 303 | eop->wrt = reloc_wrt(value); |
| 304 | } else { |
| 305 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 306 | "operand %d: expression is not simple" |
| 307 | " or relocatable", oper_num); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 310 | |
| 311 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 312 | * We're about to call stdscan(), which will eat the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 313 | * comma that we're currently sitting on between |
| 314 | * arguments. However, we'd better check first that it |
| 315 | * _is_ a comma. |
| 316 | */ |
| 317 | if (i == 0) /* also could be EOL */ |
| 318 | break; |
| 319 | if (i != ',') { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 320 | error (ERR_NONFATAL, "comma expected after operand %d", |
| 321 | oper_num); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 322 | result->opcode = -1;/* unrecoverable parse error: */ |
| 323 | return result; /* ignore this instruction */ |
| 324 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 325 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 326 | |
| 327 | if (result->opcode == I_INCBIN) { |
| 328 | /* |
| 329 | * Correct syntax for INCBIN is that there should be |
| 330 | * one string operand, followed by one or two numeric |
| 331 | * operands. |
| 332 | */ |
| 333 | if (!result->eops || result->eops->type != EOT_DB_STRING) |
| 334 | error (ERR_NONFATAL, "`incbin' expects a file name"); |
| 335 | else if (result->eops->next && |
| 336 | result->eops->next->type != EOT_DB_NUMBER) |
| 337 | error (ERR_NONFATAL, "`incbin': second parameter is", |
| 338 | " non-numeric"); |
| 339 | else if (result->eops->next && result->eops->next->next && |
| 340 | result->eops->next->next->type != EOT_DB_NUMBER) |
| 341 | error (ERR_NONFATAL, "`incbin': third parameter is", |
| 342 | " non-numeric"); |
| 343 | else if (result->eops->next && result->eops->next->next && |
| 344 | result->eops->next->next->next) |
| 345 | error (ERR_NONFATAL, "`incbin': more than three parameters"); |
| 346 | else |
| 347 | return result; |
| 348 | /* |
| 349 | * If we reach here, one of the above errors happened. |
| 350 | * Throw the instruction away. |
| 351 | */ |
| 352 | result->opcode = -1; |
| 353 | return result; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 354 | } else /* DB ... */ |
| 355 | if (oper_num == 0) |
| 356 | error (ERR_WARNING|ERR_PASS1, |
| 357 | "no operand for data declaration"); |
| 358 | else |
| 359 | result->operands = oper_num; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 360 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 361 | return result; |
| 362 | } |
| 363 | |
| 364 | /* right. Now we begin to parse the operands. There may be up to three |
| 365 | * of these, separated by commas, and terminated by a zero token. */ |
| 366 | |
| 367 | for (operand = 0; operand < 3; operand++) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 368 | expr *value; /* used most of the time */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 369 | int mref; /* is this going to be a memory ref? */ |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 370 | int bracket; /* is it a [] mref, or a & mref? */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 371 | int setsize = 0; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 372 | |
| 373 | result->oprs[operand].addr_size = 0;/* have to zero this whatever */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 374 | result->oprs[operand].eaflags = 0; /* and this */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 375 | result->oprs[operand].opflags = 0; |
| 376 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 377 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 378 | if (i == 0) break; /* end of operands: get out of here */ |
| 379 | result->oprs[operand].type = 0; /* so far, no override */ |
| 380 | while (i == TOKEN_SPECIAL) {/* size specifiers */ |
| 381 | switch ((int)tokval.t_integer) { |
| 382 | case S_BYTE: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 383 | if (!setsize) /* we want to use only the first */ |
| 384 | result->oprs[operand].type |= BITS8; |
| 385 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 386 | break; |
| 387 | case S_WORD: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 388 | if (!setsize) |
| 389 | result->oprs[operand].type |= BITS16; |
| 390 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 391 | break; |
| 392 | case S_DWORD: |
| 393 | case S_LONG: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 394 | if (!setsize) |
| 395 | result->oprs[operand].type |= BITS32; |
| 396 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 397 | break; |
| 398 | case S_QWORD: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 399 | if (!setsize) |
| 400 | result->oprs[operand].type |= BITS64; |
| 401 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 402 | break; |
| 403 | case S_TWORD: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 404 | if (!setsize) |
| 405 | result->oprs[operand].type |= BITS80; |
| 406 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 407 | break; |
| 408 | case S_TO: |
| 409 | result->oprs[operand].type |= TO; |
| 410 | break; |
H. Peter Anvin | 01377d8 | 2002-05-21 03:16:33 +0000 | [diff] [blame] | 411 | case S_STRICT: |
| 412 | result->oprs[operand].type |= STRICT; |
| 413 | break; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 414 | case S_FAR: |
| 415 | result->oprs[operand].type |= FAR; |
| 416 | break; |
| 417 | case S_NEAR: |
| 418 | result->oprs[operand].type |= NEAR; |
| 419 | break; |
| 420 | case S_SHORT: |
| 421 | result->oprs[operand].type |= SHORT; |
| 422 | break; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 423 | default: |
| 424 | error (ERR_NONFATAL, "invalid operand size specification"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 425 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 426 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 427 | } |
| 428 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 429 | if (i == '[' || i == '&') { /* memory reference */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 430 | mref = TRUE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 431 | bracket = (i == '['); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 432 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 433 | if (i == TOKEN_SPECIAL) { /* check for address size override */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 434 | if (tasm_compatible_mode) { |
| 435 | switch ((int)tokval.t_integer) { |
| 436 | /* For TASM compatibility a size override inside the |
| 437 | * brackets changes the size of the operand, not the |
| 438 | * address type of the operand as it does in standard |
| 439 | * NASM syntax. Hence: |
| 440 | * |
| 441 | * mov eax,[DWORD val] |
| 442 | * |
| 443 | * is valid syntax in TASM compatibility mode. Note that |
| 444 | * you lose the ability to override the default address |
| 445 | * type for the instruction, but we never use anything |
| 446 | * but 32-bit flat model addressing in our code. |
| 447 | */ |
| 448 | case S_BYTE: |
| 449 | result->oprs[operand].type |= BITS8; |
| 450 | break; |
| 451 | case S_WORD: |
| 452 | result->oprs[operand].type |= BITS16; |
| 453 | break; |
| 454 | case S_DWORD: |
| 455 | case S_LONG: |
| 456 | result->oprs[operand].type |= BITS32; |
| 457 | break; |
| 458 | case S_QWORD: |
| 459 | result->oprs[operand].type |= BITS64; |
| 460 | break; |
| 461 | case S_TWORD: |
| 462 | result->oprs[operand].type |= BITS80; |
| 463 | break; |
| 464 | default: |
| 465 | error (ERR_NONFATAL, "invalid operand size specification"); |
| 466 | } |
| 467 | } else { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 468 | /* Standard NASM compatible syntax */ |
| 469 | switch ((int)tokval.t_integer) { |
| 470 | case S_NOSPLIT: |
| 471 | result->oprs[operand].eaflags |= EAF_TIMESTWO; |
| 472 | break; |
| 473 | case S_BYTE: |
| 474 | result->oprs[operand].eaflags |= EAF_BYTEOFFS; |
| 475 | break; |
| 476 | case S_WORD: |
| 477 | result->oprs[operand].addr_size = 16; |
| 478 | result->oprs[operand].eaflags |= EAF_WORDOFFS; |
| 479 | break; |
| 480 | case S_DWORD: |
| 481 | case S_LONG: |
| 482 | result->oprs[operand].addr_size = 32; |
| 483 | result->oprs[operand].eaflags |= EAF_WORDOFFS; |
| 484 | break; |
| 485 | default: |
| 486 | error (ERR_NONFATAL, "invalid size specification in" |
| 487 | " effective address"); |
| 488 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 489 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 490 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 491 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 492 | } else { /* immediate operand, or register */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 493 | mref = FALSE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 494 | bracket = FALSE; /* placate optimisers */ |
| 495 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 496 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 497 | value = evaluate (stdscan, NULL, &tokval, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 498 | &result->oprs[operand].opflags, |
| 499 | critical, error, &hints); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 500 | i = tokval.t_type; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 501 | if (result->oprs[operand].opflags & OPFLAG_FORWARD) { |
| 502 | result->forw_ref = TRUE; |
| 503 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 504 | if (!value) { /* error in evaluator */ |
| 505 | result->opcode = -1; /* unrecoverable parse error: */ |
| 506 | return result; /* ignore this instruction */ |
| 507 | } |
| 508 | if (i == ':' && mref) { /* it was seg:offset */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 509 | /* |
| 510 | * Process the segment override. |
| 511 | */ |
| 512 | if (value[1].type!=0 || value->value!=1 || |
| 513 | REG_SREG & ~reg_flags[value->type]) |
| 514 | error (ERR_NONFATAL, "invalid segment override"); |
| 515 | else if (result->nprefix == MAXPREFIX) |
| 516 | error (ERR_NONFATAL, |
| 517 | "instruction has more than %d prefixes", |
| 518 | MAXPREFIX); |
| 519 | else |
| 520 | result->prefixes[result->nprefix++] = value->type; |
| 521 | |
| 522 | i = stdscan(NULL, &tokval); /* then skip the colon */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 523 | if (i == TOKEN_SPECIAL) { /* another check for size override */ |
| 524 | switch ((int)tokval.t_integer) { |
| 525 | case S_WORD: |
| 526 | result->oprs[operand].addr_size = 16; |
| 527 | break; |
| 528 | case S_DWORD: |
| 529 | case S_LONG: |
| 530 | result->oprs[operand].addr_size = 32; |
| 531 | break; |
| 532 | default: |
| 533 | error (ERR_NONFATAL, "invalid size specification in" |
| 534 | " effective address"); |
| 535 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 536 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 537 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 538 | value = evaluate (stdscan, NULL, &tokval, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 539 | &result->oprs[operand].opflags, |
| 540 | critical, error, &hints); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 541 | i = tokval.t_type; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 542 | if (result->oprs[operand].opflags & OPFLAG_FORWARD) { |
| 543 | result->forw_ref = TRUE; |
| 544 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 545 | /* and get the offset */ |
| 546 | if (!value) { /* but, error in evaluator */ |
| 547 | result->opcode = -1; /* unrecoverable parse error: */ |
| 548 | return result; /* ignore this instruction */ |
| 549 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 550 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 551 | if (mref && bracket) { /* find ] at the end */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 552 | if (i != ']') { |
| 553 | error (ERR_NONFATAL, "parser: expecting ]"); |
| 554 | do { /* error recovery again */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 555 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 556 | } while (i != 0 && i != ','); |
| 557 | } else /* we got the required ] */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 558 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 559 | } else { /* immediate operand */ |
| 560 | if (i != 0 && i != ',' && i != ':') { |
| 561 | error (ERR_NONFATAL, "comma or end of line expected"); |
| 562 | do { /* error recovery */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 563 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 564 | } while (i != 0 && i != ','); |
| 565 | } else if (i == ':') { |
| 566 | result->oprs[operand].type |= COLON; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* now convert the exprs returned from evaluate() into operand |
| 571 | * descriptions... */ |
| 572 | |
| 573 | if (mref) { /* it's a memory reference */ |
| 574 | expr *e = value; |
| 575 | int b, i, s; /* basereg, indexreg, scale */ |
| 576 | long o; /* offset */ |
| 577 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 578 | b = i = -1, o = s = 0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 579 | result->oprs[operand].hintbase = hints.base; |
| 580 | result->oprs[operand].hinttype = hints.type; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 581 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 582 | if (e->type <= EXPR_REG_END) { /* this bit's a register */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 583 | if (e->value == 1) /* in fact it can be basereg */ |
| 584 | b = e->type; |
| 585 | else /* no, it has to be indexreg */ |
| 586 | i = e->type, s = e->value; |
| 587 | e++; |
| 588 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 589 | if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */ |
| 590 | { |
| 591 | if (b != -1) /* If the first was the base, ... */ |
| 592 | i = e->type, s = e->value; /* second has to be indexreg */ |
| 593 | |
| 594 | else if (e->value != 1) /* If both want to be index */ |
| 595 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 596 | error(ERR_NONFATAL, "beroset-p-592-invalid effective address"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 597 | result->opcode = -1; |
| 598 | return result; |
| 599 | } |
| 600 | else |
| 601 | b = e->type; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 602 | e++; |
| 603 | } |
| 604 | if (e->type != 0) { /* is there an offset? */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 605 | if (e->type <= EXPR_REG_END) /* in fact, is there an error? */ |
| 606 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 607 | error (ERR_NONFATAL, "beroset-p-603-invalid effective address"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 608 | result->opcode = -1; |
| 609 | return result; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 610 | } |
| 611 | else |
| 612 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 613 | if (e->type == EXPR_UNKNOWN) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 614 | o = 0; /* doesn't matter what */ |
| 615 | result->oprs[operand].wrt = NO_SEG; /* nor this */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 616 | result->oprs[operand].segment = NO_SEG; /* or this */ |
| 617 | while (e->type) e++; /* go to the end of the line */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 618 | } |
| 619 | else |
| 620 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 621 | if (e->type == EXPR_SIMPLE) { |
| 622 | o = e->value; |
| 623 | e++; |
| 624 | } |
| 625 | if (e->type == EXPR_WRT) { |
| 626 | result->oprs[operand].wrt = e->value; |
| 627 | e++; |
| 628 | } else |
| 629 | result->oprs[operand].wrt = NO_SEG; |
| 630 | /* |
| 631 | * Look for a segment base type. |
| 632 | */ |
| 633 | if (e->type && e->type < EXPR_SEGBASE) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 634 | error (ERR_NONFATAL, "beroset-p-630-invalid effective address"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 635 | result->opcode = -1; |
| 636 | return result; |
| 637 | } |
| 638 | while (e->type && e->value == 0) |
| 639 | e++; |
| 640 | if (e->type && e->value != 1) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 641 | error (ERR_NONFATAL, "beroset-p-637-invalid effective address"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 642 | result->opcode = -1; |
| 643 | return result; |
| 644 | } |
| 645 | if (e->type) { |
| 646 | result->oprs[operand].segment = |
| 647 | e->type - EXPR_SEGBASE; |
| 648 | e++; |
| 649 | } else |
| 650 | result->oprs[operand].segment = NO_SEG; |
| 651 | while (e->type && e->value == 0) |
| 652 | e++; |
| 653 | if (e->type) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 654 | error (ERR_NONFATAL, "beroset-p-650-invalid effective address"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 655 | result->opcode = -1; |
| 656 | return result; |
| 657 | } |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame] | 658 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 659 | } |
| 660 | } else { |
| 661 | o = 0; |
| 662 | result->oprs[operand].wrt = NO_SEG; |
| 663 | result->oprs[operand].segment = NO_SEG; |
| 664 | } |
| 665 | |
| 666 | if (e->type != 0) { /* there'd better be nothing left! */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 667 | error (ERR_NONFATAL, "beroset-p-663-invalid effective address"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 668 | result->opcode = -1; |
| 669 | return result; |
| 670 | } |
| 671 | |
| 672 | result->oprs[operand].type |= MEMORY; |
| 673 | if (b==-1 && (i==-1 || s==0)) |
| 674 | result->oprs[operand].type |= MEM_OFFS; |
| 675 | result->oprs[operand].basereg = b; |
| 676 | result->oprs[operand].indexreg = i; |
| 677 | result->oprs[operand].scale = s; |
| 678 | result->oprs[operand].offset = o; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 679 | } |
| 680 | else /* it's not a memory reference */ |
| 681 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 682 | if (is_just_unknown(value)) { /* it's immediate but unknown */ |
| 683 | result->oprs[operand].type |= IMMEDIATE; |
| 684 | result->oprs[operand].offset = 0; /* don't care */ |
| 685 | result->oprs[operand].segment = NO_SEG; /* don't care again */ |
| 686 | result->oprs[operand].wrt = NO_SEG;/* still don't care */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 687 | } |
| 688 | else if (is_reloc(value)) /* it's immediate */ |
| 689 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 690 | result->oprs[operand].type |= IMMEDIATE; |
| 691 | result->oprs[operand].offset = reloc_value(value); |
| 692 | result->oprs[operand].segment = reloc_seg(value); |
| 693 | result->oprs[operand].wrt = reloc_wrt(value); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 694 | if (is_simple(value)) { |
| 695 | if (reloc_value(value)==1) |
| 696 | result->oprs[operand].type |= UNITY; |
H. Peter Anvin | 8c1da7b | 2002-05-22 20:45:09 +0000 | [diff] [blame] | 697 | if (optimizing>=0 && |
| 698 | !(result->oprs[operand].type & STRICT)) { |
H. Peter Anvin | 4cf1748 | 2002-04-30 21:01:38 +0000 | [diff] [blame] | 699 | if (reloc_value(value) >= -128 && |
| 700 | reloc_value(value) <= 127) |
| 701 | result->oprs[operand].type |= SBYTE; |
H. Peter Anvin | 4cf1748 | 2002-04-30 21:01:38 +0000 | [diff] [blame] | 702 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 703 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 704 | } |
| 705 | else /* it's a register */ |
| 706 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 707 | if (value->type>=EXPR_SIMPLE || value->value!=1) { |
| 708 | error (ERR_NONFATAL, "invalid operand type"); |
| 709 | result->opcode = -1; |
| 710 | return result; |
| 711 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * check that its only 1 register, not an expression... |
| 715 | */ |
| 716 | for (i = 1; value[i].type; i++) |
| 717 | if (value[i].value) { |
| 718 | error (ERR_NONFATAL, "invalid operand type"); |
| 719 | result->opcode = -1; |
| 720 | return result; |
| 721 | } |
| 722 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 723 | /* clear overrides, except TO which applies to FPU regs */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 724 | if (result->oprs[operand].type & ~TO) { |
| 725 | /* |
| 726 | * we want to produce a warning iff the specified size |
| 727 | * is different from the register size |
| 728 | */ |
| 729 | i = result->oprs[operand].type & SIZE_MASK; |
| 730 | } |
| 731 | else |
| 732 | i = 0; |
| 733 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 734 | result->oprs[operand].type &= TO; |
| 735 | result->oprs[operand].type |= REGISTER; |
| 736 | result->oprs[operand].type |= reg_flags[value->type]; |
| 737 | result->oprs[operand].basereg = value->type; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 738 | |
| 739 | if (i && (result->oprs[operand].type & SIZE_MASK) != i) |
| 740 | error (ERR_WARNING|ERR_PASS1, |
| 741 | "register size specification ignored"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | result->operands = operand; /* set operand count */ |
| 747 | |
| 748 | while (operand<3) /* clear remaining operands */ |
| 749 | result->oprs[operand++].type = 0; |
| 750 | |
| 751 | /* |
| 752 | * Transform RESW, RESD, RESQ, REST into RESB. |
| 753 | */ |
| 754 | switch (result->opcode) { |
| 755 | case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break; |
| 756 | case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break; |
| 757 | case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break; |
| 758 | case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break; |
| 759 | } |
| 760 | |
| 761 | return result; |
| 762 | } |
| 763 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 764 | static int is_comma_next (void) |
| 765 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 766 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 767 | int i; |
| 768 | struct tokenval tv; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 769 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 770 | p = stdscan_bufptr; |
| 771 | i = stdscan (NULL, &tv); |
| 772 | stdscan_bufptr = p; |
| 773 | return (i == ',' || i == ';' || !i); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 774 | } |
| 775 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 776 | void cleanup_insn (insn *i) |
| 777 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 778 | extop *e; |
| 779 | |
| 780 | while (i->eops) { |
| 781 | e = i->eops; |
| 782 | i->eops = i->eops->next; |
| 783 | nasm_free (e); |
| 784 | } |
| 785 | } |