PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/sdcc-260/binutils-avr/gas/config/tc-sh.c

#
C | 2436 lines | 1869 code | 276 blank | 291 comment | 578 complexity | 431fdb9a192dbffa11074a3bf3fdfaeb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /* tc-sh.c -- Assemble code for the Hitachi Super-H
  2. Copyright (C) 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /*
  17. Written By Steve Chamberlain
  18. sac@cygnus.com
  19. */
  20. #include <stdio.h>
  21. #include "as.h"
  22. #include "bfd.h"
  23. #include "subsegs.h"
  24. #define DEFINE_TABLE
  25. #include "opcodes/sh-opc.h"
  26. #include <ctype.h>
  27. const char comment_chars[] = "!";
  28. const char line_separator_chars[] = ";";
  29. const char line_comment_chars[] = "!#";
  30. static void s_uses PARAMS ((int));
  31. static void sh_count_relocs PARAMS ((bfd *, segT, PTR));
  32. static void sh_frob_section PARAMS ((bfd *, segT, PTR));
  33. /* This table describes all the machine specific pseudo-ops the assembler
  34. has to support. The fields are:
  35. pseudo-op name without dot
  36. function to call to execute this pseudo-op
  37. Integer arg to pass to the function
  38. */
  39. void cons ();
  40. void s_align_bytes ();
  41. static void s_uacons PARAMS ((int));
  42. int shl = 0;
  43. static void
  44. little (ignore)
  45. int ignore;
  46. {
  47. shl = 1;
  48. target_big_endian = 0;
  49. }
  50. const pseudo_typeS md_pseudo_table[] =
  51. {
  52. {"int", cons, 4},
  53. {"word", cons, 2},
  54. {"form", listing_psize, 0},
  55. {"little", little, 0},
  56. {"heading", listing_title, 0},
  57. {"import", s_ignore, 0},
  58. {"page", listing_eject, 0},
  59. {"program", s_ignore, 0},
  60. {"uses", s_uses, 0},
  61. {"uaword", s_uacons, 2},
  62. {"ualong", s_uacons, 4},
  63. {0, 0, 0}
  64. };
  65. /*int md_reloc_size; */
  66. int sh_relax; /* set if -relax seen */
  67. /* Whether -small was seen. */
  68. int sh_small;
  69. const char EXP_CHARS[] = "eE";
  70. /* Chars that mean this number is a floating point constant */
  71. /* As in 0f12.456 */
  72. /* or 0d1.2345e12 */
  73. const char FLT_CHARS[] = "rRsSfFdDxXpP";
  74. #define C(a,b) ENCODE_RELAX(a,b)
  75. #define JREG 14 /* Register used as a temp when relaxing */
  76. #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
  77. #define GET_WHAT(x) ((x>>4))
  78. /* These are the three types of relaxable instrction */
  79. #define COND_JUMP 1
  80. #define COND_JUMP_DELAY 2
  81. #define UNCOND_JUMP 3
  82. #define END 4
  83. #define UNDEF_DISP 0
  84. #define COND8 1
  85. #define COND12 2
  86. #define COND32 3
  87. #define UNCOND12 1
  88. #define UNCOND32 2
  89. #define UNDEF_WORD_DISP 4
  90. #define UNCOND12 1
  91. #define UNCOND32 2
  92. /* Branch displacements are from the address of the branch plus
  93. four, thus all minimum and maximum values have 4 added to them. */
  94. #define COND8_F 258
  95. #define COND8_M -252
  96. #define COND8_LENGTH 2
  97. /* There is one extra instruction before the branch, so we must add
  98. two more bytes to account for it. */
  99. #define COND12_F 4100
  100. #define COND12_M -4090
  101. #define COND12_LENGTH 6
  102. #define COND12_DELAY_LENGTH 4
  103. /* ??? The minimum and maximum values are wrong, but this does not matter
  104. since this relocation type is not supported yet. */
  105. #define COND32_F (1<<30)
  106. #define COND32_M -(1<<30)
  107. #define COND32_LENGTH 14
  108. #define UNCOND12_F 4098
  109. #define UNCOND12_M -4092
  110. #define UNCOND12_LENGTH 2
  111. /* ??? The minimum and maximum values are wrong, but this does not matter
  112. since this relocation type is not supported yet. */
  113. #define UNCOND32_F (1<<30)
  114. #define UNCOND32_M -(1<<30)
  115. #define UNCOND32_LENGTH 14
  116. const relax_typeS md_relax_table[C (END, 0)] = {
  117. { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  118. { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  119. { 0 },
  120. /* C (COND_JUMP, COND8) */
  121. { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
  122. /* C (COND_JUMP, COND12) */
  123. { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
  124. /* C (COND_JUMP, COND32) */
  125. { COND32_F, COND32_M, COND32_LENGTH, 0, },
  126. { 0 }, { 0 }, { 0 }, { 0 },
  127. { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  128. { 0 },
  129. /* C (COND_JUMP_DELAY, COND8) */
  130. { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP_DELAY, COND12) },
  131. /* C (COND_JUMP_DELAY, COND12) */
  132. { COND12_F, COND12_M, COND12_DELAY_LENGTH, C (COND_JUMP_DELAY, COND32), },
  133. /* C (COND_JUMP_DELAY, COND32) */
  134. { COND32_F, COND32_M, COND32_LENGTH, 0, },
  135. { 0 }, { 0 }, { 0 }, { 0 },
  136. { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  137. { 0 },
  138. /* C (UNCOND_JUMP, UNCOND12) */
  139. { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
  140. /* C (UNCOND_JUMP, UNCOND32) */
  141. { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
  142. { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  143. { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
  144. };
  145. static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
  146. /*
  147. This function is called once, at assembler startup time. This should
  148. set up all the tables, etc that the MD part of the assembler needs
  149. */
  150. void
  151. md_begin ()
  152. {
  153. sh_opcode_info *opcode;
  154. char *prev_name = "";
  155. if (! shl)
  156. target_big_endian = 1;
  157. opcode_hash_control = hash_new ();
  158. /* Insert unique names into hash table */
  159. for (opcode = sh_table; opcode->name; opcode++)
  160. {
  161. if (strcmp (prev_name, opcode->name))
  162. {
  163. prev_name = opcode->name;
  164. hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
  165. }
  166. else
  167. {
  168. /* Make all the opcodes with the same name point to the same
  169. string */
  170. opcode->name = prev_name;
  171. }
  172. }
  173. }
  174. static int reg_m;
  175. static int reg_n;
  176. static int reg_b;
  177. static expressionS immediate; /* absolute expression */
  178. typedef struct
  179. {
  180. sh_arg_type type;
  181. int reg;
  182. }
  183. sh_operand_info;
  184. /* try and parse a reg name, returns number of chars consumed */
  185. static int
  186. parse_reg (src, mode, reg)
  187. char *src;
  188. int *mode;
  189. int *reg;
  190. {
  191. /* We use !isalnum for the next character after the register name, to
  192. make sure that we won't accidentally recognize a symbol name such as
  193. 'sram' as being a reference to the register 'sr'. */
  194. if (src[0] == 'r')
  195. {
  196. if (src[1] >= '0' && src[1] <= '7' && strncmp(&src[2], "_bank", 5) == 0
  197. && ! isalnum ((unsigned char) src[7]))
  198. {
  199. *mode = A_REG_B;
  200. *reg = (src[1] - '0');
  201. return 7;
  202. }
  203. }
  204. if (src[0] == 'r')
  205. {
  206. if (src[1] == '1')
  207. {
  208. if (src[2] >= '0' && src[2] <= '5'
  209. && ! isalnum ((unsigned char) src[3]))
  210. {
  211. *mode = A_REG_N;
  212. *reg = 10 + src[2] - '0';
  213. return 3;
  214. }
  215. }
  216. if (src[1] >= '0' && src[1] <= '9'
  217. && ! isalnum ((unsigned char) src[2]))
  218. {
  219. *mode = A_REG_N;
  220. *reg = (src[1] - '0');
  221. return 2;
  222. }
  223. }
  224. if (src[0] == 's'
  225. && src[1] == 's'
  226. && src[2] == 'r' && ! isalnum ((unsigned char) src[3]))
  227. {
  228. *mode = A_SSR;
  229. return 3;
  230. }
  231. if (src[0] == 's' && src[1] == 'p' && src[2] == 'c'
  232. && ! isalnum ((unsigned char) src[3]))
  233. {
  234. *mode = A_SPC;
  235. return 3;
  236. }
  237. if (src[0] == 's' && src[1] == 'g' && src[2] == 'r'
  238. && ! isalnum ((unsigned char) src[3]))
  239. {
  240. *mode = A_SGR;
  241. return 3;
  242. }
  243. if (src[0] == 'd' && src[1] == 'b' && src[2] == 'r'
  244. && ! isalnum ((unsigned char) src[3]))
  245. {
  246. *mode = A_DBR;
  247. return 3;
  248. }
  249. if (src[0] == 's' && src[1] == 'r' && ! isalnum ((unsigned char) src[2]))
  250. {
  251. *mode = A_SR;
  252. return 2;
  253. }
  254. if (src[0] == 's' && src[1] == 'p' && ! isalnum ((unsigned char) src[2]))
  255. {
  256. *mode = A_REG_N;
  257. *reg = 15;
  258. return 2;
  259. }
  260. if (src[0] == 'p' && src[1] == 'r' && ! isalnum ((unsigned char) src[2]))
  261. {
  262. *mode = A_PR;
  263. return 2;
  264. }
  265. if (src[0] == 'p' && src[1] == 'c' && ! isalnum ((unsigned char) src[2]))
  266. {
  267. *mode = A_DISP_PC;
  268. return 2;
  269. }
  270. if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r'
  271. && ! isalnum ((unsigned char) src[3]))
  272. {
  273. *mode = A_GBR;
  274. return 3;
  275. }
  276. if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r'
  277. && ! isalnum ((unsigned char) src[3]))
  278. {
  279. *mode = A_VBR;
  280. return 3;
  281. }
  282. if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c'
  283. && ! isalnum ((unsigned char) src[4]))
  284. {
  285. if (src[3] == 'l')
  286. {
  287. *mode = A_MACL;
  288. return 4;
  289. }
  290. if (src[3] == 'h')
  291. {
  292. *mode = A_MACH;
  293. return 4;
  294. }
  295. }
  296. if (src[0] == 'f' && src[1] == 'r')
  297. {
  298. if (src[2] == '1')
  299. {
  300. if (src[3] >= '0' && src[3] <= '5'
  301. && ! isalnum ((unsigned char) src[4]))
  302. {
  303. *mode = F_REG_N;
  304. *reg = 10 + src[3] - '0';
  305. return 4;
  306. }
  307. }
  308. if (src[2] >= '0' && src[2] <= '9'
  309. && ! isalnum ((unsigned char) src[3]))
  310. {
  311. *mode = F_REG_N;
  312. *reg = (src[2] - '0');
  313. return 3;
  314. }
  315. }
  316. if (src[0] == 'd' && src[1] == 'r')
  317. {
  318. if (src[2] == '1')
  319. {
  320. if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
  321. && ! isalnum ((unsigned char) src[4]))
  322. {
  323. *mode = D_REG_N;
  324. *reg = 10 + src[3] - '0';
  325. return 4;
  326. }
  327. }
  328. if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
  329. && ! isalnum ((unsigned char) src[3]))
  330. {
  331. *mode = D_REG_N;
  332. *reg = (src[2] - '0');
  333. return 3;
  334. }
  335. }
  336. if (src[0] == 'x' && src[1] == 'd')
  337. {
  338. if (src[2] == '1')
  339. {
  340. if (src[3] >= '0' && src[3] <= '4' && ! ((src[3] - '0') & 1)
  341. && ! isalnum ((unsigned char) src[4]))
  342. {
  343. *mode = X_REG_N;
  344. *reg = 11 + src[3] - '0';
  345. return 4;
  346. }
  347. }
  348. if (src[2] >= '0' && src[2] <= '8' && ! ((src[2] - '0') & 1)
  349. && ! isalnum ((unsigned char) src[3]))
  350. {
  351. *mode = X_REG_N;
  352. *reg = (src[2] - '0') + 1;
  353. return 3;
  354. }
  355. }
  356. if (src[0] == 'f' && src[1] == 'v')
  357. {
  358. if (src[2] == '1'&& src[3] == '2' && ! isalnum ((unsigned char) src[4]))
  359. {
  360. *mode = V_REG_N;
  361. *reg = 12;
  362. return 4;
  363. }
  364. if ((src[2] == '0' || src[2] == '4' || src[2] == '8')
  365. && ! isalnum ((unsigned char) src[3]))
  366. {
  367. *mode = V_REG_N;
  368. *reg = (src[2] - '0');
  369. return 3;
  370. }
  371. }
  372. if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l'
  373. && ! isalnum ((unsigned char) src[4]))
  374. {
  375. *mode = FPUL_N;
  376. return 4;
  377. }
  378. if (src[0] == 'f' && src[1] == 'p' && src[2] == 's' && src[3] == 'c'
  379. && src[4] == 'r' && ! isalnum ((unsigned char) src[5]))
  380. {
  381. *mode = FPSCR_N;
  382. return 5;
  383. }
  384. if (src[0] == 'x' && src[1] == 'm' && src[2] == 't' && src[3] == 'r'
  385. && src[4] == 'x' && ! isalnum ((unsigned char) src[5]))
  386. {
  387. *mode = XMTRX_M4;
  388. return 5;
  389. }
  390. return 0;
  391. }
  392. static symbolS *dot()
  393. {
  394. const char *fake;
  395. /* JF: '.' is pseudo symbol with value of current location
  396. in current segment. */
  397. fake = FAKE_LABEL_NAME;
  398. return symbol_new (fake,
  399. now_seg,
  400. (valueT) frag_now_fix (),
  401. frag_now);
  402. }
  403. static
  404. char *
  405. parse_exp (s)
  406. char *s;
  407. {
  408. char *save;
  409. char *new;
  410. save = input_line_pointer;
  411. input_line_pointer = s;
  412. expression (&immediate);
  413. if (immediate.X_op == O_absent)
  414. as_bad (_("missing operand"));
  415. new = input_line_pointer;
  416. input_line_pointer = save;
  417. return new;
  418. }
  419. /* The many forms of operand:
  420. Rn Register direct
  421. @Rn Register indirect
  422. @Rn+ Autoincrement
  423. @-Rn Autodecrement
  424. @(disp:4,Rn)
  425. @(disp:8,GBR)
  426. @(disp:8,PC)
  427. @(R0,Rn)
  428. @(R0,GBR)
  429. disp:8
  430. disp:12
  431. #imm8
  432. pr, gbr, vbr, macl, mach
  433. */
  434. static
  435. char *
  436. parse_at (src, op)
  437. char *src;
  438. sh_operand_info *op;
  439. {
  440. int len;
  441. int mode;
  442. src++;
  443. if (src[0] == '-')
  444. {
  445. /* Must be predecrement */
  446. src++;
  447. len = parse_reg (src, &mode, &(op->reg));
  448. if (mode != A_REG_N)
  449. as_bad (_("illegal register after @-"));
  450. op->type = A_DEC_N;
  451. src += len;
  452. }
  453. else if (src[0] == '(')
  454. {
  455. /* Could be @(disp, rn), @(disp, gbr), @(disp, pc), @(r0, gbr) or
  456. @(r0, rn) */
  457. src++;
  458. len = parse_reg (src, &mode, &(op->reg));
  459. if (len && mode == A_REG_N)
  460. {
  461. src += len;
  462. if (op->reg != 0)
  463. {
  464. as_bad (_("must be @(r0,...)"));
  465. }
  466. if (src[0] == ',')
  467. src++;
  468. /* Now can be rn or gbr */
  469. len = parse_reg (src, &mode, &(op->reg));
  470. if (mode == A_GBR)
  471. {
  472. op->type = A_R0_GBR;
  473. }
  474. else if (mode == A_REG_N)
  475. {
  476. op->type = A_IND_R0_REG_N;
  477. }
  478. else
  479. {
  480. as_bad (_("syntax error in @(r0,...)"));
  481. }
  482. }
  483. else
  484. {
  485. /* Must be an @(disp,.. thing) */
  486. src = parse_exp (src);
  487. if (src[0] == ',')
  488. src++;
  489. /* Now can be rn, gbr or pc */
  490. len = parse_reg (src, &mode, &op->reg);
  491. if (len)
  492. {
  493. if (mode == A_REG_N)
  494. {
  495. op->type = A_DISP_REG_N;
  496. }
  497. else if (mode == A_GBR)
  498. {
  499. op->type = A_DISP_GBR;
  500. }
  501. else if (mode == A_DISP_PC)
  502. {
  503. /* Turn a plain @(4,pc) into @(.+4,pc) */
  504. if (immediate.X_op == O_constant) {
  505. immediate.X_add_symbol = dot();
  506. immediate.X_op = O_symbol;
  507. }
  508. op->type = A_DISP_PC;
  509. }
  510. else
  511. {
  512. as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
  513. }
  514. }
  515. else
  516. {
  517. as_bad (_("syntax error in @(disp,[Rn, gbr, pc])"));
  518. }
  519. }
  520. src += len;
  521. if (src[0] != ')')
  522. as_bad (_("expecting )"));
  523. else
  524. src++;
  525. }
  526. else
  527. {
  528. src += parse_reg (src, &mode, &(op->reg));
  529. if (mode != A_REG_N)
  530. {
  531. as_bad (_("illegal register after @"));
  532. }
  533. if (src[0] == '+')
  534. {
  535. op->type = A_INC_N;
  536. src++;
  537. }
  538. else
  539. {
  540. op->type = A_IND_N;
  541. }
  542. }
  543. return src;
  544. }
  545. static void
  546. get_operand (ptr, op)
  547. char **ptr;
  548. sh_operand_info *op;
  549. {
  550. char *src = *ptr;
  551. int mode = -1;
  552. unsigned int len;
  553. if (src[0] == '#')
  554. {
  555. src++;
  556. *ptr = parse_exp (src);
  557. op->type = A_IMM;
  558. return;
  559. }
  560. else if (src[0] == '@')
  561. {
  562. *ptr = parse_at (src, op);
  563. return;
  564. }
  565. len = parse_reg (src, &mode, &(op->reg));
  566. if (len)
  567. {
  568. *ptr = src + len;
  569. op->type = mode;
  570. return;
  571. }
  572. else
  573. {
  574. /* Not a reg, the only thing left is a displacement */
  575. *ptr = parse_exp (src);
  576. op->type = A_DISP_PC;
  577. return;
  578. }
  579. }
  580. static
  581. char *
  582. get_operands (info, args, operand)
  583. sh_opcode_info *info;
  584. char *args;
  585. sh_operand_info *operand;
  586. {
  587. char *ptr = args;
  588. if (info->arg[0])
  589. {
  590. ptr++;
  591. get_operand (&ptr, operand + 0);
  592. if (info->arg[1])
  593. {
  594. if (*ptr == ',')
  595. {
  596. ptr++;
  597. }
  598. get_operand (&ptr, operand + 1);
  599. if (info->arg[2])
  600. {
  601. if (*ptr == ',')
  602. {
  603. ptr++;
  604. }
  605. get_operand (&ptr, operand + 2);
  606. }
  607. else
  608. {
  609. operand[2].type = 0;
  610. }
  611. }
  612. else
  613. {
  614. operand[1].type = 0;
  615. operand[2].type = 0;
  616. }
  617. }
  618. else
  619. {
  620. operand[0].type = 0;
  621. operand[1].type = 0;
  622. operand[2].type = 0;
  623. }
  624. return ptr;
  625. }
  626. /* Passed a pointer to a list of opcodes which use different
  627. addressing modes, return the opcode which matches the opcodes
  628. provided
  629. */
  630. static
  631. sh_opcode_info *
  632. get_specific (opcode, operands)
  633. sh_opcode_info *opcode;
  634. sh_operand_info *operands;
  635. {
  636. sh_opcode_info *this_try = opcode;
  637. char *name = opcode->name;
  638. int n = 0;
  639. while (opcode->name)
  640. {
  641. this_try = opcode++;
  642. if (this_try->name != name)
  643. {
  644. /* We've looked so far down the table that we've run out of
  645. opcodes with the same name */
  646. return 0;
  647. }
  648. /* look at both operands needed by the opcodes and provided by
  649. the user - since an arg test will often fail on the same arg
  650. again and again, we'll try and test the last failing arg the
  651. first on each opcode try */
  652. for (n = 0; this_try->arg[n]; n++)
  653. {
  654. sh_operand_info *user = operands + n;
  655. sh_arg_type arg = this_try->arg[n];
  656. switch (arg)
  657. {
  658. case A_IMM:
  659. case A_BDISP12:
  660. case A_BDISP8:
  661. case A_DISP_GBR:
  662. case A_DISP_PC:
  663. case A_MACH:
  664. case A_PR:
  665. case A_MACL:
  666. if (user->type != arg)
  667. goto fail;
  668. break;
  669. case A_R0:
  670. /* opcode needs r0 */
  671. if (user->type != A_REG_N || user->reg != 0)
  672. goto fail;
  673. break;
  674. case A_R0_GBR:
  675. if (user->type != A_R0_GBR || user->reg != 0)
  676. goto fail;
  677. break;
  678. case F_FR0:
  679. if (user->type != F_REG_N || user->reg != 0)
  680. goto fail;
  681. break;
  682. case A_REG_N:
  683. case A_INC_N:
  684. case A_DEC_N:
  685. case A_IND_N:
  686. case A_IND_R0_REG_N:
  687. case A_DISP_REG_N:
  688. case F_REG_N:
  689. case D_REG_N:
  690. case X_REG_N:
  691. case V_REG_N:
  692. case FPUL_N:
  693. case FPSCR_N:
  694. /* Opcode needs rn */
  695. if (user->type != arg)
  696. goto fail;
  697. reg_n = user->reg;
  698. break;
  699. case FD_REG_N:
  700. if (user->type != F_REG_N && user->type != D_REG_N)
  701. goto fail;
  702. reg_n = user->reg;
  703. break;
  704. case DX_REG_N:
  705. if (user->type != D_REG_N && user->type != X_REG_N)
  706. goto fail;
  707. reg_n = user->reg;
  708. break;
  709. case A_GBR:
  710. case A_SR:
  711. case A_VBR:
  712. case A_SSR:
  713. case A_SPC:
  714. case A_SGR:
  715. case A_DBR:
  716. if (user->type != arg)
  717. goto fail;
  718. break;
  719. case A_REG_B:
  720. if (user->type != arg)
  721. goto fail;
  722. reg_b = user->reg;
  723. break;
  724. case A_REG_M:
  725. case A_INC_M:
  726. case A_DEC_M:
  727. case A_IND_M:
  728. case A_IND_R0_REG_M:
  729. case A_DISP_REG_M:
  730. /* Opcode needs rn */
  731. if (user->type != arg - A_REG_M + A_REG_N)
  732. goto fail;
  733. reg_m = user->reg;
  734. break;
  735. case F_REG_M:
  736. case D_REG_M:
  737. case X_REG_M:
  738. case V_REG_M:
  739. case FPUL_M:
  740. case FPSCR_M:
  741. /* Opcode needs rn */
  742. if (user->type != arg - F_REG_M + F_REG_N)
  743. goto fail;
  744. reg_m = user->reg;
  745. break;
  746. case DX_REG_M:
  747. if (user->type != D_REG_N && user->type != X_REG_N)
  748. goto fail;
  749. reg_m = user->reg;
  750. break;
  751. case XMTRX_M4:
  752. if (user->type != XMTRX_M4)
  753. goto fail;
  754. reg_m = 4;
  755. break;
  756. default:
  757. printf (_("unhandled %d\n"), arg);
  758. goto fail;
  759. }
  760. }
  761. return this_try;
  762. fail:;
  763. }
  764. return 0;
  765. }
  766. int
  767. check (operand, low, high)
  768. expressionS *operand;
  769. int low;
  770. int high;
  771. {
  772. if (operand->X_op != O_constant
  773. || operand->X_add_number < low
  774. || operand->X_add_number > high)
  775. {
  776. as_bad (_("operand must be absolute in range %d..%d"), low, high);
  777. }
  778. return operand->X_add_number;
  779. }
  780. static void
  781. insert (where, how, pcrel)
  782. char *where;
  783. int how;
  784. int pcrel;
  785. {
  786. fix_new_exp (frag_now,
  787. where - frag_now->fr_literal,
  788. 2,
  789. &immediate,
  790. pcrel,
  791. how);
  792. }
  793. static void
  794. build_relax (opcode)
  795. sh_opcode_info *opcode;
  796. {
  797. int high_byte = target_big_endian ? 0 : 1;
  798. char *p;
  799. if (opcode->arg[0] == A_BDISP8)
  800. {
  801. int what = (opcode->nibbles[1] & 4) ? COND_JUMP_DELAY : COND_JUMP;
  802. p = frag_var (rs_machine_dependent,
  803. md_relax_table[C (what, COND32)].rlx_length,
  804. md_relax_table[C (what, COND8)].rlx_length,
  805. C (what, 0),
  806. immediate.X_add_symbol,
  807. immediate.X_add_number,
  808. 0);
  809. p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
  810. }
  811. else if (opcode->arg[0] == A_BDISP12)
  812. {
  813. p = frag_var (rs_machine_dependent,
  814. md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
  815. md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
  816. C (UNCOND_JUMP, 0),
  817. immediate.X_add_symbol,
  818. immediate.X_add_number,
  819. 0);
  820. p[high_byte] = (opcode->nibbles[0] << 4);
  821. }
  822. }
  823. /* Now we know what sort of opcodes it is, lets build the bytes -
  824. */
  825. static void
  826. build_Mytes (opcode, operand)
  827. sh_opcode_info *opcode;
  828. sh_operand_info *operand;
  829. {
  830. int index;
  831. char nbuf[4];
  832. char *output = frag_more (2);
  833. int low_byte = target_big_endian ? 1 : 0;
  834. nbuf[0] = 0;
  835. nbuf[1] = 0;
  836. nbuf[2] = 0;
  837. nbuf[3] = 0;
  838. for (index = 0; index < 4; index++)
  839. {
  840. sh_nibble_type i = opcode->nibbles[index];
  841. if (i < 16)
  842. {
  843. nbuf[index] = i;
  844. }
  845. else
  846. {
  847. switch (i)
  848. {
  849. case REG_N:
  850. nbuf[index] = reg_n;
  851. break;
  852. case REG_M:
  853. nbuf[index] = reg_m;
  854. break;
  855. case REG_NM:
  856. nbuf[index] = reg_n | (reg_m >> 2);
  857. break;
  858. case REG_B:
  859. nbuf[index] = reg_b | 0x08;
  860. break;
  861. case DISP_4:
  862. insert (output + low_byte, BFD_RELOC_SH_IMM4, 0);
  863. break;
  864. case IMM_4BY4:
  865. insert (output + low_byte, BFD_RELOC_SH_IMM4BY4, 0);
  866. break;
  867. case IMM_4BY2:
  868. insert (output + low_byte, BFD_RELOC_SH_IMM4BY2, 0);
  869. break;
  870. case IMM_4:
  871. insert (output + low_byte, BFD_RELOC_SH_IMM4, 0);
  872. break;
  873. case IMM_8BY4:
  874. insert (output + low_byte, BFD_RELOC_SH_IMM8BY4, 0);
  875. break;
  876. case IMM_8BY2:
  877. insert (output + low_byte, BFD_RELOC_SH_IMM8BY2, 0);
  878. break;
  879. case IMM_8:
  880. insert (output + low_byte, BFD_RELOC_SH_IMM8, 0);
  881. break;
  882. case PCRELIMM_8BY4:
  883. insert (output, BFD_RELOC_SH_PCRELIMM8BY4, 1);
  884. break;
  885. case PCRELIMM_8BY2:
  886. insert (output, BFD_RELOC_SH_PCRELIMM8BY2, 1);
  887. break;
  888. default:
  889. printf (_("failed for %d\n"), i);
  890. }
  891. }
  892. }
  893. if (! target_big_endian) {
  894. output[1] = (nbuf[0] << 4) | (nbuf[1]);
  895. output[0] = (nbuf[2] << 4) | (nbuf[3]);
  896. }
  897. else {
  898. output[0] = (nbuf[0] << 4) | (nbuf[1]);
  899. output[1] = (nbuf[2] << 4) | (nbuf[3]);
  900. }
  901. }
  902. /* This is the guts of the machine-dependent assembler. STR points to a
  903. machine dependent instruction. This function is supposed to emit
  904. the frags/bytes it assembles to.
  905. */
  906. void
  907. md_assemble (str)
  908. char *str;
  909. {
  910. unsigned char *op_start;
  911. unsigned char *op_end;
  912. sh_operand_info operand[3];
  913. sh_opcode_info *opcode;
  914. char name[20];
  915. int nlen = 0;
  916. /* Drop leading whitespace */
  917. while (*str == ' ')
  918. str++;
  919. /* find the op code end */
  920. for (op_start = op_end = (unsigned char *) (str);
  921. *op_end
  922. && nlen < 20
  923. && !is_end_of_line[*op_end] && *op_end != ' ';
  924. op_end++)
  925. {
  926. unsigned char c = op_start[nlen];
  927. /* The machine independent code will convert CMP/EQ into cmp/EQ
  928. because it thinks the '/' is the end of the symbol. Instead of
  929. hacking up the machine independent code, we just deal with it
  930. here. */
  931. c = isupper (c) ? tolower (c) : c;
  932. name[nlen] = c;
  933. nlen++;
  934. }
  935. name[nlen] = 0;
  936. if (nlen == 0)
  937. {
  938. as_bad (_("can't find opcode "));
  939. }
  940. opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
  941. if (opcode == NULL)
  942. {
  943. as_bad (_("unknown opcode"));
  944. return;
  945. }
  946. if (sh_relax
  947. && ! seg_info (now_seg)->tc_segment_info_data.in_code)
  948. {
  949. /* Output a CODE reloc to tell the linker that the following
  950. bytes are instructions, not data. */
  951. fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
  952. BFD_RELOC_SH_CODE);
  953. seg_info (now_seg)->tc_segment_info_data.in_code = 1;
  954. }
  955. if (opcode->arg[0] == A_BDISP12
  956. || opcode->arg[0] == A_BDISP8)
  957. {
  958. parse_exp (op_end + 1);
  959. build_relax (opcode);
  960. }
  961. else
  962. {
  963. if (opcode->arg[0] == A_END)
  964. {
  965. /* Ignore trailing whitespace. If there is any, it has already
  966. been compressed to a single space. */
  967. if (*op_end == ' ')
  968. op_end++;
  969. }
  970. else
  971. {
  972. op_end = get_operands (opcode, op_end, operand);
  973. }
  974. opcode = get_specific (opcode, operand);
  975. if (opcode == 0)
  976. {
  977. /* Couldn't find an opcode which matched the operands */
  978. char *where = frag_more (2);
  979. where[0] = 0x0;
  980. where[1] = 0x0;
  981. as_bad (_("invalid operands for opcode"));
  982. return;
  983. }
  984. if (*op_end)
  985. as_bad (_("excess operands: '%s'"), op_end);
  986. build_Mytes (opcode, operand);
  987. }
  988. }
  989. /* This routine is called each time a label definition is seen. It
  990. emits a BFD_RELOC_SH_LABEL reloc if necessary. */
  991. void
  992. sh_frob_label ()
  993. {
  994. static fragS *last_label_frag;
  995. static int last_label_offset;
  996. if (sh_relax
  997. && seg_info (now_seg)->tc_segment_info_data.in_code)
  998. {
  999. int offset;
  1000. offset = frag_now_fix ();
  1001. if (frag_now != last_label_frag
  1002. || offset != last_label_offset)
  1003. {
  1004. fix_new (frag_now, offset, 2, &abs_symbol, 0, 0, BFD_RELOC_SH_LABEL);
  1005. last_label_frag = frag_now;
  1006. last_label_offset = offset;
  1007. }
  1008. }
  1009. }
  1010. /* This routine is called when the assembler is about to output some
  1011. data. It emits a BFD_RELOC_SH_DATA reloc if necessary. */
  1012. void
  1013. sh_flush_pending_output ()
  1014. {
  1015. if (sh_relax
  1016. && seg_info (now_seg)->tc_segment_info_data.in_code)
  1017. {
  1018. fix_new (frag_now, frag_now_fix (), 2, &abs_symbol, 0, 0,
  1019. BFD_RELOC_SH_DATA);
  1020. seg_info (now_seg)->tc_segment_info_data.in_code = 0;
  1021. }
  1022. }
  1023. symbolS *
  1024. DEFUN (md_undefined_symbol, (name),
  1025. char *name)
  1026. {
  1027. return 0;
  1028. }
  1029. #ifdef OBJ_COFF
  1030. void
  1031. DEFUN (tc_crawl_symbol_chain, (headers),
  1032. object_headers * headers)
  1033. {
  1034. printf (_("call to tc_crawl_symbol_chain \n"));
  1035. }
  1036. void
  1037. DEFUN (tc_headers_hook, (headers),
  1038. object_headers * headers)
  1039. {
  1040. printf (_("call to tc_headers_hook \n"));
  1041. }
  1042. #endif
  1043. /* Various routines to kill one day */
  1044. /* Equal to MAX_PRECISION in atof-ieee.c */
  1045. #define MAX_LITTLENUMS 6
  1046. /* Turn a string in input_line_pointer into a floating point constant of type
  1047. type, and store the appropriate bytes in *litP. The number of LITTLENUMS
  1048. emitted is stored in *sizeP . An error message is returned, or NULL on OK.
  1049. */
  1050. char *
  1051. md_atof (type, litP, sizeP)
  1052. int type;
  1053. char *litP;
  1054. int *sizeP;
  1055. {
  1056. int prec;
  1057. LITTLENUM_TYPE words[4];
  1058. char *t;
  1059. int i;
  1060. switch (type)
  1061. {
  1062. case 'f':
  1063. prec = 2;
  1064. break;
  1065. case 'd':
  1066. prec = 4;
  1067. break;
  1068. default:
  1069. *sizeP = 0;
  1070. return _("bad call to md_atof");
  1071. }
  1072. t = atof_ieee (input_line_pointer, type, words);
  1073. if (t)
  1074. input_line_pointer = t;
  1075. *sizeP = prec * 2;
  1076. if (! target_big_endian)
  1077. {
  1078. for (i = prec - 1; i >= 0; i--)
  1079. {
  1080. md_number_to_chars (litP, (valueT) words[i], 2);
  1081. litP += 2;
  1082. }
  1083. }
  1084. else
  1085. {
  1086. for (i = 0; i < prec; i++)
  1087. {
  1088. md_number_to_chars (litP, (valueT) words[i], 2);
  1089. litP += 2;
  1090. }
  1091. }
  1092. return NULL;
  1093. }
  1094. /* Handle the .uses pseudo-op. This pseudo-op is used just before a
  1095. call instruction. It refers to a label of the instruction which
  1096. loads the register which the call uses. We use it to generate a
  1097. special reloc for the linker. */
  1098. static void
  1099. s_uses (ignore)
  1100. int ignore;
  1101. {
  1102. expressionS ex;
  1103. if (! sh_relax)
  1104. as_warn (_(".uses pseudo-op seen when not relaxing"));
  1105. expression (&ex);
  1106. if (ex.X_op != O_symbol || ex.X_add_number != 0)
  1107. {
  1108. as_bad (_("bad .uses format"));
  1109. ignore_rest_of_line ();
  1110. return;
  1111. }
  1112. fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, BFD_RELOC_SH_USES);
  1113. demand_empty_rest_of_line ();
  1114. }
  1115. CONST char *md_shortopts = "";
  1116. struct option md_longopts[] = {
  1117. #define OPTION_RELAX (OPTION_MD_BASE)
  1118. #define OPTION_LITTLE (OPTION_MD_BASE + 1)
  1119. #define OPTION_SMALL (OPTION_LITTLE + 1)
  1120. {"relax", no_argument, NULL, OPTION_RELAX},
  1121. {"little", no_argument, NULL, OPTION_LITTLE},
  1122. {"small", no_argument, NULL, OPTION_SMALL},
  1123. {NULL, no_argument, NULL, 0}
  1124. };
  1125. size_t md_longopts_size = sizeof(md_longopts);
  1126. int
  1127. md_parse_option (c, arg)
  1128. int c;
  1129. char *arg;
  1130. {
  1131. switch (c)
  1132. {
  1133. case OPTION_RELAX:
  1134. sh_relax = 1;
  1135. break;
  1136. case OPTION_LITTLE:
  1137. shl = 1;
  1138. target_big_endian = 0;
  1139. break;
  1140. case OPTION_SMALL:
  1141. sh_small = 1;
  1142. break;
  1143. default:
  1144. return 0;
  1145. }
  1146. return 1;
  1147. }
  1148. void
  1149. md_show_usage (stream)
  1150. FILE *stream;
  1151. {
  1152. fprintf(stream, _("\
  1153. SH options:\n\
  1154. -little generate little endian code\n\
  1155. -relax alter jump instructions for long displacements\n\
  1156. -small align sections to 4 byte boundaries, not 16\n"));
  1157. }
  1158. void
  1159. tc_Nout_fix_to_chars ()
  1160. {
  1161. printf (_("call to tc_Nout_fix_to_chars \n"));
  1162. abort ();
  1163. }
  1164. /* This struct is used to pass arguments to sh_count_relocs through
  1165. bfd_map_over_sections. */
  1166. struct sh_count_relocs
  1167. {
  1168. /* Symbol we are looking for. */
  1169. symbolS *sym;
  1170. /* Count of relocs found. */
  1171. int count;
  1172. };
  1173. /* Count the number of fixups in a section which refer to a particular
  1174. symbol. When using BFD_ASSEMBLER, this is called via
  1175. bfd_map_over_sections. */
  1176. /*ARGSUSED*/
  1177. static void
  1178. sh_count_relocs (abfd, sec, data)
  1179. bfd *abfd;
  1180. segT sec;
  1181. PTR data;
  1182. {
  1183. struct sh_count_relocs *info = (struct sh_count_relocs *) data;
  1184. segment_info_type *seginfo;
  1185. symbolS *sym;
  1186. fixS *fix;
  1187. seginfo = seg_info (sec);
  1188. if (seginfo == NULL)
  1189. return;
  1190. sym = info->sym;
  1191. for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
  1192. {
  1193. if (fix->fx_addsy == sym)
  1194. {
  1195. ++info->count;
  1196. fix->fx_tcbit = 1;
  1197. }
  1198. }
  1199. }
  1200. /* Handle the count relocs for a particular section. When using
  1201. BFD_ASSEMBLER, this is called via bfd_map_over_sections. */
  1202. /*ARGSUSED*/
  1203. static void
  1204. sh_frob_section (abfd, sec, ignore)
  1205. bfd *abfd;
  1206. segT sec;
  1207. PTR ignore;
  1208. {
  1209. segment_info_type *seginfo;
  1210. fixS *fix;
  1211. seginfo = seg_info (sec);
  1212. if (seginfo == NULL)
  1213. return;
  1214. for (fix = seginfo->fix_root; fix != NULL; fix = fix->fx_next)
  1215. {
  1216. symbolS *sym;
  1217. bfd_vma val;
  1218. fixS *fscan;
  1219. struct sh_count_relocs info;
  1220. if (fix->fx_r_type != BFD_RELOC_SH_USES)
  1221. continue;
  1222. /* The BFD_RELOC_SH_USES reloc should refer to a defined local
  1223. symbol in the same section. */
  1224. sym = fix->fx_addsy;
  1225. if (sym == NULL
  1226. || fix->fx_subsy != NULL
  1227. || fix->fx_addnumber != 0
  1228. || S_GET_SEGMENT (sym) != sec
  1229. #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
  1230. || S_GET_STORAGE_CLASS (sym) == C_EXT
  1231. #endif
  1232. || S_IS_EXTERNAL (sym))
  1233. {
  1234. as_warn_where (fix->fx_file, fix->fx_line,
  1235. _(".uses does not refer to a local symbol in the same section"));
  1236. continue;
  1237. }
  1238. /* Look through the fixups again, this time looking for one
  1239. at the same location as sym. */
  1240. val = S_GET_VALUE (sym);
  1241. for (fscan = seginfo->fix_root;
  1242. fscan != NULL;
  1243. fscan = fscan->fx_next)
  1244. if (val == fscan->fx_frag->fr_address + fscan->fx_where
  1245. && fscan->fx_r_type != BFD_RELOC_SH_ALIGN
  1246. && fscan->fx_r_type != BFD_RELOC_SH_CODE
  1247. && fscan->fx_r_type != BFD_RELOC_SH_DATA
  1248. && fscan->fx_r_type != BFD_RELOC_SH_LABEL)
  1249. break;
  1250. if (fscan == NULL)
  1251. {
  1252. as_warn_where (fix->fx_file, fix->fx_line,
  1253. _("can't find fixup pointed to by .uses"));
  1254. continue;
  1255. }
  1256. if (fscan->fx_tcbit)
  1257. {
  1258. /* We've already done this one. */
  1259. continue;
  1260. }
  1261. /* fscan should also be a fixup to a local symbol in the same
  1262. section. */
  1263. sym = fscan->fx_addsy;
  1264. if (sym == NULL
  1265. || fscan->fx_subsy != NULL
  1266. || fscan->fx_addnumber != 0
  1267. || S_GET_SEGMENT (sym) != sec
  1268. #if ! defined (BFD_ASSEMBLER) && defined (OBJ_COFF)
  1269. || S_GET_STORAGE_CLASS (sym) == C_EXT
  1270. #endif
  1271. || S_IS_EXTERNAL (sym))
  1272. {
  1273. as_warn_where (fix->fx_file, fix->fx_line,
  1274. _(".uses target does not refer to a local symbol in the same section"));
  1275. continue;
  1276. }
  1277. /* Now we look through all the fixups of all the sections,
  1278. counting the number of times we find a reference to sym. */
  1279. info.sym = sym;
  1280. info.count = 0;
  1281. #ifdef BFD_ASSEMBLER
  1282. bfd_map_over_sections (stdoutput, sh_count_relocs, (PTR) &info);
  1283. #else
  1284. {
  1285. int iscan;
  1286. for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
  1287. sh_count_relocs ((bfd *) NULL, iscan, (PTR) &info);
  1288. }
  1289. #endif
  1290. if (info.count < 1)
  1291. abort ();
  1292. /* Generate a BFD_RELOC_SH_COUNT fixup at the location of sym.
  1293. We have already adjusted the value of sym to include the
  1294. fragment address, so we undo that adjustment here. */
  1295. subseg_change (sec, 0);
  1296. fix_new (symbol_get_frag (sym),
  1297. S_GET_VALUE (sym) - symbol_get_frag (sym)->fr_address,
  1298. 4, &abs_symbol, info.count, 0, BFD_RELOC_SH_COUNT);
  1299. }
  1300. }
  1301. /* This function is called after the symbol table has been completed,
  1302. but before the relocs or section contents have been written out.
  1303. If we have seen any .uses pseudo-ops, they point to an instruction
  1304. which loads a register with the address of a function. We look
  1305. through the fixups to find where the function address is being
  1306. loaded from. We then generate a COUNT reloc giving the number of
  1307. times that function address is referred to. The linker uses this
  1308. information when doing relaxing, to decide when it can eliminate
  1309. the stored function address entirely. */
  1310. void
  1311. sh_frob_file ()
  1312. {
  1313. if (! sh_relax)
  1314. return;
  1315. #ifdef BFD_ASSEMBLER
  1316. bfd_map_over_sections (stdoutput, sh_frob_section, (PTR) NULL);
  1317. #else
  1318. {
  1319. int iseg;
  1320. for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
  1321. sh_frob_section ((bfd *) NULL, iseg, (PTR) NULL);
  1322. }
  1323. #endif
  1324. }
  1325. /* Called after relaxing. Set the correct sizes of the fragments, and
  1326. create relocs so that md_apply_fix will fill in the correct values. */
  1327. void
  1328. md_convert_frag (headers, seg, fragP)
  1329. #ifdef BFD_ASSEMBLER
  1330. bfd *headers;
  1331. #else
  1332. object_headers *headers;
  1333. #endif
  1334. segT seg;
  1335. fragS *fragP;
  1336. {
  1337. int donerelax = 0;
  1338. switch (fragP->fr_subtype)
  1339. {
  1340. case C (COND_JUMP, COND8):
  1341. case C (COND_JUMP_DELAY, COND8):
  1342. subseg_change (seg, 0);
  1343. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
  1344. 1, BFD_RELOC_SH_PCDISP8BY2);
  1345. fragP->fr_fix += 2;
  1346. fragP->fr_var = 0;
  1347. break;
  1348. case C (UNCOND_JUMP, UNCOND12):
  1349. subseg_change (seg, 0);
  1350. fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
  1351. 1, BFD_RELOC_SH_PCDISP12BY2);
  1352. fragP->fr_fix += 2;
  1353. fragP->fr_var = 0;
  1354. break;
  1355. case C (UNCOND_JUMP, UNCOND32):
  1356. case C (UNCOND_JUMP, UNDEF_WORD_DISP):
  1357. if (fragP->fr_symbol == NULL)
  1358. as_bad (_("at 0x%lx, displacement overflows 12-bit field"),
  1359. (unsigned long) fragP->fr_address);
  1360. else if (S_IS_DEFINED (fragP->fr_symbol))
  1361. as_bad (_("at 0x%lx, displacement to defined symbol %s overflows 12-bit field"),
  1362. (unsigned long) fragP->fr_address,
  1363. S_GET_NAME (fragP->fr_symbol));
  1364. else
  1365. as_bad (_("at 0x%lx, displacement to undefined symbol %s overflows 12-bit field"),
  1366. (unsigned long) fragP->fr_address,
  1367. S_GET_NAME (fragP->fr_symbol));
  1368. #if 0 /* This code works, but generates poor code and the compiler
  1369. should never produce a sequence that requires it to be used. */
  1370. /* A jump wont fit in 12 bits, make code which looks like
  1371. bra foo
  1372. mov.w @(0, PC), r14
  1373. .long disp
  1374. foo: bra @r14
  1375. */
  1376. int t = buffer[0] & 0x10;
  1377. buffer[highbyte] = 0xa0; /* branch over move and disp */
  1378. buffer[lowbyte] = 3;
  1379. buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
  1380. buffer[lowbyte+2] = 0x00;
  1381. buffer[highbyte+4] = 0; /* space for 32 bit jump disp */
  1382. buffer[lowbyte+4] = 0;
  1383. buffer[highbyte+6] = 0;
  1384. buffer[lowbyte+6] = 0;
  1385. buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
  1386. buffer[lowbyte+8] = t ? 0xb : 0x2b;
  1387. buffer[highbyte+10] = 0x20; /* build nop */
  1388. buffer[lowbyte+10] = 0x0b;
  1389. /* Make reloc for the long disp */
  1390. fix_new (fragP,
  1391. fragP->fr_fix + 4,
  1392. 4,
  1393. fragP->fr_symbol,
  1394. fragP->fr_offset,
  1395. 0,
  1396. BFD_RELOC_32);
  1397. fragP->fr_fix += UNCOND32_LENGTH;
  1398. fragP->fr_var = 0;
  1399. donerelax = 1;
  1400. #endif
  1401. break;
  1402. case C (COND_JUMP, COND12):
  1403. case C (COND_JUMP_DELAY, COND12):
  1404. /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
  1405. /* I found that a relax failure for gcc.c-torture/execute/930628-1.c
  1406. was due to gas incorrectly relaxing an out-of-range conditional
  1407. branch with delay slot. It turned:
  1408. bf.s L6 (slot mov.l r12,@(44,r0))
  1409. into:
  1410. 2c: 8f 01 a0 8b bf.s 32 <_main+32> (slot bra L6)
  1411. 30: 00 09 nop
  1412. 32: 10 cb mov.l r12,@(44,r0)
  1413. Therefore, branches with delay slots have to be handled
  1414. differently from ones without delay slots. */
  1415. {
  1416. unsigned char *buffer =
  1417. (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
  1418. int highbyte = target_big_endian ? 0 : 1;
  1419. int lowbyte = target_big_endian ? 1 : 0;
  1420. int delay = fragP->fr_subtype == C (COND_JUMP_DELAY, COND12);
  1421. /* Toggle the true/false bit of the bcond. */
  1422. buffer[highbyte] ^= 0x2;
  1423. /* If this is a dalayed branch, we may not put the the bra in the
  1424. slot. So we change it to a non-delayed branch, like that:
  1425. b! cond slot_label; bra disp; slot_label: slot_insn
  1426. ??? We should try if swapping the conditional branch and
  1427. its delay-slot insn already makes the branch reach. */
  1428. /* Build a relocation to six / four bytes farther on. */
  1429. subseg_change (seg, 0);
  1430. fix_new (fragP, fragP->fr_fix, 2,
  1431. #ifdef BFD_ASSEMBLER
  1432. section_symbol (seg),
  1433. #else
  1434. seg_info (seg)->dot,
  1435. #endif
  1436. fragP->fr_address + fragP->fr_fix + (delay ? 4 : 6),
  1437. 1, BFD_RELOC_SH_PCDISP8BY2);
  1438. /* Set up a jump instruction. */
  1439. buffer[highbyte + 2] = 0xa0;
  1440. buffer[lowbyte + 2] = 0;
  1441. fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
  1442. fragP->fr_offset, 1, BFD_RELOC_SH_PCDISP12BY2);
  1443. if (delay)
  1444. {
  1445. buffer[highbyte] &= ~0x4; /* Removes delay slot from branch. */
  1446. fragP->fr_fix += 4;
  1447. }
  1448. else
  1449. {
  1450. /* Fill in a NOP instruction. */
  1451. buffer[highbyte + 4] = 0x0;
  1452. buffer[lowbyte + 4] = 0x9;
  1453. fragP->fr_fix += 6;
  1454. }
  1455. fragP->fr_var = 0;
  1456. donerelax = 1;
  1457. }
  1458. break;
  1459. case C (COND_JUMP, COND32):
  1460. case C (COND_JUMP_DELAY, COND32):
  1461. case C (COND_JUMP, UNDEF_WORD_DISP):
  1462. case C (COND_JUMP_DELAY, UNDEF_WORD_DISP):
  1463. if (fragP->fr_symbol == NULL)
  1464. as_bad (_("at 0x%lx, displacement overflows 8-bit field"),
  1465. (unsigned long) fragP->fr_address);
  1466. else if (S_IS_DEFINED (fragP->fr_symbol))
  1467. as_bad (_("at 0x%lx, displacement to defined symbol %s overflows 8-bit field "),
  1468. (unsigned long) fragP->fr_address,
  1469. S_GET_NAME (fragP->fr_symbol));
  1470. else
  1471. as_bad (_("at 0x%lx, displacement to undefined symbol %s overflows 8-bit field "),
  1472. (unsigned long) fragP->fr_address,
  1473. S_GET_NAME (fragP->fr_symbol));
  1474. #if 0 /* This code works, but generates poor code, and the compiler
  1475. should never produce a sequence that requires it to be used. */
  1476. /* A bcond won't fit and it won't go into a 12 bit
  1477. displacement either, the code sequence looks like:
  1478. b!cond foop
  1479. mov.w @(n, PC), r14
  1480. jmp @r14
  1481. nop
  1482. .long where
  1483. foop:
  1484. */
  1485. buffer[0] ^= 0x2; /* Toggle T/F bit */
  1486. #define JREG 14
  1487. buffer[1] = 5; /* branch over mov, jump, nop and ptr */
  1488. buffer[2] = 0xd0 | JREG; /* Build mov insn */
  1489. buffer[3] = 0x2;
  1490. buffer[4] = 0x40 | JREG; /* Build jmp @JREG */
  1491. buffer[5] = 0x0b;
  1492. buffer[6] = 0x20; /* build nop */
  1493. buffer[7] = 0x0b;
  1494. buffer[8] = 0; /* space for 32 bit jump disp */
  1495. buffer[9] = 0;
  1496. buffer[10] = 0;
  1497. buffer[11] = 0;
  1498. buffer[12] = 0;
  1499. buffer[13] = 0;
  1500. /* Make reloc for the long disp */
  1501. fix_new (fragP,
  1502. fragP->fr_fix + 8,
  1503. 4,
  1504. fragP->fr_symbol,
  1505. fragP->fr_offset,
  1506. 0,
  1507. BFD_RELOC_32);
  1508. fragP->fr_fix += COND32_LENGTH;
  1509. fragP->fr_var = 0;
  1510. donerelax = 1;
  1511. #endif
  1512. break;
  1513. default:
  1514. abort ();
  1515. }
  1516. if (donerelax && !sh_relax)
  1517. as_warn_where (fragP->fr_file, fragP->fr_line,
  1518. _("overflow in branch to %s; converted into longer instruction sequence"),
  1519. (fragP->fr_symbol != NULL
  1520. ? S_GET_NAME (fragP->fr_symbol)
  1521. : ""));
  1522. }
  1523. valueT
  1524. DEFUN (md_section_align, (seg, size),
  1525. segT seg AND
  1526. valueT size)
  1527. {
  1528. #ifdef BFD_ASSEMBLER
  1529. #ifdef OBJ_ELF
  1530. return size;
  1531. #else /* ! OBJ_ELF */
  1532. return ((size + (1 << bfd_get_section_alignment (stdoutput, seg)) - 1)
  1533. & (-1 << bfd_get_section_alignment (stdoutput, seg)));
  1534. #endif /* ! OBJ_ELF */
  1535. #else /* ! BFD_ASSEMBLER */
  1536. return ((size + (1 << section_alignment[(int) seg]) - 1)
  1537. & (-1 << section_alignment[(int) seg]));
  1538. #endif /* ! BFD_ASSEMBLER */
  1539. }
  1540. /* This static variable is set by s_uacons to tell sh_cons_align that
  1541. the expession does not need to be aligned. */
  1542. static int sh_no_align_cons = 0;
  1543. /* This handles the unaligned space allocation pseudo-ops, such as
  1544. .uaword. .uaword is just like .word, but the value does not need
  1545. to be aligned. */
  1546. static void
  1547. s_uacons (bytes)
  1548. int bytes;
  1549. {
  1550. /* Tell sh_cons_align not to align this value. */
  1551. sh_no_align_cons = 1;
  1552. cons (bytes);
  1553. }
  1554. /* If a .word, et. al., pseud-op is seen, warn if the value is not
  1555. aligned correctly. Note that this can cause warnings to be issued
  1556. when assembling initialized structured which were declared with the
  1557. packed attribute. FIXME: Perhaps we should require an option to
  1558. enable this warning? */
  1559. void
  1560. sh_cons_align (nbytes)
  1561. int nbytes;
  1562. {
  1563. int nalign;
  1564. char *p;
  1565. if (sh_no_align_cons)
  1566. {
  1567. /* This is an unaligned pseudo-op. */
  1568. sh_no_align_cons = 0;
  1569. return;
  1570. }
  1571. nalign = 0;
  1572. while ((nbytes & 1) == 0)
  1573. {
  1574. ++nalign;
  1575. nbytes >>= 1;
  1576. }
  1577. if (nalign == 0)
  1578. return;
  1579. if (now_seg == absolute_section)
  1580. {
  1581. if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
  1582. as_warn (_("misaligned data"));
  1583. return;
  1584. }
  1585. p = frag_var (rs_align_code, 1, 1, (relax_substateT) 0,
  1586. (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
  1587. record_alignment (now_seg, nalign);
  1588. }
  1589. /* When relaxing, we need to output a reloc for any .align directive
  1590. that requests alignment to a four byte boundary or larger. This is
  1591. also where we check for misaligned data. */
  1592. void
  1593. sh_handle_align (frag)
  1594. fragS *frag;
  1595. {
  1596. if (sh_relax
  1597. && frag->fr_type == rs_align
  1598. && frag->fr_address + frag->fr_fix > 0
  1599. && frag->fr_offset > 1
  1600. && now_seg != bss_section)
  1601. fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
  1602. BFD_RELOC_SH_ALIGN);
  1603. if (frag->fr_type == rs_align_code
  1604. && frag->fr_next->fr_address - frag->fr_address - frag->fr_fix != 0)
  1605. as_warn_where (frag->fr_file, frag->fr_line, _("misaligned data"));
  1606. }
  1607. /* This macro decides whether a particular reloc is an entry in a
  1608. switch table. It is used when relaxing, because the linker needs
  1609. to know about all such entries so that it can adjust them if
  1610. necessary. */
  1611. #ifdef BFD_ASSEMBLER
  1612. #define SWITCH_TABLE_CONS(fix) (0)
  1613. #else
  1614. #define SWITCH_TABLE_CONS(fix) \
  1615. ((fix)->fx_r_type == 0 \
  1616. && ((fix)->fx_size == 2 \
  1617. || (fix)->fx_size == 1 \
  1618. || (fix)->fx_size == 4))
  1619. #endif
  1620. #define SWITCH_TABLE(fix) \
  1621. ((fix)->fx_addsy != NULL \
  1622. && (fix)->fx_subsy != NULL \
  1623. && S_GET_SEGMENT ((fix)->fx_addsy) == text_section \
  1624. && S_GET_SEGMENT ((fix)->fx_subsy) == text_section \
  1625. && ((fix)->fx_r_type == BFD_RELOC_32 \
  1626. || (fix)->fx_r_type == BFD_RELOC_16 \
  1627. || (fix)->fx_r_type == BFD_RELOC_8 \
  1628. || SWITCH_TABLE_CONS (fix)))
  1629. /* See whether we need to force a relocation into the output file.
  1630. This is used to force out switch and PC relative relocations when
  1631. relaxing. */
  1632. int
  1633. sh_force_relocation (fix)
  1634. fixS *fix;
  1635. {
  1636. if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  1637. || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  1638. return 1;
  1639. if (! sh_relax)
  1640. return 0;
  1641. return (fix->fx_pcrel
  1642. || SWITCH_TABLE (fix)
  1643. || fix->fx_r_type == BFD_RELOC_SH_COUNT
  1644. || fix->fx_r_type == BFD_RELOC_SH_ALIGN
  1645. || fix->fx_r_type == BFD_RELOC_SH_CODE
  1646. || fix->fx_r_type == BFD_RELOC_SH_DATA
  1647. || fix->fx_r_type == BFD_RELOC_SH_LABEL);
  1648. }
  1649. #ifdef OBJ_ELF
  1650. boolean
  1651. sh_fix_adjustable (fixP)
  1652. fixS *fixP;
  1653. {
  1654. if (fixP->fx_addsy == NULL)
  1655. return 1;
  1656. /* We need the symbol name for the VTABLE entries */
  1657. if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  1658. || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  1659. return 0;
  1660. return 1;
  1661. }
  1662. #endif
  1663. /* Apply a fixup to the object file. */
  1664. #ifdef BFD_ASSEMBLER
  1665. int
  1666. md_apply_fix (fixP, valp)
  1667. fixS *fixP;
  1668. valueT *valp;
  1669. #else
  1670. void
  1671. md_apply_fix (fixP, val)
  1672. fixS *fixP;
  1673. long val;
  1674. #endif
  1675. {
  1676. char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
  1677. int lowbyte = target_big_endian ? 1 : 0;
  1678. int highbyte = target_big_endian ? 0 : 1;
  1679. #ifdef BFD_ASSEMBLER
  1680. long val = *valp;
  1681. #endif
  1682. long max, min;
  1683. int shift;
  1684. #ifdef BFD_ASSEMBLER
  1685. /* adjust_reloc_syms won't convert a reloc against a weak symbol
  1686. into a reloc against a section, but bfd_install_relocation will
  1687. screw up if the symbol is defined, so we have to adjust val here
  1688. to avoid the screw up later. */
  1689. if (fixP->fx_addsy != NULL
  1690. && S_IS_WEAK (fixP->fx_addsy))
  1691. val -= S_GET_VALUE (fixP->fx_addsy);
  1692. #endif
  1693. #ifndef BFD_ASSEMBLER
  1694. if (fixP->fx_r_type == 0)
  1695. {
  1696. if (fixP->fx_size == 2)
  1697. fixP->fx_r_type = BFD_RELOC_16;
  1698. else if (fixP->fx_size == 4)
  1699. fixP->fx_r_type = BFD_RELOC_32;
  1700. else if (fixP->fx_size == 1)
  1701. fixP->fx_r_type = BFD_RELOC_8;
  1702. else
  1703. abort ();
  1704. }
  1705. #endif
  1706. max = min = 0;
  1707. shift = 0;
  1708. switch (fixP->fx_r_type)
  1709. {
  1710. case BFD_RELOC_SH_IMM4:
  1711. max = 0xf;
  1712. *buf = (*buf & 0xf0) | (val & 0xf);
  1713. break;
  1714. case BFD_RELOC_SH_IMM4BY2:
  1715. max = 0xf;
  1716. shift = 1;
  1717. *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
  1718. break;
  1719. case BFD_RELOC_SH_IMM4BY4:
  1720. max = 0xf;
  1721. shift = 2;
  1722. *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
  1723. break;
  1724. case BFD_RELOC_SH_IMM8BY2:
  1725. max = 0xff;
  1726. shift = 1;
  1727. *buf = val >> 1;
  1728. break;
  1729. case BFD_RELOC_SH_IMM8BY4:
  1730. max = 0xff;
  1731. shift = 2;
  1732. *buf = val >> 2;
  1733. break;
  1734. case BFD_RELOC_8:
  1735. case BFD_RELOC_SH_IMM8:
  1736. /* Sometimes the 8 bit value is sign extended (e.g., add) and
  1737. sometimes it is not (e.g., and). We permit any 8 bit value.
  1738. Note that adding further restrictions may invalidate
  1739. reasonable looking assembly code, such as ``and -0x1,r0''. */
  1740. max = 0xff;
  1741. min = - 0xff;
  1742. *buf++ = val;
  1743. break;
  1744. case BFD_RELOC_SH_PCRELIMM8BY4:
  1745. /* The lower two bits of the PC are cleared before the
  1746. displacement is added in. We can assume that the destination
  1747. is on a 4 byte bounday. If this instruction is also on a 4
  1748. byte boundary, then we want
  1749. (target - here) / 4
  1750. and target - here is a multiple of 4.
  1751. Otherwise, we are on a 2 byte boundary, and we want
  1752. (target - (here - 2)) / 4
  1753. and target - here is not a multiple of 4. Computing
  1754. (target - (here - 2)) / 4 == (target - here + 2) / 4
  1755. works for both cases, since in the first case the addition of
  1756. 2 will be removed by the division. target - here is in the
  1757. variable val. */
  1758. val = (val + 2) / 4;
  1759. if (val & ~0xff)
  1760. as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
  1761. buf[lowbyte] = val;
  1762. break;
  1763. case BFD_RELOC_SH_PCRELIMM8BY2:
  1764. val /= 2;
  1765. if (val & ~0xff)
  1766. as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
  1767. buf[lowbyte] = val;
  1768. break;
  1769. case BFD_RELOC_SH_PCDISP8BY2:
  1770. val /= 2;
  1771. if (val < -0x80 || val > 0x7f)
  1772. as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
  1773. buf[lowbyte] = val;
  1774. break;
  1775. case BFD_RELOC_SH_PCDISP12BY2:
  1776. val /= 2;
  1777. if (val < -0x800 || val >= 0x7ff)
  1778. as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
  1779. buf[lowbyte] = val & 0xff;
  1780. buf[highbyte] |= (val >> 8) & 0xf;
  1781. break;
  1782. case BFD_RELOC_32:
  1783. if (! target_big_endian)
  1784. {
  1785. *buf++ = val >> 0;
  1786. *buf++ = val >> 8;
  1787. *buf++ = val >> 16;
  1788. *buf++ = val >> 24;
  1789. }
  1790. else
  1791. {
  1792. *buf++ = val >> 24;
  1793. *buf++ = val >> 16;
  1794. *buf++ = val >> 8;
  1795. *buf++ = val >> 0;
  1796. }
  1797. break;
  1798. case BFD_RELOC_16:
  1799. if (! target_big_endian)
  1800. {
  1801. *buf++ = val >> 0;
  1802. *buf++ = val >> 8;
  1803. }
  1804. else
  1805. {
  1806. *buf++ = val >> 8;
  1807. *buf++ = val >> 0;
  1808. }
  1809. break;
  1810. case BFD_RELOC_SH_USES:
  1811. /* Pass the value into sh_coff_reloc_mangle. */
  1812. fixP->fx_addnumber = val;
  1813. break;
  1814. case BFD_RELOC_SH_COUNT:
  1815. case BFD_RELOC_SH_ALIGN:
  1816. case BFD_RELOC_SH_CODE:
  1817. case BFD_RELOC_SH_DATA:
  1818. case BFD_RELOC_SH_LABEL:
  1819. /* Nothing to do here. */
  1820. break;
  1821. case BFD_RELOC_VTABLE_INHERIT:
  1822. case BFD_RELOC_VTABLE_ENTRY:
  1823. fixP->fx_done = 0;
  1824. #ifdef BFD_ASSEMBLER
  1825. return 0;
  1826. #else
  1827. return;
  1828. #endif
  1829. default:
  1830. abort ();
  1831. }
  1832. if (shift != 0)
  1833. {
  1834. if ((val & ((1 << shift) - 1)) != 0)
  1835. as_bad_where (fixP->fx_file, fixP->fx_line, _("misaligned offset"));
  1836. if (val >= 0)
  1837. val >>= shift;
  1838. else
  1839. val = ((val >> shift)
  1840. | ((long) -1 & ~ ((long) -1 >> shift)));
  1841. }
  1842. if (max != 0 && (val < min || val > max))
  1843. as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
  1844. #ifdef BFD_ASSEMBLER
  1845. return 0;
  1846. #endif
  1847. }
  1848. /* Called just before address relaxation. Return the length
  1849. by which a fragment must grow to reach it's destination. */
  1850. int
  1851. md_estimate_size_before_relax (fragP, segment_type)
  1852. register fragS *fragP;
  1853. register segT segment_type;
  1854. {
  1855. switch (fragP->fr_subtype)
  1856. {
  1857. case C (UNCOND_JUMP, UNDEF_DISP):
  1858. /* used to be a branch to somewhere which was unknown */
  1859. if (!fragP->fr_symbol)
  1860. {
  1861. fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
  1862. fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
  1863. }
  1864. else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
  1865. {
  1866. fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
  1867. fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
  1868. }
  1869. else
  1870. {
  1871. fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
  1872. fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
  1873. return md_relax_

Large files files are truncated, but you can click here to view the full file