ELF: make debugging functions static
[nasm/sigaren-mirror.git] / parser.c
blobe6126a50ac0e285e32e05f1996b486fd43bc8305
1 /* parser.c source line parser for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
9 */
11 #include "compiler.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <inttypes.h>
20 #include "nasm.h"
21 #include "insns.h"
22 #include "nasmlib.h"
23 #include "stdscan.h"
24 #include "parser.h"
25 #include "float.h"
26 #include "tables.h"
28 extern int in_abs_seg; /* ABSOLUTE segment flag */
29 extern int32_t abs_seg; /* ABSOLUTE segment */
30 extern int32_t abs_offset; /* ABSOLUTE segment offset */
32 static int is_comma_next(void);
34 static int i;
35 static struct tokenval tokval;
36 static efunc error;
37 static struct ofmt *outfmt; /* Structure of addresses of output routines */
38 static struct location *location; /* Pointer to current line's segment,offset */
40 void parser_global_info(struct ofmt *output, struct location * locp)
42 outfmt = output;
43 location = locp;
46 static int prefix_slot(enum prefixes prefix)
48 switch (prefix) {
49 case P_WAIT:
50 return PPS_WAIT;
51 case R_CS:
52 case R_DS:
53 case R_SS:
54 case R_ES:
55 case R_FS:
56 case R_GS:
57 return PPS_SEG;
58 case P_LOCK:
59 case P_REP:
60 case P_REPE:
61 case P_REPZ:
62 case P_REPNE:
63 case P_REPNZ:
64 return PPS_LREP;
65 case P_O16:
66 case P_O32:
67 case P_O64:
68 case P_OSP:
69 return PPS_OSIZE;
70 case P_A16:
71 case P_A32:
72 case P_A64:
73 case P_ASP:
74 return PPS_ASIZE;
75 default:
76 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
77 return -1;
81 static void process_size_override(insn * result, int operand)
83 if (tasm_compatible_mode) {
84 switch ((int)tokval.t_integer) {
85 /* For TASM compatibility a size override inside the
86 * brackets changes the size of the operand, not the
87 * address type of the operand as it does in standard
88 * NASM syntax. Hence:
90 * mov eax,[DWORD val]
92 * is valid syntax in TASM compatibility mode. Note that
93 * you lose the ability to override the default address
94 * type for the instruction, but we never use anything
95 * but 32-bit flat model addressing in our code.
97 case S_BYTE:
98 result->oprs[operand].type |= BITS8;
99 break;
100 case S_WORD:
101 result->oprs[operand].type |= BITS16;
102 break;
103 case S_DWORD:
104 case S_LONG:
105 result->oprs[operand].type |= BITS32;
106 break;
107 case S_QWORD:
108 result->oprs[operand].type |= BITS64;
109 break;
110 case S_TWORD:
111 result->oprs[operand].type |= BITS80;
112 break;
113 case S_OWORD:
114 result->oprs[operand].type |= BITS128;
115 break;
116 default:
117 error(ERR_NONFATAL,
118 "invalid operand size specification");
119 break;
121 } else {
122 /* Standard NASM compatible syntax */
123 switch ((int)tokval.t_integer) {
124 case S_NOSPLIT:
125 result->oprs[operand].eaflags |= EAF_TIMESTWO;
126 break;
127 case S_REL:
128 result->oprs[operand].eaflags |= EAF_REL;
129 break;
130 case S_ABS:
131 result->oprs[operand].eaflags |= EAF_ABS;
132 break;
133 case S_BYTE:
134 result->oprs[operand].disp_size = 8;
135 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
136 break;
137 case P_A16:
138 case P_A32:
139 case P_A64:
140 if (result->prefixes[PPS_ASIZE] &&
141 result->prefixes[PPS_ASIZE] != tokval.t_integer)
142 error(ERR_NONFATAL,
143 "conflicting address size specifications");
144 else
145 result->prefixes[PPS_ASIZE] = tokval.t_integer;
146 break;
147 case S_WORD:
148 result->oprs[operand].disp_size = 16;
149 result->oprs[operand].eaflags |= EAF_WORDOFFS;
150 break;
151 case S_DWORD:
152 case S_LONG:
153 result->oprs[operand].disp_size = 32;
154 result->oprs[operand].eaflags |= EAF_WORDOFFS;
155 break;
156 case S_QWORD:
157 result->oprs[operand].disp_size = 64;
158 result->oprs[operand].eaflags |= EAF_WORDOFFS;
159 break;
160 default:
161 error(ERR_NONFATAL, "invalid size specification in"
162 " effective address");
163 break;
168 insn *parse_line(int pass, char *buffer, insn * result,
169 efunc errfunc, evalfunc evaluate, ldfunc ldef)
171 int operand;
172 int critical;
173 struct eval_hints hints;
174 int j;
175 bool first;
176 bool insn_is_label = false;
177 bool recover;
179 restart_parse:
180 first = true;
181 result->forw_ref = false;
182 error = errfunc;
184 stdscan_reset();
185 stdscan_bufptr = buffer;
186 i = stdscan(NULL, &tokval);
188 result->label = NULL; /* Assume no label */
189 result->eops = NULL; /* must do this, whatever happens */
190 result->operands = 0; /* must initialize this */
192 if (i == 0) { /* blank line - ignore */
193 result->opcode = -1; /* and no instruction either */
194 return result;
196 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
197 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
198 error(ERR_NONFATAL, "label or instruction expected"
199 " at start of line");
200 result->opcode = -1;
201 return result;
204 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
205 /* there's a label here */
206 first = false;
207 result->label = tokval.t_charptr;
208 i = stdscan(NULL, &tokval);
209 if (i == ':') { /* skip over the optional colon */
210 i = stdscan(NULL, &tokval);
211 } else if (i == 0) {
212 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
213 "label alone on a line without a colon might be in error");
215 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
217 * FIXME: location->segment could be NO_SEG, in which case
218 * it is possible we should be passing 'abs_seg'. Look into this.
219 * Work out whether that is *really* what we should be doing.
220 * Generally fix things. I think this is right as it is, but
221 * am still not certain.
223 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
224 location->offset, NULL, true, false, outfmt, errfunc);
228 if (i == 0) {
229 result->opcode = -1; /* this line contains just a label */
230 return result;
233 for (j = 0; j < MAXPREFIX; j++)
234 result->prefixes[j] = P_none;
235 result->times = 1L;
237 while (i == TOKEN_PREFIX ||
238 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
240 first = false;
243 * Handle special case: the TIMES prefix.
245 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
246 expr *value;
248 i = stdscan(NULL, &tokval);
249 value =
250 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
251 i = tokval.t_type;
252 if (!value) { /* but, error in evaluator */
253 result->opcode = -1; /* unrecoverable parse error: */
254 return result; /* ignore this instruction */
256 if (!is_simple(value)) {
257 error(ERR_NONFATAL,
258 "non-constant argument supplied to TIMES");
259 result->times = 1L;
260 } else {
261 result->times = value->value;
262 if (value->value < 0 && pass0 == 2) {
263 error(ERR_NONFATAL, "TIMES value %d is negative",
264 value->value);
265 result->times = 0;
268 } else {
269 int slot = prefix_slot(tokval.t_integer);
270 if (result->prefixes[slot]) {
271 if (result->prefixes[slot] == tokval.t_integer)
272 error(ERR_WARNING,
273 "instruction has redundant prefixes");
274 else
275 error(ERR_NONFATAL,
276 "instruction has conflicting prefixes");
278 result->prefixes[slot] = tokval.t_integer;
279 i = stdscan(NULL, &tokval);
283 if (i != TOKEN_INSN) {
284 int j;
285 enum prefixes pfx;
287 for (j = 0; j < MAXPREFIX; j++)
288 if ((pfx = result->prefixes[j]) != P_none)
289 break;
291 if (i == 0 && pfx != P_none) {
293 * Instruction prefixes are present, but no actual
294 * instruction. This is allowed: at this point we
295 * invent a notional instruction of RESB 0.
297 result->opcode = I_RESB;
298 result->operands = 1;
299 result->oprs[0].type = IMMEDIATE;
300 result->oprs[0].offset = 0L;
301 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
302 return result;
303 } else {
304 error(ERR_NONFATAL, "parser: instruction expected");
305 result->opcode = -1;
306 return result;
310 result->opcode = tokval.t_integer;
311 result->condition = tokval.t_inttwo;
314 * INCBIN cannot be satisfied with incorrectly
315 * evaluated operands, since the correct values _must_ be known
316 * on the first pass. Hence, even in pass one, we set the
317 * `critical' flag on calling evaluate(), so that it will bomb
318 * out on undefined symbols.
320 if (result->opcode == I_INCBIN) {
321 critical = (pass0 < 2 ? 1 : 2);
323 } else
324 critical = (pass == 2 ? 2 : 0);
326 if (result->opcode == I_DB || result->opcode == I_DW ||
327 result->opcode == I_DD || result->opcode == I_DQ ||
328 result->opcode == I_DT || result->opcode == I_DO ||
329 result->opcode == I_DY || result->opcode == I_INCBIN) {
330 extop *eop, **tail = &result->eops, **fixptr;
331 int oper_num = 0;
332 int32_t sign;
334 result->eops_float = false;
337 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
339 while (1) {
340 i = stdscan(NULL, &tokval);
341 if (i == 0)
342 break;
343 else if (first && i == ':') {
344 insn_is_label = true;
345 goto restart_parse;
347 first = false;
348 fixptr = tail;
349 eop = *tail = nasm_malloc(sizeof(extop));
350 tail = &eop->next;
351 eop->next = NULL;
352 eop->type = EOT_NOTHING;
353 oper_num++;
354 sign = +1;
356 /* is_comma_next() here is to distinguish this from
357 a string used as part of an expression... */
358 if (i == TOKEN_STR && is_comma_next()) {
359 eop->type = EOT_DB_STRING;
360 eop->stringval = tokval.t_charptr;
361 eop->stringlen = tokval.t_inttwo;
362 i = stdscan(NULL, &tokval); /* eat the comma */
363 } else if (i == TOKEN_STRFUNC) {
364 bool parens = false;
365 const char *funcname = tokval.t_charptr;
366 enum strfunc func = tokval.t_integer;
367 i = stdscan(NULL, &tokval);
368 if (i == '(') {
369 parens = true;
370 i = stdscan(NULL, &tokval);
372 if (i != TOKEN_STR) {
373 error(ERR_NONFATAL,
374 "%s must be followed by a string constant",
375 funcname);
376 eop->type = EOT_NOTHING;
377 } else {
378 eop->type = EOT_DB_STRING_FREE;
379 eop->stringlen =
380 string_transform(tokval.t_charptr, tokval.t_inttwo,
381 &eop->stringval, func);
382 if (eop->stringlen == (size_t)-1) {
383 error(ERR_NONFATAL, "invalid string for transform");
384 eop->type = EOT_NOTHING;
387 if (parens && i && i != ')') {
388 i = stdscan(NULL, &tokval);
389 if (i != ')') {
390 error(ERR_NONFATAL, "unterminated %s function",
391 funcname);
394 if (i && i != ',')
395 i = stdscan(NULL, &tokval);
396 } else if (i == '-' || i == '+') {
397 char *save = stdscan_bufptr;
398 int token = i;
399 sign = (i == '-') ? -1 : 1;
400 i = stdscan(NULL, &tokval);
401 if (i != TOKEN_FLOAT) {
402 stdscan_bufptr = save;
403 i = tokval.t_type = token;
404 goto is_expression;
405 } else {
406 goto is_float;
408 } else if (i == TOKEN_FLOAT) {
409 is_float:
410 eop->type = EOT_DB_STRING;
411 result->eops_float = true;
412 switch (result->opcode) {
413 case I_DB:
414 eop->stringlen = 1;
415 break;
416 case I_DW:
417 eop->stringlen = 2;
418 break;
419 case I_DD:
420 eop->stringlen = 4;
421 break;
422 case I_DQ:
423 eop->stringlen = 8;
424 break;
425 case I_DT:
426 eop->stringlen = 10;
427 break;
428 case I_DO:
429 eop->stringlen = 16;
430 break;
431 case I_DY:
432 error(ERR_NONFATAL, "floating-point constant"
433 " encountered in DY instruction");
434 eop->stringlen = 0;
435 break;
436 default:
437 error(ERR_NONFATAL, "floating-point constant"
438 " encountered in unknown instruction");
440 * fix suggested by Pedro Gimeno... original line
441 * was:
442 * eop->type = EOT_NOTHING;
444 eop->stringlen = 0;
445 break;
447 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
448 tail = &eop->next;
449 *fixptr = eop;
450 eop->stringval = (char *)eop + sizeof(extop);
451 if (!eop->stringlen ||
452 !float_const(tokval.t_charptr, sign,
453 (uint8_t *)eop->stringval,
454 eop->stringlen, error))
455 eop->type = EOT_NOTHING;
456 i = stdscan(NULL, &tokval); /* eat the comma */
457 } else {
458 /* anything else, assume it is an expression */
459 expr *value;
461 is_expression:
462 value = evaluate(stdscan, NULL, &tokval, NULL,
463 critical, error, NULL);
464 i = tokval.t_type;
465 if (!value) { /* error in evaluator */
466 result->opcode = -1; /* unrecoverable parse error: */
467 return result; /* ignore this instruction */
469 if (is_unknown(value)) {
470 eop->type = EOT_DB_NUMBER;
471 eop->offset = 0; /* doesn't matter what we put */
472 eop->segment = eop->wrt = NO_SEG; /* likewise */
473 } else if (is_reloc(value)) {
474 eop->type = EOT_DB_NUMBER;
475 eop->offset = reloc_value(value);
476 eop->segment = reloc_seg(value);
477 eop->wrt = reloc_wrt(value);
478 } else {
479 error(ERR_NONFATAL,
480 "operand %d: expression is not simple"
481 " or relocatable", oper_num);
486 * We're about to call stdscan(), which will eat the
487 * comma that we're currently sitting on between
488 * arguments. However, we'd better check first that it
489 * _is_ a comma.
491 if (i == 0) /* also could be EOL */
492 break;
493 if (i != ',') {
494 error(ERR_NONFATAL, "comma expected after operand %d",
495 oper_num);
496 result->opcode = -1; /* unrecoverable parse error: */
497 return result; /* ignore this instruction */
501 if (result->opcode == I_INCBIN) {
503 * Correct syntax for INCBIN is that there should be
504 * one string operand, followed by one or two numeric
505 * operands.
507 if (!result->eops || result->eops->type != EOT_DB_STRING)
508 error(ERR_NONFATAL, "`incbin' expects a file name");
509 else if (result->eops->next &&
510 result->eops->next->type != EOT_DB_NUMBER)
511 error(ERR_NONFATAL, "`incbin': second parameter is",
512 " non-numeric");
513 else if (result->eops->next && result->eops->next->next &&
514 result->eops->next->next->type != EOT_DB_NUMBER)
515 error(ERR_NONFATAL, "`incbin': third parameter is",
516 " non-numeric");
517 else if (result->eops->next && result->eops->next->next &&
518 result->eops->next->next->next)
519 error(ERR_NONFATAL,
520 "`incbin': more than three parameters");
521 else
522 return result;
524 * If we reach here, one of the above errors happened.
525 * Throw the instruction away.
527 result->opcode = -1;
528 return result;
529 } else /* DB ... */ if (oper_num == 0)
530 error(ERR_WARNING | ERR_PASS1,
531 "no operand for data declaration");
532 else
533 result->operands = oper_num;
535 return result;
538 /* right. Now we begin to parse the operands. There may be up to four
539 * of these, separated by commas, and terminated by a zero token. */
541 for (operand = 0; operand < MAX_OPERANDS; operand++) {
542 expr *value; /* used most of the time */
543 int mref; /* is this going to be a memory ref? */
544 int bracket; /* is it a [] mref, or a & mref? */
545 int setsize = 0;
547 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
548 result->oprs[operand].eaflags = 0; /* and this */
549 result->oprs[operand].opflags = 0;
551 i = stdscan(NULL, &tokval);
552 if (i == 0)
553 break; /* end of operands: get out of here */
554 else if (first && i == ':') {
555 insn_is_label = true;
556 goto restart_parse;
558 first = false;
559 result->oprs[operand].type = 0; /* so far, no override */
560 while (i == TOKEN_SPECIAL) { /* size specifiers */
561 switch ((int)tokval.t_integer) {
562 case S_BYTE:
563 if (!setsize) /* we want to use only the first */
564 result->oprs[operand].type |= BITS8;
565 setsize = 1;
566 break;
567 case S_WORD:
568 if (!setsize)
569 result->oprs[operand].type |= BITS16;
570 setsize = 1;
571 break;
572 case S_DWORD:
573 case S_LONG:
574 if (!setsize)
575 result->oprs[operand].type |= BITS32;
576 setsize = 1;
577 break;
578 case S_QWORD:
579 if (!setsize)
580 result->oprs[operand].type |= BITS64;
581 setsize = 1;
582 break;
583 case S_TWORD:
584 if (!setsize)
585 result->oprs[operand].type |= BITS80;
586 setsize = 1;
587 break;
588 case S_OWORD:
589 if (!setsize)
590 result->oprs[operand].type |= BITS128;
591 setsize = 1;
592 break;
593 case S_YWORD:
594 if (!setsize)
595 result->oprs[operand].type |= BITS256;
596 setsize = 1;
597 break;
598 case S_TO:
599 result->oprs[operand].type |= TO;
600 break;
601 case S_STRICT:
602 result->oprs[operand].type |= STRICT;
603 break;
604 case S_FAR:
605 result->oprs[operand].type |= FAR;
606 break;
607 case S_NEAR:
608 result->oprs[operand].type |= NEAR;
609 break;
610 case S_SHORT:
611 result->oprs[operand].type |= SHORT;
612 break;
613 default:
614 error(ERR_NONFATAL, "invalid operand size specification");
616 i = stdscan(NULL, &tokval);
619 if (i == '[' || i == '&') { /* memory reference */
620 mref = true;
621 bracket = (i == '[');
622 i = stdscan(NULL, &tokval); /* then skip the colon */
623 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
624 process_size_override(result, operand);
625 i = stdscan(NULL, &tokval);
627 } else { /* immediate operand, or register */
628 mref = false;
629 bracket = false; /* placate optimisers */
632 if ((result->oprs[operand].type & FAR) && !mref &&
633 result->opcode != I_JMP && result->opcode != I_CALL) {
634 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
637 value = evaluate(stdscan, NULL, &tokval,
638 &result->oprs[operand].opflags,
639 critical, error, &hints);
640 i = tokval.t_type;
641 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
642 result->forw_ref = true;
644 if (!value) { /* error in evaluator */
645 result->opcode = -1; /* unrecoverable parse error: */
646 return result; /* ignore this instruction */
648 if (i == ':' && mref) { /* it was seg:offset */
650 * Process the segment override.
652 if (value[1].type != 0 || value->value != 1 ||
653 REG_SREG & ~nasm_reg_flags[value->type])
654 error(ERR_NONFATAL, "invalid segment override");
655 else if (result->prefixes[PPS_SEG])
656 error(ERR_NONFATAL,
657 "instruction has conflicting segment overrides");
658 else {
659 result->prefixes[PPS_SEG] = value->type;
660 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
661 result->oprs[operand].eaflags |= EAF_FSGS;
664 i = stdscan(NULL, &tokval); /* then skip the colon */
665 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
666 process_size_override(result, operand);
667 i = stdscan(NULL, &tokval);
669 value = evaluate(stdscan, NULL, &tokval,
670 &result->oprs[operand].opflags,
671 critical, error, &hints);
672 i = tokval.t_type;
673 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
674 result->forw_ref = true;
676 /* and get the offset */
677 if (!value) { /* but, error in evaluator */
678 result->opcode = -1; /* unrecoverable parse error: */
679 return result; /* ignore this instruction */
683 recover = false;
684 if (mref && bracket) { /* find ] at the end */
685 if (i != ']') {
686 error(ERR_NONFATAL, "parser: expecting ]");
687 recover = true;
688 } else { /* we got the required ] */
689 i = stdscan(NULL, &tokval);
690 if (i != 0 && i != ',') {
691 error(ERR_NONFATAL, "comma or end of line expected");
692 recover = true;
695 } else { /* immediate operand */
696 if (i != 0 && i != ',' && i != ':') {
697 error(ERR_NONFATAL, "comma, colon or end of line expected");
698 recover = true;
699 } else if (i == ':') {
700 result->oprs[operand].type |= COLON;
703 if (recover) {
704 do { /* error recovery */
705 i = stdscan(NULL, &tokval);
706 } while (i != 0 && i != ',');
709 /* now convert the exprs returned from evaluate() into operand
710 * descriptions... */
712 if (mref) { /* it's a memory reference */
713 expr *e = value;
714 int b, i, s; /* basereg, indexreg, scale */
715 int64_t o; /* offset */
717 b = i = -1, o = s = 0;
718 result->oprs[operand].hintbase = hints.base;
719 result->oprs[operand].hinttype = hints.type;
721 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
722 if (e->value == 1) /* in fact it can be basereg */
723 b = e->type;
724 else /* no, it has to be indexreg */
725 i = e->type, s = e->value;
726 e++;
728 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
729 if (b != -1) /* If the first was the base, ... */
730 i = e->type, s = e->value; /* second has to be indexreg */
732 else if (e->value != 1) { /* If both want to be index */
733 error(ERR_NONFATAL,
734 "beroset-p-592-invalid effective address");
735 result->opcode = -1;
736 return result;
737 } else
738 b = e->type;
739 e++;
741 if (e->type != 0) { /* is there an offset? */
742 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
743 error(ERR_NONFATAL,
744 "beroset-p-603-invalid effective address");
745 result->opcode = -1;
746 return result;
747 } else {
748 if (e->type == EXPR_UNKNOWN) {
749 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
750 o = 0; /* doesn't matter what */
751 result->oprs[operand].wrt = NO_SEG; /* nor this */
752 result->oprs[operand].segment = NO_SEG; /* or this */
753 while (e->type)
754 e++; /* go to the end of the line */
755 } else {
756 if (e->type == EXPR_SIMPLE) {
757 o = e->value;
758 e++;
760 if (e->type == EXPR_WRT) {
761 result->oprs[operand].wrt = e->value;
762 e++;
763 } else
764 result->oprs[operand].wrt = NO_SEG;
766 * Look for a segment base type.
768 if (e->type && e->type < EXPR_SEGBASE) {
769 error(ERR_NONFATAL,
770 "beroset-p-630-invalid effective address");
771 result->opcode = -1;
772 return result;
774 while (e->type && e->value == 0)
775 e++;
776 if (e->type && e->value != 1) {
777 error(ERR_NONFATAL,
778 "beroset-p-637-invalid effective address");
779 result->opcode = -1;
780 return result;
782 if (e->type) {
783 result->oprs[operand].segment =
784 e->type - EXPR_SEGBASE;
785 e++;
786 } else
787 result->oprs[operand].segment = NO_SEG;
788 while (e->type && e->value == 0)
789 e++;
790 if (e->type) {
791 error(ERR_NONFATAL,
792 "beroset-p-650-invalid effective address");
793 result->opcode = -1;
794 return result;
798 } else {
799 o = 0;
800 result->oprs[operand].wrt = NO_SEG;
801 result->oprs[operand].segment = NO_SEG;
804 if (e->type != 0) { /* there'd better be nothing left! */
805 error(ERR_NONFATAL,
806 "beroset-p-663-invalid effective address");
807 result->opcode = -1;
808 return result;
811 /* It is memory, but it can match any r/m operand */
812 result->oprs[operand].type |= MEMORY_ANY;
814 if (b == -1 && (i == -1 || s == 0)) {
815 int is_rel = globalbits == 64 &&
816 !(result->oprs[operand].eaflags & EAF_ABS) &&
817 ((globalrel &&
818 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
819 (result->oprs[operand].eaflags & EAF_REL));
821 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
823 result->oprs[operand].basereg = b;
824 result->oprs[operand].indexreg = i;
825 result->oprs[operand].scale = s;
826 result->oprs[operand].offset = o;
827 } else { /* it's not a memory reference */
829 if (is_just_unknown(value)) { /* it's immediate but unknown */
830 result->oprs[operand].type |= IMMEDIATE;
831 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
832 result->oprs[operand].offset = 0; /* don't care */
833 result->oprs[operand].segment = NO_SEG; /* don't care again */
834 result->oprs[operand].wrt = NO_SEG; /* still don't care */
836 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT))
838 /* Be optimistic */
839 result->oprs[operand].type |= SBYTE16 | SBYTE32 | SBYTE64;
841 } else if (is_reloc(value)) { /* it's immediate */
842 result->oprs[operand].type |= IMMEDIATE;
843 result->oprs[operand].offset = reloc_value(value);
844 result->oprs[operand].segment = reloc_seg(value);
845 result->oprs[operand].wrt = reloc_wrt(value);
846 if (is_simple(value)) {
847 if (reloc_value(value) == 1)
848 result->oprs[operand].type |= UNITY;
849 if (optimizing >= 0 &&
850 !(result->oprs[operand].type & STRICT)) {
851 int64_t v64 = reloc_value(value);
852 int32_t v32 = (int32_t)v64;
853 int16_t v16 = (int16_t)v32;
855 if (v64 >= -128 && v64 <= 127)
856 result->oprs[operand].type |= SBYTE64;
857 if (v32 >= -128 && v32 <= 127)
858 result->oprs[operand].type |= SBYTE32;
859 if (v16 >= -128 && v16 <= 127)
860 result->oprs[operand].type |= SBYTE16;
863 } else { /* it's a register */
864 unsigned int rs;
866 if (value->type >= EXPR_SIMPLE || value->value != 1) {
867 error(ERR_NONFATAL, "invalid operand type");
868 result->opcode = -1;
869 return result;
873 * check that its only 1 register, not an expression...
875 for (i = 1; value[i].type; i++)
876 if (value[i].value) {
877 error(ERR_NONFATAL, "invalid operand type");
878 result->opcode = -1;
879 return result;
882 /* clear overrides, except TO which applies to FPU regs */
883 if (result->oprs[operand].type & ~TO) {
885 * we want to produce a warning iff the specified size
886 * is different from the register size
888 rs = result->oprs[operand].type & SIZE_MASK;
889 } else
890 rs = 0;
892 result->oprs[operand].type &= TO;
893 result->oprs[operand].type |= REGISTER;
894 result->oprs[operand].type |= nasm_reg_flags[value->type];
895 result->oprs[operand].basereg = value->type;
897 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
898 error(ERR_WARNING | ERR_PASS1,
899 "register size specification ignored");
904 result->operands = operand; /* set operand count */
906 /* clear remaining operands */
907 while (operand < MAX_OPERANDS)
908 result->oprs[operand++].type = 0;
911 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
913 switch (result->opcode) {
914 case I_RESW:
915 result->opcode = I_RESB;
916 result->oprs[0].offset *= 2;
917 break;
918 case I_RESD:
919 result->opcode = I_RESB;
920 result->oprs[0].offset *= 4;
921 break;
922 case I_RESQ:
923 result->opcode = I_RESB;
924 result->oprs[0].offset *= 8;
925 break;
926 case I_REST:
927 result->opcode = I_RESB;
928 result->oprs[0].offset *= 10;
929 break;
930 case I_RESO:
931 result->opcode = I_RESB;
932 result->oprs[0].offset *= 16;
933 break;
934 case I_RESY:
935 result->opcode = I_RESB;
936 result->oprs[0].offset *= 32;
937 break;
938 default:
939 break;
942 return result;
945 static int is_comma_next(void)
947 char *p;
948 int i;
949 struct tokenval tv;
951 p = stdscan_bufptr;
952 i = stdscan(NULL, &tv);
953 stdscan_bufptr = p;
954 return (i == ',' || i == ';' || !i);
957 void cleanup_insn(insn * i)
959 extop *e;
961 while ((e = i->eops)) {
962 i->eops = e->next;
963 if (e->type == EOT_DB_STRING_FREE)
964 nasm_free(e->stringval);
965 nasm_free(e);