]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/parser.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* parser.c source line parser for the Netwide Assembler
26 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
27 * Julian Hall. All rights reserved. The software is
28 * redistributable under the licence given in the file "Licence"
29 * distributed in the NASM archive.
31 * initial version 27/iii/95 by Simon Tatham
45 static long reg_flags
[] = { /* sizes and special flags */
46 0, REG8
, REG_AL
, REG_AX
, REG8
, REG8
, REG16
, REG16
, REG8
, REG_CL
,
47 REG_CREG
, REG_CREG
, REG_CREG
, REG_CR4
, REG_CS
, REG_CX
, REG8
,
48 REG16
, REG8
, REG_DREG
, REG_DREG
, REG_DREG
, REG_DREG
, REG_DREG
,
49 REG_DREG
, REG_DESS
, REG_DX
, REG_EAX
, REG32
, REG32
, REG_ECX
,
50 REG32
, REG32
, REG_DESS
, REG32
, REG32
, REG_FSGS
, REG_FSGS
,
51 MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
, MMXREG
,
52 REG16
, REG16
, REG_DESS
, FPU0
, FPUREG
, FPUREG
, FPUREG
, FPUREG
,
53 FPUREG
, FPUREG
, FPUREG
, REG_TREG
, REG_TREG
, REG_TREG
, REG_TREG
,
57 enum { /* special tokens */
58 S_BYTE
, S_DWORD
, S_FAR
, S_LONG
, S_NEAR
, S_NOSPLIT
, S_QWORD
,
59 S_SHORT
, S_TO
, S_TWORD
, S_WORD
62 static int is_comma_next (void);
65 static struct tokenval tokval
;
68 insn
*parse_line (int pass
, char *buffer
, insn
*result
,
69 efunc errfunc
, evalfunc evaluate
, evalinfofunc einfo
) {
72 struct eval_hints hints
;
74 result
->forw_ref
= FALSE
;
79 stdscan_bufptr
= buffer
;
80 i
= stdscan(NULL
, &tokval
);
82 result
->eops
= NULL
; /* must do this, whatever happens */
83 result
->operands
= 0; /* must initialise this */
85 if (i
==0) { /* blank line - ignore */
86 result
->label
= NULL
; /* so, no label on it */
87 result
->opcode
= -1; /* and no instruction either */
90 if (i
!= TOKEN_ID
&& i
!= TOKEN_INSN
&& i
!= TOKEN_PREFIX
&&
91 (i
!=TOKEN_REG
|| (REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
92 error (ERR_NONFATAL
, "label or instruction expected"
99 if (i
== TOKEN_ID
) { /* there's a label here */
100 result
->label
= tokval
.t_charptr
;
101 einfo (result
->label
, 0L, 0L);
102 i
= stdscan(NULL
, &tokval
);
103 if (i
== ':') { /* skip over the optional colon */
104 i
= stdscan(NULL
, &tokval
);
105 } else if (i
== 0 && pass
== 1) {
106 error (ERR_WARNING
|ERR_WARN_OL
,
107 "label alone on a line without a colon might be in error");
109 } else /* no label; so, moving swiftly on */
110 result
->label
= NULL
;
113 result
->opcode
= -1; /* this line contains just a label */
120 while (i
== TOKEN_PREFIX
||
121 (i
==TOKEN_REG
&& !(REG_SREG
& ~reg_flags
[tokval
.t_integer
]))) {
123 * Handle special case: the TIMES prefix.
125 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
128 i
= stdscan(NULL
, &tokval
);
129 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
, pass
, error
, NULL
);
131 if (!value
) { /* but, error in evaluator */
132 result
->opcode
= -1; /* unrecoverable parse error: */
133 return result
; /* ignore this instruction */
135 if (!is_simple (value
)) {
137 "non-constant argument supplied to TIMES");
140 result
->times
= value
->value
;
141 if (value
->value
< 0)
142 error(ERR_NONFATAL
, "TIMES value %d is negative",
146 if (result
->nprefix
== MAXPREFIX
)
148 "instruction has more than %d prefixes", MAXPREFIX
);
150 result
->prefixes
[result
->nprefix
++] = tokval
.t_integer
;
151 i
= stdscan(NULL
, &tokval
);
155 if (i
!= TOKEN_INSN
) {
156 if (result
->nprefix
> 0 && i
== 0) {
158 * Instruction prefixes are present, but no actual
159 * instruction. This is allowed: at this point we
160 * invent a notional instruction of RESB 0.
162 result
->opcode
= I_RESB
;
163 result
->operands
= 1;
164 result
->oprs
[0].type
= IMMEDIATE
;
165 result
->oprs
[0].offset
= 0L;
166 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
169 error (ERR_NONFATAL
, "parser: instruction expected");
175 result
->opcode
= tokval
.t_integer
;
176 result
->condition
= tokval
.t_inttwo
;
179 * RESB, RESW and RESD cannot be satisfied with incorrectly
180 * evaluated operands, since the correct values _must_ be known
181 * on the first pass. Hence, even in pass one, we set the
182 * `critical' flag on calling evaluate(), so that it will bomb
183 * out on undefined symbols. Nasty, but there's nothing we can
186 * For the moment, EQU has the same difficulty, so we'll
189 if (result
->opcode
== I_RESB
||
190 result
->opcode
== I_RESW
||
191 result
->opcode
== I_RESD
||
192 result
->opcode
== I_RESQ
||
193 result
->opcode
== I_REST
||
194 result
->opcode
== I_EQU
)
197 critical
= (pass
==2 ? 2 : 0);
199 if (result
->opcode
== I_DB
||
200 result
->opcode
== I_DW
||
201 result
->opcode
== I_DD
||
202 result
->opcode
== I_DQ
||
203 result
->opcode
== I_DT
||
204 result
->opcode
== I_INCBIN
) {
205 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
209 * Begin to read the DB/DW/DD/DQ/DT operands.
212 i
= stdscan(NULL
, &tokval
);
216 eop
= *tail
= nasm_malloc(sizeof(extop
));
219 eop
->type
= EOT_NOTHING
;
222 if (i
== TOKEN_NUM
&& tokval
.t_charptr
&& is_comma_next()) {
223 eop
->type
= EOT_DB_STRING
;
224 eop
->stringval
= tokval
.t_charptr
;
225 eop
->stringlen
= tokval
.t_inttwo
;
226 i
= stdscan(NULL
, &tokval
); /* eat the comma */
230 if (i
== TOKEN_FLOAT
|| i
== '-') {
234 char *save
= stdscan_bufptr
;
235 i
= stdscan(NULL
, &tokval
);
237 if (i
!= TOKEN_FLOAT
) {
238 stdscan_bufptr
= save
;
239 i
= tokval
.t_type
= '-';
243 if (i
== TOKEN_FLOAT
) {
244 eop
->type
= EOT_DB_STRING
;
245 if (result
->opcode
== I_DD
)
247 else if (result
->opcode
== I_DQ
)
249 else if (result
->opcode
== I_DT
)
252 error(ERR_NONFATAL
, "floating-point constant"
253 " encountered in `D%c' instruction",
254 result
->opcode
== I_DW
? 'W' : 'B');
255 eop
->type
= EOT_NOTHING
;
257 eop
= nasm_realloc(eop
, sizeof(extop
)+eop
->stringlen
);
260 eop
->stringval
= (char *)eop
+ sizeof(extop
);
261 if (!float_const (tokval
.t_charptr
, sign
,
262 (unsigned char *)eop
->stringval
,
263 eop
->stringlen
, error
))
264 eop
->type
= EOT_NOTHING
;
265 i
= stdscan(NULL
, &tokval
); /* eat the comma */
270 /* anything else */ {
272 value
= evaluate (stdscan
, NULL
, &tokval
, NULL
,
273 critical
, error
, NULL
);
275 if (!value
) { /* error in evaluator */
276 result
->opcode
= -1;/* unrecoverable parse error: */
277 return result
; /* ignore this instruction */
279 if (is_unknown(value
)) {
280 eop
->type
= EOT_DB_NUMBER
;
281 eop
->offset
= 0; /* doesn't matter what we put */
282 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
283 } else if (is_reloc(value
)) {
284 eop
->type
= EOT_DB_NUMBER
;
285 eop
->offset
= reloc_value(value
);
286 eop
->segment
= reloc_seg(value
);
287 eop
->wrt
= reloc_wrt(value
);
290 "operand %d: expression is not simple"
291 " or relocatable", oper_num
);
296 * We're about to call stdscan(), which will eat the
297 * comma that we're currently sitting on between
298 * arguments. However, we'd better check first that it
301 if (i
== 0) /* also could be EOL */
304 error (ERR_NONFATAL
, "comma expected after operand %d",
306 result
->opcode
= -1;/* unrecoverable parse error: */
307 return result
; /* ignore this instruction */
311 if (result
->opcode
== I_INCBIN
) {
313 * Correct syntax for INCBIN is that there should be
314 * one string operand, followed by one or two numeric
317 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
318 error (ERR_NONFATAL
, "`incbin' expects a file name");
319 else if (result
->eops
->next
&&
320 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
321 error (ERR_NONFATAL
, "`incbin': second parameter is",
323 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
324 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
325 error (ERR_NONFATAL
, "`incbin': third parameter is",
327 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
328 result
->eops
->next
->next
->next
)
329 error (ERR_NONFATAL
, "`incbin': more than three parameters");
333 * If we reach here, one of the above errors happened.
334 * Throw the instruction away.
343 /* right. Now we begin to parse the operands. There may be up to three
344 * of these, separated by commas, and terminated by a zero token. */
346 for (operand
= 0; operand
< 3; operand
++) {
347 expr
*value
; /* used most of the time */
348 int mref
; /* is this going to be a memory ref? */
349 int bracket
; /* is it a [] mref, or a & mref? */
351 result
->oprs
[operand
].addr_size
= 0;/* have to zero this whatever */
352 result
->oprs
[operand
].eaflags
= 0; /* and this */
353 i
= stdscan(NULL
, &tokval
);
354 if (i
== 0) break; /* end of operands: get out of here */
355 result
->oprs
[operand
].type
= 0; /* so far, no override */
356 while (i
== TOKEN_SPECIAL
) {/* size specifiers */
357 switch ((int)tokval
.t_integer
) {
359 result
->oprs
[operand
].type
|= BITS8
;
362 result
->oprs
[operand
].type
|= BITS16
;
366 result
->oprs
[operand
].type
|= BITS32
;
369 result
->oprs
[operand
].type
|= BITS64
;
372 result
->oprs
[operand
].type
|= BITS80
;
375 result
->oprs
[operand
].type
|= TO
;
378 result
->oprs
[operand
].type
|= FAR
;
381 result
->oprs
[operand
].type
|= NEAR
;
384 result
->oprs
[operand
].type
|= SHORT
;
387 i
= stdscan(NULL
, &tokval
);
390 if (i
== '[' || i
== '&') { /* memory reference */
392 bracket
= (i
== '[');
393 i
= stdscan(NULL
, &tokval
);
394 if (i
== TOKEN_SPECIAL
) { /* check for address size override */
395 switch ((int)tokval
.t_integer
) {
397 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
400 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
403 result
->oprs
[operand
].addr_size
= 16;
404 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
408 result
->oprs
[operand
].addr_size
= 32;
409 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
412 error (ERR_NONFATAL
, "invalid size specification in"
413 " effective address");
415 i
= stdscan(NULL
, &tokval
);
417 } else { /* immediate operand, or register */
419 bracket
= FALSE
; /* placate optimisers */
422 value
= evaluate (stdscan
, NULL
, &tokval
,
423 &result
->forw_ref
, critical
, error
, &hints
);
425 if (!value
) { /* error in evaluator */
426 result
->opcode
= -1; /* unrecoverable parse error: */
427 return result
; /* ignore this instruction */
429 if (i
== ':' && mref
) { /* it was seg:offset */
431 * Process the segment override.
433 if (value
[1].type
!=0 || value
->value
!=1 ||
434 REG_SREG
& ~reg_flags
[value
->type
])
435 error (ERR_NONFATAL
, "invalid segment override");
436 else if (result
->nprefix
== MAXPREFIX
)
438 "instruction has more than %d prefixes",
441 result
->prefixes
[result
->nprefix
++] = value
->type
;
443 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
444 if (i
== TOKEN_SPECIAL
) { /* another check for size override */
445 switch ((int)tokval
.t_integer
) {
447 result
->oprs
[operand
].addr_size
= 16;
451 result
->oprs
[operand
].addr_size
= 32;
454 error (ERR_NONFATAL
, "invalid size specification in"
455 " effective address");
457 i
= stdscan(NULL
, &tokval
);
459 value
= evaluate (stdscan
, NULL
, &tokval
,
460 &result
->forw_ref
, critical
, error
, &hints
);
462 /* and get the offset */
463 if (!value
) { /* but, error in evaluator */
464 result
->opcode
= -1; /* unrecoverable parse error: */
465 return result
; /* ignore this instruction */
468 if (mref
&& bracket
) { /* find ] at the end */
470 error (ERR_NONFATAL
, "parser: expecting ]");
471 do { /* error recovery again */
472 i
= stdscan(NULL
, &tokval
);
473 } while (i
!= 0 && i
!= ',');
474 } else /* we got the required ] */
475 i
= stdscan(NULL
, &tokval
);
476 } else { /* immediate operand */
477 if (i
!= 0 && i
!= ',' && i
!= ':') {
478 error (ERR_NONFATAL
, "comma or end of line expected");
479 do { /* error recovery */
480 i
= stdscan(NULL
, &tokval
);
481 } while (i
!= 0 && i
!= ',');
482 } else if (i
== ':') {
483 result
->oprs
[operand
].type
|= COLON
;
487 /* now convert the exprs returned from evaluate() into operand
490 if (mref
) { /* it's a memory reference */
492 int b
, i
, s
; /* basereg, indexreg, scale */
495 b
= i
= -1, o
= s
= 0;
496 result
->oprs
[operand
].hintbase
= hints
.base
;
497 result
->oprs
[operand
].hinttype
= hints
.type
;
499 if (e
->type
<= EXPR_REG_END
) { /* this bit's a register */
500 if (e
->value
== 1) /* in fact it can be basereg */
502 else /* no, it has to be indexreg */
503 i
= e
->type
, s
= e
->value
;
506 if (e
->type
&& e
->type
<= EXPR_REG_END
) {/* it's a 2nd register */
507 if (e
->value
!= 1) { /* it has to be indexreg */
508 if (i
!= -1) { /* but it can't be */
509 error(ERR_NONFATAL
, "invalid effective address");
513 i
= e
->type
, s
= e
->value
;
514 } else { /* it can be basereg */
515 if (b
!= -1) /* or can it? */
522 if (e
->type
!= 0) { /* is there an offset? */
523 if (e
->type
<= EXPR_REG_END
) {/* in fact, is there an error? */
524 error (ERR_NONFATAL
, "invalid effective address");
528 if (e
->type
== EXPR_UNKNOWN
) {
529 o
= 0; /* doesn't matter what */
530 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
531 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
532 while (e
->type
) e
++; /* go to the end of the line */
534 if (e
->type
== EXPR_SIMPLE
) {
538 if (e
->type
== EXPR_WRT
) {
539 result
->oprs
[operand
].wrt
= e
->value
;
542 result
->oprs
[operand
].wrt
= NO_SEG
;
544 * Look for a segment base type.
546 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
547 error (ERR_NONFATAL
, "invalid effective address");
551 while (e
->type
&& e
->value
== 0)
553 if (e
->type
&& e
->value
!= 1) {
554 error (ERR_NONFATAL
, "invalid effective address");
559 result
->oprs
[operand
].segment
=
560 e
->type
- EXPR_SEGBASE
;
563 result
->oprs
[operand
].segment
= NO_SEG
;
564 while (e
->type
&& e
->value
== 0)
567 error (ERR_NONFATAL
, "invalid effective address");
575 result
->oprs
[operand
].wrt
= NO_SEG
;
576 result
->oprs
[operand
].segment
= NO_SEG
;
579 if (e
->type
!= 0) { /* there'd better be nothing left! */
580 error (ERR_NONFATAL
, "invalid effective address");
585 result
->oprs
[operand
].type
|= MEMORY
;
586 if (b
==-1 && (i
==-1 || s
==0))
587 result
->oprs
[operand
].type
|= MEM_OFFS
;
588 result
->oprs
[operand
].basereg
= b
;
589 result
->oprs
[operand
].indexreg
= i
;
590 result
->oprs
[operand
].scale
= s
;
591 result
->oprs
[operand
].offset
= o
;
592 } else { /* it's not a memory reference */
593 if (is_just_unknown(value
)) { /* it's immediate but unknown */
594 result
->oprs
[operand
].type
|= IMMEDIATE
;
595 result
->oprs
[operand
].offset
= 0; /* don't care */
596 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
597 result
->oprs
[operand
].wrt
= NO_SEG
;/* still don't care */
598 } else if (is_reloc(value
)) { /* it's immediate */
599 result
->oprs
[operand
].type
|= IMMEDIATE
;
600 result
->oprs
[operand
].offset
= reloc_value(value
);
601 result
->oprs
[operand
].segment
= reloc_seg(value
);
602 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
603 if (is_simple(value
) && reloc_value(value
)==1)
604 result
->oprs
[operand
].type
|= UNITY
;
605 } else { /* it's a register */
606 if (value
->type
>=EXPR_SIMPLE
|| value
->value
!=1) {
607 error (ERR_NONFATAL
, "invalid operand type");
611 /* clear overrides, except TO which applies to FPU regs */
612 result
->oprs
[operand
].type
&= TO
;
613 result
->oprs
[operand
].type
|= REGISTER
;
614 result
->oprs
[operand
].type
|= reg_flags
[value
->type
];
615 result
->oprs
[operand
].basereg
= value
->type
;
620 result
->operands
= operand
; /* set operand count */
622 while (operand
<3) /* clear remaining operands */
623 result
->oprs
[operand
++].type
= 0;
626 * Transform RESW, RESD, RESQ, REST into RESB.
628 switch (result
->opcode
) {
629 case I_RESW
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=2; break;
630 case I_RESD
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=4; break;
631 case I_RESQ
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=8; break;
632 case I_REST
: result
->opcode
=I_RESB
; result
->oprs
[0].offset
*=10; break;
638 static int is_comma_next (void) {
644 i
= stdscan (NULL
, &tv
);
646 return (i
== ',' || i
== ';' || !i
);
649 void cleanup_insn (insn
*i
) {
654 i
->eops
= i
->eops
->next
;