* ld-mmix/bpo-9.d, ld-mmix/bpo-10.d, ld-mmix/bpo-11.d: Adjust for
[binutils.git] / gas / config / tc-tahoe.c
blob034df4827dba0260556710750adb417e6083d881
1 /* This file is tc-tahoe.c
3 Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1995, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22 #include "as.h"
23 #include "safe-ctype.h"
24 #include "obstack.h"
26 /* This bit glommed from tahoe-inst.h. */
28 typedef unsigned char byte;
29 typedef byte tahoe_opcodeT;
31 /* This is part of tahoe-ins-parse.c & friends.
32 We want to parse a tahoe instruction text into a tree defined here. */
34 #define TIT_MAX_OPERANDS (4) /* maximum number of operands in one
35 single tahoe instruction */
37 struct top /* tahoe instruction operand */
39 int top_ndx; /* -1, or index register. eg 7=[R7] */
40 int top_reg; /* -1, or register number. eg 7 = R7 or (R7) */
41 byte top_mode; /* Addressing mode byte. This byte, defines
42 which of the 11 modes opcode is. */
44 char top_access; /* Access type wanted for this opperand
45 'b'branch ' 'no-instruction 'amrvw' */
46 char top_width; /* Operand width expected, one of "bwlq?-:!" */
48 char * top_error; /* Say if operand is inappropriate */
50 segT seg_of_operand; /* segment as returned by expression()*/
52 expressionS exp_of_operand; /* The expression as parsed by expression()*/
54 byte top_dispsize; /* Number of bytes in the displacement if we
55 can figure it out */
58 /* The addressing modes for an operand. These numbers are the acutal values
59 for certain modes, so be carefull if you screw with them. */
60 #define TAHOE_DIRECT_REG (0x50)
61 #define TAHOE_REG_DEFERRED (0x60)
63 #define TAHOE_REG_DISP (0xE0)
64 #define TAHOE_REG_DISP_DEFERRED (0xF0)
66 #define TAHOE_IMMEDIATE (0x8F)
67 #define TAHOE_IMMEDIATE_BYTE (0x88)
68 #define TAHOE_IMMEDIATE_WORD (0x89)
69 #define TAHOE_IMMEDIATE_LONGWORD (0x8F)
70 #define TAHOE_ABSOLUTE_ADDR (0x9F)
72 #define TAHOE_DISPLACED_RELATIVE (0xEF)
73 #define TAHOE_DISP_REL_DEFERRED (0xFF)
75 #define TAHOE_AUTO_DEC (0x7E)
76 #define TAHOE_AUTO_INC (0x8E)
77 #define TAHOE_AUTO_INC_DEFERRED (0x9E)
78 /* INDEXED_REG is decided by the existance or lack of a [reg]. */
80 /* These are encoded into top_width when top_access=='b'
81 and it's a psuedo op. */
82 #define TAHOE_WIDTH_ALWAYS_JUMP '-'
83 #define TAHOE_WIDTH_CONDITIONAL_JUMP '?'
84 #define TAHOE_WIDTH_BIG_REV_JUMP '!'
85 #define TAHOE_WIDTH_BIG_NON_REV_JUMP ':'
87 /* The hex code for certain tahoe commands and modes.
88 This is just for readability. */
89 #define TAHOE_JMP (0x71)
90 #define TAHOE_PC_REL_LONG (0xEF)
91 #define TAHOE_BRB (0x11)
92 #define TAHOE_BRW (0x13)
93 /* These, when 'ored' with, or added to, a register number,
94 set up the number for the displacement mode. */
95 #define TAHOE_PC_OR_BYTE (0xA0)
96 #define TAHOE_PC_OR_WORD (0xC0)
97 #define TAHOE_PC_OR_LONG (0xE0)
99 struct tit /* Get it out of the sewer, it stands for
100 tahoe instruction tree (Geeze!). */
102 tahoe_opcodeT tit_opcode; /* The opcode. */
103 byte tit_operands; /* How many operands are here. */
104 struct top tit_operand[TIT_MAX_OPERANDS]; /* Operands */
105 char *tit_error; /* "" or fatal error text */
108 /* end: tahoe-inst.h */
110 /* tahoe.c - tahoe-specific -
111 Not part of gas yet.
114 #include "opcode/tahoe.h"
116 /* This is the number to put at the beginning of the a.out file */
117 long omagic = OMAGIC;
119 /* These chars start a comment anywhere in a source file (except inside
120 another comment or a quoted string. */
121 const char comment_chars[] = "#;";
123 /* These chars only start a comment at the beginning of a line. */
124 const char line_comment_chars[] = "#";
126 /* Chars that can be used to separate mant from exp in floating point nums */
127 const char EXP_CHARS[] = "eE";
129 /* Chars that mean this number is a floating point constant
130 as in 0f123.456
131 or 0d1.234E-12 (see exp chars above)
132 Note: The Tahoe port doesn't support floating point constants. This is
133 consistant with 'as' If it's needed, I can always add it later. */
134 const char FLT_CHARS[] = "df";
136 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
137 changed in read.c . Ideally it shouldn't have to know about it at all,
138 but nothing is ideal around here.
139 (The tahoe has plenty of room, so the change currently isn't needed.)
142 static struct tit t; /* A tahoe instruction after decoding. */
144 void float_cons ();
145 /* A table of pseudo ops (sans .), the function called, and an integer op
146 that the function is called with. */
148 const pseudo_typeS md_pseudo_table[] =
150 {"dfloat", float_cons, 'd'},
151 {"ffloat", float_cons, 'f'},
156 * For Tahoe, relative addresses of "just the right length" are pretty easy.
157 * The branch displacement is always the last operand, even in
158 * synthetic instructions.
159 * For Tahoe, we encode the relax_substateTs (in e.g. fr_substate) as:
161 * 4 3 2 1 0 bit number
162 * ---/ /--+-------+-------+-------+-------+-------+
163 * | what state ? | how long ? |
164 * ---/ /--+-------+-------+-------+-------+-------+
166 * The "how long" bits are 00=byte, 01=word, 10=long.
167 * This is a Un*x convention.
168 * Not all lengths are legit for a given value of (what state).
169 * The four states are listed below.
170 * The "how long" refers merely to the displacement length.
171 * The address usually has some constant bytes in it as well.
174 States for Tahoe address relaxing.
175 1. TAHOE_WIDTH_ALWAYS_JUMP (-)
176 Format: "b-"
177 Tahoe opcodes are: (Hex)
178 jr 11
179 jbr 11
180 Simple branch.
181 Always, 1 byte opcode, then displacement/absolute.
182 If word or longword, change opcode to brw or jmp.
184 2. TAHOE_WIDTH_CONDITIONAL_JUMP (?)
185 J<cond> where <cond> is a simple flag test.
186 Format: "b?"
187 Tahoe opcodes are: (Hex)
188 jneq/jnequ 21
189 jeql/jeqlu 31
190 jgtr 41
191 jleq 51
192 jgeq 81
193 jlss 91
194 jgtru a1
195 jlequ b1
196 jvc c1
197 jvs d1
198 jlssu/jcs e1
199 jgequ/jcc f1
200 Always, you complement 4th bit to reverse the condition.
201 Always, 1-byte opcode, then 1-byte displacement.
203 3. TAHOE_WIDTH_BIG_REV_JUMP (!)
204 Jbc/Jbs where cond tests a memory bit.
205 Format: "rlvlb!"
206 Tahoe opcodes are: (Hex)
207 jbs 0e
208 jbc 1e
209 Always, you complement 4th bit to reverse the condition.
210 Always, 1-byte opcde, longword, longword-address, 1-word-displacement
212 4. TAHOE_WIDTH_BIG_NON_REV_JUMP (:)
213 JaoblXX/Jbssi
214 Format: "rlmlb:"
215 Tahoe opcodes are: (Hex)
216 aojlss 2f
217 jaoblss 2f
218 aojleq 3f
219 jaobleq 3f
220 jbssi 5f
221 Always, we cannot reverse the sense of the branch; we have a word
222 displacement.
224 We need to modify the opcode is for class 1, 2 and 3 instructions.
225 After relax() we may complement the 4th bit of 2 or 3 to reverse sense of
226 branch.
228 We sometimes store context in the operand literal. This way we can figure out
229 after relax() what the original addressing mode was. (Was is pc_rel, or
230 pc_rel_disp? That sort of thing.) */
232 /* These displacements are relative to the START address of the
233 displacement which is at the start of the displacement, not the end of
234 the instruction. The hardware pc_rel is at the end of the instructions.
235 That's why all the displacements have the length of the displacement added
236 to them. (WF + length(word))
238 The first letter is Byte, Word.
239 2nd letter is Forward, Backward. */
240 #define BF (1+ 127)
241 #define BB (1+-128)
242 #define WF (2+ 32767)
243 #define WB (2+-32768)
244 /* Dont need LF, LB because they always reach. [They are coded as 0.] */
246 #define C(a,b) ENCODE_RELAX(a,b)
247 /* This macro has no side-effects. */
248 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
249 #define RELAX_STATE(s) ((s) >> 2)
250 #define RELAX_LENGTH(s) ((s) & 3)
252 #define STATE_ALWAYS_BRANCH (1)
253 #define STATE_CONDITIONAL_BRANCH (2)
254 #define STATE_BIG_REV_BRANCH (3)
255 #define STATE_BIG_NON_REV_BRANCH (4)
256 #define STATE_PC_RELATIVE (5)
258 #define STATE_BYTE (0)
259 #define STATE_WORD (1)
260 #define STATE_LONG (2)
261 #define STATE_UNDF (3) /* Symbol undefined in pass1 */
263 /* This is the table used by gas to figure out relaxing modes. The fields are
264 forward_branch reach, backward_branch reach, number of bytes it would take,
265 where the next biggest branch is. */
266 const relax_typeS md_relax_table[] =
269 1, 1, 0, 0
270 }, /* error sentinel 0,0 */
272 1, 1, 0, 0
273 }, /* unused 0,1 */
275 1, 1, 0, 0
276 }, /* unused 0,2 */
278 1, 1, 0, 0
279 }, /* unused 0,3 */
280 /* Unconditional branch cases "jrb"
281 The relax part is the actual displacement */
283 BF, BB, 1, C (1, 1)
284 }, /* brb B`foo 1,0 */
286 WF, WB, 2, C (1, 2)
287 }, /* brw W`foo 1,1 */
289 0, 0, 5, 0
290 }, /* Jmp L`foo 1,2 */
292 1, 1, 0, 0
293 }, /* unused 1,3 */
294 /* Reversible Conditional Branch. If the branch won't reach, reverse
295 it, and jump over a brw or a jmp that will reach. The relax part is the
296 actual address. */
298 BF, BB, 1, C (2, 1)
299 }, /* b<cond> B`foo 2,0 */
301 WF + 2, WB + 2, 4, C (2, 2)
302 }, /* brev over, brw W`foo, over: 2,1 */
304 0, 0, 7, 0
305 }, /* brev over, jmp L`foo, over: 2,2 */
307 1, 1, 0, 0
308 }, /* unused 2,3 */
309 /* Another type of reversable branch. But this only has a word
310 displacement. */
312 1, 1, 0, 0
313 }, /* unused 3,0 */
315 WF, WB, 2, C (3, 2)
316 }, /* jbX W`foo 3,1 */
318 0, 0, 8, 0
319 }, /* jrevX over, jmp L`foo, over: 3,2 */
321 1, 1, 0, 0
322 }, /* unused 3,3 */
323 /* These are the non reversable branches, all of which have a word
324 displacement. If I can't reach, branch over a byte branch, to a
325 jump that will reach. The jumped branch jumps over the reaching
326 branch, to continue with the flow of the program. It's like playing
327 leap frog. */
329 1, 1, 0, 0
330 }, /* unused 4,0 */
332 WF, WB, 2, C (4, 2)
333 }, /* aobl_ W`foo 4,1 */
335 0, 0, 10, 0
336 }, /*aobl_ W`hop,br over,hop: jmp L^foo,over 4,2*/
338 1, 1, 0, 0
339 }, /* unused 4,3 */
340 /* Normal displacement mode, no jumping or anything like that.
341 The relax points to one byte before the address, thats why all
342 the numbers are up by one. */
344 BF + 1, BB + 1, 2, C (5, 1)
345 }, /* B^"foo" 5,0 */
347 WF + 1, WB + 1, 3, C (5, 2)
348 }, /* W^"foo" 5,1 */
350 0, 0, 5, 0
351 }, /* L^"foo" 5,2 */
353 1, 1, 0, 0
354 }, /* unused 5,3 */
357 #undef C
358 #undef BF
359 #undef BB
360 #undef WF
361 #undef WB
362 /* End relax stuff */
364 /* Handle of the OPCODE hash table. NULL means any use before
365 md_begin() will crash. */
366 static struct hash_control *op_hash;
368 /* Init function. Build the hash table. */
369 void
370 md_begin ()
372 struct tot *tP;
373 char *errorval = 0;
374 int synthetic_too = 1; /* If 0, just use real opcodes. */
376 op_hash = hash_new ();
378 for (tP = totstrs; *tP->name && !errorval; tP++)
379 errorval = hash_insert (op_hash, tP->name, &tP->detail);
381 if (synthetic_too)
382 for (tP = synthetic_totstrs; *tP->name && !errorval; tP++)
383 errorval = hash_insert (op_hash, tP->name, &tP->detail);
385 if (errorval)
386 as_fatal (errorval);
389 const char *md_shortopts = "ad:STt:V";
390 struct option md_longopts[] = {
391 {NULL, no_argument, NULL, 0}
393 size_t md_longopts_size = sizeof (md_longopts);
396 md_parse_option (c, arg)
397 int c;
398 char *arg;
400 switch (c)
402 case 'a':
403 as_warn (_("The -a option doesn't exist. (Despite what the man page says!"));
404 break;
406 case 'd':
407 as_warn (_("Displacement length %s ignored!"), arg);
408 break;
410 case 'S':
411 as_warn (_("SYMBOL TABLE not implemented"));
412 break;
414 case 'T':
415 as_warn (_("TOKEN TRACE not implemented"));
416 break;
418 case 't':
419 as_warn (_("I don't need or use temp. file \"%s\"."), arg);
420 break;
422 case 'V':
423 as_warn (_("I don't use an interpass file! -V ignored"));
424 break;
426 default:
427 return 0;
430 return 1;
433 void
434 md_show_usage (stream)
435 FILE *stream;
437 fprintf (stream, _("\
438 Tahoe options:\n\
439 -a ignored\n\
440 -d LENGTH ignored\n\
441 -J ignored\n\
442 -S ignored\n\
443 -t FILE ignored\n\
444 -T ignored\n\
445 -V ignored\n"));
448 /* The functions in this section take numbers in the machine format, and
449 munges them into Tahoe byte order.
450 They exist primarily for cross assembly purpose. */
451 void /* Knows about order of bytes in address. */
452 md_number_to_chars (con, value, nbytes)
453 char con[]; /* Return 'nbytes' of chars here. */
454 valueT value; /* The value of the bits. */
455 int nbytes; /* Number of bytes in the output. */
457 number_to_chars_bigendian (con, value, nbytes);
460 #ifdef comment
461 void /* Knows about order of bytes in address. */
462 md_number_to_imm (con, value, nbytes)
463 char con[]; /* Return 'nbytes' of chars here. */
464 long int value; /* The value of the bits. */
465 int nbytes; /* Number of bytes in the output. */
467 md_number_to_chars (con, value, nbytes);
470 #endif /* comment */
472 void
473 md_apply_fix3 (fixP, valP, seg)
474 fixS *fixP ATTRIBUTE_UNUSED;
475 valueT * valP ATTRIBUTE_UNUSED;
476 segT seg ATTRIBUTE_UNUSED:
478 /* Should never be called. */
479 know (0);
482 void /* Knows about order of bytes in address. */
483 md_number_to_disp (con, value, nbytes)
484 char con[]; /* Return 'nbytes' of chars here. */
485 long int value; /* The value of the bits. */
486 int nbytes; /* Number of bytes in the output. */
488 md_number_to_chars (con, value, nbytes);
491 void /* Knows about order of bytes in address. */
492 md_number_to_field (con, value, nbytes)
493 char con[]; /* Return 'nbytes' of chars here. */
494 long int value; /* The value of the bits. */
495 int nbytes; /* Number of bytes in the output. */
497 md_number_to_chars (con, value, nbytes);
500 /* Put the bits in an order that a tahoe will understand, despite the ordering
501 of the native machine.
502 On Tahoe: first 4 bytes are normal unsigned big endian long,
503 next three bytes are symbolnum, in kind of 3 byte big endian (least sig. byte last).
504 The last byte is broken up with bit 7 as pcrel,
505 bits 6 & 5 as length,
506 bit 4 as extern and the last nibble as 'undefined'. */
508 #if comment
509 void
510 md_ri_to_chars (ri_p, ri)
511 struct relocation_info *ri_p, ri;
513 byte the_bytes[sizeof (struct relocation_info)];
514 /* The reason I can't just encode these directly into ri_p is that
515 ri_p may point to ri. */
517 /* This is easy */
518 md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address));
520 /* now the fun stuff */
521 the_bytes[4] = (ri.r_symbolnum >> 16) & 0x0ff;
522 the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
523 the_bytes[6] = ri.r_symbolnum & 0x0ff;
524 the_bytes[7] = (((ri.r_extern << 4) & 0x10) | ((ri.r_length << 5) & 0x60) |
525 ((ri.r_pcrel << 7) & 0x80)) & 0xf0;
527 bcopy (the_bytes, (char *) ri_p, sizeof (struct relocation_info));
530 #endif /* comment */
532 /* Put the bits in an order that a tahoe will understand, despite the ordering
533 of the native machine.
534 On Tahoe: first 4 bytes are normal unsigned big endian long,
535 next three bytes are symbolnum, in kind of 3 byte big endian (least sig. byte last).
536 The last byte is broken up with bit 7 as pcrel,
537 bits 6 & 5 as length,
538 bit 4 as extern and the last nibble as 'undefined'. */
540 void
541 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
542 char *where;
543 fixS *fixP;
544 relax_addressT segment_address_in_file;
546 long r_symbolnum;
548 know (fixP->fx_addsy != NULL);
550 md_number_to_chars (where,
551 fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
554 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
555 ? S_GET_TYPE (fixP->fx_addsy)
556 : fixP->fx_addsy->sy_number);
558 where[4] = (r_symbolnum >> 16) & 0x0ff;
559 where[5] = (r_symbolnum >> 8) & 0x0ff;
560 where[6] = r_symbolnum & 0x0ff;
561 where[7] = (((is_pcrel (fixP) << 7) & 0x80)
562 | ((((fixP->fx_type == FX_8 || fixP->fx_type == FX_PCREL8
564 : (fixP->fx_type == FX_16 || fixP->fx_type == FX_PCREL16
566 : (fixP->fx_type == FX_32 || fixP->fx_type == FX_PCREL32
568 : 42)))) << 5) & 0x60)
569 | ((!S_IS_DEFINED (fixP->fx_addsy) << 4) & 0x10));
572 /* Relocate byte stuff */
574 /* This is for broken word. */
575 const int md_short_jump_size = 3;
577 void
578 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
579 char *ptr;
580 addressT from_addr, to_addr;
581 fragS *frag;
582 symbolS *to_symbol;
584 valueT offset;
586 offset = to_addr - (from_addr + 1);
587 *ptr++ = TAHOE_BRW;
588 md_number_to_chars (ptr, offset, 2);
591 const int md_long_jump_size = 6;
592 const int md_reloc_size = 8; /* Size of relocation record */
594 void
595 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
596 char *ptr;
597 addressT from_addr, to_addr;
598 fragS *frag;
599 symbolS *to_symbol;
601 valueT offset;
603 offset = to_addr - (from_addr + 4);
604 *ptr++ = TAHOE_JMP;
605 *ptr++ = TAHOE_PC_REL_LONG;
606 md_number_to_chars (ptr, offset, 4);
609 /* md_estimate_size_before_relax(), called just before relax().
610 Any symbol that is now undefined will not become defined.
611 Return the correct fr_subtype in the frag and the growth beyond
612 fr_fix. */
614 md_estimate_size_before_relax (fragP, segment_type)
615 register fragS *fragP;
616 segT segment_type; /* N_DATA or N_TEXT. */
618 if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
620 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
622 /* Non-relaxable cases. */
623 char *p;
624 int old_fr_fix;
626 old_fr_fix = fragP->fr_fix;
627 p = fragP->fr_literal + old_fr_fix;
628 switch (RELAX_STATE (fragP->fr_subtype))
630 case STATE_PC_RELATIVE:
631 *p |= TAHOE_PC_OR_LONG;
632 /* We now know how big it will be, one long word. */
633 fragP->fr_fix += 1 + 4;
634 fix_new (fragP, old_fr_fix + 1, fragP->fr_symbol,
635 fragP->fr_offset, FX_PCREL32, NULL);
636 break;
638 case STATE_CONDITIONAL_BRANCH:
639 *fragP->fr_opcode ^= 0x10; /* Reverse sense of branch. */
640 *p++ = 6;
641 *p++ = TAHOE_JMP;
642 *p++ = TAHOE_PC_REL_LONG;
643 fragP->fr_fix += 1 + 1 + 1 + 4;
644 fix_new (fragP, old_fr_fix + 3, fragP->fr_symbol,
645 fragP->fr_offset, FX_PCREL32, NULL);
646 break;
648 case STATE_BIG_REV_BRANCH:
649 *fragP->fr_opcode ^= 0x10; /* Reverse sense of branch. */
650 *p++ = 0;
651 *p++ = 6;
652 *p++ = TAHOE_JMP;
653 *p++ = TAHOE_PC_REL_LONG;
654 fragP->fr_fix += 2 + 2 + 4;
655 fix_new (fragP, old_fr_fix + 4, fragP->fr_symbol,
656 fragP->fr_offset, FX_PCREL32, NULL);
657 break;
659 case STATE_BIG_NON_REV_BRANCH:
660 *p++ = 2;
661 *p++ = 0;
662 *p++ = TAHOE_BRB;
663 *p++ = 6;
664 *p++ = TAHOE_JMP;
665 *p++ = TAHOE_PC_REL_LONG;
666 fragP->fr_fix += 2 + 2 + 2 + 4;
667 fix_new (fragP, old_fr_fix + 6, fragP->fr_symbol,
668 fragP->fr_offset, FX_PCREL32, NULL);
669 break;
671 case STATE_ALWAYS_BRANCH:
672 *fragP->fr_opcode = TAHOE_JMP;
673 *p++ = TAHOE_PC_REL_LONG;
674 fragP->fr_fix += 1 + 4;
675 fix_new (fragP, old_fr_fix + 1, fragP->fr_symbol,
676 fragP->fr_offset, FX_PCREL32, NULL);
677 break;
679 default:
680 abort ();
682 frag_wane (fragP);
684 /* Return the growth in the fixed part of the frag. */
685 return fragP->fr_fix - old_fr_fix;
688 /* Relaxable cases. Set up the initial guess for the variable
689 part of the frag. */
690 switch (RELAX_STATE (fragP->fr_subtype))
692 case STATE_PC_RELATIVE:
693 fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
694 break;
695 case STATE_CONDITIONAL_BRANCH:
696 fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
697 break;
698 case STATE_BIG_REV_BRANCH:
699 fragP->fr_subtype = ENCODE_RELAX (STATE_BIG_REV_BRANCH, STATE_WORD);
700 break;
701 case STATE_BIG_NON_REV_BRANCH:
702 fragP->fr_subtype = ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH, STATE_WORD);
703 break;
704 case STATE_ALWAYS_BRANCH:
705 fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE);
706 break;
710 if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
711 abort ();
713 /* Return the size of the variable part of the frag. */
714 return md_relax_table[fragP->fr_subtype].rlx_length;
718 * md_convert_frag();
720 * Called after relax() is finished.
721 * In: Address of frag.
722 * fr_type == rs_machine_dependent.
723 * fr_subtype is what the address relaxed to.
725 * Out: Any fixSs and constants are set up.
726 * Caller will turn frag into a ".space 0".
728 void
729 md_convert_frag (headers, seg, fragP)
730 object_headers *headers;
731 segT seg;
732 register fragS *fragP;
734 register char *addressP; /* -> _var to change. */
735 register char *opcodeP; /* -> opcode char(s) to change. */
736 register short int extension = 0; /* Size of relaxed address.
737 Added to fr_fix: incl. ALL var chars. */
738 register symbolS *symbolP;
739 register long int where;
740 register long int address_of_var;
741 /* Where, in file space, is _var of *fragP? */
742 register long int target_address;
743 /* Where, in file space, does addr point? */
745 know (fragP->fr_type == rs_machine_dependent);
746 where = fragP->fr_fix;
747 addressP = fragP->fr_literal + where;
748 opcodeP = fragP->fr_opcode;
749 symbolP = fragP->fr_symbol;
750 know (symbolP);
751 target_address = S_GET_VALUE (symbolP) + fragP->fr_offset;
752 address_of_var = fragP->fr_address + where;
753 switch (fragP->fr_subtype)
755 case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
756 /* *addressP holds the registers number, plus 0x10, if it's deferred
757 mode. To set up the right mode, just OR the size of this displacement */
758 /* Byte displacement. */
759 *addressP++ |= TAHOE_PC_OR_BYTE;
760 *addressP = target_address - (address_of_var + 2);
761 extension = 2;
762 break;
764 case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
765 /* Word displacement. */
766 *addressP++ |= TAHOE_PC_OR_WORD;
767 md_number_to_chars (addressP, target_address - (address_of_var + 3), 2);
768 extension = 3;
769 break;
771 case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG):
772 /* Long word displacement. */
773 *addressP++ |= TAHOE_PC_OR_LONG;
774 md_number_to_chars (addressP, target_address - (address_of_var + 5), 4);
775 extension = 5;
776 break;
778 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
779 *addressP = target_address - (address_of_var + 1);
780 extension = 1;
781 break;
783 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
784 *opcodeP ^= 0x10; /* Reverse sense of test. */
785 *addressP++ = 3; /* Jump over word branch */
786 *addressP++ = TAHOE_BRW;
787 md_number_to_chars (addressP, target_address - (address_of_var + 4), 2);
788 extension = 4;
789 break;
791 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG):
792 *opcodeP ^= 0x10; /* Reverse sense of test. */
793 *addressP++ = 6;
794 *addressP++ = TAHOE_JMP;
795 *addressP++ = TAHOE_PC_REL_LONG;
796 md_number_to_chars (addressP, target_address, 4);
797 extension = 7;
798 break;
800 case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE):
801 *addressP = target_address - (address_of_var + 1);
802 extension = 1;
803 break;
805 case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD):
806 *opcodeP = TAHOE_BRW;
807 md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
808 extension = 2;
809 break;
811 case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG):
812 *opcodeP = TAHOE_JMP;
813 *addressP++ = TAHOE_PC_REL_LONG;
814 md_number_to_chars (addressP, target_address - (address_of_var + 5), 4);
815 extension = 5;
816 break;
818 case ENCODE_RELAX (STATE_BIG_REV_BRANCH, STATE_WORD):
819 md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
820 extension = 2;
821 break;
823 case ENCODE_RELAX (STATE_BIG_REV_BRANCH, STATE_LONG):
824 *opcodeP ^= 0x10;
825 *addressP++ = 0;
826 *addressP++ = 6;
827 *addressP++ = TAHOE_JMP;
828 *addressP++ = TAHOE_PC_REL_LONG;
829 md_number_to_chars (addressP, target_address, 4);
830 extension = 8;
831 break;
833 case ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH, STATE_WORD):
834 md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
835 extension = 2;
836 break;
838 case ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH, STATE_LONG):
839 *addressP++ = 0;
840 *addressP++ = 2;
841 *addressP++ = TAHOE_BRB;
842 *addressP++ = 6;
843 *addressP++ = TAHOE_JMP;
844 *addressP++ = TAHOE_PC_REL_LONG;
845 md_number_to_chars (addressP, target_address, 4);
846 extension = 10;
847 break;
849 default:
850 BAD_CASE (fragP->fr_subtype);
851 break;
853 fragP->fr_fix += extension;
854 } /* md_convert_frag */
857 /* This is the stuff for md_assemble. */
858 #define FP_REG 13
859 #define SP_REG 14
860 #define PC_REG 15
861 #define BIGGESTREG PC_REG
864 * Parse the string pointed to by START
865 * If it represents a valid register, point START to the character after
866 * the last valid register char, and return the register number (0-15).
867 * If invalid, leave START alone, return -1.
868 * The format has to be exact. I don't do things like eat leading zeros
869 * or the like.
870 * Note: This doesn't check for the next character in the string making
871 * this invalid. Ex: R123 would return 12, it's the callers job to check
872 * what start is point to apon return.
874 * Valid registers are R1-R15, %1-%15, FP (13), SP (14), PC (15)
875 * Case doesn't matter.
878 tahoe_reg_parse (start)
879 char **start; /* A pointer to the string to parse. */
881 register char *regpoint = *start;
882 register int regnum = -1;
884 switch (*regpoint++)
886 case '%': /* Registers can start with a %,
887 R or r, and then a number. */
888 case 'R':
889 case 'r':
890 if (ISDIGIT (*regpoint))
892 /* Got the first digit. */
893 regnum = *regpoint++ - '0';
894 if ((regnum == 1) && ISDIGIT (*regpoint))
896 /* Its a two digit number. */
897 regnum = 10 + (*regpoint++ - '0');
898 if (regnum > BIGGESTREG)
899 { /* Number too big? */
900 regnum = -1;
904 break;
905 case 'F': /* Is it the FP */
906 case 'f':
907 switch (*regpoint++)
909 case 'p':
910 case 'P':
911 regnum = FP_REG;
913 break;
914 case 's': /* How about the SP */
915 case 'S':
916 switch (*regpoint++)
918 case 'p':
919 case 'P':
920 regnum = SP_REG;
922 break;
923 case 'p': /* OR the PC even */
924 case 'P':
925 switch (*regpoint++)
927 case 'c':
928 case 'C':
929 regnum = PC_REG;
931 break;
934 if (regnum != -1)
935 { /* No error, so move string pointer */
936 *start = regpoint;
938 return regnum; /* Return results */
939 } /* tahoe_reg_parse */
942 * This chops up an operand and figures out its modes and stuff.
943 * It's a little touchy about extra characters.
944 * Optex to start with one extra character so it can be overwritten for
945 * the backward part of the parsing.
946 * You can't put a bunch of extra characters in side to
947 * make the command look cute. ie: * foo ( r1 ) [ r0 ]
948 * If you like doing a lot of typing, try COBOL!
949 * Actually, this parser is a little weak all around. It's designed to be
950 * used with compliers, so I emphisise correct decoding of valid code quickly
951 * rather that catching every possable error.
952 * Note: This uses the expression function, so save input_line_pointer before
953 * calling.
955 * Sperry defines the semantics of address modes (and values)
956 * by a two-letter code, explained here.
958 * letter 1: access type
960 * a address calculation - no data access, registers forbidden
961 * b branch displacement
962 * m read - let go of bus - write back "modify"
963 * r read
964 * w write
965 * v bit field address: like 'a' but registers are OK
967 * letter 2: data type (i.e. width, alignment)
969 * b byte
970 * w word
971 * l longword
972 * q quadword (Even regs < 14 allowed) (if 12, you get a warning)
973 * - unconditional synthetic jbr operand
974 * ? simple synthetic reversable branch operand
975 * ! complex synthetic reversable branch operand
976 * : complex synthetic non-reversable branch operand
978 * The '-?!:' letter 2's are not for external consumption. They are used
979 * by GAS for psuedo ops relaxing code.
981 * After parsing topP has:
983 * top_ndx: -1, or the index register. eg 7=[R7]
984 * top_reg: -1, or register number. eg 7 = R7 or (R7)
985 * top_mode: The addressing mode byte. This byte, defines which of
986 * the 11 modes opcode is.
987 * top_access: Access type wanted for this opperand 'b'branch ' '
988 * no-instruction 'amrvw'
989 * top_width: Operand width expected, one of "bwlq?-:!"
990 * exp_of_operand: The expression as parsed by expression()
991 * top_dispsize: Number of bytes in the displacement if we can figure it
992 * out and it's relavent.
994 * Need syntax checks built.
997 void
998 tip_op (optex, topP)
999 char *optex; /* The users text input, with one leading character */
1000 struct top *topP; /* The tahoe instruction with some fields already set:
1001 in: access, width
1002 out: ndx, reg, mode, error, dispsize */
1005 int mode = 0; /* This operand's mode. */
1006 char segfault = *optex; /* To keep the back parsing from freaking. */
1007 char *point = optex + 1; /* Parsing from front to back. */
1008 char *end; /* Parsing from back to front. */
1009 int reg = -1; /* major register, -1 means absent */
1010 int imreg = -1; /* Major register in immediate mode */
1011 int ndx = -1; /* index register number, -1 means absent */
1012 char dec_inc = ' '; /* Is the SP auto-incremented '+' or
1013 auto-decremented '-' or neither ' '. */
1014 int immediate = 0; /* 1 if '$' immediate mode */
1015 int call_width = 0; /* If the caller casts the displacement */
1016 int abs_width = 0; /* The width of the absolute displacment */
1017 int com_width = 0; /* Displacement width required by branch */
1018 int deferred = 0; /* 1 if '*' deferral is used */
1019 byte disp_size = 0; /* How big is this operand. 0 == don't know */
1020 char *op_bad = ""; /* Bad operand error */
1022 char *tp, *temp, c; /* Temporary holders */
1024 char access = topP->top_access; /* Save on a deref. */
1025 char width = topP->top_width;
1027 int really_none = 0; /* Empty expressions evaluate to 0
1028 but I need to know if it's there or not */
1029 expressionS *expP; /* -> expression values for this operand */
1031 /* Does this command restrict the displacement size. */
1032 if (access == 'b')
1033 com_width = (width == 'b' ? 1 :
1034 (width == 'w' ? 2 :
1035 (width == 'l' ? 4 : 0)));
1037 *optex = '\0'; /* This is kind of a back stop for all
1038 the searches to fail on if needed.*/
1039 if (*point == '*')
1040 { /* A dereference? */
1041 deferred = 1;
1042 point++;
1045 /* Force words into a certain mode */
1046 /* Bitch, Bitch, Bitch! */
1048 * Using the ^ operator is ambigous. If I have an absolute label
1049 * called 'w' set to, say 2, and I have the expression 'w^1', do I get
1050 * 1, forced to be in word displacement mode, or do I get the value of
1051 * 'w' or'ed with 1 (3 in this case).
1052 * The default is 'w' as an offset, so that's what I use.
1053 * Stick with `, it does the same, and isn't ambig.
1056 if (*point != '\0' && ((point[1] == '^') || (point[1] == '`')))
1057 switch (*point)
1059 case 'b':
1060 case 'B':
1061 case 'w':
1062 case 'W':
1063 case 'l':
1064 case 'L':
1065 if (com_width)
1066 as_warn (_("Casting a branch displacement is bad form, and is ignored."));
1067 else
1069 c = TOLOWER (*point);
1070 call_width = ((c == 'b') ? 1 :
1071 ((c == 'w') ? 2 : 4));
1073 point += 2;
1074 break;
1077 /* Setting immediate mode */
1078 if (*point == '$')
1080 immediate = 1;
1081 point++;
1085 * I've pulled off all the easy stuff off the front, move to the end and
1086 * yank.
1089 for (end = point; *end != '\0'; end++) /* Move to the end. */
1092 if (end != point) /* Null string? */
1093 end--;
1095 if (end > point && *end == ' ' && end[-1] != '\'')
1096 end--; /* Hop white space */
1098 /* Is this an index reg. */
1099 if ((*end == ']') && (end[-1] != '\''))
1101 temp = end;
1103 /* Find opening brace. */
1104 for (--end; (*end != '[' && end != point); end--)
1107 /* If I found the opening brace, get the index register number. */
1108 if (*end == '[')
1110 tp = end + 1; /* tp should point to the start of a reg. */
1111 ndx = tahoe_reg_parse (&tp);
1112 if (tp != temp)
1113 { /* Reg. parse error. */
1114 ndx = -1;
1116 else
1118 end--; /* Found it, move past brace. */
1120 if (ndx == -1)
1122 op_bad = _("Couldn't parse the [index] in this operand.");
1123 end = point; /* Force all the rest of the tests to fail. */
1126 else
1128 op_bad = _("Couldn't find the opening '[' for the index of this operand.");
1129 end = point; /* Force all the rest of the tests to fail. */
1133 /* Post increment? */
1134 if (*end == '+')
1136 dec_inc = '+';
1137 /* was: *end--; */
1138 end--;
1141 /* register in parens? */
1142 if ((*end == ')') && (end[-1] != '\''))
1144 temp = end;
1146 /* Find opening paren. */
1147 for (--end; (*end != '(' && end != point); end--)
1150 /* If I found the opening paren, get the register number. */
1151 if (*end == '(')
1153 tp = end + 1;
1154 reg = tahoe_reg_parse (&tp);
1155 if (tp != temp)
1157 /* Not a register, but could be part of the expression. */
1158 reg = -1;
1159 end = temp; /* Rest the pointer back */
1161 else
1163 end--; /* Found the reg. move before opening paren. */
1166 else
1168 op_bad = _("Couldn't find the opening '(' for the deref of this operand.");
1169 end = point; /* Force all the rest of the tests to fail. */
1173 /* Pre decrement? */
1174 if (*end == '-')
1176 if (dec_inc != ' ')
1178 op_bad = _("Operand can't be both pre-inc and post-dec.");
1179 end = point;
1181 else
1183 dec_inc = '-';
1184 /* was: *end--; */
1185 end--;
1190 * Everything between point and end is the 'expression', unless it's
1191 * a register name.
1194 c = end[1];
1195 end[1] = '\0';
1197 tp = point;
1198 imreg = tahoe_reg_parse (&point); /* Get the immediate register
1199 if it is there.*/
1200 if (*point != '\0')
1202 /* If there is junk after point, then the it's not immediate reg. */
1203 point = tp;
1204 imreg = -1;
1207 if (imreg != -1 && reg != -1)
1208 op_bad = _("I parsed 2 registers in this operand.");
1211 * Evaluate whats left of the expression to see if it's valid.
1212 * Note again: This assumes that the calling expression has saved
1213 * input_line_pointer. (Nag, nag, nag!)
1216 if (*op_bad == '\0')
1218 /* Statement has no syntax goofs yet: let's sniff the expression. */
1219 input_line_pointer = point;
1220 expP = &(topP->exp_of_operand);
1221 topP->seg_of_operand = expression (expP);
1222 switch (expP->X_op)
1224 case O_absent:
1225 /* No expression. For BSD4.2 compatibility, missing expression is
1226 absolute 0 */
1227 expP->X_op = O_constant;
1228 expP->X_add_number = 0;
1229 really_none = 1;
1230 case O_constant:
1231 /* for SEG_ABSOLUTE, we shouldnt need to set X_op_symbol,
1232 X_add_symbol to any particular value. */
1233 /* But, we will program defensively. Since this situation occurs
1234 rarely so it costs us little to do so. */
1235 expP->X_add_symbol = NULL;
1236 expP->X_op_symbol = NULL;
1237 /* How many bytes are needed to express this abs value? */
1238 abs_width =
1239 ((((expP->X_add_number & 0xFFFFFF80) == 0) ||
1240 ((expP->X_add_number & 0xFFFFFF80) == 0xFFFFFF80)) ? 1 :
1241 (((expP->X_add_number & 0xFFFF8000) == 0) ||
1242 ((expP->X_add_number & 0xFFFF8000) == 0xFFFF8000)) ? 2 : 4);
1244 case O_symbol:
1245 break;
1247 default:
1249 * Major bug. We can't handle the case of an operator
1250 * expression in a synthetic opcode variable-length
1251 * instruction. We don't have a frag type that is smart
1252 * enough to relax an operator, and so we just force all
1253 * operators to behave like SEG_PASS1s. Clearly, if there is
1254 * a demand we can invent a new or modified frag type and
1255 * then coding up a frag for this case will be easy.
1257 need_pass_2 = 1;
1258 op_bad = _("Can't relocate expression error.");
1259 break;
1261 case O_big:
1262 /* This is an error. Tahoe doesn't allow any expressions
1263 bigger that a 32 bit long word. Any bigger has to be referenced
1264 by address. */
1265 op_bad = _("Expression is too large for a 32 bits.");
1266 break;
1268 if (*input_line_pointer != '\0')
1270 op_bad = _("Junk at end of expression.");
1274 end[1] = c;
1276 /* I'm done, so restore optex */
1277 *optex = segfault;
1280 * At this point in the game, we (in theory) have all the components of
1281 * the operand at least parsed. Now it's time to check for syntax/semantic
1282 * errors, and build the mode.
1283 * This is what I have:
1284 * deferred = 1 if '*'
1285 * call_width = 0,1,2,4
1286 * abs_width = 0,1,2,4
1287 * com_width = 0,1,2,4
1288 * immediate = 1 if '$'
1289 * ndx = -1 or reg num
1290 * dec_inc = '-' or '+' or ' '
1291 * reg = -1 or reg num
1292 * imreg = -1 or reg num
1293 * topP->exp_of_operand
1294 * really_none
1296 /* Is there a displacement size? */
1297 disp_size = (call_width ? call_width :
1298 (com_width ? com_width :
1299 abs_width ? abs_width : 0));
1301 if (*op_bad == '\0')
1303 if (imreg != -1)
1305 /* Rn */
1306 mode = TAHOE_DIRECT_REG;
1307 if (deferred || immediate || (dec_inc != ' ') ||
1308 (reg != -1) || !really_none)
1309 op_bad = _("Syntax error in direct register mode.");
1310 else if (ndx != -1)
1311 op_bad = _("You can't index a register in direct register mode.");
1312 else if (imreg == SP_REG && access == 'r')
1313 op_bad =
1314 _("SP can't be the source operand with direct register addressing.");
1315 else if (access == 'a')
1316 op_bad = _("Can't take the address of a register.");
1317 else if (access == 'b')
1318 op_bad = _("Direct Register can't be used in a branch.");
1319 else if (width == 'q' && ((imreg % 2) || (imreg > 13)))
1320 op_bad = _("For quad access, the register must be even and < 14.");
1321 else if (call_width)
1322 op_bad = _("You can't cast a direct register.");
1324 if (*op_bad == '\0')
1326 /* No errors, check for warnings */
1327 if (width == 'q' && imreg == 12)
1328 as_warn (_("Using reg 14 for quadwords can tromp the FP register."));
1330 reg = imreg;
1333 /* We know: imm = -1 */
1335 else if (dec_inc == '-')
1337 /* -(SP) */
1338 mode = TAHOE_AUTO_DEC;
1339 if (deferred || immediate || !really_none)
1340 op_bad = _("Syntax error in auto-dec mode.");
1341 else if (ndx != -1)
1342 op_bad = _("You can't have an index auto dec mode.");
1343 else if (access == 'r')
1344 op_bad = _("Auto dec mode cant be used for reading.");
1345 else if (reg != SP_REG)
1346 op_bad = _("Auto dec only works of the SP register.");
1347 else if (access == 'b')
1348 op_bad = _("Auto dec can't be used in a branch.");
1349 else if (width == 'q')
1350 op_bad = _("Auto dec won't work with quadwords.");
1352 /* We know: imm = -1, dec_inc != '-' */
1354 else if (dec_inc == '+')
1356 if (immediate || !really_none)
1357 op_bad = _("Syntax error in one of the auto-inc modes.");
1358 else if (deferred)
1360 /* *(SP)+ */
1361 mode = TAHOE_AUTO_INC_DEFERRED;
1362 if (reg != SP_REG)
1363 op_bad = _("Auto inc deferred only works of the SP register.");
1364 else if (ndx != -1)
1365 op_bad = _("You can't have an index auto inc deferred mode.");
1366 else if (access == 'b')
1367 op_bad = _("Auto inc can't be used in a branch.");
1369 else
1371 /* (SP)+ */
1372 mode = TAHOE_AUTO_INC;
1373 if (access == 'm' || access == 'w')
1374 op_bad = _("You can't write to an auto inc register.");
1375 else if (reg != SP_REG)
1376 op_bad = _("Auto inc only works of the SP register.");
1377 else if (access == 'b')
1378 op_bad = _("Auto inc can't be used in a branch.");
1379 else if (width == 'q')
1380 op_bad = _("Auto inc won't work with quadwords.");
1381 else if (ndx != -1)
1382 op_bad = _("You can't have an index in auto inc mode.");
1385 /* We know: imm = -1, dec_inc == ' ' */
1387 else if (reg != -1)
1389 if ((ndx != -1) && (reg == SP_REG))
1390 op_bad = _("You can't index the sp register.");
1391 if (deferred)
1393 /* *<disp>(Rn) */
1394 mode = TAHOE_REG_DISP_DEFERRED;
1395 if (immediate)
1396 op_bad = _("Syntax error in register displaced mode.");
1398 else if (really_none)
1400 /* (Rn) */
1401 mode = TAHOE_REG_DEFERRED;
1402 /* if reg = SP then cant be indexed */
1404 else
1406 /* <disp>(Rn) */
1407 mode = TAHOE_REG_DISP;
1410 /* We know: imm = -1, dec_inc == ' ', Reg = -1 */
1412 else
1414 if (really_none)
1415 op_bad = _("An offest is needed for this operand.");
1416 if (deferred && immediate)
1418 /* *$<ADDR> */
1419 mode = TAHOE_ABSOLUTE_ADDR;
1420 disp_size = 4;
1422 else if (immediate)
1424 /* $<disp> */
1425 mode = TAHOE_IMMEDIATE;
1426 if (ndx != -1)
1427 op_bad = _("You can't index a register in immediate mode.");
1428 if (access == 'a')
1429 op_bad = _("Immediate access can't be used as an address.");
1430 /* ponder the wisdom of a cast because it doesn't do any good. */
1432 else if (deferred)
1434 /* *<disp> */
1435 mode = TAHOE_DISP_REL_DEFERRED;
1437 else
1439 /* <disp> */
1440 mode = TAHOE_DISPLACED_RELATIVE;
1446 * At this point, all the errors we can do have be checked for.
1447 * We can build the 'top'. */
1449 topP->top_ndx = ndx;
1450 topP->top_reg = reg;
1451 topP->top_mode = mode;
1452 topP->top_error = op_bad;
1453 topP->top_dispsize = disp_size;
1454 } /* tip_op */
1457 * t i p ( )
1459 * This converts a string into a tahoe instruction.
1460 * The string must be a bare single instruction in tahoe (with BSD4 frobs)
1461 * format.
1462 * It provides at most one fatal error message (which stops the scan)
1463 * some warning messages as it finds them.
1464 * The tahoe instruction is returned in exploded form.
1466 * The exploded instruction is returned to a struct tit of your choice.
1467 * #include "tahoe-inst.h" to know what a struct tit is.
1471 static void
1472 tip (titP, instring)
1473 struct tit *titP; /* We build an exploded instruction here. */
1474 char *instring; /* Text of a vax instruction: we modify. */
1476 register struct tot_wot *twP = NULL; /* How to bit-encode this opcode. */
1477 register char *p; /* 1/skip whitespace.2/scan vot_how */
1478 register char *q; /* */
1479 register unsigned char count; /* counts number of operands seen */
1480 register struct top *operandp;/* scan operands in struct tit */
1481 register char *alloperr = ""; /* error over all operands */
1482 register char c; /* Remember char, (we clobber it
1483 with '\0' temporarily). */
1484 char *save_input_line_pointer;
1486 if (*instring == ' ')
1487 ++instring; /* Skip leading whitespace. */
1488 for (p = instring; *p && *p != ' '; p++)
1489 ; /* MUST end in end-of-string or
1490 exactly 1 space. */
1491 /* Scanned up to end of operation-code. */
1492 /* Operation-code is ended with whitespace. */
1493 if (p == instring)
1495 titP->tit_error = _("No operator");
1496 count = 0;
1497 titP->tit_opcode = 0;
1499 else
1501 c = *p;
1502 *p = '\0';
1504 * Here with instring pointing to what better be an op-name, and p
1505 * pointing to character just past that.
1506 * We trust instring points to an op-name, with no whitespace.
1508 twP = (struct tot_wot *) hash_find (op_hash, instring);
1509 *p = c; /* Restore char after op-code. */
1510 if (twP == 0)
1512 titP->tit_error = _("Unknown operator");
1513 count = 0;
1514 titP->tit_opcode = 0;
1516 else
1519 * We found a match! So let's pick up as many operands as the
1520 * instruction wants, and even gripe if there are too many.
1521 * We expect comma to seperate each operand.
1522 * We let instring track the text, while p tracks a part of the
1523 * struct tot.
1526 count = 0; /* no operands seen yet */
1527 instring = p + (*p != '\0'); /* point past the operation code */
1528 /* tip_op() screws with the input_line_pointer, so save it before
1529 I jump in */
1530 save_input_line_pointer = input_line_pointer;
1531 for (p = twP->args, operandp = titP->tit_operand;
1532 !*alloperr && *p;
1533 operandp++, p += 2)
1536 * Here to parse one operand. Leave instring pointing just
1537 * past any one ',' that marks the end of this operand.
1539 if (!p[1])
1540 as_fatal (_("Compiler bug: ODD number of bytes in arg structure %s."),
1541 twP->args);
1542 else if (*instring)
1544 for (q = instring; (*q != ',' && *q != '\0'); q++)
1546 if (*q == '\'' && q[1] != '\0') /* Jump quoted characters */
1547 q++;
1549 c = *q;
1551 * Q points to ',' or '\0' that ends argument. C is that
1552 * character.
1554 *q = '\0';
1555 operandp->top_access = p[0];
1556 operandp->top_width = p[1];
1557 tip_op (instring - 1, operandp);
1558 *q = c; /* Restore input text. */
1559 if (*(operandp->top_error))
1561 alloperr = operandp->top_error;
1563 instring = q + (c ? 1 : 0); /* next operand (if any) */
1564 count++; /* won another argument, may have an operr */
1566 else
1567 alloperr = _("Not enough operands");
1569 /* Restore the pointer. */
1570 input_line_pointer = save_input_line_pointer;
1572 if (!*alloperr)
1574 if (*instring == ' ')
1575 instring++; /* Skip whitespace. */
1576 if (*instring)
1577 alloperr = _("Too many operands");
1579 titP->tit_error = alloperr;
1583 titP->tit_opcode = twP->code; /* The op-code. */
1584 titP->tit_operands = count;
1585 } /* tip */
1587 /* md_assemble() emit frags for 1 instruction */
1588 void
1589 md_assemble (instruction_string)
1590 char *instruction_string; /* A string: assemble 1 instruction. */
1592 char *p;
1593 register struct top *operandP;/* An operand. Scans all operands. */
1594 /* char c_save; fixme: remove this line *//* What used to live after an expression. */
1595 /* struct frag *fragP; fixme: remove this line *//* Fragment of code we just made. */
1596 /* register struct top *end_operandP; fixme: remove this line *//* -> slot just after last operand
1597 Limit of the for (each operand). */
1598 register expressionS *expP; /* -> expression values for this operand */
1600 /* These refer to an instruction operand expression. */
1601 segT to_seg; /* Target segment of the address. */
1603 register valueT this_add_number;
1604 register symbolS *this_add_symbol; /* +ve (minuend) symbol. */
1606 /* tahoe_opcodeT opcode_as_number; fixme: remove this line *//* The opcode as a number. */
1607 char *opcodeP; /* Where it is in a frag. */
1608 /* char *opmodeP; fixme: remove this line *//* Where opcode type is, in a frag. */
1610 int dispsize; /* From top_dispsize: tahoe_operand_width
1611 (in bytes) */
1612 int is_undefined; /* 1 if operand expression's
1613 segment not known yet. */
1614 int pc_rel; /* Is this operand pc relative? */
1616 /* Decode the operand. */
1617 tip (&t, instruction_string);
1620 * Check to see if this operand decode properly.
1621 * Notice that we haven't made any frags yet.
1622 * If it goofed, then this instruction will wedge in any pass,
1623 * and we can safely flush it, without causing interpass symbol phase
1624 * errors. That is, without changing label values in different passes.
1626 if (*t.tit_error)
1628 as_warn (_("Ignoring statement due to \"%s\""), t.tit_error);
1630 else
1632 /* We saw no errors in any operands - try to make frag(s) */
1633 /* Emit op-code. */
1634 /* Remember where it is, in case we want to modify the op-code later. */
1635 opcodeP = frag_more (1);
1636 *opcodeP = t.tit_opcode;
1637 /* Now do each operand. */
1638 for (operandP = t.tit_operand;
1639 operandP < t.tit_operand + t.tit_operands;
1640 operandP++)
1641 { /* for each operand */
1642 expP = &(operandP->exp_of_operand);
1643 if (operandP->top_ndx >= 0)
1645 /* Indexed addressing byte
1646 Legality of indexed mode already checked: it is OK */
1647 FRAG_APPEND_1_CHAR (0x40 + operandP->top_ndx);
1648 } /* if(top_ndx>=0) */
1650 /* Here to make main operand frag(s). */
1651 this_add_number = expP->X_add_number;
1652 this_add_symbol = expP->X_add_symbol;
1653 to_seg = operandP->seg_of_operand;
1654 know (to_seg == SEG_UNKNOWN || \
1655 to_seg == SEG_ABSOLUTE || \
1656 to_seg == SEG_DATA || \
1657 to_seg == SEG_TEXT || \
1658 to_seg == SEG_BSS);
1659 is_undefined = (to_seg == SEG_UNKNOWN);
1660 /* Do we know how big this opperand is? */
1661 dispsize = operandP->top_dispsize;
1662 pc_rel = 0;
1663 /* Deal with the branch possabilities. (Note, this doesn't include
1664 jumps.)*/
1665 if (operandP->top_access == 'b')
1667 /* Branches must be expressions. A psuedo branch can also jump to
1668 an absolute address. */
1669 if (to_seg == now_seg || is_undefined)
1671 /* If is_undefined, then it might BECOME now_seg by relax time. */
1672 if (dispsize)
1674 /* I know how big the branch is supposed to be (it's a normal
1675 branch), so I set up the frag, and let GAS do the rest. */
1676 p = frag_more (dispsize);
1677 fix_new (frag_now, p - frag_now->fr_literal,
1678 this_add_symbol, this_add_number,
1679 size_to_fx (dispsize, 1),
1680 NULL);
1682 else
1684 /* (to_seg==now_seg || to_seg == SEG_UNKNOWN) && dispsize==0 */
1685 /* If we don't know how big it is, then its a synthetic branch,
1686 so we set up a simple relax state. */
1687 switch (operandP->top_width)
1689 case TAHOE_WIDTH_CONDITIONAL_JUMP:
1690 /* Simple (conditional) jump. I may have to reverse the
1691 condition of opcodeP, and then jump to my destination.
1692 I set 1 byte aside for the branch off set, and could need 6
1693 more bytes for the pc_rel jump */
1694 frag_var (rs_machine_dependent, 7, 1,
1695 ENCODE_RELAX (STATE_CONDITIONAL_BRANCH,
1696 is_undefined ? STATE_UNDF : STATE_BYTE),
1697 this_add_symbol, this_add_number, opcodeP);
1698 break;
1699 case TAHOE_WIDTH_ALWAYS_JUMP:
1700 /* Simple (unconditional) jump. I may have to convert this to
1701 a word branch, or an absolute jump. */
1702 frag_var (rs_machine_dependent, 5, 1,
1703 ENCODE_RELAX (STATE_ALWAYS_BRANCH,
1704 is_undefined ? STATE_UNDF : STATE_BYTE),
1705 this_add_symbol, this_add_number, opcodeP);
1706 break;
1707 /* The smallest size for the next 2 cases is word. */
1708 case TAHOE_WIDTH_BIG_REV_JUMP:
1709 frag_var (rs_machine_dependent, 8, 2,
1710 ENCODE_RELAX (STATE_BIG_REV_BRANCH,
1711 is_undefined ? STATE_UNDF : STATE_WORD),
1712 this_add_symbol, this_add_number,
1713 opcodeP);
1714 break;
1715 case TAHOE_WIDTH_BIG_NON_REV_JUMP:
1716 frag_var (rs_machine_dependent, 10, 2,
1717 ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH,
1718 is_undefined ? STATE_UNDF : STATE_WORD),
1719 this_add_symbol, this_add_number,
1720 opcodeP);
1721 break;
1722 default:
1723 as_fatal (_("Compliler bug: Got a case (%d) I wasn't expecting."),
1724 operandP->top_width);
1728 else
1730 /* to_seg != now_seg && to_seg != seg_unknown (still in branch)
1731 In other words, I'm jumping out of my segment so extend the
1732 branches to jumps, and let GAS fix them. */
1734 /* These are "branches" what will always be branches around a jump
1735 to the correct addresss in real life.
1736 If to_seg is SEG_ABSOLUTE, just encode the branch in,
1737 else let GAS fix the address. */
1739 switch (operandP->top_width)
1741 /* The theory:
1742 For SEG_ABSOLUTE, then mode is ABSOLUTE_ADDR, jump
1743 to that addresss (not pc_rel).
1744 For other segs, address is a long word PC rel jump. */
1745 case TAHOE_WIDTH_CONDITIONAL_JUMP:
1746 /* b<cond> */
1747 /* To reverse the condition in a TAHOE branch,
1748 complement bit 4 */
1749 *opcodeP ^= 0x10;
1750 p = frag_more (7);
1751 *p++ = 6;
1752 *p++ = TAHOE_JMP;
1753 *p++ = (operandP->top_mode ==
1754 TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR :
1755 TAHOE_PC_REL_LONG);
1756 fix_new (frag_now, p - frag_now->fr_literal,
1757 this_add_symbol, this_add_number,
1758 (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL);
1760 * Now (eg) BLEQ 1f
1761 * JMP foo
1762 * 1:
1764 break;
1765 case TAHOE_WIDTH_ALWAYS_JUMP:
1766 /* br, just turn it into a jump */
1767 *opcodeP = TAHOE_JMP;
1768 p = frag_more (5);
1769 *p++ = (operandP->top_mode ==
1770 TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR :
1771 TAHOE_PC_REL_LONG);
1772 fix_new (frag_now, p - frag_now->fr_literal,
1773 this_add_symbol, this_add_number,
1774 (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL);
1775 /* Now (eg) JMP foo */
1776 break;
1777 case TAHOE_WIDTH_BIG_REV_JUMP:
1778 p = frag_more (8);
1779 *opcodeP ^= 0x10;
1780 *p++ = 0;
1781 *p++ = 6;
1782 *p++ = TAHOE_JMP;
1783 *p++ = (operandP->top_mode ==
1784 TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR :
1785 TAHOE_PC_REL_LONG);
1786 fix_new (frag_now, p - frag_now->fr_literal,
1787 this_add_symbol, this_add_number,
1788 (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL);
1790 * Now (eg) ACBx 1f
1791 * JMP foo
1792 * 1:
1794 break;
1795 case TAHOE_WIDTH_BIG_NON_REV_JUMP:
1796 p = frag_more (10);
1797 *p++ = 0;
1798 *p++ = 2;
1799 *p++ = TAHOE_BRB;
1800 *p++ = 6;
1801 *p++ = TAHOE_JMP;
1802 *p++ = (operandP->top_mode ==
1803 TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR :
1804 TAHOE_PC_REL_LONG);
1805 fix_new (frag_now, p - frag_now->fr_literal,
1806 this_add_symbol, this_add_number,
1807 (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL);
1809 * Now (eg) xOBxxx 1f
1810 * BRB 2f
1811 * 1: JMP @#foo
1812 * 2:
1814 break;
1815 case 'b':
1816 case 'w':
1817 as_warn (_("Real branch displacements must be expressions."));
1818 break;
1819 default:
1820 as_fatal (_("Complier error: I got an unknown synthetic branch :%c"),
1821 operandP->top_width);
1822 break;
1826 else
1828 /* It ain't a branch operand. */
1829 switch (operandP->top_mode)
1831 /* Auto-foo access, only works for one reg (SP)
1832 so the only thing needed is the mode. */
1833 case TAHOE_AUTO_DEC:
1834 case TAHOE_AUTO_INC:
1835 case TAHOE_AUTO_INC_DEFERRED:
1836 FRAG_APPEND_1_CHAR (operandP->top_mode);
1837 break;
1839 /* Numbered Register only access. Only thing needed is the
1840 mode + Register number */
1841 case TAHOE_DIRECT_REG:
1842 case TAHOE_REG_DEFERRED:
1843 FRAG_APPEND_1_CHAR (operandP->top_mode + operandP->top_reg);
1844 break;
1846 /* An absolute address. It's size is always 5 bytes.
1847 (mode_type + 4 byte address). */
1848 case TAHOE_ABSOLUTE_ADDR:
1849 know ((this_add_symbol == NULL));
1850 p = frag_more (5);
1851 *p = TAHOE_ABSOLUTE_ADDR;
1852 md_number_to_chars (p + 1, this_add_number, 4);
1853 break;
1855 /* Immediate data. If the size isn't known, then it's an address
1856 + and offset, which is 4 bytes big. */
1857 case TAHOE_IMMEDIATE:
1858 if (this_add_symbol != NULL)
1860 p = frag_more (5);
1861 *p++ = TAHOE_IMMEDIATE_LONGWORD;
1862 fix_new (frag_now, p - frag_now->fr_literal,
1863 this_add_symbol, this_add_number,
1864 FX_32, NULL);
1866 else
1868 /* It's an integer, and I know it's size. */
1869 if ((unsigned) this_add_number < 0x40)
1871 /* Will it fit in a literal? */
1872 FRAG_APPEND_1_CHAR ((byte) this_add_number);
1874 else
1876 p = frag_more (dispsize + 1);
1877 switch (dispsize)
1879 case 1:
1880 *p++ = TAHOE_IMMEDIATE_BYTE;
1881 *p = (byte) this_add_number;
1882 break;
1883 case 2:
1884 *p++ = TAHOE_IMMEDIATE_WORD;
1885 md_number_to_chars (p, this_add_number, 2);
1886 break;
1887 case 4:
1888 *p++ = TAHOE_IMMEDIATE_LONGWORD;
1889 md_number_to_chars (p, this_add_number, 4);
1890 break;
1894 break;
1896 /* Distance from the PC. If the size isn't known, we have to relax
1897 into it. The difference between this and disp(sp) is that
1898 this offset is pc_rel, and disp(sp) isn't.
1899 Note the drop through code. */
1901 case TAHOE_DISPLACED_RELATIVE:
1902 case TAHOE_DISP_REL_DEFERRED:
1903 operandP->top_reg = PC_REG;
1904 pc_rel = 1;
1906 /* Register, plus a displacement mode. Save the register number,
1907 and weather its deffered or not, and relax the size if it isn't
1908 known. */
1909 case TAHOE_REG_DISP:
1910 case TAHOE_REG_DISP_DEFERRED:
1911 if (operandP->top_mode == TAHOE_DISP_REL_DEFERRED ||
1912 operandP->top_mode == TAHOE_REG_DISP_DEFERRED)
1913 operandP->top_reg += 0x10; /* deffered mode is always 0x10 higher
1914 than it's non-deffered sibling. */
1916 /* Is this a value out of this segment?
1917 The first part of this conditional is a cludge to make gas
1918 produce the same output as 'as' when there is a lable, in
1919 the current segment, displaceing a register. It's strange,
1920 and no one in their right mind would do it, but it's easy
1921 to cludge. */
1922 if ((dispsize == 0 && !pc_rel) ||
1923 (to_seg != now_seg && !is_undefined && to_seg != SEG_ABSOLUTE))
1924 dispsize = 4;
1926 if (dispsize == 0)
1929 * We have a SEG_UNKNOWN symbol, or the size isn't cast.
1930 * It might turn out to be in the same segment as
1931 * the instruction, permitting relaxation.
1933 p = frag_var (rs_machine_dependent, 5, 2,
1934 ENCODE_RELAX (STATE_PC_RELATIVE,
1935 is_undefined ? STATE_UNDF : STATE_BYTE),
1936 this_add_symbol, this_add_number, 0);
1937 *p = operandP->top_reg;
1939 else
1941 /* Either this is an abs, or a cast. */
1942 p = frag_more (dispsize + 1);
1943 switch (dispsize)
1945 case 1:
1946 *p = TAHOE_PC_OR_BYTE + operandP->top_reg;
1947 break;
1948 case 2:
1949 *p = TAHOE_PC_OR_WORD + operandP->top_reg;
1950 break;
1951 case 4:
1952 *p = TAHOE_PC_OR_LONG + operandP->top_reg;
1953 break;
1955 fix_new (frag_now, p + 1 - frag_now->fr_literal,
1956 this_add_symbol, this_add_number,
1957 size_to_fx (dispsize, pc_rel), NULL);
1959 break;
1960 default:
1961 as_fatal (_("Barf, bad mode %x\n"), operandP->top_mode);
1964 } /* for(operandP) */
1965 } /* if(!need_pass_2 && !goofed) */
1966 } /* tahoe_assemble() */
1968 /* We have no need to default values of symbols. */
1970 symbolS *
1971 md_undefined_symbol (name)
1972 char *name;
1974 return 0;
1975 } /* md_undefined_symbol() */
1977 /* Round up a section size to the appropriate boundary. */
1978 valueT
1979 md_section_align (segment, size)
1980 segT segment;
1981 valueT size;
1983 return ((size + 7) & ~7); /* Round all sects to multiple of 8 */
1984 } /* md_section_align() */
1986 /* Exactly what point is a PC-relative offset relative TO?
1987 On the sparc, they're relative to the address of the offset, plus
1988 its size. This gets us to the following instruction.
1989 (??? Is this right? FIXME-SOON) */
1990 long
1991 md_pcrel_from (fixP)
1992 fixS *fixP;
1994 return (((fixP->fx_type == FX_8
1995 || fixP->fx_type == FX_PCREL8)
1997 : ((fixP->fx_type == FX_16
1998 || fixP->fx_type == FX_PCREL16)
2000 : ((fixP->fx_type == FX_32
2001 || fixP->fx_type == FX_PCREL32)
2003 : 0))) + fixP->fx_where + fixP->fx_frag->fr_address);
2004 } /* md_pcrel_from() */
2007 tc_is_pcrel (fixP)
2008 fixS *fixP;
2010 /* should never be called */
2011 know (0);
2012 return (0);
2013 } /* tc_is_pcrel() */