PageRenderTime 69ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mingw-w64-v2.0.999/binutils/src/gas/config/tc-m68hc11.c

#
C | 4493 lines | 3521 code | 541 blank | 431 comment | 1193 complexity | ffe5b29f83b80da1e7012f091761c993 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, LGPL-3.0, Unlicense, GPL-2.0, LGPL-2.0, BSD-3-Clause, GPL-3.0
  1. /* tc-m68hc11.c -- Assembler code for the Motorola 68HC11 & 68HC12.
  2. Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010,
  3. 2011, 2012
  4. Free Software Foundation, Inc.
  5. Written by Stephane Carrez (stcarrez@nerim.fr)
  6. XGATE and S12X added by James Murray (jsm@jsm-net.demon.co.uk)
  7. 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 3, or (at your option)
  11. any later version.
  12. GAS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING. If not, write to
  18. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  19. Boston, MA 02110-1301, USA. */
  20. #include "as.h"
  21. #include "safe-ctype.h"
  22. #include "subsegs.h"
  23. #include "opcode/m68hc11.h"
  24. #include "dwarf2dbg.h"
  25. #include "elf/m68hc11.h"
  26. const char comment_chars[] = ";!";
  27. const char line_comment_chars[] = "#*";
  28. const char line_separator_chars[] = "";
  29. const char EXP_CHARS[] = "eE";
  30. const char FLT_CHARS[] = "dD";
  31. #define STATE_CONDITIONAL_BRANCH (1)
  32. #define STATE_PC_RELATIVE (2)
  33. #define STATE_INDEXED_OFFSET (3)
  34. #define STATE_INDEXED_PCREL (4)
  35. #define STATE_XBCC_BRANCH (5)
  36. #define STATE_CONDITIONAL_BRANCH_6812 (6)
  37. #define STATE_BYTE (0)
  38. #define STATE_BITS5 (0)
  39. #define STATE_WORD (1)
  40. #define STATE_BITS9 (1)
  41. #define STATE_LONG (2)
  42. #define STATE_BITS16 (2)
  43. #define STATE_UNDF (3) /* Symbol undefined in pass1 */
  44. /* This macro has no side-effects. */
  45. #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
  46. #define RELAX_STATE(s) ((s) >> 2)
  47. #define RELAX_LENGTH(s) ((s) & 3)
  48. #define IS_OPCODE(C1,C2) (((C1) & 0x0FF) == ((C2) & 0x0FF))
  49. /* This table describes how you change sizes for the various types of variable
  50. size expressions. This version only supports two kinds. */
  51. /* The fields are:
  52. How far Forward this mode will reach.
  53. How far Backward this mode will reach.
  54. How many bytes this mode will add to the size of the frag.
  55. Which mode to go to if the offset won't fit in this one. */
  56. relax_typeS md_relax_table[] =
  57. {
  58. {1, 1, 0, 0}, /* First entries aren't used. */
  59. {1, 1, 0, 0}, /* For no good reason except. */
  60. {1, 1, 0, 0}, /* that the VAX doesn't either. */
  61. {1, 1, 0, 0},
  62. /* Relax for bcc <L>.
  63. These insns are translated into b!cc +3 jmp L. */
  64. {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD)},
  65. {0, 0, 3, 0},
  66. {1, 1, 0, 0},
  67. {1, 1, 0, 0},
  68. /* Relax for bsr <L> and bra <L>.
  69. These insns are translated into jsr and jmp. */
  70. {(127), (-128), 0, ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD)},
  71. {0, 0, 1, 0},
  72. {1, 1, 0, 0},
  73. {1, 1, 0, 0},
  74. /* Relax for indexed offset: 5-bits, 9-bits, 16-bits. */
  75. {(15), (-16), 0, ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9)},
  76. {(255), (-256), 1, ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16)},
  77. {0, 0, 2, 0},
  78. {1, 1, 0, 0},
  79. /* Relax for PC relative offset: 5-bits, 9-bits, 16-bits.
  80. For the 9-bit case, there will be a -1 correction to take into
  81. account the new byte that's why the range is -255..256. */
  82. {(15), (-16), 0, ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9)},
  83. {(256), (-255), 1, ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16)},
  84. {0, 0, 2, 0},
  85. {1, 1, 0, 0},
  86. /* Relax for dbeq/ibeq/tbeq r,<L>:
  87. These insns are translated into db!cc +3 jmp L. */
  88. {(255), (-256), 0, ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD)},
  89. {0, 0, 3, 0},
  90. {1, 1, 0, 0},
  91. {1, 1, 0, 0},
  92. /* Relax for bcc <L> on 68HC12.
  93. These insns are translated into lbcc <L>. */
  94. {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD)},
  95. {0, 0, 2, 0},
  96. {1, 1, 0, 0},
  97. {1, 1, 0, 0},
  98. };
  99. /* 68HC11 and 68HC12 registers. They are numbered according to the 68HC12. */
  100. typedef enum register_id
  101. {
  102. REG_NONE = -1,
  103. REG_A = 0,
  104. REG_B = 1,
  105. REG_CCR = 2,
  106. REG_D = 4,
  107. REG_X = 5,
  108. REG_Y = 6,
  109. REG_SP = 7,
  110. REG_PC = 8,
  111. REG_R0 = 0,
  112. REG_R1 = 1,
  113. REG_R2 = 2,
  114. REG_R3 = 3,
  115. REG_R4 = 4,
  116. REG_R5 = 5,
  117. REG_R6 = 6,
  118. REG_R7 = 7,
  119. REG_SP_XG = 8,
  120. REG_PC_XG = 9,
  121. REG_CCR_XG = 10
  122. } register_id;
  123. typedef struct operand
  124. {
  125. expressionS exp;
  126. register_id reg1;
  127. register_id reg2;
  128. int mode;
  129. } operand;
  130. struct m68hc11_opcode_def
  131. {
  132. long format;
  133. int min_operands;
  134. int max_operands;
  135. int nb_modes;
  136. int used;
  137. struct m68hc11_opcode *opcode;
  138. };
  139. static struct m68hc11_opcode_def *m68hc11_opcode_defs = 0;
  140. static int m68hc11_nb_opcode_defs = 0;
  141. typedef struct alias
  142. {
  143. const char *name;
  144. const char *alias;
  145. } alias;
  146. static alias alias_opcodes[] =
  147. {
  148. {"cpd", "cmpd"},
  149. {"cpx", "cmpx"},
  150. {"cpy", "cmpy"},
  151. {0, 0}
  152. };
  153. struct m9s12xg_opcode_def
  154. {
  155. long format;
  156. int min_operands;
  157. int max_operands;
  158. int nb_modes;
  159. int used;
  160. struct m9s12xg_opcode *opcode;
  161. };
  162. /* Local functions. */
  163. static register_id reg_name_search (char *);
  164. static register_id register_name (void);
  165. static int cmp_opcode (struct m68hc11_opcode *, struct m68hc11_opcode *);
  166. static char *print_opcode_format (struct m68hc11_opcode *, int);
  167. static char *skip_whites (char *);
  168. static int check_range (long, int);
  169. static void print_opcode_list (void);
  170. static void get_default_target (void);
  171. static void print_insn_format (char *);
  172. static int get_operand (operand *, int, long);
  173. static void fixup8 (expressionS *, int, int);
  174. static void fixup16 (expressionS *, int, int);
  175. static void fixup24 (expressionS *, int, int);
  176. static void fixup8_xg (expressionS *, int, int);
  177. static unsigned char convert_branch (unsigned char);
  178. static char *m68hc11_new_insn (int);
  179. static void build_dbranch_insn (struct m68hc11_opcode *,
  180. operand *, int, int);
  181. static int build_indexed_byte (operand *, int, int);
  182. static int build_reg_mode (operand *, int);
  183. static struct m68hc11_opcode *find (struct m68hc11_opcode_def *,
  184. operand *, int);
  185. static struct m68hc11_opcode *find_opcode (struct m68hc11_opcode_def *,
  186. operand *, int *);
  187. static void build_jump_insn (struct m68hc11_opcode *, operand *, int, int);
  188. static void build_insn_xg (struct m68hc11_opcode *, operand *, int);
  189. static void build_insn (struct m68hc11_opcode *, operand *, int);
  190. static int relaxable_symbol (symbolS *);
  191. /* Pseudo op to indicate a relax group. */
  192. static void s_m68hc11_relax (int);
  193. /* Pseudo op to control the ELF flags. */
  194. static void s_m68hc11_mode (int);
  195. /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
  196. are using 'rtc' for returning. It is necessary to use 'call'
  197. to invoke them. This is also used by the debugger to correctly
  198. find the stack frame. */
  199. static void s_m68hc11_mark_symbol (int);
  200. /* Controls whether relative branches can be turned into long branches.
  201. When the relative offset is too large, the insn are changed:
  202. bra -> jmp
  203. bsr -> jsr
  204. bcc -> b!cc +3
  205. jmp L
  206. dbcc -> db!cc +3
  207. jmp L
  208. Setting the flag forbidds this. */
  209. static short flag_fixed_branches = 0;
  210. /* Force to use long jumps (absolute) instead of relative branches. */
  211. static short flag_force_long_jumps = 0;
  212. /* Change the direct addressing mode into an absolute addressing mode
  213. when the insn does not support direct addressing.
  214. For example, "clr *ZD0" is normally not possible and is changed
  215. into "clr ZDO". */
  216. static short flag_strict_direct_addressing = 1;
  217. /* When an opcode has invalid operand, print out the syntax of the opcode
  218. to stderr. */
  219. static short flag_print_insn_syntax = 0;
  220. /* Dumps the list of instructions with syntax and then exit:
  221. 1 -> Only dumps the list (sorted by name)
  222. 2 -> Generate an example (or test) that can be compiled. */
  223. static short flag_print_opcodes = 0;
  224. /* Opcode hash table. */
  225. static struct hash_control *m68hc11_hash;
  226. /* Current cpu (either cpu6811 or cpu6812). This is determined automagically
  227. by 'get_default_target' by looking at default BFD vector. This is overridden
  228. with the -m<cpu> option. */
  229. static int current_architecture = 0;
  230. /* Default cpu determined by 'get_default_target'. */
  231. static const char *default_cpu;
  232. /* Number of opcodes in the sorted table (filtered by current cpu). */
  233. static int num_opcodes;
  234. /* The opcodes sorted by name and filtered by current cpu. */
  235. static struct m68hc11_opcode *m68hc11_sorted_opcodes;
  236. /* ELF flags to set in the output file header. */
  237. static int elf_flags = E_M68HC11_F64;
  238. /* These are the machine dependent pseudo-ops. These are included so
  239. the assembler can work on the output from the SUN C compiler, which
  240. generates these. */
  241. /* This table describes all the machine specific pseudo-ops the assembler
  242. has to support. The fields are:
  243. pseudo-op name without dot
  244. function to call to execute this pseudo-op
  245. Integer arg to pass to the function. */
  246. const pseudo_typeS md_pseudo_table[] =
  247. {
  248. /* The following pseudo-ops are supported for MRI compatibility. */
  249. {"fcb", cons, 1},
  250. {"fdb", cons, 2},
  251. {"fqb", cons, 4},
  252. {"fcc", stringer, 8 + 1},
  253. {"rmb", s_space, 0},
  254. /* Motorola ALIS. */
  255. {"xrefb", s_ignore, 0}, /* Same as xref */
  256. /* Gcc driven relaxation. */
  257. {"relax", s_m68hc11_relax, 0},
  258. /* .mode instruction (ala SH). */
  259. {"mode", s_m68hc11_mode, 0},
  260. /* .far instruction. */
  261. {"far", s_m68hc11_mark_symbol, STO_M68HC12_FAR},
  262. /* .interrupt instruction. */
  263. {"interrupt", s_m68hc11_mark_symbol, STO_M68HC12_INTERRUPT},
  264. {0, 0, 0}
  265. };
  266. /* Options and initialization. */
  267. const char *md_shortopts = "Sm:";
  268. struct option md_longopts[] =
  269. {
  270. #define OPTION_FORCE_LONG_BRANCH (OPTION_MD_BASE)
  271. {"force-long-branches", no_argument, NULL, OPTION_FORCE_LONG_BRANCH},
  272. {"force-long-branchs", no_argument, NULL, OPTION_FORCE_LONG_BRANCH}, /* Misspelt version kept for backwards compatibility. */
  273. #define OPTION_SHORT_BRANCHES (OPTION_MD_BASE + 1)
  274. {"short-branches", no_argument, NULL, OPTION_SHORT_BRANCHES},
  275. {"short-branchs", no_argument, NULL, OPTION_SHORT_BRANCHES}, /* Misspelt version kept for backwards compatibility. */
  276. #define OPTION_STRICT_DIRECT_MODE (OPTION_MD_BASE + 2)
  277. {"strict-direct-mode", no_argument, NULL, OPTION_STRICT_DIRECT_MODE},
  278. #define OPTION_PRINT_INSN_SYNTAX (OPTION_MD_BASE + 3)
  279. {"print-insn-syntax", no_argument, NULL, OPTION_PRINT_INSN_SYNTAX},
  280. #define OPTION_PRINT_OPCODES (OPTION_MD_BASE + 4)
  281. {"print-opcodes", no_argument, NULL, OPTION_PRINT_OPCODES},
  282. #define OPTION_GENERATE_EXAMPLE (OPTION_MD_BASE + 5)
  283. {"generate-example", no_argument, NULL, OPTION_GENERATE_EXAMPLE},
  284. #define OPTION_MSHORT (OPTION_MD_BASE + 6)
  285. {"mshort", no_argument, NULL, OPTION_MSHORT},
  286. #define OPTION_MLONG (OPTION_MD_BASE + 7)
  287. {"mlong", no_argument, NULL, OPTION_MLONG},
  288. #define OPTION_MSHORT_DOUBLE (OPTION_MD_BASE + 8)
  289. {"mshort-double", no_argument, NULL, OPTION_MSHORT_DOUBLE},
  290. #define OPTION_MLONG_DOUBLE (OPTION_MD_BASE + 9)
  291. {"mlong-double", no_argument, NULL, OPTION_MLONG_DOUBLE},
  292. #define OPTION_XGATE_RAMOFFSET (OPTION_MD_BASE + 10)
  293. {"xgate-ramoffset", no_argument, NULL, OPTION_XGATE_RAMOFFSET},
  294. {NULL, no_argument, NULL, 0}
  295. };
  296. size_t md_longopts_size = sizeof (md_longopts);
  297. /* Get the target cpu for the assembler. This is based on the configure
  298. options and on the -m68hc11/-m68hc12 option. If no option is specified,
  299. we must get the default. */
  300. const char *
  301. m68hc11_arch_format (void)
  302. {
  303. get_default_target ();
  304. if (current_architecture & cpu6811)
  305. return "elf32-m68hc11";
  306. else
  307. return "elf32-m68hc12";
  308. }
  309. enum bfd_architecture
  310. m68hc11_arch (void)
  311. {
  312. get_default_target ();
  313. if (current_architecture & cpu6811)
  314. return bfd_arch_m68hc11;
  315. else
  316. return bfd_arch_m68hc12;
  317. }
  318. int
  319. m68hc11_mach (void)
  320. {
  321. return 0;
  322. }
  323. /* Listing header selected according to cpu. */
  324. const char *
  325. m68hc11_listing_header (void)
  326. {
  327. if (current_architecture & cpu6811)
  328. return "M68HC11 GAS ";
  329. else if (current_architecture & cpuxgate)
  330. return "XGATE GAS ";
  331. else if (current_architecture & cpu9s12x)
  332. return "S12X GAS ";
  333. else
  334. return "M68HC12 GAS ";
  335. }
  336. void
  337. md_show_usage (FILE *stream)
  338. {
  339. get_default_target ();
  340. fprintf (stream, _("\
  341. Motorola 68HC11/68HC12/68HCS12 options:\n\
  342. -m68hc11 | -m68hc12 |\n\
  343. -m68hcs12 | -mm9s12x |\n\
  344. -mm9s12xg specify the processor [default %s]\n\
  345. -mshort use 16-bit int ABI (default)\n\
  346. -mlong use 32-bit int ABI\n\
  347. -mshort-double use 32-bit double ABI\n\
  348. -mlong-double use 64-bit double ABI (default)\n\
  349. --force-long-branches always turn relative branches into absolute ones\n\
  350. -S,--short-branches do not turn relative branches into absolute ones\n\
  351. when the offset is out of range\n\
  352. --strict-direct-mode do not turn the direct mode into extended mode\n\
  353. when the instruction does not support direct mode\n\
  354. --print-insn-syntax print the syntax of instruction in case of error\n\
  355. --print-opcodes print the list of instructions with syntax\n\
  356. --xgate-ramoffset offset ram addresses by 0xc000\n\
  357. --generate-example generate an example of each instruction\n\
  358. (used for testing)\n"), default_cpu);
  359. }
  360. /* Try to identify the default target based on the BFD library. */
  361. static void
  362. get_default_target (void)
  363. {
  364. const bfd_target *target;
  365. bfd abfd;
  366. if (current_architecture != 0)
  367. return;
  368. default_cpu = "unknown";
  369. target = bfd_find_target (0, &abfd);
  370. if (target && target->name)
  371. {
  372. if (strcmp (target->name, "elf32-m68hc12") == 0)
  373. {
  374. current_architecture = cpu6812;
  375. default_cpu = "m68hc12";
  376. }
  377. else if (strcmp (target->name, "elf32-m68hc11") == 0)
  378. {
  379. current_architecture = cpu6811;
  380. default_cpu = "m68hc11";
  381. }
  382. else
  383. {
  384. as_bad (_("Default target `%s' is not supported."), target->name);
  385. }
  386. }
  387. }
  388. void
  389. m68hc11_print_statistics (FILE *file)
  390. {
  391. int i;
  392. struct m68hc11_opcode_def *opc;
  393. hash_print_statistics (file, "opcode table", m68hc11_hash);
  394. opc = m68hc11_opcode_defs;
  395. if (opc == 0 || m68hc11_nb_opcode_defs == 0)
  396. return;
  397. /* Dump the opcode statistics table. */
  398. fprintf (file, _("Name # Modes Min ops Max ops Modes mask # Used\n"));
  399. for (i = 0; i < m68hc11_nb_opcode_defs; i++, opc++)
  400. {
  401. fprintf (file, "%-7.7s %5d %7d %7d 0x%08lx %7d\n",
  402. opc->opcode->name,
  403. opc->nb_modes,
  404. opc->min_operands, opc->max_operands, opc->format, opc->used);
  405. }
  406. }
  407. int
  408. md_parse_option (int c, char *arg)
  409. {
  410. get_default_target ();
  411. switch (c)
  412. {
  413. /* -S means keep external to 2 bit offset rather than 16 bit one. */
  414. case OPTION_SHORT_BRANCHES:
  415. case 'S':
  416. flag_fixed_branches = 1;
  417. break;
  418. case OPTION_FORCE_LONG_BRANCH:
  419. flag_force_long_jumps = 1;
  420. break;
  421. case OPTION_PRINT_INSN_SYNTAX:
  422. flag_print_insn_syntax = 1;
  423. break;
  424. case OPTION_PRINT_OPCODES:
  425. flag_print_opcodes = 1;
  426. break;
  427. case OPTION_STRICT_DIRECT_MODE:
  428. flag_strict_direct_addressing = 0;
  429. break;
  430. case OPTION_GENERATE_EXAMPLE:
  431. flag_print_opcodes = 2;
  432. break;
  433. case OPTION_MSHORT:
  434. elf_flags &= ~E_M68HC11_I32;
  435. break;
  436. case OPTION_MLONG:
  437. elf_flags |= E_M68HC11_I32;
  438. break;
  439. case OPTION_MSHORT_DOUBLE:
  440. elf_flags &= ~E_M68HC11_F64;
  441. break;
  442. case OPTION_MLONG_DOUBLE:
  443. elf_flags |= E_M68HC11_F64;
  444. break;
  445. case OPTION_XGATE_RAMOFFSET:
  446. elf_flags |= E_M68HC11_XGATE_RAMOFFSET;
  447. break;
  448. case 'm':
  449. if ((strcasecmp (arg, "68hc11") == 0)
  450. || (strcasecmp (arg, "m68hc11") == 0))
  451. current_architecture = cpu6811;
  452. else if ((strcasecmp (arg, "68hc12") == 0)
  453. || (strcasecmp (arg, "m68hc12") == 0))
  454. current_architecture = cpu6812;
  455. else if ((strcasecmp (arg, "68hcs12") == 0)
  456. || (strcasecmp (arg, "m68hcs12") == 0))
  457. current_architecture = cpu6812 | cpu6812s;
  458. else if (strcasecmp (arg, "m9s12x") == 0)
  459. current_architecture = cpu6812 | cpu6812s | cpu9s12x;
  460. else if ((strcasecmp (arg, "m9s12xg") == 0)
  461. || (strcasecmp (arg, "xgate") == 0))
  462. /* xgate for backwards compatability */
  463. current_architecture = cpuxgate;
  464. else
  465. as_bad (_("Option `%s' is not recognized."), arg);
  466. break;
  467. default:
  468. return 0;
  469. }
  470. return 1;
  471. }
  472. symbolS *
  473. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  474. {
  475. return 0;
  476. }
  477. char *
  478. md_atof (int type, char *litP, int *sizeP)
  479. {
  480. return ieee_md_atof (type, litP, sizeP, TRUE);
  481. }
  482. valueT
  483. md_section_align (asection *seg, valueT addr)
  484. {
  485. int align = bfd_get_section_alignment (stdoutput, seg);
  486. return ((addr + (1 << align) - 1) & (-1 << align));
  487. }
  488. static int
  489. cmp_opcode (struct m68hc11_opcode *op1, struct m68hc11_opcode *op2)
  490. {
  491. return strcmp (op1->name, op2->name);
  492. }
  493. #define IS_CALL_SYMBOL(MODE) \
  494. (((MODE) & (M6812_OP_PAGE|M6811_OP_IND16)) \
  495. == ((M6812_OP_PAGE|M6811_OP_IND16)))
  496. /* Initialize the assembler. Create the opcode hash table
  497. (sorted on the names) with the M6811 opcode table
  498. (from opcode library). */
  499. void
  500. md_begin (void)
  501. {
  502. char *prev_name = "";
  503. struct m68hc11_opcode *opcodes;
  504. struct m68hc11_opcode_def *opc = 0;
  505. int i, j;
  506. get_default_target ();
  507. m68hc11_hash = hash_new ();
  508. /* Get a writable copy of the opcode table and sort it on the names. */
  509. opcodes = (struct m68hc11_opcode *) xmalloc (m68hc11_num_opcodes *
  510. sizeof (struct
  511. m68hc11_opcode));
  512. m68hc11_sorted_opcodes = opcodes;
  513. num_opcodes = 0;
  514. for (i = 0; i < m68hc11_num_opcodes; i++)
  515. {
  516. if (m68hc11_opcodes[i].arch & current_architecture)
  517. {
  518. opcodes[num_opcodes] = m68hc11_opcodes[i];
  519. if (opcodes[num_opcodes].name[0] == 'b'
  520. && opcodes[num_opcodes].format & M6811_OP_JUMP_REL
  521. && !(opcodes[num_opcodes].format & M6811_OP_BITMASK))
  522. {
  523. num_opcodes++;
  524. opcodes[num_opcodes] = m68hc11_opcodes[i];
  525. }
  526. num_opcodes++;
  527. for (j = 0; alias_opcodes[j].name != 0; j++)
  528. if (strcmp (m68hc11_opcodes[i].name, alias_opcodes[j].name) == 0)
  529. {
  530. opcodes[num_opcodes] = m68hc11_opcodes[i];
  531. opcodes[num_opcodes].name = alias_opcodes[j].alias;
  532. num_opcodes++;
  533. break;
  534. }
  535. }
  536. }
  537. qsort (opcodes, num_opcodes, sizeof (struct m68hc11_opcode),
  538. (int (*) (const void*, const void*)) cmp_opcode);
  539. opc = (struct m68hc11_opcode_def *)
  540. xmalloc (num_opcodes * sizeof (struct m68hc11_opcode_def));
  541. m68hc11_opcode_defs = opc--;
  542. /* Insert unique names into hash table. The M6811 instruction set
  543. has several identical opcode names that have different opcodes based
  544. on the operands. This hash table then provides a quick index to
  545. the first opcode with a particular name in the opcode table. */
  546. for (i = 0; i < num_opcodes; i++, opcodes++)
  547. {
  548. int expect;
  549. if (strcmp (prev_name, opcodes->name))
  550. {
  551. prev_name = (char *) opcodes->name;
  552. opc++;
  553. opc->format = 0;
  554. opc->min_operands = 100;
  555. opc->max_operands = 0;
  556. opc->nb_modes = 0;
  557. opc->opcode = opcodes;
  558. opc->used = 0;
  559. hash_insert (m68hc11_hash, opcodes->name, opc);
  560. }
  561. opc->nb_modes++;
  562. opc->format |= opcodes->format;
  563. /* See how many operands this opcode needs. */
  564. expect = 0;
  565. if (opcodes->arch == cpuxgate)
  566. {
  567. if (opcodes->format & (M68XG_OP_IMM3 | M68XG_OP_R | M68XG_OP_REL9
  568. | M68XG_OP_REL10 ))
  569. expect = 1;
  570. else if (opcodes->format & (M68XG_OP_R_R | M68XG_OP_R_IMM4
  571. | M68XG_OP_R_IMM8 | M68XG_OP_R_IMM8))
  572. expect = 2;
  573. else if (opcodes->format & (M68XG_OP_R_R_R | M68XG_OP_R_R_OFFS5
  574. | M68XG_OP_RD_RB_RI | M68XG_OP_RD_RB_RIp
  575. | M68XG_OP_RD_RB_mRI))
  576. expect = 3;
  577. }
  578. else
  579. {
  580. if (opcodes->format & M6811_OP_MASK)
  581. expect++;
  582. if (opcodes->format & M6811_OP_BITMASK)
  583. expect++;
  584. if (opcodes->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  585. expect++;
  586. if (opcodes->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
  587. expect++;
  588. /* Special case for call instruction. */
  589. if ((opcodes->format & M6812_OP_PAGE)
  590. && !(opcodes->format & M6811_OP_IND16))
  591. expect++;
  592. }
  593. if (expect < opc->min_operands)
  594. opc->min_operands = expect;
  595. if (IS_CALL_SYMBOL (opcodes->format))
  596. expect++;
  597. if (expect > opc->max_operands)
  598. opc->max_operands = expect;
  599. }
  600. opc++;
  601. m68hc11_nb_opcode_defs = opc - m68hc11_opcode_defs;
  602. if (flag_print_opcodes)
  603. {
  604. print_opcode_list ();
  605. exit (EXIT_SUCCESS);
  606. }
  607. }
  608. void
  609. m68hc11_init_after_args (void)
  610. {
  611. }
  612. /* Builtin help. */
  613. /* Return a string that represents the operand format for the instruction.
  614. When example is true, this generates an example of operand. This is used
  615. to give an example and also to generate a test. */
  616. static char *
  617. print_opcode_format (struct m68hc11_opcode *opcode, int example)
  618. {
  619. static char buf[128];
  620. int format = opcode->format;
  621. char *p;
  622. p = buf;
  623. buf[0] = 0;
  624. if (current_architecture == cpuxgate)
  625. {
  626. if (format & M68XG_OP_IMM3)
  627. {
  628. if (example)
  629. sprintf (p, "#%d", rand () & 0x007);
  630. else
  631. strcpy (p, _("imm3"));
  632. p = &p[strlen (p)];
  633. }
  634. else if (format & M68XG_OP_R)
  635. {
  636. if (example)
  637. sprintf (p, "R%d", rand () & 0x07);
  638. else
  639. strcpy (p, _("RD"));
  640. p = &p[strlen (p)];
  641. }
  642. else if (format & M68XG_OP_R_R)
  643. {
  644. if (example)
  645. sprintf (p, "R%d,R%d", rand () & 0x07, rand () & 0x07);
  646. else
  647. strcpy (p, _("RD,RS"));
  648. p = &p[strlen (p)];
  649. }
  650. else if (format & M68XG_OP_R_IMM4)
  651. {
  652. if (example)
  653. sprintf (p, "R%d,#%d", rand () & 0x07, rand () & 0x0f);
  654. else
  655. strcpy (p, _("RI, #imm4"));
  656. p = &p[strlen (p)];
  657. }
  658. else if (format & M68XG_OP_R_R_R)
  659. {
  660. if (example)
  661. sprintf (p, "R%d,R%d,R%d", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  662. else
  663. strcpy (p, "RD,RS1,RS2");
  664. p = &p[strlen (p)];
  665. }
  666. else if (format & M68XG_OP_REL9)
  667. {
  668. if (example)
  669. sprintf (p, "%d", rand () & 0x1FF);
  670. else
  671. strcpy (p, "<rel9>");
  672. p = &p[strlen (p)];
  673. }
  674. else if (format & M68XG_OP_REL10)
  675. {
  676. if (example)
  677. sprintf (p, "%d", rand () & 0x3FF);
  678. else
  679. strcpy (p, "<rel10>");
  680. p = &p[strlen (p)];
  681. }
  682. else if (format & M68XG_OP_R_R_OFFS5)
  683. {
  684. if (example)
  685. sprintf (p, "R%d, (R%d, #0x%x)", rand () & 0x07, rand () & 0x07, rand () & 0x1f);
  686. else
  687. strcpy (p, _("RD, (RI,#offs5)"));
  688. p = &p[strlen (p)];
  689. }
  690. else if (format & M68XG_OP_RD_RB_RI)
  691. {
  692. if (example)
  693. sprintf (p, "R%d, (R%d, R%d)", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  694. else
  695. strcpy (p, "RD, (RB, RI)");
  696. p = &p[strlen (p)];
  697. }
  698. else if (format & M68XG_OP_RD_RB_RIp)
  699. {
  700. if (example)
  701. sprintf (p, "R%d, (R%d, R%d+)", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  702. else
  703. strcpy (p, "RD, (RB, RI+)");
  704. p = &p[strlen (p)];
  705. }
  706. else if (format & M68XG_OP_RD_RB_mRI)
  707. {
  708. if (example)
  709. sprintf (p, "R%d, (R%d, -R%d)", rand () & 0x07, rand () & 0x07, rand () & 0x07);
  710. else
  711. strcpy (p, "RD, (RB, -RI)");
  712. p = &p[strlen (p)];
  713. }
  714. else if (format & M68XG_OP_R_IMM8)
  715. {
  716. if (example)
  717. sprintf (p, "R%d, #0x%x", rand () & 0x07, rand () & 0xff);
  718. else
  719. strcpy (p, "RD, #imm8");
  720. p = &p[strlen (p)];
  721. }
  722. else if (format & M68XG_OP_R_IMM16)
  723. {
  724. if (example)
  725. sprintf (p, "R%d, #0x%x", rand () & 0x07, rand () & 0xffff);
  726. else
  727. strcpy (p, "RD, #imm16");
  728. p = &p[strlen (p)];
  729. }
  730. }
  731. else
  732. {
  733. if (format & M6811_OP_IMM8)
  734. {
  735. if (example)
  736. sprintf (p, "#%d", rand () & 0x0FF);
  737. else
  738. strcpy (p, _("#<imm8>"));
  739. p = &p[strlen (p)];
  740. }
  741. if (format & M6811_OP_IMM16)
  742. {
  743. if (example)
  744. sprintf (p, "#%d", rand () & 0x0FFFF);
  745. else
  746. strcpy (p, _("#<imm16>"));
  747. p = &p[strlen (p)];
  748. }
  749. if (format & M6811_OP_IX)
  750. {
  751. if (example)
  752. sprintf (p, "%d,X", rand () & 0x0FF);
  753. else
  754. strcpy (p, _("<imm8>,X"));
  755. p = &p[strlen (p)];
  756. }
  757. if (format & M6811_OP_IY)
  758. {
  759. if (example)
  760. sprintf (p, "%d,X", rand () & 0x0FF);
  761. else
  762. strcpy (p, _("<imm8>,X"));
  763. p = &p[strlen (p)];
  764. }
  765. if (format & M6812_OP_IDX)
  766. {
  767. if (example)
  768. sprintf (p, "%d,X", rand () & 0x0FF);
  769. else
  770. strcpy (p, "n,r");
  771. p = &p[strlen (p)];
  772. }
  773. if (format & M6812_OP_PAGE)
  774. {
  775. if (example)
  776. sprintf (p, ", %d", rand () & 0x0FF);
  777. else
  778. strcpy (p, ", <page>");
  779. p = &p[strlen (p)];
  780. }
  781. if (format & M6811_OP_DIRECT)
  782. {
  783. if (example)
  784. sprintf (p, "*Z%d", rand () & 0x0FF);
  785. else
  786. strcpy (p, _("*<abs8>"));
  787. p = &p[strlen (p)];
  788. }
  789. if (format & M6811_OP_BITMASK)
  790. {
  791. if (buf[0])
  792. *p++ = ' ';
  793. if (example)
  794. sprintf (p, "#$%02x", rand () & 0x0FF);
  795. else
  796. strcpy (p, _("#<mask>"));
  797. p = &p[strlen (p)];
  798. if (format & M6811_OP_JUMP_REL)
  799. *p++ = ' ';
  800. }
  801. if (format & M6811_OP_IND16)
  802. {
  803. if (example)
  804. sprintf (p, _("symbol%d"), rand () & 0x0FF);
  805. else
  806. strcpy (p, _("<abs>"));
  807. p = &p[strlen (p)];
  808. }
  809. if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  810. {
  811. if (example)
  812. {
  813. if (format & M6811_OP_BITMASK)
  814. {
  815. sprintf (p, ".+%d", rand () & 0x7F);
  816. }
  817. else
  818. {
  819. sprintf (p, "L%d", rand () & 0x0FF);
  820. }
  821. }
  822. else
  823. strcpy (p, _("<label>"));
  824. }
  825. }
  826. return buf;
  827. }
  828. /* Prints the list of instructions with the possible operands. */
  829. static void
  830. print_opcode_list (void)
  831. {
  832. int i;
  833. char *prev_name = "";
  834. struct m68hc11_opcode *opcodes;
  835. int example = flag_print_opcodes == 2;
  836. if (example)
  837. printf (_("# Example of `%s' instructions\n\t.sect .text\n_start:\n"),
  838. default_cpu);
  839. opcodes = m68hc11_sorted_opcodes;
  840. /* Walk the list sorted on names (by md_begin). We only report
  841. one instruction per line, and we collect the different operand
  842. formats. */
  843. for (i = 0; i < num_opcodes; i++, opcodes++)
  844. {
  845. char *fmt = print_opcode_format (opcodes, example);
  846. if (example)
  847. {
  848. printf ("L%d:\t", i);
  849. printf ("%s %s\n", opcodes->name, fmt);
  850. }
  851. else
  852. {
  853. if (strcmp (prev_name, opcodes->name))
  854. {
  855. if (i > 0)
  856. printf ("\n");
  857. printf ("%-5.5s ", opcodes->name);
  858. prev_name = (char *) opcodes->name;
  859. }
  860. if (fmt[0])
  861. printf (" [%s]", fmt);
  862. }
  863. }
  864. printf ("\n");
  865. }
  866. /* Print the instruction format. This operation is called when some
  867. instruction is not correct. Instruction format is printed as an
  868. error message. */
  869. static void
  870. print_insn_format (char *name)
  871. {
  872. struct m68hc11_opcode_def *opc;
  873. struct m68hc11_opcode *opcode;
  874. char buf[128];
  875. opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, name);
  876. if (opc == NULL)
  877. {
  878. as_bad (_("Instruction `%s' is not recognized."), name);
  879. return;
  880. }
  881. opcode = opc->opcode;
  882. as_bad (_("Instruction formats for `%s':"), name);
  883. do
  884. {
  885. char *fmt;
  886. fmt = print_opcode_format (opcode, 0);
  887. sprintf (buf, "\t%-5.5s %s", opcode->name, fmt);
  888. as_bad ("%s", buf);
  889. opcode++;
  890. }
  891. while (strcmp (opcode->name, name) == 0);
  892. }
  893. /* Analysis of 68HC11 and 68HC12 operands. */
  894. /* reg_name_search() finds the register number given its name.
  895. Returns the register number or REG_NONE on failure. */
  896. static register_id
  897. reg_name_search (char *name)
  898. {
  899. if (strcasecmp (name, "x") == 0 || strcasecmp (name, "ix") == 0)
  900. return REG_X;
  901. if (strcasecmp (name, "y") == 0 || strcasecmp (name, "iy") == 0)
  902. return REG_Y;
  903. if (strcasecmp (name, "a") == 0)
  904. return REG_A;
  905. if (strcasecmp (name, "b") == 0)
  906. return REG_B;
  907. if (strcasecmp (name, "d") == 0)
  908. return REG_D;
  909. if (strcasecmp (name, "sp") == 0)
  910. return REG_SP;
  911. if (strcasecmp (name, "pc") == 0)
  912. return REG_PC;
  913. if (strcasecmp (name, "ccr") == 0)
  914. return REG_CCR;
  915. /* XGATE */
  916. if (strcasecmp (name, "r0") == 0)
  917. return REG_R0;
  918. if (strcasecmp (name, "r1") == 0)
  919. return REG_R1;
  920. if (strcasecmp (name, "r2") == 0)
  921. return REG_R2;
  922. if (strcasecmp (name, "r3") == 0)
  923. return REG_R3;
  924. if (strcasecmp (name, "r4") == 0)
  925. return REG_R4;
  926. if (strcasecmp (name, "r5") == 0)
  927. return REG_R5;
  928. if (strcasecmp (name, "r6") == 0)
  929. return REG_R6;
  930. if (strcasecmp (name, "r7") == 0)
  931. return REG_R7;
  932. if (strcasecmp (name, "sp") == 0)
  933. return REG_SP_XG;
  934. if (strcasecmp (name, "pc") == 0)
  935. return REG_PC_XG;
  936. if (strcasecmp (name, "ccr") == 0)
  937. return REG_CCR_XG;
  938. return REG_NONE;
  939. }
  940. static char *
  941. skip_whites (char *p)
  942. {
  943. while (*p == ' ' || *p == '\t')
  944. p++;
  945. return p;
  946. }
  947. /* Check the string at input_line_pointer
  948. to see if it is a valid register name. */
  949. static register_id
  950. register_name (void)
  951. {
  952. register_id reg_number;
  953. char c, *p = input_line_pointer;
  954. if (!is_name_beginner (*p++))
  955. return REG_NONE;
  956. while (is_part_of_name (*p++))
  957. continue;
  958. c = *--p;
  959. if (c)
  960. *p++ = 0;
  961. /* Look to see if it's in the register table. */
  962. reg_number = reg_name_search (input_line_pointer);
  963. if (reg_number != REG_NONE)
  964. {
  965. if (c)
  966. *--p = c;
  967. input_line_pointer = p;
  968. return reg_number;
  969. }
  970. if (c)
  971. *--p = c;
  972. return reg_number;
  973. }
  974. #define M6811_OP_CALL_ADDR 0x00800000
  975. #define M6811_OP_PAGE_ADDR 0x04000000
  976. /* Parse a string of operands and return an array of expressions.
  977. Operand mode[0] mode[1] exp[0] exp[1]
  978. #n M6811_OP_IMM16 - O_*
  979. *<exp> M6811_OP_DIRECT - O_*
  980. .{+-}<exp> M6811_OP_JUMP_REL - O_*
  981. <exp> M6811_OP_IND16 - O_*
  982. ,r N,r M6812_OP_IDX M6812_OP_REG O_constant O_register
  983. n,-r M6812_PRE_DEC M6812_OP_REG O_constant O_register
  984. n,+r M6812_PRE_INC " "
  985. n,r- M6812_POST_DEC " "
  986. n,r+ M6812_POST_INC " "
  987. A,r B,r D,r M6811_OP_REG M6812_OP_REG O_register O_register
  988. [D,r] M6811_OP_D_IDX M6812_OP_REG O_register O_register
  989. [n,r] M6811_OP_D_IDX_2 M6812_OP_REG O_constant O_register */
  990. static int
  991. get_operand (operand *oper, int which, long opmode)
  992. {
  993. char *p = input_line_pointer;
  994. int mode;
  995. register_id reg;
  996. oper->exp.X_op = O_absent;
  997. oper->reg1 = REG_NONE;
  998. oper->reg2 = REG_NONE;
  999. mode = M6811_OP_NONE;
  1000. p = skip_whites (p);
  1001. if (*p == 0 || *p == '\n' || *p == '\r')
  1002. {
  1003. input_line_pointer = p;
  1004. return 0;
  1005. }
  1006. if (*p == '*' && (opmode & (M6811_OP_DIRECT | M6811_OP_IND16)))
  1007. {
  1008. mode = M6811_OP_DIRECT;
  1009. p++;
  1010. }
  1011. else if (*p == '#')
  1012. {
  1013. if (!(opmode & (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK)))
  1014. {
  1015. as_bad (_("Immediate operand is not allowed for operand %d."),
  1016. which);
  1017. return -1;
  1018. }
  1019. mode = M6811_OP_IMM16;
  1020. p++;
  1021. if (strncmp (p, "%hi", 3) == 0)
  1022. {
  1023. p += 3;
  1024. mode |= M6811_OP_HIGH_ADDR;
  1025. }
  1026. else if (strncmp (p, "%lo", 3) == 0)
  1027. {
  1028. p += 3;
  1029. mode |= M6811_OP_LOW_ADDR;
  1030. }
  1031. /* %page modifier is used to obtain only the page number
  1032. of the address of a function. */
  1033. else if (strncmp (p, "%page", 5) == 0)
  1034. {
  1035. p += 5;
  1036. mode |= M6811_OP_PAGE_ADDR;
  1037. }
  1038. /* %addr modifier is used to obtain the physical address part
  1039. of the function (16-bit). For 68HC12 the function will be
  1040. mapped in the 16K window at 0x8000 and the value will be
  1041. within that window (although the function address may not fit
  1042. in 16-bit). See bfd/elf32-m68hc12.c for the translation. */
  1043. else if (strncmp (p, "%addr", 5) == 0)
  1044. {
  1045. p += 5;
  1046. mode |= M6811_OP_CALL_ADDR;
  1047. }
  1048. }
  1049. else if (*p == '.' && (p[1] == '+' || p[1] == '-'))
  1050. {
  1051. p++;
  1052. mode = M6811_OP_JUMP_REL;
  1053. }
  1054. else if (*p == '[')
  1055. {
  1056. if (current_architecture & cpu6811)
  1057. as_bad (_("Indirect indexed addressing is not valid for 68HC11."));
  1058. p++;
  1059. mode = M6812_OP_D_IDX;
  1060. p = skip_whites (p);
  1061. }
  1062. else if (*p == ',') /* Special handling of ,x and ,y. */
  1063. {
  1064. p++;
  1065. input_line_pointer = p;
  1066. reg = register_name ();
  1067. if (reg != REG_NONE)
  1068. {
  1069. oper->reg1 = reg;
  1070. oper->exp.X_op = O_constant;
  1071. oper->exp.X_add_number = 0;
  1072. oper->mode = M6812_OP_IDX;
  1073. return 1;
  1074. }
  1075. as_bad (_("Spurious `,' or bad indirect register addressing mode."));
  1076. return -1;
  1077. }
  1078. /* Handle 68HC12 page specification in 'call foo,%page(bar)'. */
  1079. else if ((opmode & M6812_OP_PAGE) && strncmp (p, "%page", 5) == 0)
  1080. {
  1081. p += 5;
  1082. mode = M6811_OP_PAGE_ADDR | M6812_OP_PAGE | M6811_OP_IND16;
  1083. }
  1084. input_line_pointer = p;
  1085. if (mode == M6811_OP_NONE || mode == M6812_OP_D_IDX)
  1086. reg = register_name ();
  1087. else
  1088. reg = REG_NONE;
  1089. if (reg != REG_NONE)
  1090. {
  1091. p = skip_whites (input_line_pointer);
  1092. if (*p == ']' && mode == M6812_OP_D_IDX)
  1093. {
  1094. as_bad
  1095. (_("Missing second register or offset for indexed-indirect mode."));
  1096. return -1;
  1097. }
  1098. oper->reg1 = reg;
  1099. oper->mode = mode | M6812_OP_REG;
  1100. if (*p != ',')
  1101. {
  1102. if (mode == M6812_OP_D_IDX)
  1103. {
  1104. as_bad (_("Missing second register for indexed-indirect mode."));
  1105. return -1;
  1106. }
  1107. return 1;
  1108. }
  1109. p++;
  1110. input_line_pointer = p;
  1111. reg = register_name ();
  1112. if (reg != REG_NONE)
  1113. {
  1114. p = skip_whites (input_line_pointer);
  1115. if (mode == M6812_OP_D_IDX)
  1116. {
  1117. if (*p != ']')
  1118. {
  1119. as_bad (_("Missing `]' to close indexed-indirect mode."));
  1120. return -1;
  1121. }
  1122. p++;
  1123. oper->mode = M6812_OP_D_IDX;
  1124. }
  1125. input_line_pointer = p;
  1126. oper->reg2 = reg;
  1127. return 1;
  1128. }
  1129. return 1;
  1130. }
  1131. /* In MRI mode, isolate the operand because we can't distinguish
  1132. operands from comments. */
  1133. if (flag_mri)
  1134. {
  1135. char c = 0;
  1136. p = skip_whites (p);
  1137. while (*p && *p != ' ' && *p != '\t')
  1138. p++;
  1139. if (*p)
  1140. {
  1141. c = *p;
  1142. *p = 0;
  1143. }
  1144. /* Parse as an expression. */
  1145. expression (&oper->exp);
  1146. if (c)
  1147. {
  1148. *p = c;
  1149. }
  1150. }
  1151. else
  1152. {
  1153. expression (&oper->exp);
  1154. }
  1155. if (oper->exp.X_op == O_illegal)
  1156. {
  1157. as_bad (_("Illegal operand."));
  1158. return -1;
  1159. }
  1160. else if (oper->exp.X_op == O_absent)
  1161. {
  1162. as_bad (_("Missing operand."));
  1163. return -1;
  1164. }
  1165. p = input_line_pointer;
  1166. if (mode == M6811_OP_NONE || mode == M6811_OP_DIRECT
  1167. || mode == M6812_OP_D_IDX)
  1168. {
  1169. p = skip_whites (input_line_pointer);
  1170. if (*p == ',')
  1171. {
  1172. int possible_mode = M6811_OP_NONE;
  1173. char *old_input_line;
  1174. old_input_line = p;
  1175. p++;
  1176. /* 68HC12 pre increment or decrement. */
  1177. if (mode == M6811_OP_NONE)
  1178. {
  1179. if (*p == '-')
  1180. {
  1181. possible_mode = M6812_PRE_DEC;
  1182. p++;
  1183. }
  1184. else if (*p == '+')
  1185. {
  1186. possible_mode = M6812_PRE_INC;
  1187. p++;
  1188. }
  1189. p = skip_whites (p);
  1190. }
  1191. input_line_pointer = p;
  1192. reg = register_name ();
  1193. /* Backtrack if we have a valid constant expression and
  1194. it does not correspond to the offset of the 68HC12 indexed
  1195. addressing mode (as in N,x). */
  1196. if (reg == REG_NONE && mode == M6811_OP_NONE
  1197. && possible_mode != M6811_OP_NONE)
  1198. {
  1199. oper->mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
  1200. input_line_pointer = skip_whites (old_input_line);
  1201. return 1;
  1202. }
  1203. if (possible_mode != M6811_OP_NONE)
  1204. mode = possible_mode;
  1205. if ((current_architecture & cpu6811)
  1206. && possible_mode != M6811_OP_NONE)
  1207. as_bad (_("Pre-increment mode is not valid for 68HC11"));
  1208. /* Backtrack. */
  1209. if (which == 0 && opmode & M6812_OP_IDX_P2
  1210. && reg != REG_X && reg != REG_Y
  1211. && reg != REG_PC && reg != REG_SP)
  1212. {
  1213. reg = REG_NONE;
  1214. input_line_pointer = p;
  1215. }
  1216. if (reg == REG_NONE && mode != M6811_OP_DIRECT
  1217. && !(mode == M6811_OP_NONE && opmode & M6811_OP_IND16))
  1218. {
  1219. as_bad (_("Wrong register in register indirect mode."));
  1220. return -1;
  1221. }
  1222. if (mode == M6812_OP_D_IDX)
  1223. {
  1224. p = skip_whites (input_line_pointer);
  1225. if (*p++ != ']')
  1226. {
  1227. as_bad (_("Missing `]' to close register indirect operand."));
  1228. return -1;
  1229. }
  1230. input_line_pointer = p;
  1231. oper->reg1 = reg;
  1232. oper->mode = M6812_OP_D_IDX_2;
  1233. return 1;
  1234. }
  1235. if (reg != REG_NONE)
  1236. {
  1237. oper->reg1 = reg;
  1238. if (mode == M6811_OP_NONE)
  1239. {
  1240. p = input_line_pointer;
  1241. if (*p == '-')
  1242. {
  1243. mode = M6812_POST_DEC;
  1244. p++;
  1245. if (current_architecture & cpu6811)
  1246. as_bad
  1247. (_("Post-decrement mode is not valid for 68HC11."));
  1248. }
  1249. else if (*p == '+')
  1250. {
  1251. mode = M6812_POST_INC;
  1252. p++;
  1253. if (current_architecture & cpu6811)
  1254. as_bad
  1255. (_("Post-increment mode is not valid for 68HC11."));
  1256. }
  1257. else
  1258. mode = M6812_OP_IDX;
  1259. input_line_pointer = p;
  1260. }
  1261. else
  1262. mode |= M6812_OP_IDX;
  1263. oper->mode = mode;
  1264. return 1;
  1265. }
  1266. input_line_pointer = old_input_line;
  1267. }
  1268. if (mode == M6812_OP_D_IDX_2)
  1269. {
  1270. as_bad (_("Invalid indexed indirect mode."));
  1271. return -1;
  1272. }
  1273. }
  1274. /* If the mode is not known until now, this is either a label
  1275. or an indirect address. */
  1276. if (mode == M6811_OP_NONE)
  1277. mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
  1278. p = input_line_pointer;
  1279. while (*p == ' ' || *p == '\t')
  1280. p++;
  1281. input_line_pointer = p;
  1282. oper->mode = mode;
  1283. return 1;
  1284. }
  1285. #define M6812_AUTO_INC_DEC (M6812_PRE_INC | M6812_PRE_DEC \
  1286. | M6812_POST_INC | M6812_POST_DEC)
  1287. /* Checks that the number 'num' fits for a given mode. */
  1288. static int
  1289. check_range (long num, int mode)
  1290. {
  1291. if (current_architecture == cpuxgate)
  1292. {
  1293. switch (mode)
  1294. {
  1295. case M68XG_OP_IMM3:
  1296. return (num >= 0 && num <= 7) ? 1 : 0;
  1297. case M68XG_OP_R_IMM4:
  1298. return (num >= 0 && num <= 15) ? 1 : 0;
  1299. case M68XG_OP_R_R_OFFS5:
  1300. return (num >= 0 && num <= 31) ? 1 : 0;
  1301. case M68XG_OP_R_IMM8:
  1302. return (num >= 0 && num <= 255) ? 1 : 0;
  1303. case M68XG_OP_R_IMM16:
  1304. return (num >= 0 && num <= 65535) ? 1 : 0;
  1305. case M68XG_OP_B_MARKER:
  1306. return (num >= -512 && num <= 511) ? 1 : 0;
  1307. case M68XG_OP_BRA_MARKER:
  1308. return (num >= -1024 && num <= 1023) ? 1 : 0;
  1309. default:
  1310. return 0;
  1311. }
  1312. }
  1313. else
  1314. {
  1315. /* Auto increment and decrement are ok for [-8..8] without 0. */
  1316. if (mode & M6812_AUTO_INC_DEC)
  1317. return (num != 0 && num <= 8 && num >= -8);
  1318. /* The 68HC12 supports 5, 9 and 16-bit offsets. */
  1319. if (mode & (M6812_INDEXED_IND | M6812_INDEXED | M6812_OP_IDX))
  1320. mode = M6811_OP_IND16;
  1321. if (mode & M6812_OP_JUMP_REL16)
  1322. mode = M6811_OP_IND16;
  1323. mode &= ~M6811_OP_BRANCH;
  1324. switch (mode)
  1325. {
  1326. case M6811_OP_IX:
  1327. case M6811_OP_IY:
  1328. case M6811_OP_DIRECT:
  1329. return (num >= 0 && num <= 255) ? 1 : 0;
  1330. case M6811_OP_BITMASK:
  1331. case M6811_OP_IMM8:
  1332. case M6812_OP_PAGE:
  1333. return (((num & 0xFFFFFF00) == 0) || ((num & 0xFFFFFF00) == 0xFFFFFF00))
  1334. ? 1 : 0;
  1335. case M6811_OP_JUMP_REL:
  1336. return (num >= -128 && num <= 127) ? 1 : 0;
  1337. case M6811_OP_IND16:
  1338. case M6811_OP_IND16 | M6812_OP_PAGE:
  1339. case M6811_OP_IMM16:
  1340. return (((num & 0xFFFF0000) == 0) || ((num & 0xFFFF0000) == 0xFFFF0000))
  1341. ? 1 : 0;
  1342. case M6812_OP_IBCC_MARKER:
  1343. case M6812_OP_TBCC_MARKER:
  1344. case M6812_OP_DBCC_MARKER:
  1345. return (num >= -256 && num <= 255) ? 1 : 0;
  1346. case M6812_OP_TRAP_ID:
  1347. return ((num >= 0x30 && num <= 0x39)
  1348. || (num >= 0x40 && num <= 0x0ff)) ? 1 : 0;
  1349. default:
  1350. return 0;
  1351. }
  1352. }
  1353. }
  1354. /* Gas fixup generation. */
  1355. /* Put a 1 byte expression described by 'oper'. If this expression contains
  1356. unresolved symbols, generate an 8-bit fixup. */
  1357. static void
  1358. fixup8 (expressionS *oper, int mode, int opmode)
  1359. {
  1360. char *f;
  1361. f = frag_more (1);
  1362. if (oper->X_op == O_constant)
  1363. {
  1364. if (mode & M6812_OP_TRAP_ID
  1365. && !check_range (oper->X_add_number, M6812_OP_TRAP_ID))
  1366. {
  1367. static char trap_id_warn_once = 0;
  1368. as_bad (_("Trap id `%ld' is out of range."), oper->X_add_number);
  1369. if (trap_id_warn_once == 0)
  1370. {
  1371. trap_id_warn_once = 1;
  1372. as_bad (_("Trap id must be within [0x30..0x39] or [0x40..0xff]."));
  1373. }
  1374. }
  1375. if (!(mode & M6812_OP_TRAP_ID)
  1376. && !check_range (oper->X_add_number, mode))
  1377. {
  1378. as_bad (_("Operand out of 8-bit range: `%ld'."), oper->X_add_number);
  1379. }
  1380. number_to_chars_bigendian (f, oper->X_add_number & 0x0FF, 1);
  1381. }
  1382. else if (oper->X_op != O_register)
  1383. {
  1384. if (mode & M6812_OP_TRAP_ID)
  1385. as_bad (_("The trap id must be a constant."));
  1386. if (mode == M6811_OP_JUMP_REL)
  1387. {
  1388. fixS *fixp;
  1389. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1390. oper, TRUE, BFD_RELOC_8_PCREL);
  1391. fixp->fx_pcrel_adjust = 1;
  1392. }
  1393. else
  1394. {
  1395. fixS *fixp;
  1396. int reloc;
  1397. /* Now create an 8-bit fixup. If there was some %hi, %lo
  1398. or %page modifier, generate the reloc accordingly. */
  1399. if (opmode & M6811_OP_HIGH_ADDR)
  1400. reloc = BFD_RELOC_M68HC11_HI8;
  1401. else if (opmode & M6811_OP_LOW_ADDR)
  1402. reloc = BFD_RELOC_M68HC11_LO8;
  1403. else if (opmode & M6811_OP_PAGE_ADDR)
  1404. reloc = BFD_RELOC_M68HC11_PAGE;
  1405. else
  1406. reloc = BFD_RELOC_8;
  1407. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1408. oper, FALSE, reloc);
  1409. if (reloc != BFD_RELOC_8)
  1410. fixp->fx_no_overflow = 1;
  1411. }
  1412. number_to_chars_bigendian (f, 0, 1);
  1413. }
  1414. else
  1415. {
  1416. as_fatal (_("Operand `%x' not recognized in fixup8."), oper->X_op);
  1417. }
  1418. }
  1419. /* Put a 2 byte expression described by 'oper'. If this expression contains
  1420. unresolved symbols, generate a 16-bit fixup. */
  1421. static void
  1422. fixup16 (expressionS *oper, int mode, int opmode ATTRIBUTE_UNUSED)
  1423. {
  1424. char *f;
  1425. f = frag_more (2);
  1426. if (oper->X_op == O_constant)
  1427. {
  1428. if (!check_range (oper->X_add_number, mode))
  1429. {
  1430. as_bad (_("Operand out of 16-bit range: `%ld'."),
  1431. oper->X_add_number);
  1432. }
  1433. number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFF, 2);
  1434. }
  1435. else if (oper->X_op != O_register)
  1436. {
  1437. fixS *fixp;
  1438. int reloc;
  1439. if ((opmode & M6811_OP_CALL_ADDR) && (mode & M6811_OP_IMM16))
  1440. reloc = BFD_RELOC_M68HC11_LO16;
  1441. else if (mode & M6812_OP_JUMP_REL16)
  1442. reloc = BFD_RELOC_16_PCREL;
  1443. else if (mode & M6812_OP_PAGE)
  1444. reloc = BFD_RELOC_M68HC11_LO16;
  1445. else
  1446. reloc = BFD_RELOC_16;
  1447. /* Now create a 16-bit fixup. */
  1448. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 2,
  1449. oper,
  1450. reloc == BFD_RELOC_16_PCREL,
  1451. reloc);
  1452. number_to_chars_bigendian (f, 0, 2);
  1453. if (reloc == BFD_RELOC_16_PCREL)
  1454. fixp->fx_pcrel_adjust = 2;
  1455. if (reloc == BFD_RELOC_M68HC11_LO16)
  1456. fixp->fx_no_overflow = 1;
  1457. }
  1458. else
  1459. {
  1460. as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
  1461. }
  1462. }
  1463. /* Put a 3 byte expression described by 'oper'. If this expression contains
  1464. unresolved symbols, generate a 24-bit fixup. */
  1465. static void
  1466. fixup24 (expressionS *oper, int mode, int opmode ATTRIBUTE_UNUSED)
  1467. {
  1468. char *f;
  1469. f = frag_more (3);
  1470. if (oper->X_op == O_constant)
  1471. {
  1472. if (!check_range (oper->X_add_number, mode))
  1473. {
  1474. as_bad (_("Operand out of 16-bit range: `%ld'."),
  1475. oper->X_add_number);
  1476. }
  1477. number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFFFF, 3);
  1478. }
  1479. else if (oper->X_op != O_register)
  1480. {
  1481. /* Now create a 24-bit fixup. */
  1482. fix_new_exp (frag_now, f - frag_now->fr_literal, 3,
  1483. oper, FALSE, BFD_RELOC_M68HC11_24);
  1484. number_to_chars_bigendian (f, 0, 3);
  1485. }
  1486. else
  1487. {
  1488. as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
  1489. }
  1490. }
  1491. /* XGATE Put a 1 byte expression described by 'oper'. If this expression
  1492. containts unresolved symbols, generate an 8-bit fixup. */
  1493. static void
  1494. fixup8_xg (expressionS *oper, int mode, int opmode)
  1495. {
  1496. char *f;
  1497. f = frag_more (1);
  1498. if (oper->X_op == O_constant)
  1499. {
  1500. fixS *fixp;
  1501. int reloc;
  1502. if ((opmode & M6811_OP_HIGH_ADDR) || (opmode & M6811_OP_LOW_ADDR))
  1503. {
  1504. if (opmode & M6811_OP_HIGH_ADDR)
  1505. reloc = BFD_RELOC_M68HC11_HI8;
  1506. else
  1507. reloc = BFD_RELOC_M68HC11_LO8;
  1508. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1509. oper, FALSE, reloc);
  1510. fixp->fx_no_overflow = 1;
  1511. number_to_chars_bigendian (f, 0, 1);
  1512. }
  1513. else
  1514. {
  1515. if (!(check_range (oper->X_add_number, mode)))
  1516. as_bad (_("Operand out of 8-bit range: `%ld'."),
  1517. oper->X_add_number);
  1518. number_to_chars_bigendian (f, oper->X_add_number & 0x0FF, 1);
  1519. }
  1520. }
  1521. else if (oper->X_op != O_register)
  1522. {
  1523. if (mode == M68XG_OP_REL9)
  1524. {
  1525. fixS *fixp;
  1526. /* Future improvement:
  1527. This fixup/reloc isn't adding on constants to symbols. */
  1528. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal -1, 2,
  1529. oper, TRUE, BFD_RELOC_M68HC12_9_PCREL);
  1530. fixp->fx_pcrel_adjust = 1;
  1531. }
  1532. else if (mode == M68XG_OP_REL10)
  1533. {
  1534. fixS *fixp;
  1535. /* Future improvement:
  1536. This fixup/reloc isn't adding on constants to symbols. */
  1537. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal -1, 2,
  1538. oper, TRUE, BFD_RELOC_M68HC12_10_PCREL);
  1539. fixp->fx_pcrel_adjust = 1;
  1540. }
  1541. else
  1542. {
  1543. fixS *fixp;
  1544. int reloc;
  1545. /* Now create an 8-bit fixup. If there was some %hi, %lo
  1546. modifier, generate the reloc accordingly. */
  1547. if (opmode & M6811_OP_HIGH_ADDR)
  1548. reloc = BFD_RELOC_M68HC11_HI8;
  1549. else if (opmode & M6811_OP_LOW_ADDR)
  1550. reloc = BFD_RELOC_M68HC11_LO8;
  1551. else
  1552. reloc = BFD_RELOC_8;
  1553. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  1554. oper, FALSE, reloc);
  1555. if (reloc != BFD_RELOC_8)
  1556. fixp->fx_no_overflow = 1;
  1557. }
  1558. number_to_chars_bigendian (f, 0, 1);
  1559. }
  1560. else
  1561. as_fatal (_("Operand `%x' not recognized in fixup8."), oper->X_op);
  1562. }
  1563. /* 68HC11 and 68HC12 code generation. */
  1564. /* Translate the short branch/bsr instruction into a long branch. */
  1565. static unsigned char
  1566. convert_branch (unsigned char code)
  1567. {
  1568. if (IS_OPCODE (code, M6812_BSR))
  1569. return M6812_JSR;
  1570. else if (IS_OPCODE (code, M6811_BSR))
  1571. return M6811_JSR;
  1572. else if (IS_OPCODE (code, M6811_BRA))
  1573. return (current_architecture & cpu6812) ? M6812_JMP : M6811_JMP;
  1574. else
  1575. as_fatal (_("Unexpected branch conversion with `%x'"), code);
  1576. /* Keep gcc happy. */
  1577. return M6811_JSR;
  1578. }
  1579. /* Start a new insn that contains at least 'size' bytes. Record the
  1580. line information of that insn in the dwarf2 debug sections. */
  1581. static char *
  1582. m68hc11_new_insn (int size)
  1583. {
  1584. char *f;
  1585. f = frag_more (size);
  1586. dwarf2_emit_insn (size);
  1587. return f;
  1588. }
  1589. /* Builds a jump instruction (bra, bcc, bsr). */
  1590. static void
  1591. build_jump_insn (struct m68hc11_opcode *opcode, operand operands[],
  1592. int nb_operands, int jmp_mode)
  1593. {
  1594. unsigned char code;
  1595. char *f;
  1596. unsigned long n;
  1597. /* The relative branch conversion is not supported for
  1598. brclr and brset. */
  1599. gas_assert ((opcode->format & M6811_OP_BITMASK) == 0);
  1600. gas_assert (nb_operands == 1);
  1601. gas_assert (operands[0].reg1 == REG_NONE && operands[0].reg2 == REG_NONE);
  1602. code = opcode->opcode;
  1603. n = operands[0].exp.X_add_number;
  1604. /* Turn into a long branch:
  1605. - when force long branch option (and not for jbcc pseudos),
  1606. - when jbcc and the constant is out of -128..127 range,
  1607. - when branch optimization is allowed and branch out of range. */
  1608. if ((jmp_mode == 0 && flag_force_long_jumps)
  1609. || (operands[0].exp.X_op == O_constant
  1610. && (!check_range (n, opcode->format) &&
  1611. (jmp_mode == 1 || flag_fixed_branches == 0))))
  1612. {
  1613. fix_new (frag_now, frag_now_fix (), 0,
  1614. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  1615. if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
  1616. {
  1617. code = convert_branch (code);
  1618. f = m68hc11_new_insn (1);
  1619. number_to_chars_bigendian (f, code, 1);
  1620. }
  1621. else if (current_architecture & cpu6812)
  1622. {
  1623. /* 68HC12: translate the bcc into a lbcc. */
  1624. f = m68hc11_new_insn (2);
  1625. number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
  1626. number_to_chars_bigendian (f + 1, code, 1);
  1627. fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16,
  1628. M6812_OP_JUMP_REL16);
  1629. return;
  1630. }
  1631. else
  1632. {
  1633. /* 68HC11: translate the bcc into b!cc +3; jmp <L>. */
  1634. f = m68hc11_new_insn (3);
  1635. code ^= 1;
  1636. number_to_chars_bigendian (f, code, 1);
  1637. number_to_chars_bigendian (f + 1, 3, 1);
  1638. number_to_chars_bigendian (f + 2, M6811_JMP, 1);
  1639. }
  1640. fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
  1641. return;
  1642. }
  1643. /* Branch with a constant that must fit in 8-bits. */
  1644. if (operands[0].exp.X_op == O_constant)
  1645. {
  1646. if (!check_range (n, opcode->format))
  1647. {
  1648. as_bad (_("Operand out of range for a relative branch: `%ld'"),
  1649. n);
  1650. }
  1651. else if (opcode->format & M6812_OP_JUMP_REL16)
  1652. {
  1653. f = m68hc11_new_insn (4);
  1654. number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
  1655. number_to_chars_bigendian (f + 1, code, 1);
  1656. number_to_chars_bigendian (f + 2, n & 0x0ffff, 2);
  1657. }
  1658. else
  1659. {
  1660. f = m68hc11_new_insn (2);
  1661. number_to_chars_bigendian (f, code, 1);
  1662. number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
  1663. }
  1664. }
  1665. else if (opcode->format & M6812_OP_JUMP_REL16)
  1666. {
  1667. fix_new (frag_now, frag_now_fix (), 0,
  1668. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  1669. f = m68hc11_new_insn (2);
  1670. number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
  1671. number_to_chars_bigendian (f + 1, code, 1);
  1672. fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16, M6812_OP_JUMP_REL16);
  1673. }
  1674. else
  1675. {
  1676. char *op;
  1677. fix_new (frag_now, frag_now_fix (), 0,
  1678. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  1679. /* Branch offset must fit in 8-bits, don't do some relax. */
  1680. if (jmp_mode == 0 && flag_fixed_branches)
  1681. {
  1682. op = m68hc11_new_insn (1);
  1683. number_to_chars_bigendian (op, code, 1);
  1684. fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
  1685. }
  1686. /* bra/bsr made be changed into jmp/jsr. */
  1687. else if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
  1688. {
  1689. /* Allocate worst case storage. */
  1690. op = m68hc11_new_insn (3);
  1691. number_to_chars_bigendian (op, code, 1);
  1692. number_to_chars_bigendian (op + 1, 0, 1);
  1693. frag_variant (rs_machine_dependent, 1, 1,
  1694. ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF),
  1695. operands[0].exp.X_add_symbol, (offsetT) n,
  1696. op);
  1697. }
  1698. else if (current_architecture & cpu6812)
  1699. {
  1700. op = m68hc11_new_insn (2);
  1701. number_to_chars_bigendian (op, code, 1);
  1702. number_to_chars_bigendian (op + 1, 0, 1);
  1703. frag_var (rs_machine_dependent, 2, 2,
  1704. ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_UNDF),
  1705. operands[0].exp.X_add_symbol, (offsetT) n, op);
  1706. }
  1707. else
  1708. {
  1709. op = m68hc11_new_insn (2);
  1710. number_to_chars_bigendian (op, code, 1);
  1711. number_to_chars_bigendian (op + 1, 0, 1);
  1712. frag_var (rs_machine_dependent, 3, 3,
  1713. ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF),
  1714. operands[0].exp.X_add_symbol, (offsetT) n, op);
  1715. }
  1716. }
  1717. }
  1718. /* Builds a dbne/dbeq/tbne/tbeq instruction. */
  1719. static void
  1720. build_dbranch_insn (struct m68hc11_opcode *opcode, operand operands[],
  1721. int nb_operands, int jmp_mode)
  1722. {
  1723. unsigned char code;
  1724. char *f;
  1725. unsigned long n;
  1726. /* The relative branch conversion is not supported for
  1727. brclr and brset. */
  1728. gas_assert ((opcode->format & M6811_OP_BITMASK) == 0);
  1729. gas_assert (nb_operands == 2);
  1730. gas_assert (operands[0].reg1 != REG_NONE);
  1731. code = opcode->opcode & 0x0FF;
  1732. f = m68hc11_new_insn (1);
  1733. number_to_chars_bigendian (f, code, 1);
  1734. n = operands[1].exp.X_add_number;
  1735. code = operands[0].reg1;
  1736. if (operands[0].reg1 == REG_NONE || operands[0].reg1 == REG_CCR
  1737. || operands[0].reg1 == REG_PC)
  1738. as_bad (_("Invalid register for dbcc/tbcc instruction."));
  1739. if (opcode->format & M6812_OP_IBCC_MARKER)
  1740. code |= 0x80;
  1741. else if (opcode->format & M6812_OP_TBCC_MARKER)
  1742. code |= 0x40;
  1743. if (!(opcode->format & M6812_OP_EQ_MARKER))
  1744. code |= 0x20;
  1745. /* Turn into a long branch:
  1746. - when force long branch option (and not for jbcc pseudos),
  1747. - when jdbcc and the constant is out of -256..255 range,
  1748. - when branch optimization is allowed and branch out of range. */
  1749. if ((jmp_mode == 0 && flag_force_long_jumps)
  1750. || (operands[1].exp.X_op == O_constant
  1751. && (!check_range (n, M6812_OP_IBCC_MARKER) &&
  1752. (jmp_mode == 1 || flag_fixed_branches == 0))))
  1753. {
  1754. f = frag_more (2);
  1755. code ^= 0x20;
  1756. number_to_chars_bigendian (f, code, 1);
  1757. number_to_chars_bigendian (f + 1, M6812_JMP, 1);
  1758. fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
  1759. return;
  1760. }
  1761. /* Branch with a constant that must fit in 9-bits. */
  1762. if (operands[1].exp.X_op == O_constant)
  1763. {
  1764. if (!check_range (n, M6812_OP_IBCC_MARKER))
  1765. {
  1766. as_bad (_("Operand out of range for a relative branch: `%ld'"),
  1767. n);
  1768. }
  1769. else
  1770. {
  1771. if ((long) n < 0)
  1772. code |= 0x10;
  1773. f = frag_more (2);
  1774. number_to_chars_bigendian (f, code, 1);
  1775. number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
  1776. }
  1777. }
  1778. else
  1779. {
  1780. /* Branch offset must fit in 8-bits, don't do some relax. */
  1781. if (jmp_mode == 0 && flag_fixed_branches)
  1782. {
  1783. fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
  1784. }
  1785. else
  1786. {
  1787. f = frag_more (2);
  1788. number_to_chars_bigendian (f, code, 1);
  1789. number_to_chars_bigendian (f + 1, 0, 1);
  1790. frag_var (rs_machine_dependent, 3, 3,
  1791. ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_UNDF),
  1792. operands[1].exp.X_add_symbol, (offsetT) n, f);
  1793. }
  1794. }
  1795. }
  1796. #define OP_EXTENDED (M6811_OP_PAGE2 | M6811_OP_PAGE3 | M6811_OP_PAGE4)
  1797. /* Assemble the post index byte for 68HC12 extended addressing modes. */
  1798. static int
  1799. build_indexed_byte (operand *op, int format ATTRIBUTE_UNUSED, int move_insn)
  1800. {
  1801. unsigned char byte = 0;
  1802. char *f;
  1803. int mode;
  1804. long val;
  1805. val = op->exp.X_add_number;
  1806. mode = op->mode;
  1807. if (mode & M6812_AUTO_INC_DEC)
  1808. {
  1809. byte = 0x20;
  1810. if (mode & (M6812_POST_INC | M6812_POST_DEC))
  1811. byte |= 0x10;
  1812. if (op->exp.X_op == O_constant)
  1813. {
  1814. if (!check_range (val, mode))
  1815. as_bad (_("Increment/decrement value is out of range: `%ld'."),
  1816. val);
  1817. if (mode & (M6812_POST_INC | M6812_PRE_INC))
  1818. byte |= (val - 1) & 0x07;
  1819. else
  1820. byte |= (8 - ((val) & 7)) | 0x8;
  1821. }
  1822. switch (op->reg1)
  1823. {
  1824. case REG_NONE:
  1825. as_fatal (_("Expecting a register."));
  1826. case REG_X:
  1827. byte |= 0;
  1828. break;
  1829. case REG_Y:
  1830. byte |= 0x40;
  1831. break;
  1832. case REG_SP:
  1833. byte |= 0x80;
  1834. break;
  1835. default:
  1836. as_bad (_("Invalid register for post/pre increment."));
  1837. break;
  1838. }
  1839. f = frag_more (1);
  1840. number_to_chars_bigendian (f, byte, 1);
  1841. return 1;
  1842. }
  1843. if (mode & (M6812_OP_IDX | M6812_OP_D_IDX_2))
  1844. {
  1845. switch (op->reg1)
  1846. {
  1847. case REG_X:
  1848. byte = 0;
  1849. break;
  1850. case REG_Y:
  1851. byte = 1;
  1852. break;
  1853. case REG_SP:
  1854. byte = 2;
  1855. break;
  1856. case REG_PC:
  1857. byte = 3;
  1858. break;
  1859. default:
  1860. as_bad (_("Invalid register."));
  1861. break;
  1862. }
  1863. if (op->exp.X_op == O_constant)
  1864. {
  1865. if (!check_range (val, M6812_OP_IDX))
  1866. as_bad (_("Offset out of 16-bit range: %ld."), val);
  1867. if (move_insn && !(val >= -16 && val <= 15)
  1868. && ((!(mode & M6812_OP_IDX) && !(mode & M6812_OP_D_IDX_2))
  1869. || !(current_architecture & cpu9s12x)))
  1870. {
  1871. as_bad (_("Offset out of 5-bit range for movw/movb insn: %ld."),
  1872. val);
  1873. return -1;
  1874. }
  1875. if (val >= -16 && val <= 15 && !(mode & M6812_OP_D_IDX_2))
  1876. {
  1877. byte = byte << 6;
  1878. byte |= val & 0x1f;
  1879. f = frag_more (1);
  1880. number_to_chars_bigendian (f, byte, 1);
  1881. return 1;
  1882. }
  1883. else if (val >= -256 && val <= 255 && !(mode & M6812_OP_D_IDX_2))
  1884. {
  1885. byte = byte << 3;
  1886. byte |= 0xe0;
  1887. if (val < 0)
  1888. byte |= 0x1;
  1889. f = frag_more (2);
  1890. number_to_chars_bigendian (f, byte, 1);
  1891. number_to_chars_bigendian (f + 1, val & 0x0FF, 1);
  1892. return 2;
  1893. }
  1894. else
  1895. {
  1896. byte = byte << 3;
  1897. if (mode & M6812_OP_D_IDX_2)
  1898. byte |= 0xe3;
  1899. else
  1900. byte |= 0xe2;
  1901. f = frag_more (3);
  1902. number_to_chars_bigendian (f, byte, 1);
  1903. number_to_chars_bigendian (f + 1, val & 0x0FFFF, 2);
  1904. return 3;
  1905. }
  1906. }
  1907. if (mode & M6812_OP_D_IDX_2)
  1908. {
  1909. byte = (byte << 3) | 0xe3;
  1910. f = frag_more (1);
  1911. number_to_chars_bigendian (f, byte, 1);
  1912. fixup16 (&op->exp, 0, 0);
  1913. }
  1914. else if (op->reg1 != REG_PC)
  1915. {
  1916. symbolS *sym;
  1917. offsetT off;
  1918. f = frag_more (1);
  1919. number_to_chars_bigendian (f, byte, 1);
  1920. sym = op->exp.X_add_symbol;
  1921. off = op->exp.X_add_number;
  1922. if (op->exp.X_op != O_symbol)
  1923. {
  1924. sym = make_expr_symbol (&op->exp);
  1925. off = 0;
  1926. }
  1927. /* movb/movw cannot be relaxed. */
  1928. if (move_insn)
  1929. {
  1930. if ((mode & M6812_OP_IDX) && (current_architecture & cpu9s12x))
  1931. {
  1932. /* Must treat as a 16bit relocate as size of final result is unknown. */
  1933. byte <<= 3;
  1934. byte |= 0b11100010;
  1935. number_to_chars_bigendian (f, byte, 1);
  1936. fix_new (frag_now, f - frag_now->fr_literal, 2,
  1937. sym, off, 0, BFD_RELOC_M68HC12_16B);
  1938. f = frag_more (2);
  1939. return 1;
  1940. }
  1941. else
  1942. {
  1943. /* Non-S12X will fail at relocate stage if offset out of range. */
  1944. byte <<= 6;
  1945. number_to_chars_bigendian (f, byte, 1);
  1946. fix_new (frag_now, f - frag_now->fr_literal, 1,
  1947. sym, off, 0, BFD_RELOC_M68HC12_5B);
  1948. return 1;
  1949. }
  1950. }
  1951. else
  1952. {
  1953. number_to_chars_bigendian (f, byte, 1);
  1954. frag_var (rs_machine_dependent, 2, 2,
  1955. ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_UNDF),
  1956. sym, off, f);
  1957. }
  1958. }
  1959. else
  1960. {
  1961. f = frag_more (1);
  1962. /* movb/movw cannot be relaxed. */
  1963. if (move_insn)
  1964. {
  1965. byte <<= 6;
  1966. number_to_chars_bigendian (f, byte, 1);
  1967. fix_new (frag_now, f - frag_now->fr_literal, 1,
  1968. op->exp.X_add_symbol, op->exp.X_add_number, 0, BFD_RELOC_M68HC12_5B);
  1969. return 1;
  1970. }
  1971. else
  1972. {
  1973. number_to_chars_bigendian (f, byte, 1);
  1974. frag_var (rs_machine_dependent, 2, 2,
  1975. ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_UNDF),
  1976. op->exp.X_add_symbol,
  1977. op->exp.X_add_number, f);
  1978. }
  1979. }
  1980. return 3;
  1981. }
  1982. if (mode & (M6812_OP_REG | M6812_OP_D_IDX))
  1983. {
  1984. if (mode & M6812_OP_D_IDX)
  1985. {
  1986. if (op->reg1 != REG_D)
  1987. as_bad (_("Expecting register D for indexed indirect mode."));
  1988. if ((move_insn) && (!(current_architecture & cpu9s12x)))
  1989. as_bad (_("Indexed indirect mode is not allowed for movb/movw."));
  1990. byte = 0xE7;
  1991. }
  1992. else
  1993. {
  1994. switch (op->reg1)
  1995. {
  1996. case REG_A:
  1997. byte = 0xE4;
  1998. break;
  1999. case REG_B:
  2000. byte = 0xE5;
  2001. break;
  2002. default:
  2003. as_bad (_("Invalid accumulator register."));
  2004. case REG_D:
  2005. byte = 0xE6;
  2006. break;
  2007. }
  2008. }
  2009. switch (op->reg2)
  2010. {
  2011. case REG_X:
  2012. break;
  2013. case REG_Y:
  2014. byte |= (1 << 3);
  2015. break;
  2016. case REG_SP:
  2017. byte |= (2 << 3);
  2018. break;
  2019. case REG_PC:
  2020. byte |= (3 << 3);
  2021. break;
  2022. default:
  2023. as_bad (_("Invalid indexed register."));
  2024. break;
  2025. }
  2026. f = frag_more (1);
  2027. number_to_chars_bigendian (f, byte, 1);
  2028. return 1;
  2029. }
  2030. fprintf (stderr, "mode = 0x%x\nop->reg1 = 0x%x\nop->reg2 = 0x%x\n",
  2031. mode, op->reg1, op->reg2);
  2032. as_fatal (_("Addressing mode not implemented yet."));
  2033. return 0;
  2034. }
  2035. /* Assemble the 68HC12 register mode byte. */
  2036. static int
  2037. build_reg_mode (operand *op, int format)
  2038. {
  2039. unsigned char byte;
  2040. char *f;
  2041. if ((format & M6812_OP_SEX_MARKER)
  2042. && (op->reg1 != REG_A) && (op->reg1 != REG_B) && (op->reg1 != REG_CCR)
  2043. && (!(current_architecture & cpu9s12x)))
  2044. as_bad (_("Invalid source register for this instruction, use 'tfr'."));
  2045. else if (op->reg1 == REG_NONE || op->reg1 == REG_PC)
  2046. as_bad (_("Invalid source register."));
  2047. if (format & M6812_OP_SEX_MARKER
  2048. && op->reg2 != REG_D
  2049. && op->reg2 != REG_X && op->reg2 != REG_Y && op->reg2 != REG_SP)
  2050. as_bad (_("Invalid destination register for this instruction, use 'tfr'."));
  2051. else if (op->reg2 == REG_NONE || op->reg2 == REG_PC)
  2052. as_bad (_("Invalid destination register."));
  2053. byte = (op->reg1 << 4) | (op->reg2);
  2054. if (format & M6812_OP_EXG_MARKER)
  2055. byte |= 0x80;
  2056. if ((format & M6812_OP_SEX_MARKER)
  2057. && (op->reg1 == REG_D) && (current_architecture & cpu9s12x))
  2058. byte |= 0x08;
  2059. f = frag_more (1);
  2060. number_to_chars_bigendian (f, byte, 1);
  2061. return 1;
  2062. }
  2063. /* build_insn_xg takes a pointer to the opcode entry in the opcode table,
  2064. the array of operand expressions and builds the corresponding instruction. */
  2065. static void
  2066. build_insn_xg (struct m68hc11_opcode *opcode,
  2067. operand operands[],
  2068. int nb_operands ATTRIBUTE_UNUSED)
  2069. {
  2070. char *f;
  2071. long format;
  2072. /* Put the page code instruction if there is one. */
  2073. format = opcode->format;
  2074. if (!(operands[0].mode & (M6811_OP_LOW_ADDR | M6811_OP_HIGH_ADDR)))
  2075. /* Need to retain those two modes, but clear for others. */
  2076. operands[0].mode = 0;
  2077. if (format & M68XG_OP_R_IMM8)
  2078. {
  2079. /* These opcodes are byte followed by imm8. */
  2080. f = m68hc11_new_insn (1);
  2081. number_to_chars_bigendian (f, opcode->opcode >> 8, 1);
  2082. fixup8_xg (&operands[0].exp, format, operands[0].mode);
  2083. }
  2084. else if (format & M68XG_OP_R_IMM16)
  2085. {
  2086. fixS *fixp;
  2087. /* These opcodes expand into two imm8 instructions.
  2088. Emit as low:high as per the Freescale datasheet.
  2089. The linker requires them to be adjacent to handle the upper byte. */
  2090. /* Build low byte. */
  2091. f = m68hc11_new_insn (1);
  2092. number_to_chars_bigendian (f, opcode->opcode >> 8, 1);
  2093. operands[0].mode = M6811_OP_LOW_ADDR;
  2094. f = frag_more (1);
  2095. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  2096. &operands[0].exp, FALSE, BFD_RELOC_M68HC12_LO8XG);
  2097. fixp->fx_no_overflow = 1;
  2098. number_to_chars_bigendian (f, 0, 1);
  2099. /* Build high byte. */
  2100. f = m68hc11_new_insn (1);
  2101. number_to_chars_bigendian (f, (opcode->opcode >> 8) | 0x08, 1);
  2102. operands[0].mode = M6811_OP_HIGH_ADDR;
  2103. f = frag_more (1);
  2104. fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
  2105. &operands[0].exp, FALSE, BFD_RELOC_M68HC12_HI8XG);
  2106. fixp->fx_no_overflow = 1;
  2107. number_to_chars_bigendian (f, 0, 1);
  2108. }
  2109. else if (format & M68XG_OP_REL9)
  2110. {
  2111. f = m68hc11_new_insn (1);
  2112. number_to_chars_bigendian (f, opcode->opcode >> 8, 1); /* High byte. */
  2113. fixup8_xg (&operands[0].exp, format, M68XG_OP_REL9);
  2114. }
  2115. else if (format & M68XG_OP_REL10)
  2116. {
  2117. f = m68hc11_new_insn (1);
  2118. number_to_chars_bigendian (f, opcode->opcode >> 8, 1); /* High byte. */
  2119. fixup8_xg (&operands[0].exp, format, M68XG_OP_REL10);
  2120. }
  2121. else
  2122. {
  2123. f = m68hc11_new_insn (2);
  2124. number_to_chars_bigendian (f, opcode->opcode, 2);
  2125. }
  2126. return;
  2127. }
  2128. /* build_insn takes a pointer to the opcode entry in the opcode table,
  2129. the array of operand expressions and builds the corresponding instruction.
  2130. This operation only deals with non relative jumps insn (need special
  2131. handling). */
  2132. static void
  2133. build_insn (struct m68hc11_opcode *opcode,
  2134. operand operands[],
  2135. int nb_operands ATTRIBUTE_UNUSED)
  2136. {
  2137. int i;
  2138. char *f;
  2139. long format;
  2140. int move_insn = 0;
  2141. /* Put the page code instruction if there is one. */
  2142. format = opcode->format;
  2143. if (format & M6811_OP_BRANCH)
  2144. fix_new (frag_now, frag_now_fix (), 0,
  2145. &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
  2146. if (format & OP_EXTENDED)
  2147. {
  2148. int page_code;
  2149. f = m68hc11_new_insn (2);
  2150. if (format & M6811_OP_PAGE2)
  2151. page_code = M6811_OPCODE_PAGE2;
  2152. else if (format & M6811_OP_PAGE3)
  2153. page_code = M6811_OPCODE_PAGE3;
  2154. else
  2155. page_code = M6811_OPCODE_PAGE4;
  2156. number_to_chars_bigendian (f, page_code, 1);
  2157. f++;
  2158. }
  2159. else
  2160. f = m68hc11_new_insn (1);
  2161. number_to_chars_bigendian (f, opcode->opcode, 1);
  2162. i = 0;
  2163. /* The 68HC12 movb and movw instructions are special. We have to handle
  2164. them in a special way. */
  2165. if (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
  2166. {
  2167. move_insn = 1;
  2168. if (format & M6812_OP_IDX)
  2169. {
  2170. build_indexed_byte (&operands[0], format, 1);
  2171. i = 1;
  2172. format &= ~M6812_OP_IDX;
  2173. }
  2174. if (format & M6812_OP_IDX_P2)
  2175. {
  2176. build_indexed_byte (&operands[1], format, 1);
  2177. i = 0;
  2178. format &= ~M6812_OP_IDX_P2;
  2179. }
  2180. }
  2181. if (format & (M6811_OP_DIRECT | M6811_OP_IMM8))
  2182. {
  2183. fixup8 (&operands[i].exp,
  2184. format & (M6811_OP_DIRECT | M6811_OP_IMM8 | M6812_OP_TRAP_ID),
  2185. operands[i].mode);
  2186. i++;
  2187. }
  2188. else if (IS_CALL_SYMBOL (format) && nb_operands == 1)
  2189. {
  2190. format &= ~M6812_OP_PAGE;
  2191. fixup24 (&operands[i].exp, format & M6811_OP_IND16,
  2192. operands[i].mode);
  2193. i++;
  2194. }
  2195. else if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
  2196. {
  2197. fixup16 (&operands[i].exp,
  2198. format & (M6811_OP_IMM16 | M6811_OP_IND16 | M6812_OP_PAGE),
  2199. operands[i].mode);
  2200. i++;
  2201. }
  2202. else if (format & (M6811_OP_IX | M6811_OP_IY))
  2203. {
  2204. if ((format & M6811_OP_IX) && (operands[0].reg1 != REG_X))
  2205. as_bad (_("Invalid indexed register, expecting register X."));
  2206. if ((format & M6811_OP_IY) && (operands[0].reg1 != REG_Y))
  2207. as_bad (_("Invalid indexed register, expecting register Y."));
  2208. fixup8 (&operands[0].exp, M6811_OP_IX, operands[0].mode);
  2209. i = 1;
  2210. }
  2211. else if (format &
  2212. (M6812_OP_IDX | M6812_OP_IDX_2 | M6812_OP_IDX_1
  2213. | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  2214. {
  2215. build_indexed_byte (&operands[i], format, move_insn);
  2216. i++;
  2217. }
  2218. else if (format & M6812_OP_REG && current_architecture & cpu6812)
  2219. {
  2220. build_reg_mode (&operands[i], format);
  2221. i++;
  2222. }
  2223. if (format & M6811_OP_BITMASK)
  2224. {
  2225. fixup8 (&operands[i].exp, M6811_OP_BITMASK, operands[i].mode);
  2226. i++;
  2227. }
  2228. if (format & M6811_OP_JUMP_REL)
  2229. {
  2230. fixup8 (&operands[i].exp, M6811_OP_JUMP_REL, operands[i].mode);
  2231. }
  2232. else if (format & M6812_OP_IND16_P2)
  2233. {
  2234. fixup16 (&operands[1].exp, M6811_OP_IND16, operands[1].mode);
  2235. }
  2236. if (format & M6812_OP_PAGE)
  2237. {
  2238. fixup8 (&operands[i].exp, M6812_OP_PAGE, operands[i].mode);
  2239. }
  2240. }
  2241. /* Opcode identification and operand analysis. */
  2242. /* find() gets a pointer to an entry in the opcode table. It must look at all
  2243. opcodes with the same name and use the operands to choose the correct
  2244. opcode. Returns the opcode pointer if there was a match and 0 if none. */
  2245. static struct m68hc11_opcode *
  2246. find (struct m68hc11_opcode_def *opc, operand operands[], int nb_operands)
  2247. {
  2248. int i, match, pos;
  2249. struct m68hc11_opcode *opcode;
  2250. struct m68hc11_opcode *op_indirect;
  2251. op_indirect = 0;
  2252. opcode = opc->opcode;
  2253. /* Now search the opcode table table for one with operands
  2254. that matches what we've got. */
  2255. if (current_architecture & cpuxgate)
  2256. {
  2257. /* Many XGATE insns are simple enough that we get an exact match. */
  2258. for (pos = match = 0; match == 0 && pos < opc->nb_modes; pos++, opcode++)
  2259. if (opcode->format == operands[nb_operands-1].mode)
  2260. return opcode;
  2261. return 0;
  2262. }
  2263. /* Non XGATE */
  2264. /* Now search the opcode table table for one with operands
  2265. that matches what we've got. We're only done if the operands matched so
  2266. far AND there are no more to check. */
  2267. for (pos = match = 0; match == 0 && pos < opc->nb_modes; pos++, opcode++)
  2268. {
  2269. int poss_indirect = 0;
  2270. long format = opcode->format;
  2271. int expect;
  2272. expect = 0;
  2273. if (opcode->format & M6811_OP_MASK)
  2274. expect++;
  2275. if (opcode->format & M6811_OP_BITMASK)
  2276. expect++;
  2277. if (opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  2278. expect++;
  2279. if (opcode->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
  2280. expect++;
  2281. if ((opcode->format & M6812_OP_PAGE)
  2282. && (!IS_CALL_SYMBOL (opcode->format) || nb_operands == 2))
  2283. expect++;
  2284. for (i = 0; expect == nb_operands && i < nb_operands; i++)
  2285. {
  2286. int mode = operands[i].mode;
  2287. if (mode & M6811_OP_IMM16)
  2288. {
  2289. if (format &
  2290. (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK))
  2291. continue;
  2292. break;
  2293. }
  2294. if (mode == M6811_OP_DIRECT)
  2295. {
  2296. if (format & M6811_OP_DIRECT)
  2297. continue;
  2298. /* If the operand is a page 0 operand, remember a
  2299. possible <abs-16> addressing mode. We mark
  2300. this and continue to check other operands. */
  2301. if (format & M6811_OP_IND16
  2302. && flag_strict_direct_addressing && op_indirect == 0)
  2303. {
  2304. poss_indirect = 1;
  2305. continue;
  2306. }
  2307. break;
  2308. }
  2309. if (mode & M6811_OP_IND16)
  2310. {
  2311. if (i == 0 && (format & M6811_OP_IND16) != 0)
  2312. continue;
  2313. if (i != 0 && (format & M6812_OP_PAGE) != 0)
  2314. continue;
  2315. if (i != 0 && (format & M6812_OP_IND16_P2) != 0)
  2316. continue;
  2317. if (i == 0 && (format & M6811_OP_BITMASK))
  2318. break;
  2319. }
  2320. if (mode & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  2321. {
  2322. if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  2323. continue;
  2324. }
  2325. if (mode & M6812_OP_REG)
  2326. {
  2327. if (i == 0
  2328. && (format & M6812_OP_REG)
  2329. && (operands[i].reg2 == REG_NONE))
  2330. continue;
  2331. if (i == 0
  2332. && (format & M6812_OP_REG)
  2333. && (format & M6812_OP_REG_2)
  2334. && (operands[i].reg2 != REG_NONE))
  2335. continue;
  2336. if (i == 0
  2337. && (format & M6812_OP_IDX)
  2338. && (operands[i].reg2 != REG_NONE))
  2339. continue;
  2340. if (i == 0
  2341. && (format & M6812_OP_IDX)
  2342. && (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)))
  2343. continue;
  2344. if (i == 1
  2345. && (format & M6812_OP_IDX_P2))
  2346. continue;
  2347. break;
  2348. }
  2349. if (mode & M6812_OP_IDX)
  2350. {
  2351. if (format & M6811_OP_IX && operands[i].reg1 == REG_X)
  2352. continue;
  2353. if (format & M6811_OP_IY && operands[i].reg1 == REG_Y)
  2354. continue;
  2355. if (i == 0
  2356. && format & (M6812_OP_IDX | M6812_OP_IDX_1 | M6812_OP_IDX_2)
  2357. && (operands[i].reg1 == REG_X
  2358. || operands[i].reg1 == REG_Y
  2359. || operands[i].reg1 == REG_SP
  2360. || operands[i].reg1 == REG_PC))
  2361. continue;
  2362. if (i == 1 && (format & M6812_OP_IDX_P2))
  2363. continue;
  2364. }
  2365. if (mode & format & (M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  2366. {
  2367. if (i == 0)
  2368. continue;
  2369. }
  2370. if (mode & M6812_AUTO_INC_DEC)
  2371. {
  2372. if (i == 0
  2373. && format & (M6812_OP_IDX | M6812_OP_IDX_1 |
  2374. M6812_OP_IDX_2))
  2375. continue;
  2376. if (i == 1 && format & M6812_OP_IDX_P2)
  2377. continue;
  2378. }
  2379. break;
  2380. }
  2381. match = i == nb_operands;
  2382. /* Operands are ok but an operand uses page 0 addressing mode
  2383. while the insn supports abs-16 mode. Keep a reference to this
  2384. insns in case there is no insn supporting page 0 addressing. */
  2385. if (match && poss_indirect)
  2386. {
  2387. op_indirect = opcode;
  2388. match = 0;
  2389. }
  2390. if (match)
  2391. break;
  2392. }
  2393. /* Page 0 addressing is used but not supported by any insn.
  2394. If absolute addresses are supported, we use that insn. */
  2395. if (match == 0 && op_indirect)
  2396. {
  2397. opcode = op_indirect;
  2398. match = 1;
  2399. }
  2400. return match ? opcode : 0;
  2401. }
  2402. /* Find the real opcode and its associated operands. We use a progressive
  2403. approach here. On entry, 'opc' points to the first opcode in the
  2404. table that matches the opcode name in the source line. We try to
  2405. isolate an operand, find a possible match in the opcode table.
  2406. We isolate another operand if no match were found. The table 'operands'
  2407. is filled while operands are recognized.
  2408. Returns the opcode pointer that matches the opcode name in the
  2409. source line and the associated operands. */
  2410. static struct m68hc11_opcode *
  2411. find_opcode (struct m68hc11_opcode_def *opc, operand operands[],
  2412. int *nb_operands)
  2413. {
  2414. struct m68hc11_opcode *opcode;
  2415. int i;
  2416. if (opc->max_operands == 0)
  2417. {
  2418. *nb_operands = 0;
  2419. return opc->opcode;
  2420. }
  2421. for (i = 0; i < opc->max_operands;)
  2422. {
  2423. int result;
  2424. result = get_operand (&operands[i], i, opc->format);
  2425. if (result <= 0)
  2426. return 0;
  2427. /* Special case where the bitmask of the bclr/brclr
  2428. instructions is not introduced by #.
  2429. Example: bclr 3,x $80. */
  2430. if (i == 1 && (opc->format & M6811_OP_BITMASK)
  2431. && (operands[i].mode & M6811_OP_IND16))
  2432. {
  2433. operands[i].mode = M6811_OP_IMM16;
  2434. }
  2435. i += result;
  2436. *nb_operands = i;
  2437. if (i >= opc->min_operands)
  2438. {
  2439. opcode = find (opc, operands, i);
  2440. /* Another special case for 'call foo,page' instructions.
  2441. Since we support 'call foo' and 'call foo,page' we must look
  2442. if the optional page specification is present otherwise we will
  2443. assemble immediately and treat the page spec as garbage. */
  2444. if (opcode && !(opcode->format & M6812_OP_PAGE))
  2445. return opcode;
  2446. if (opcode && *input_line_pointer != ',')
  2447. return opcode;
  2448. }
  2449. if (*input_line_pointer == ',')
  2450. input_line_pointer++;
  2451. }
  2452. return 0;
  2453. }
  2454. #define M6812_XBCC_MARKER (M6812_OP_TBCC_MARKER \
  2455. | M6812_OP_DBCC_MARKER \
  2456. | M6812_OP_IBCC_MARKER)
  2457. /* Gas line assembler entry point. */
  2458. /* This is the main entry point for the machine-dependent assembler. str
  2459. points to a machine-dependent instruction. This function is supposed to
  2460. emit the frags/bytes it assembles to. */
  2461. void
  2462. md_assemble (char *str)
  2463. {
  2464. struct m68hc11_opcode_def *opc;
  2465. struct m68hc11_opcode *opcode;
  2466. struct m68hc11_opcode opcode_local;
  2467. unsigned char *op_start, *op_end;
  2468. char *save;
  2469. char name[20];
  2470. int nlen = 0;
  2471. operand operands[M6811_MAX_OPERANDS];
  2472. int nb_operands = 0;
  2473. int branch_optimize = 0;
  2474. int alias_id = -1;
  2475. /* Drop leading whitespace. */
  2476. while (*str == ' ')
  2477. str++;
  2478. /* Find the opcode end and get the opcode in 'name'. The opcode is forced
  2479. lower case (the opcode table only has lower case op-codes). */
  2480. for (op_start = op_end = (unsigned char *) str;
  2481. *op_end && !is_end_of_line[*op_end] && *op_end != ' ';
  2482. op_end++)
  2483. {
  2484. name[nlen] = TOLOWER (op_start[nlen]);
  2485. nlen++;
  2486. if (nlen == sizeof (name) - 1)
  2487. break;
  2488. }
  2489. name[nlen] = 0;
  2490. if (nlen == 0)
  2491. {
  2492. as_bad (_("No instruction or missing opcode."));
  2493. return;
  2494. }
  2495. if (current_architecture == cpuxgate)
  2496. {
  2497. /* Find the opcode definition given its name. */
  2498. opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, name);
  2499. if (opc == NULL)
  2500. {
  2501. as_bad (_("Opcode `%s' is not recognized."), name);
  2502. return;
  2503. }
  2504. /* Grab a local copy. */
  2505. opcode_local.name = opc->opcode->name;
  2506. /* These will be incomplete where multiple variants exist. */
  2507. opcode_local.opcode = opc->opcode->opcode;
  2508. opcode_local.format = opc->opcode->format;
  2509. save = input_line_pointer;
  2510. input_line_pointer = (char *) op_end;
  2511. if (opc->format == M68XG_OP_NONE)
  2512. {
  2513. /* No special handling required. */
  2514. opcode_local.format = M68XG_OP_NONE;
  2515. build_insn_xg (opc->opcode, operands, 0);
  2516. return;
  2517. }
  2518. /* Special handling of TFR. */
  2519. if (strncmp (opc->opcode->name, "tfr",3) == 0)
  2520. {
  2521. /* There must be two operands with a comma. */
  2522. input_line_pointer = skip_whites (input_line_pointer);
  2523. operands[0].reg1 = register_name ();
  2524. if (operands[0].reg1 == REG_NONE)
  2525. {
  2526. as_bad ("Invalid register\n");
  2527. return;
  2528. }
  2529. input_line_pointer = skip_whites (input_line_pointer);
  2530. if (*input_line_pointer != ',')
  2531. {
  2532. as_bad ("Missing comma.\n");
  2533. return;
  2534. }
  2535. input_line_pointer++;
  2536. input_line_pointer = skip_whites (input_line_pointer);
  2537. operands[1].reg1 = register_name ();
  2538. if (operands[1].reg1 == REG_NONE)
  2539. {
  2540. as_bad ("Invalid register\n");
  2541. return;
  2542. }
  2543. input_line_pointer = skip_whites (input_line_pointer);
  2544. if (*input_line_pointer != '\n' && *input_line_pointer)
  2545. {
  2546. as_bad (_("Garbage at end of instruction: `%s'."),
  2547. input_line_pointer);
  2548. return;
  2549. }
  2550. if (operands[1].reg1 == REG_CCR) /* ,CCR */
  2551. opc->opcode->opcode = 0x00f8 | ( operands[0].reg1 << 8);
  2552. else if (operands[0].reg1 == REG_CCR) /* CCR, */
  2553. opc->opcode->opcode = 0x00f9 | ( operands[1].reg1 << 8);
  2554. else if (operands[1].reg1 == REG_PC) /* ,PC */
  2555. opc->opcode->opcode = 0x00fa | ( operands[0].reg1 << 8);
  2556. else
  2557. {
  2558. as_bad ("Invalid operand to TFR\n");
  2559. return;
  2560. }
  2561. /* no special handling required */
  2562. opcode_local.format = M68XG_OP_NONE;
  2563. opcode_local.opcode = opc->opcode->opcode;
  2564. build_insn_xg (&opcode_local, operands, 0);
  2565. return;
  2566. }
  2567. /* CSEM, SSEM */
  2568. if (opc->format & M68XG_OP_IMM3)
  2569. {
  2570. /* Either IMM3 or R */
  2571. input_line_pointer = skip_whites (input_line_pointer);
  2572. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2573. {
  2574. operands[0].reg1 = register_name ();
  2575. if (operands[0].reg1 == REG_NONE)
  2576. {
  2577. as_bad ("Invalid register\n");
  2578. return;
  2579. }
  2580. operands[0].mode = M68XG_OP_R;
  2581. /* One opcode has multiple modes, so find right one. */
  2582. opcode = find (opc, operands, 1);
  2583. if (opcode)
  2584. {
  2585. opcode_local.opcode = opcode->opcode
  2586. | (operands[0].reg1 << 8);
  2587. opcode_local.format = M68XG_OP_NONE;
  2588. build_insn_xg (&opcode_local, operands, 1);
  2589. }
  2590. else
  2591. as_bad ("No opcode found\n");
  2592. return;
  2593. }
  2594. else
  2595. {
  2596. if (*input_line_pointer == '#')
  2597. input_line_pointer++;
  2598. expression (&operands[0].exp);
  2599. if (operands[0].exp.X_op == O_illegal)
  2600. {
  2601. as_bad (_("Illegal operand."));
  2602. return;
  2603. }
  2604. else if (operands[0].exp.X_op == O_absent)
  2605. {
  2606. as_bad (_("Missing operand."));
  2607. return;
  2608. }
  2609. if (check_range (operands[0].exp.X_add_number,M68XG_OP_IMM3))
  2610. {
  2611. opcode_local.opcode |= (operands[0].exp.X_add_number);
  2612. operands[0].mode = M68XG_OP_IMM3;
  2613. opcode = find (opc, operands, 1);
  2614. if (opcode)
  2615. {
  2616. opcode_local.opcode = opcode->opcode;
  2617. opcode_local.opcode
  2618. |= (operands[0].exp.X_add_number) << 8;
  2619. opcode_local.format = M68XG_OP_NONE;
  2620. build_insn_xg (&opcode_local, operands, 1);
  2621. }
  2622. else
  2623. as_bad ("No opcode found\n");
  2624. return;
  2625. }
  2626. else
  2627. {
  2628. as_bad ("Number out of range for IMM3\n");
  2629. return;
  2630. }
  2631. }
  2632. }
  2633. /* Special handling of SIF. */
  2634. if (strncmp (opc->opcode->name, "sif",3) == 0)
  2635. {
  2636. /* Either OP_NONE or OP_RS. */
  2637. if (*input_line_pointer != '\n')
  2638. input_line_pointer = skip_whites (input_line_pointer);
  2639. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2640. || (*input_line_pointer == '\0'))
  2641. opc->opcode->opcode = 0x0300;
  2642. else
  2643. {
  2644. operands[0].reg1 = register_name ();
  2645. if (operands[0].reg1 == REG_NONE)
  2646. {
  2647. as_bad ("Invalid register\n");
  2648. return;
  2649. }
  2650. opcode_local.opcode = 0x00f7 | (operands[0].reg1 << 8);
  2651. }
  2652. opcode_local.format = M68XG_OP_NONE;
  2653. build_insn_xg (&opcode_local, operands, 0);
  2654. return;
  2655. }
  2656. /* SEX, PAR, JAL plus aliases NEG, TST, COM */
  2657. if (opc->format & M68XG_OP_R)
  2658. {
  2659. input_line_pointer = skip_whites (input_line_pointer);
  2660. operands[0].reg1 = register_name ();
  2661. if (operands[0].reg1 == REG_NONE)
  2662. {
  2663. as_bad ("Invalid register\n");
  2664. return;
  2665. }
  2666. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2667. || (*input_line_pointer == '\0'))
  2668. {
  2669. /* Likely to be OP R. */
  2670. if (opc->format & M68XG_OP_R)
  2671. {
  2672. operands[0].mode = M68XG_OP_R;
  2673. opcode = find (opc, operands, 1);
  2674. if (opcode)
  2675. {
  2676. if ((strncmp (opc->opcode->name, "com",3) == 0)
  2677. || (strncmp (opc->opcode->name, "neg",3) == 0))
  2678. /* Special case for com RD as alias for sub RD,R0,RS */
  2679. /* Special case for neg RD as alias for sub RD,R0,RS */
  2680. opcode_local.opcode = opcode->opcode
  2681. | (operands[0].reg1 << 8) | (operands[0].reg1 << 2);
  2682. else if (strncmp (opc->opcode->name, "tst",3) == 0)
  2683. /* Special case for tst RS alias for sub R0, RS, R0 */
  2684. opcode_local.opcode = opcode->opcode
  2685. | (operands[0].reg1 << 5);
  2686. else
  2687. opcode_local.opcode |= (operands[0].reg1 << 8);
  2688. }
  2689. opcode_local.format = M68XG_OP_NONE;
  2690. build_insn_xg (&opcode_local, operands, 0);
  2691. }
  2692. else
  2693. as_bad ("No valid mode found\n");
  2694. return;
  2695. }
  2696. }
  2697. if (opc->format & (M68XG_OP_REL9 | M68XG_OP_REL10))
  2698. {
  2699. opcode_local.format = opc->format;
  2700. input_line_pointer = skip_whites (input_line_pointer);
  2701. expression (&operands[0].exp);
  2702. if (operands[0].exp.X_op == O_illegal)
  2703. {
  2704. as_bad (_("Illegal operand."));
  2705. return;
  2706. }
  2707. else if (operands[0].exp.X_op == O_absent)
  2708. {
  2709. as_bad (_("Missing operand."));
  2710. return;
  2711. }
  2712. opcode_local.opcode = opc->opcode->opcode;
  2713. build_insn_xg (&opcode_local, operands, 1);
  2714. return;
  2715. }
  2716. /* For other command formats, parse input line and determine the mode
  2717. we are using as we go. */
  2718. input_line_pointer = skip_whites (input_line_pointer);
  2719. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2720. || (*input_line_pointer == '\0'))
  2721. return; /* nothing left */
  2722. if (*input_line_pointer == '#')
  2723. {
  2724. as_bad ("No register specified before hash\n");
  2725. return;
  2726. }
  2727. /* first operand is expected to be a register */
  2728. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2729. {
  2730. operands[0].reg1 = register_name ();
  2731. if (operands[0].reg1 == REG_NONE)
  2732. {
  2733. as_bad ("Invalid register\n");
  2734. return;
  2735. }
  2736. }
  2737. input_line_pointer = skip_whites (input_line_pointer);
  2738. if (*input_line_pointer != ',')
  2739. {
  2740. as_bad ("Missing operand\n");
  2741. return;
  2742. }
  2743. input_line_pointer++;
  2744. input_line_pointer = skip_whites (input_line_pointer);
  2745. if (*input_line_pointer == '#')
  2746. {
  2747. /* Some kind of immediate mode, check if this is possible. */
  2748. if (!(opc->format
  2749. & (M68XG_OP_R_IMM8 | M68XG_OP_R_IMM16 | M68XG_OP_R_IMM4)))
  2750. as_bad ("Invalid immediate mode for `%s'", opc->opcode->name);
  2751. else
  2752. {
  2753. input_line_pointer++;
  2754. input_line_pointer = skip_whites (input_line_pointer);
  2755. if (strncmp (input_line_pointer, "%hi", 3) == 0)
  2756. {
  2757. input_line_pointer += 3;
  2758. operands[0].mode = M6811_OP_HIGH_ADDR;
  2759. }
  2760. else if (strncmp (input_line_pointer, "%lo", 3) == 0)
  2761. {
  2762. input_line_pointer += 3;
  2763. operands[0].mode = M6811_OP_LOW_ADDR;
  2764. }
  2765. else
  2766. operands[0].mode = 0;
  2767. expression (&operands[0].exp);
  2768. if (operands[0].exp.X_op == O_illegal)
  2769. {
  2770. as_bad (_("Illegal operand."));
  2771. return;
  2772. }
  2773. else if (operands[0].exp.X_op == O_absent)
  2774. {
  2775. as_bad (_("Missing operand."));
  2776. return;
  2777. }
  2778. /* ok so far, can only be one mode */
  2779. opcode_local.format = opc->format
  2780. & (M68XG_OP_R_IMM8 | M68XG_OP_R_IMM16 | M68XG_OP_R_IMM4);
  2781. if (opcode_local.format & M68XG_OP_R_IMM4)
  2782. {
  2783. operands[0].mode = M68XG_OP_R_IMM4;
  2784. /* same opcodes have multiple modes, so find right one */
  2785. opcode = find (opc, operands, 1);
  2786. if (opcode)
  2787. opcode_local.opcode = opcode->opcode
  2788. | (operands[0].reg1 << 8);
  2789. if (operands[0].exp.X_op != O_constant)
  2790. as_bad ("Only constants supported at for IMM4 mode\n");
  2791. else
  2792. {
  2793. if (check_range
  2794. (operands[0].exp.X_add_number,M68XG_OP_R_IMM4))
  2795. opcode_local.opcode
  2796. |= (operands[0].exp.X_add_number << 4);
  2797. else
  2798. as_bad ("Number out of range for IMM4\n");
  2799. }
  2800. opcode_local.format = M68XG_OP_NONE;
  2801. }
  2802. else if (opcode_local.format & M68XG_OP_R_IMM16)
  2803. {
  2804. operands[0].mode = M68XG_OP_R_IMM16;
  2805. opcode = find (opc, operands, 1);
  2806. if (opcode)
  2807. {
  2808. opcode_local.opcode = opcode->opcode
  2809. | (operands[0].reg1 << 8);
  2810. }
  2811. }
  2812. else
  2813. {
  2814. opcode_local.opcode = opc->opcode->opcode
  2815. | (operands[0].reg1 << 8);
  2816. }
  2817. build_insn_xg (&opcode_local, operands, 1);
  2818. }
  2819. }
  2820. else if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2821. {
  2822. /* we've got as far as OP R, R */
  2823. operands[1].reg1 = register_name ();
  2824. if (operands[1].reg1 == REG_NONE)
  2825. {
  2826. as_bad ("Invalid register\n");
  2827. return;
  2828. }
  2829. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2830. || (*input_line_pointer == '\0'))
  2831. {
  2832. /* looks like OP_R_R */
  2833. if (opc->format & M68XG_OP_R_R)
  2834. {
  2835. operands[0].mode = M68XG_OP_R_R;
  2836. /* same opcodes have multiple modes, so find right one */
  2837. opcode = find (opc, operands, 1);
  2838. if (opcode)
  2839. {
  2840. if ((strncmp (opc->opcode->name, "com",3) == 0)
  2841. || (strncmp (opc->opcode->name, "mov",3) == 0)
  2842. || (strncmp (opc->opcode->name, "neg",3) == 0))
  2843. {
  2844. /* Special cases for:
  2845. com RD, RS alias for xnor RD,R0,RS
  2846. mov RD, RS alias for or RD, R0, RS
  2847. neg RD, RS alias for sub RD, R0, RS */
  2848. opcode_local.opcode = opcode->opcode
  2849. | (operands[0].reg1 << 8) | (operands[1].reg1 << 2);
  2850. }
  2851. else if ((strncmp (opc->opcode->name, "cmp",3) == 0)
  2852. || (strncmp (opc->opcode->name, "cpc",3) == 0))
  2853. {
  2854. /* special cases for:
  2855. cmp RS1, RS2 alias for sub R0, RS1, RS2
  2856. cpc RS1, RS2 alias for sbc R0, RS1, RS2 */
  2857. opcode_local.opcode = opcode->opcode
  2858. | (operands[0].reg1 << 5) | (operands[1].reg1 << 2);
  2859. }
  2860. else
  2861. {
  2862. opcode_local.opcode = opcode->opcode
  2863. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5);
  2864. }
  2865. opcode_local.format = M68XG_OP_NONE;
  2866. build_insn_xg (&opcode_local, operands, 1);
  2867. }
  2868. }
  2869. else
  2870. {
  2871. as_bad ("No valid mode found\n");
  2872. }
  2873. }
  2874. else
  2875. {
  2876. /* more data */
  2877. if (*input_line_pointer != ',')
  2878. {
  2879. as_bad (_("Missing operand."));
  2880. return;
  2881. }
  2882. input_line_pointer++;
  2883. input_line_pointer = skip_whites (input_line_pointer);
  2884. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2885. {
  2886. operands[2].reg1 = register_name ();
  2887. if (operands[2].reg1 == REG_NONE)
  2888. {
  2889. as_bad ("Invalid register\n");
  2890. return;
  2891. }
  2892. if (opc->format & M68XG_OP_R_R_R)
  2893. {
  2894. operands[0].mode = M68XG_OP_R_R_R;
  2895. opcode = find (opc, operands, 1);
  2896. if (opcode)
  2897. {
  2898. opcode_local.opcode = opcode->opcode
  2899. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5)
  2900. | (operands[2].reg1 << 2);
  2901. opcode_local.format = M68XG_OP_NONE;
  2902. build_insn_xg (&opcode_local, operands, 1);
  2903. }
  2904. }
  2905. else
  2906. {
  2907. as_bad ("No valid mode found\n");
  2908. }
  2909. }
  2910. }
  2911. }
  2912. else if (*input_line_pointer == '(') /* Indexed modes */
  2913. {
  2914. input_line_pointer++;
  2915. input_line_pointer = skip_whites (input_line_pointer);
  2916. if ((*input_line_pointer == 'R') || (*input_line_pointer == 'r'))
  2917. {
  2918. /* we've got as far as OP R, (R */
  2919. operands[1].reg1 = register_name ();
  2920. if (operands[1].reg1 == REG_NONE)
  2921. {
  2922. as_bad ("Invalid register\n");
  2923. return;
  2924. }
  2925. if ((*input_line_pointer == '\n') || (*input_line_pointer == '\r')
  2926. || (*input_line_pointer == '\0'))
  2927. {
  2928. /* Looks like OP_R_R. */
  2929. as_bad (_("Missing operand."));
  2930. return;
  2931. }
  2932. input_line_pointer = skip_whites (input_line_pointer);
  2933. if (*input_line_pointer != ',')
  2934. {
  2935. as_bad (_("Missing operand."));
  2936. return;
  2937. }
  2938. input_line_pointer++;
  2939. input_line_pointer = skip_whites (input_line_pointer);
  2940. if (*input_line_pointer == '#')
  2941. {
  2942. input_line_pointer++;
  2943. input_line_pointer = skip_whites (input_line_pointer);
  2944. expression (&operands[0].exp);
  2945. if (operands[0].exp.X_op == O_illegal)
  2946. {
  2947. as_bad (_("Illegal operand."));
  2948. return;
  2949. }
  2950. else if (operands[0].exp.X_op == O_absent)
  2951. {
  2952. as_bad (_("Missing operand."));
  2953. return;
  2954. }
  2955. input_line_pointer = skip_whites (input_line_pointer);
  2956. if (*input_line_pointer != ')')
  2957. {
  2958. as_bad ("Missing `)' to close register indirect operand.");
  2959. return;
  2960. }
  2961. else
  2962. {
  2963. input_line_pointer++;
  2964. }
  2965. /* Ok so far, can only be one mode. */
  2966. opcode_local.format = M68XG_OP_R_R_OFFS5;
  2967. operands[0].mode = M68XG_OP_R_R_OFFS5;
  2968. opcode = find (opc, operands, 1);
  2969. if (opcode)
  2970. {
  2971. opcode_local.opcode = opcode->opcode
  2972. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5);
  2973. if (operands[0].exp.X_op != O_constant)
  2974. {
  2975. as_bad
  2976. ("Only constants supported for indexed OFFS5 mode\n");
  2977. }
  2978. else
  2979. {
  2980. if (check_range (operands[0].exp.X_add_number,
  2981. M68XG_OP_R_R_OFFS5))
  2982. {
  2983. opcode_local.opcode
  2984. |= (operands[0].exp.X_add_number);
  2985. opcode_local.format = M68XG_OP_NONE;
  2986. build_insn_xg (&opcode_local, operands, 1);
  2987. }
  2988. else
  2989. {
  2990. as_bad ("Number out of range for OFFS5\n");
  2991. }
  2992. }
  2993. }
  2994. }
  2995. else
  2996. {
  2997. operands[0].mode = M68XG_OP_RD_RB_RI;
  2998. if (*input_line_pointer == '-')
  2999. {
  3000. operands[0].mode = M68XG_OP_RD_RB_mRI;
  3001. input_line_pointer++;
  3002. }
  3003. operands[2].reg1 = register_name ();
  3004. if (operands[2].reg1 == REG_NONE)
  3005. {
  3006. as_bad ("Invalid register\n");
  3007. return;
  3008. }
  3009. if (*input_line_pointer == '+')
  3010. {
  3011. if (opcode_local.format == M68XG_OP_RD_RB_mRI)
  3012. {
  3013. as_bad (_("Illegal operand."));
  3014. return;
  3015. }
  3016. operands[0].mode = M68XG_OP_RD_RB_RIp;
  3017. input_line_pointer++;
  3018. }
  3019. input_line_pointer = skip_whites (input_line_pointer);
  3020. if (*input_line_pointer != ')')
  3021. {
  3022. as_bad
  3023. ("Missing `)' to close register indirect operand.");
  3024. return;
  3025. }
  3026. else
  3027. {
  3028. input_line_pointer++;
  3029. }
  3030. opcode = find (opc, operands, 1);
  3031. if (opcode)
  3032. {
  3033. opcode_local.opcode = opcode->opcode
  3034. | (operands[0].reg1 << 8) | (operands[1].reg1 << 5)
  3035. | (operands[2].reg1 << 2);
  3036. opcode_local.format = M68XG_OP_NONE;
  3037. build_insn_xg (&opcode_local, operands, 1);
  3038. }
  3039. else
  3040. {
  3041. as_bad ("Failed to find opcode for %s %s\n",
  3042. opc->opcode->name, (char *)op_end);
  3043. }
  3044. }
  3045. }
  3046. }
  3047. else
  3048. {
  3049. as_bad (_("Failed to find a valid mode for `%s'."),
  3050. opc->opcode->name);
  3051. }
  3052. if (opc->opcode && !flag_mri)
  3053. {
  3054. char *p = input_line_pointer;
  3055. while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
  3056. p++;
  3057. if (*p != '\n' && *p)
  3058. as_bad (_("Garbage at end of instruction: `%s'."), p);
  3059. }
  3060. input_line_pointer = save;
  3061. /* Opcode is known but does not have valid operands. Print out the
  3062. syntax for this opcode. */
  3063. if (opc->opcode == 0)
  3064. {
  3065. if (flag_print_insn_syntax)
  3066. print_insn_format (name);
  3067. as_bad (_("Invalid operand for `%s'"), name);
  3068. return;
  3069. }
  3070. return;
  3071. }
  3072. /* Find the opcode definition given its name. */
  3073. opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, name);
  3074. /* If it's not recognized, look for 'jbsr' and 'jbxx'. These are
  3075. pseudo insns for relative branch. For these branches, we always
  3076. optimize them (turned into absolute branches) even if --short-branches
  3077. is given. */
  3078. if (opc == NULL && name[0] == 'j' && name[1] == 'b')
  3079. {
  3080. opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, &name[1]);
  3081. if (opc
  3082. && (!(opc->format & M6811_OP_JUMP_REL)
  3083. || (opc->format & M6811_OP_BITMASK)))
  3084. opc = 0;
  3085. if (opc)
  3086. branch_optimize = 1;
  3087. }
  3088. /* The following test should probably be removed. This does not conform
  3089. to Motorola assembler specs. */
  3090. if (opc == NULL && flag_mri)
  3091. {
  3092. if (*op_end == ' ' || *op_end == '\t')
  3093. {
  3094. while (*op_end == ' ' || *op_end == '\t')
  3095. op_end++;
  3096. if (nlen < 19
  3097. && (*op_end &&
  3098. (is_end_of_line[op_end[1]]
  3099. || op_end[1] == ' ' || op_end[1] == '\t'
  3100. || !ISALNUM (op_end[1])))
  3101. && (*op_end == 'a' || *op_end == 'b'
  3102. || *op_end == 'A' || *op_end == 'B'
  3103. || *op_end == 'd' || *op_end == 'D'
  3104. || *op_end == 'x' || *op_end == 'X'
  3105. || *op_end == 'y' || *op_end == 'Y'))
  3106. {
  3107. name[nlen++] = TOLOWER (*op_end++);
  3108. name[nlen] = 0;
  3109. opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash,
  3110. name);
  3111. }
  3112. }
  3113. }
  3114. /* Identify a possible instruction alias. There are some on the
  3115. 68HC12 to emulate a few 68HC11 instructions. */
  3116. if (opc == NULL && (current_architecture & cpu6812))
  3117. {
  3118. int i;
  3119. for (i = 0; i < m68hc12_num_alias; i++)
  3120. if (strcmp (m68hc12_alias[i].name, name) == 0)
  3121. {
  3122. alias_id = i;
  3123. break;
  3124. }
  3125. }
  3126. if (opc == NULL && alias_id < 0)
  3127. {
  3128. as_bad (_("Opcode `%s' is not recognized."), name);
  3129. return;
  3130. }
  3131. save = input_line_pointer;
  3132. input_line_pointer = (char *) op_end;
  3133. if (opc)
  3134. {
  3135. opc->used++;
  3136. opcode = find_opcode (opc, operands, &nb_operands);
  3137. }
  3138. else
  3139. opcode = 0;
  3140. if ((opcode || alias_id >= 0) && !flag_mri)
  3141. {
  3142. char *p = input_line_pointer;
  3143. while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
  3144. p++;
  3145. if (*p != '\n' && *p)
  3146. as_bad (_("Garbage at end of instruction: `%s'."), p);
  3147. }
  3148. input_line_pointer = save;
  3149. if (alias_id >= 0)
  3150. {
  3151. char *f = m68hc11_new_insn (m68hc12_alias[alias_id].size);
  3152. number_to_chars_bigendian (f, m68hc12_alias[alias_id].code1, 1);
  3153. if (m68hc12_alias[alias_id].size > 1)
  3154. number_to_chars_bigendian (f + 1, m68hc12_alias[alias_id].code2, 1);
  3155. return;
  3156. }
  3157. /* Opcode is known but does not have valid operands. Print out the
  3158. syntax for this opcode. */
  3159. if (opcode == 0)
  3160. {
  3161. if (flag_print_insn_syntax)
  3162. print_insn_format (name);
  3163. if (((strcmp (name, "movb") == 0) || (strcmp (name, "movw") == 0))
  3164. && (current_architecture & cpu9s12x))
  3165. {
  3166. char *f;
  3167. int movb;
  3168. if (strcmp (name, "movb") == 0)
  3169. movb = 8;
  3170. else
  3171. movb = 0;
  3172. /* The existing operand extract code fell over if these additional modes
  3173. were enabled in m68hc11-opc.c. So they are commented there and
  3174. decoded here instead. */
  3175. if (operands[1].mode & (M6812_OP_IDX | M6812_OP_IDX_1
  3176. | M6812_OP_IDX_2 | M6812_OP_D_IDX | M6812_OP_D_IDX_2 | M6812_PRE_INC
  3177. | M6812_PRE_DEC | M6812_POST_INC | M6812_POST_DEC ))
  3178. {
  3179. /* first check if valid mode then start building it up */
  3180. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16
  3181. | M6811_OP_IND16 | M6812_OP_IDX | M6812_OP_IDX_1
  3182. | M6812_OP_IDX_2 | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  3183. {
  3184. int opr16a;
  3185. if (operands[1].mode & (M6811_OP_IND16))
  3186. opr16a = 3;
  3187. else
  3188. opr16a = 0;
  3189. f = m68hc11_new_insn (2);
  3190. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16))
  3191. {
  3192. number_to_chars_bigendian (f, 0x1800 + movb + opr16a, 2);
  3193. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3194. if (movb)
  3195. fixup8 (&operands[0].exp, M6811_OP_IMM8,
  3196. operands[0].mode);
  3197. else
  3198. fixup16 (&operands[0].exp, M6811_OP_IMM16,
  3199. operands[0].mode);
  3200. return;
  3201. }
  3202. else if (operands[0].mode & M6811_OP_IND16)
  3203. {
  3204. number_to_chars_bigendian (f, 0x1801 + movb + opr16a, 2);
  3205. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3206. fixup16 (&operands[0].exp, M6811_OP_IND16, operands[0].mode);
  3207. return;
  3208. }
  3209. else
  3210. {
  3211. number_to_chars_bigendian (f, 0x1802 + movb + opr16a, 2);
  3212. build_indexed_byte (&operands[0], operands[0].mode, 1);
  3213. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3214. return;
  3215. }
  3216. }
  3217. }
  3218. else if (operands[1].mode & M6811_OP_IND16)
  3219. {
  3220. /* First check if this is valid mode, then start building it up. */
  3221. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16
  3222. | M6811_OP_IND16 | M6812_OP_IDX | M6812_OP_IDX_1
  3223. | M6812_OP_IDX_2 | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
  3224. {
  3225. int opr16a;
  3226. if (operands[1].mode & (M6811_OP_IND16))
  3227. opr16a = 3;
  3228. else
  3229. opr16a = 0;
  3230. f = m68hc11_new_insn (2);
  3231. /* The first two cases here should actually be covered by the
  3232. normal operand code. */
  3233. if (operands[0].mode & (M6811_OP_IMM8 | M6811_OP_IMM16))
  3234. {
  3235. number_to_chars_bigendian (f, 0x1800 + movb + opr16a, 2);
  3236. if (movb)
  3237. fixup8 (&operands[0].exp, M6811_OP_IMM8, operands[0].mode);
  3238. else
  3239. fixup16 (&operands[0].exp, M6811_OP_IMM16, operands[0].mode);
  3240. fixup16 (&operands[0].exp, M6811_OP_IND16, operands[0].mode);
  3241. return;
  3242. }
  3243. else if (operands[0].mode & M6811_OP_IND16)
  3244. {
  3245. number_to_chars_bigendian (f, 0x1801 + movb + opr16a, 2);
  3246. build_indexed_byte (&operands[1], operands[1].mode, 1);
  3247. fixup16 (&operands[0].exp, M6811_OP_IND16, operands[0].mode);
  3248. return;
  3249. }
  3250. else
  3251. {
  3252. number_to_chars_bigendian (f, 0x1802 + movb + opr16a, 2);
  3253. build_indexed_byte (&operands[0], operands[0].mode, 1);
  3254. fixup16 (&operands[1].exp, M6811_OP_IND16, operands[1].mode);
  3255. return;
  3256. }
  3257. }
  3258. }
  3259. as_bad (_("Invalid operand for `%s'"), name);
  3260. return;
  3261. }
  3262. else
  3263. {
  3264. as_bad (_("Invalid operand for `%s'"), name);
  3265. return;
  3266. }
  3267. }
  3268. /* Treat dbeq/ibeq/tbeq instructions in a special way. The branch is
  3269. relative and must be in the range -256..255 (9-bits). */
  3270. if ((opcode->format & M6812_XBCC_MARKER)
  3271. && (opcode->format & M6811_OP_JUMP_REL))
  3272. build_dbranch_insn (opcode, operands, nb_operands, branch_optimize);
  3273. /* Relative jumps instructions are taken care of separately. We have to make
  3274. sure that the relative branch is within the range -128..127. If it's out
  3275. of range, the instructions are changed into absolute instructions.
  3276. This is not supported for the brset and brclr instructions. */
  3277. else if ((opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  3278. && !(opcode->format & M6811_OP_BITMASK))
  3279. build_jump_insn (opcode, operands, nb_operands, branch_optimize);
  3280. else
  3281. build_insn (opcode, operands, nb_operands);
  3282. }
  3283. /* Pseudo op to control the ELF flags. */
  3284. static void
  3285. s_m68hc11_mode (int x ATTRIBUTE_UNUSED)
  3286. {
  3287. char *name = input_line_pointer, ch;
  3288. while (!is_end_of_line[(unsigned char) *input_line_pointer])
  3289. input_line_pointer++;
  3290. ch = *input_line_pointer;
  3291. *input_line_pointer = '\0';
  3292. if (strcmp (name, "mshort") == 0)
  3293. {
  3294. elf_flags &= ~E_M68HC11_I32;
  3295. }
  3296. else if (strcmp (name, "mlong") == 0)
  3297. {
  3298. elf_flags |= E_M68HC11_I32;
  3299. }
  3300. else if (strcmp (name, "mshort-double") == 0)
  3301. {
  3302. elf_flags &= ~E_M68HC11_F64;
  3303. }
  3304. else if (strcmp (name, "mlong-double") == 0)
  3305. {
  3306. elf_flags |= E_M68HC11_F64;
  3307. }
  3308. else
  3309. {
  3310. as_warn (_("Invalid mode: %s\n"), name);
  3311. }
  3312. *input_line_pointer = ch;
  3313. demand_empty_rest_of_line ();
  3314. }
  3315. /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
  3316. are using 'rtc' for returning. It is necessary to use 'call'
  3317. to invoke them. This is also used by the debugger to correctly
  3318. find the stack frame. */
  3319. static void
  3320. s_m68hc11_mark_symbol (int mark)
  3321. {
  3322. char *name;
  3323. int c;
  3324. symbolS *symbolP;
  3325. asymbol *bfdsym;
  3326. elf_symbol_type *elfsym;
  3327. do
  3328. {
  3329. name = input_line_pointer;
  3330. c = get_symbol_end ();
  3331. symbolP = symbol_find_or_make (name);
  3332. *input_line_pointer = c;
  3333. SKIP_WHITESPACE ();
  3334. bfdsym = symbol_get_bfdsym (symbolP);
  3335. elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
  3336. gas_assert (elfsym);
  3337. /* Mark the symbol far (using rtc for function return). */
  3338. elfsym->internal_elf_sym.st_other |= mark;
  3339. if (c == ',')
  3340. {
  3341. input_line_pointer ++;
  3342. SKIP_WHITESPACE ();
  3343. if (*input_line_pointer == '\n')
  3344. c = '\n';
  3345. }
  3346. }
  3347. while (c == ',');
  3348. demand_empty_rest_of_line ();
  3349. }
  3350. static void
  3351. s_m68hc11_relax (int ignore ATTRIBUTE_UNUSED)
  3352. {
  3353. expressionS ex;
  3354. expression (&ex);
  3355. if (ex.X_op != O_symbol || ex.X_add_number != 0)
  3356. {
  3357. as_bad (_("bad .relax format"));
  3358. ignore_rest_of_line ();
  3359. return;
  3360. }
  3361. fix_new_exp (frag_now, frag_now_fix (), 0, &ex, 1,
  3362. BFD_RELOC_M68HC11_RL_GROUP);
  3363. demand_empty_rest_of_line ();
  3364. }
  3365. /* Relocation, relaxation and frag conversions. */
  3366. /* PC-relative offsets are relative to the start of the
  3367. next instruction. That is, the address of the offset, plus its
  3368. size, since the offset is always the last part of the insn. */
  3369. long
  3370. md_pcrel_from (fixS *fixP)
  3371. {
  3372. if (fixP->fx_r_type == BFD_RELOC_M68HC11_RL_JUMP)
  3373. return 0;
  3374. return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  3375. }
  3376. /* If while processing a fixup, a reloc really needs to be created
  3377. then it is done here. */
  3378. arelent *
  3379. tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
  3380. {
  3381. arelent *reloc;
  3382. reloc = (arelent *) xmalloc (sizeof (arelent));
  3383. reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
  3384. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  3385. reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  3386. if (fixp->fx_r_type == 0)
  3387. reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
  3388. else
  3389. reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
  3390. if (reloc->howto == (reloc_howto_type *) NULL)
  3391. {
  3392. as_bad_where (fixp->fx_file, fixp->fx_line,
  3393. _("Relocation %d is not supported by object file format."),
  3394. (int) fixp->fx_r_type);
  3395. return NULL;
  3396. }
  3397. /* Since we use Rel instead of Rela, encode the vtable entry to be
  3398. used in the relocation's section offset. */
  3399. if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  3400. reloc->address = fixp->fx_offset;
  3401. reloc->addend = 0;
  3402. return reloc;
  3403. }
  3404. /* We need a port-specific relaxation function to cope with sym2 - sym1
  3405. relative expressions with both symbols in the same segment (but not
  3406. necessarily in the same frag as this insn), for example:
  3407. ldab sym2-(sym1-2),pc
  3408. sym1:
  3409. The offset can be 5, 9 or 16 bits long. */
  3410. long
  3411. m68hc11_relax_frag (segT seg ATTRIBUTE_UNUSED, fragS *fragP,
  3412. long stretch ATTRIBUTE_UNUSED)
  3413. {
  3414. long growth;
  3415. offsetT aim = 0;
  3416. symbolS *symbolP;
  3417. const relax_typeS *this_type;
  3418. const relax_typeS *start_type;
  3419. relax_substateT next_state;
  3420. relax_substateT this_state;
  3421. const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
  3422. /* We only have to cope with frags as prepared by
  3423. md_estimate_size_before_relax. The STATE_BITS16 case may geet here
  3424. because of the different reasons that it's not relaxable. */
  3425. switch (fragP->fr_subtype)
  3426. {
  3427. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16):
  3428. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16):
  3429. /* When we get to this state, the frag won't grow any more. */
  3430. return 0;
  3431. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS5):
  3432. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS5):
  3433. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9):
  3434. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9):
  3435. if (fragP->fr_symbol == NULL
  3436. || S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3437. as_fatal (_("internal inconsistency problem in %s: fr_symbol %lx"),
  3438. __FUNCTION__, (long) fragP->fr_symbol);
  3439. symbolP = fragP->fr_symbol;
  3440. if (symbol_resolved_p (symbolP))
  3441. as_fatal (_("internal inconsistency problem in %s: resolved symbol"),
  3442. __FUNCTION__);
  3443. aim = S_GET_VALUE (symbolP);
  3444. break;
  3445. default:
  3446. as_fatal (_("internal inconsistency problem in %s: fr_subtype %d"),
  3447. __FUNCTION__, fragP->fr_subtype);
  3448. }
  3449. /* The rest is stolen from relax_frag. There's no obvious way to
  3450. share the code, but fortunately no requirement to keep in sync as
  3451. long as fragP->fr_symbol does not have its segment changed. */
  3452. this_state = fragP->fr_subtype;
  3453. start_type = this_type = table + this_state;
  3454. if (aim < 0)
  3455. {
  3456. /* Look backwards. */
  3457. for (next_state = this_type->rlx_more; next_state;)
  3458. if (aim >= this_type->rlx_backward)
  3459. next_state = 0;
  3460. else
  3461. {
  3462. /* Grow to next state. */
  3463. this_state = next_state;
  3464. this_type = table + this_state;
  3465. next_state = this_type->rlx_more;
  3466. }
  3467. }
  3468. else
  3469. {
  3470. /* Look forwards. */
  3471. for (next_state = this_type->rlx_more; next_state;)
  3472. if (aim <= this_type->rlx_forward)
  3473. next_state = 0;
  3474. else
  3475. {
  3476. /* Grow to next state. */
  3477. this_state = next_state;
  3478. this_type = table + this_state;
  3479. next_state = this_type->rlx_more;
  3480. }
  3481. }
  3482. growth = this_type->rlx_length - start_type->rlx_length;
  3483. if (growth != 0)
  3484. fragP->fr_subtype = this_state;
  3485. return growth;
  3486. }
  3487. void
  3488. md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, asection *sec ATTRIBUTE_UNUSED,
  3489. fragS *fragP)
  3490. {
  3491. fixS *fixp;
  3492. long value;
  3493. long disp;
  3494. char *buffer_address = fragP->fr_literal;
  3495. /* Address in object code of the displacement. */
  3496. register int object_address = fragP->fr_fix + fragP->fr_address;
  3497. buffer_address += fragP->fr_fix;
  3498. /* The displacement of the address, from current location. */
  3499. value = S_GET_VALUE (fragP->fr_symbol);
  3500. disp = (value + fragP->fr_offset) - object_address;
  3501. switch (fragP->fr_subtype)
  3502. {
  3503. case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
  3504. fragP->fr_opcode[1] = disp;
  3505. break;
  3506. case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
  3507. /* This relax is only for bsr and bra. */
  3508. gas_assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
  3509. || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
  3510. || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
  3511. fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
  3512. fix_new (fragP, fragP->fr_fix - 1, 2,
  3513. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3514. fragP->fr_fix += 1;
  3515. break;
  3516. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
  3517. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_BYTE):
  3518. fragP->fr_opcode[1] = disp;
  3519. break;
  3520. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
  3521. /* Invert branch. */
  3522. fragP->fr_opcode[0] ^= 1;
  3523. fragP->fr_opcode[1] = 3; /* Branch offset. */
  3524. buffer_address[0] = M6811_JMP;
  3525. fix_new (fragP, fragP->fr_fix + 1, 2,
  3526. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3527. fragP->fr_fix += 3;
  3528. break;
  3529. case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD):
  3530. /* Translate branch into a long branch. */
  3531. fragP->fr_opcode[1] = fragP->fr_opcode[0];
  3532. fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
  3533. fixp = fix_new (fragP, fragP->fr_fix, 2,
  3534. fragP->fr_symbol, fragP->fr_offset, 1,
  3535. BFD_RELOC_16_PCREL);
  3536. fixp->fx_pcrel_adjust = 2;
  3537. fragP->fr_fix += 2;
  3538. break;
  3539. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS5):
  3540. if (fragP->fr_symbol != 0
  3541. && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3542. value = disp;
  3543. /* fall through */
  3544. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS5):
  3545. fragP->fr_opcode[0] = fragP->fr_opcode[0] << 6;
  3546. fragP->fr_opcode[0] |= value & 0x1f;
  3547. break;
  3548. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9):
  3549. /* For a PC-relative offset, use the displacement with a -1 correction
  3550. to take into account the additional byte of the insn. */
  3551. if (fragP->fr_symbol != 0
  3552. && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3553. value = disp - 1;
  3554. /* fall through */
  3555. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9):
  3556. fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
  3557. fragP->fr_opcode[0] |= 0xE0;
  3558. fragP->fr_opcode[0] |= (value >> 8) & 1;
  3559. fragP->fr_opcode[1] = value;
  3560. fragP->fr_fix += 1;
  3561. break;
  3562. case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16):
  3563. case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16):
  3564. fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
  3565. fragP->fr_opcode[0] |= 0xe2;
  3566. if ((fragP->fr_opcode[0] & 0x0ff) == 0x0fa
  3567. && fragP->fr_symbol != 0
  3568. && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
  3569. {
  3570. fixp = fix_new (fragP, fragP->fr_fix, 2,
  3571. fragP->fr_symbol, fragP->fr_offset,
  3572. 1, BFD_RELOC_16_PCREL);
  3573. }
  3574. else
  3575. {
  3576. fix_new (fragP, fragP->fr_fix, 2,
  3577. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3578. }
  3579. fragP->fr_fix += 2;
  3580. break;
  3581. case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE):
  3582. if (disp < 0)
  3583. fragP->fr_opcode[0] |= 0x10;
  3584. fragP->fr_opcode[1] = disp & 0x0FF;
  3585. break;
  3586. case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD):
  3587. /* Invert branch. */
  3588. fragP->fr_opcode[0] ^= 0x20;
  3589. fragP->fr_opcode[1] = 3; /* Branch offset. */
  3590. buffer_address[0] = M6812_JMP;
  3591. fix_new (fragP, fragP->fr_fix + 1, 2,
  3592. fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
  3593. fragP->fr_fix += 3;
  3594. break;
  3595. default:
  3596. break;
  3597. }
  3598. }
  3599. /* On an ELF system, we can't relax a weak symbol. The weak symbol
  3600. can be overridden at final link time by a non weak symbol. We can
  3601. relax externally visible symbol because there is no shared library
  3602. and such symbol can't be overridden (unless they are weak). */
  3603. static int
  3604. relaxable_symbol (symbolS *symbol)
  3605. {
  3606. return ! S_IS_WEAK (symbol);
  3607. }
  3608. /* Force truly undefined symbols to their maximum size, and generally set up
  3609. the frag list to be relaxed. */
  3610. int
  3611. md_estimate_size_before_relax (fragS *fragP, asection *segment)
  3612. {
  3613. if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
  3614. {
  3615. if (S_GET_SEGMENT (fragP->fr_symbol) != segment
  3616. || !relaxable_symbol (fragP->fr_symbol)
  3617. || (segment != absolute_section
  3618. && RELAX_STATE (fragP->fr_subtype) == STATE_INDEXED_OFFSET))
  3619. {
  3620. /* Non-relaxable cases. */
  3621. int old_fr_fix;
  3622. char *buffer_address;
  3623. old_fr_fix = fragP->fr_fix;
  3624. buffer_address = fragP->fr_fix + fragP->fr_literal;
  3625. switch (RELAX_STATE (fragP->fr_subtype))
  3626. {
  3627. case STATE_PC_RELATIVE:
  3628. /* This relax is only for bsr and bra. */
  3629. gas_assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
  3630. || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
  3631. || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
  3632. if (flag_fixed_branches)
  3633. as_bad_where (fragP->fr_file, fragP->fr_line,
  3634. _("bra or bsr with undefined symbol."));
  3635. /* The symbol is undefined or in a separate section.
  3636. Turn bra into a jmp and bsr into a jsr. The insn
  3637. becomes 3 bytes long (instead of 2). A fixup is
  3638. necessary for the unresolved symbol address. */
  3639. fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
  3640. fix_new (fragP, fragP->fr_fix - 1, 2, fragP->fr_symbol,
  3641. fragP->fr_offset, 0, BFD_RELOC_16);
  3642. fragP->fr_fix++;
  3643. break;
  3644. case STATE_CONDITIONAL_BRANCH:
  3645. gas_assert (current_architecture & cpu6811);
  3646. fragP->fr_opcode[0] ^= 1; /* Reverse sense of branch. */
  3647. fragP->fr_opcode[1] = 3; /* Skip next jmp insn (3 bytes). */
  3648. /* Don't use fr_opcode[2] because this may be
  3649. in a different frag. */
  3650. buffer_address[0] = M6811_JMP;
  3651. fragP->fr_fix++;
  3652. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3653. fragP->fr_offset, 0, BFD_RELOC_16);
  3654. fragP->fr_fix += 2;
  3655. break;
  3656. case STATE_INDEXED_OFFSET:
  3657. gas_assert (current_architecture & cpu6812);
  3658. if (fragP->fr_symbol
  3659. && S_GET_SEGMENT (fragP->fr_symbol) == absolute_section)
  3660. {
  3661. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_OFFSET,
  3662. STATE_BITS5);
  3663. /* Return the size of the variable part of the frag. */
  3664. return md_relax_table[fragP->fr_subtype].rlx_length;
  3665. }
  3666. else
  3667. {
  3668. /* Switch the indexed operation to 16-bit mode. */
  3669. fragP->fr_opcode[0] = fragP->fr_opcode[0] << 3;
  3670. fragP->fr_opcode[0] |= 0xe2;
  3671. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3672. fragP->fr_offset, 0, BFD_RELOC_16);
  3673. fragP->fr_fix += 2;
  3674. }
  3675. break;
  3676. case STATE_INDEXED_PCREL:
  3677. gas_assert (current_architecture & cpu6812);
  3678. if (fragP->fr_symbol
  3679. && S_GET_SEGMENT (fragP->fr_symbol) == absolute_section)
  3680. {
  3681. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_PCREL,
  3682. STATE_BITS5);
  3683. /* Return the size of the variable part of the frag. */
  3684. return md_relax_table[fragP->fr_subtype].rlx_length;
  3685. }
  3686. else
  3687. {
  3688. fragP->fr_opcode[0] = fragP->fr_opcode[0] << 3;
  3689. fragP->fr_opcode[0] |= 0xe2;
  3690. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3691. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  3692. fragP->fr_fix += 2;
  3693. }
  3694. break;
  3695. case STATE_XBCC_BRANCH:
  3696. gas_assert (current_architecture & cpu6812);
  3697. fragP->fr_opcode[0] ^= 0x20; /* Reverse sense of branch. */
  3698. fragP->fr_opcode[1] = 3; /* Skip next jmp insn (3 bytes). */
  3699. /* Don't use fr_opcode[2] because this may be
  3700. in a different frag. */
  3701. buffer_address[0] = M6812_JMP;
  3702. fragP->fr_fix++;
  3703. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3704. fragP->fr_offset, 0, BFD_RELOC_16);
  3705. fragP->fr_fix += 2;
  3706. break;
  3707. case STATE_CONDITIONAL_BRANCH_6812:
  3708. gas_assert (current_architecture & cpu6812);
  3709. /* Translate into a lbcc branch. */
  3710. fragP->fr_opcode[1] = fragP->fr_opcode[0];
  3711. fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
  3712. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
  3713. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  3714. fragP->fr_fix += 2;
  3715. break;
  3716. default:
  3717. as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
  3718. }
  3719. frag_wane (fragP);
  3720. /* Return the growth in the fixed part of the frag. */
  3721. return fragP->fr_fix - old_fr_fix;
  3722. }
  3723. /* Relaxable cases. */
  3724. switch (RELAX_STATE (fragP->fr_subtype))
  3725. {
  3726. case STATE_PC_RELATIVE:
  3727. /* This relax is only for bsr and bra. */
  3728. gas_assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
  3729. || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
  3730. || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
  3731. fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
  3732. break;
  3733. case STATE_CONDITIONAL_BRANCH:
  3734. gas_assert (current_architecture & cpu6811);
  3735. fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH,
  3736. STATE_BYTE);
  3737. break;
  3738. case STATE_INDEXED_OFFSET:
  3739. gas_assert (current_architecture & cpu6812);
  3740. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_OFFSET,
  3741. STATE_BITS5);
  3742. break;
  3743. case STATE_INDEXED_PCREL:
  3744. gas_assert (current_architecture & cpu6812);
  3745. fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_PCREL,
  3746. STATE_BITS5);
  3747. break;
  3748. case STATE_XBCC_BRANCH:
  3749. gas_assert (current_architecture & cpu6812);
  3750. fragP->fr_subtype = ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE);
  3751. break;
  3752. case STATE_CONDITIONAL_BRANCH_6812:
  3753. gas_assert (current_architecture & cpu6812);
  3754. fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812,
  3755. STATE_BYTE);
  3756. break;
  3757. }
  3758. }
  3759. if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
  3760. as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
  3761. /* Return the size of the variable part of the frag. */
  3762. return md_relax_table[fragP->fr_subtype].rlx_length;
  3763. }
  3764. /* See whether we need to force a relocation into the output file. */
  3765. int
  3766. tc_m68hc11_force_relocation (fixS *fixP)
  3767. {
  3768. if (fixP->fx_r_type == BFD_RELOC_M68HC11_RL_GROUP)
  3769. return 1;
  3770. return generic_force_reloc (fixP);
  3771. }
  3772. /* Here we decide which fixups can be adjusted to make them relative
  3773. to the beginning of the section instead of the symbol. Basically
  3774. we need to make sure that the linker relaxation is done
  3775. correctly, so in some cases we force the original symbol to be
  3776. used. */
  3777. int
  3778. tc_m68hc11_fix_adjustable (fixS *fixP)
  3779. {
  3780. switch (fixP->fx_r_type)
  3781. {
  3782. /* For the linker relaxation to work correctly, these relocs
  3783. need to be on the symbol itself. */
  3784. case BFD_RELOC_16:
  3785. case BFD_RELOC_M68HC11_RL_JUMP:
  3786. case BFD_RELOC_M68HC11_RL_GROUP:
  3787. case BFD_RELOC_VTABLE_INHERIT:
  3788. case BFD_RELOC_VTABLE_ENTRY:
  3789. case BFD_RELOC_32:
  3790. /* The memory bank addressing translation also needs the original
  3791. symbol. */
  3792. case BFD_RELOC_M68HC11_LO16:
  3793. case BFD_RELOC_M68HC11_PAGE:
  3794. case BFD_RELOC_M68HC11_24:
  3795. return 0;
  3796. default:
  3797. return 1;
  3798. }
  3799. }
  3800. void
  3801. md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
  3802. {
  3803. char *where;
  3804. long value = * valP;
  3805. if (fixP->fx_addsy == (symbolS *) NULL)
  3806. fixP->fx_done = 1;
  3807. /* We don't actually support subtracting a symbol. */
  3808. if (fixP->fx_subsy != (symbolS *) NULL)
  3809. as_bad_where (fixP->fx_file, fixP->fx_line, _("Expression too complex."));
  3810. /* Patch the instruction with the resolved operand. Elf relocation
  3811. info will also be generated to take care of linker/loader fixups.
  3812. The 68HC11 addresses only 64Kb, we are only concerned by 8 and 16-bit
  3813. relocs. BFD_RELOC_8 is basically used for .page0 access (the linker
  3814. will warn for overflows). BFD_RELOC_8_PCREL should not be generated
  3815. because it's either resolved or turned out into non-relative insns (see
  3816. relax table, bcc, bra, bsr transformations)
  3817. The BFD_RELOC_32 is necessary for the support of --gstabs. */
  3818. where = fixP->fx_frag->fr_literal + fixP->fx_where;
  3819. switch (fixP->fx_r_type)
  3820. {
  3821. case BFD_RELOC_32:
  3822. bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
  3823. break;
  3824. case BFD_RELOC_24:
  3825. case BFD_RELOC_M68HC11_24:
  3826. bfd_putb16 ((bfd_vma) (value & 0x0ffff), (unsigned char *) where);
  3827. ((bfd_byte*) where)[2] = ((value >> 16) & 0x0ff);
  3828. break;
  3829. case BFD_RELOC_16:
  3830. case BFD_RELOC_16_PCREL:
  3831. case BFD_RELOC_M68HC11_LO16:
  3832. bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
  3833. if (value < -65537 || value > 65535)
  3834. as_bad_where (fixP->fx_file, fixP->fx_line,
  3835. _("Value out of 16-bit range."));
  3836. break;
  3837. case BFD_RELOC_M68HC11_HI8:
  3838. /* Caution, %hi(<symbol>+%ld) will generate incorrect code if %lo
  3839. causes a carry. */
  3840. case BFD_RELOC_M68HC12_HI8XG:
  3841. value = value >> 8;
  3842. /* Fall through. */
  3843. case BFD_RELOC_M68HC12_LO8XG:
  3844. case BFD_RELOC_M68HC11_LO8:
  3845. case BFD_RELOC_8:
  3846. case BFD_RELOC_M68HC11_PAGE:
  3847. ((bfd_byte *) where)[0] = (bfd_byte) value;
  3848. break;
  3849. case BFD_RELOC_8_PCREL:
  3850. ((bfd_byte *) where)[0] = (bfd_byte) value;
  3851. if (value < -128 || value > 127)
  3852. as_bad_where (fixP->fx_file, fixP->fx_line,
  3853. _("Value %ld too large for 8-bit PC-relative branch."),
  3854. value);
  3855. break;
  3856. /* These next two are for XGATE. */
  3857. case BFD_RELOC_M68HC12_9_PCREL:
  3858. ((bfd_byte *) where)[0] |= (bfd_byte) ((value >>9) & 0x01);
  3859. ((bfd_byte *) where)[1] = (bfd_byte) ((value>>1) & 0xff);
  3860. if (value < -512 || value > 511)
  3861. as_bad_where (fixP->fx_file, fixP->fx_line,
  3862. _("Value %ld too large for 9-bit PC-relative branch."),
  3863. value);
  3864. break;
  3865. case BFD_RELOC_M68HC12_10_PCREL:
  3866. ((bfd_byte *) where)[0] |= (bfd_byte) ((value >>9) & 0x03);
  3867. ((bfd_byte *) where)[1] = (bfd_byte) ((value>>1) & 0xff);
  3868. if (value < -1024 || value > 1023)
  3869. as_bad_where (fixP->fx_file, fixP->fx_line,
  3870. _("Value %ld too large for 10-bit PC-relative branch."),
  3871. value);
  3872. break;
  3873. case BFD_RELOC_M68HC11_3B:
  3874. if (value <= 0 || value > 8)
  3875. as_bad_where (fixP->fx_file, fixP->fx_line,
  3876. _("Auto increment/decrement offset '%ld' is out of range."),
  3877. value);
  3878. if (where[0] & 0x8)
  3879. value = 8 - value;
  3880. else
  3881. value--;
  3882. where[0] = where[0] | (value & 0x07);
  3883. break;
  3884. case BFD_RELOC_M68HC12_5B:
  3885. if (value < -16 || value > 15)
  3886. as_bad_where (fixP->fx_file, fixP->fx_line,
  3887. _("Offset out of 5-bit range for movw/movb insn: %ld"),
  3888. value);
  3889. if (value >= 0)
  3890. where[0] |= value;
  3891. else
  3892. where[0] |= (0x10 | (16 + value));
  3893. break;
  3894. case BFD_RELOC_M68HC12_9B:
  3895. if (value < -256 || value > 255)
  3896. as_bad_where (fixP->fx_file, fixP->fx_line,
  3897. _("Offset out of 9-bit range for movw/movb insn: %ld"),
  3898. value);
  3899. /* sign bit already in xb postbyte */
  3900. if (value >= 0)
  3901. where[1] = value;
  3902. else
  3903. where[1] = (256 + value);
  3904. break;
  3905. case BFD_RELOC_M68HC12_16B:
  3906. if (value < -32768 || value > 32767)
  3907. as_bad_where (fixP->fx_file, fixP->fx_line,
  3908. _("Offset out of 16-bit range for movw/movb insn: %ld"),
  3909. value);
  3910. if (value < 0)
  3911. value += 65536;
  3912. where[1] = (value >> 8);
  3913. where[2] = (value & 0xff);
  3914. break;
  3915. case BFD_RELOC_M68HC11_RL_JUMP:
  3916. case BFD_RELOC_M68HC11_RL_GROUP:
  3917. case BFD_RELOC_VTABLE_INHERIT:
  3918. case BFD_RELOC_VTABLE_ENTRY:
  3919. fixP->fx_done = 0;
  3920. return;
  3921. default:
  3922. as_fatal (_("Line %d: unknown relocation type: 0x%x."),
  3923. fixP->fx_line, fixP->fx_r_type);
  3924. }
  3925. }
  3926. /* Set the ELF specific flags. */
  3927. void
  3928. m68hc11_elf_final_processing (void)
  3929. {
  3930. if (current_architecture & cpu6812s)
  3931. elf_flags |= EF_M68HCS12_MACH;
  3932. elf_elfheader (stdoutput)->e_flags &= ~EF_M68HC11_ABI;
  3933. elf_elfheader (stdoutput)->e_flags |= elf_flags;
  3934. }