tc-mn10200.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /* tc-mn10200.c -- Assembler code for the Matsushita 10200
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  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 3, 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, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "as.h"
  17. #include "safe-ctype.h"
  18. #include "subsegs.h"
  19. #include "opcode/mn10200.h"
  20. /* Structure to hold information about predefined registers. */
  21. struct reg_name
  22. {
  23. const char *name;
  24. int value;
  25. };
  26. /* Generic assembler global variables which must be defined by all
  27. targets. */
  28. /* Characters which always start a comment. */
  29. const char comment_chars[] = "#";
  30. /* Characters which start a comment at the beginning of a line. */
  31. const char line_comment_chars[] = ";#";
  32. /* Characters which may be used to separate multiple commands on a
  33. single line. */
  34. const char line_separator_chars[] = ";";
  35. /* Characters which are used to indicate an exponent in a floating
  36. point number. */
  37. const char EXP_CHARS[] = "eE";
  38. /* Characters which mean that a number is a floating point constant,
  39. as in 0d1.0. */
  40. const char FLT_CHARS[] = "dD";
  41. const relax_typeS md_relax_table[] =
  42. {
  43. /* bCC relaxing */
  44. {0x81, -0x7e, 2, 1},
  45. {0x8004, -0x7ffb, 5, 2},
  46. {0x800006, -0x7ffff9, 7, 0},
  47. /* bCCx relaxing */
  48. {0x81, -0x7e, 3, 4},
  49. {0x8004, -0x7ffb, 6, 5},
  50. {0x800006, -0x7ffff9, 8, 0},
  51. /* jsr relaxing */
  52. {0x8004, -0x7ffb, 3, 7},
  53. {0x800006, -0x7ffff9, 5, 0},
  54. /* jmp relaxing */
  55. {0x81, -0x7e, 2, 9},
  56. {0x8004, -0x7ffb, 3, 10},
  57. {0x800006, -0x7ffff9, 5, 0},
  58. };
  59. /* Fixups. */
  60. #define MAX_INSN_FIXUPS 5
  61. struct mn10200_fixup
  62. {
  63. expressionS exp;
  64. int opindex;
  65. bfd_reloc_code_real_type reloc;
  66. };
  67. struct mn10200_fixup fixups[MAX_INSN_FIXUPS];
  68. static int fc;
  69. const char *md_shortopts = "";
  70. struct option md_longopts[] =
  71. {
  72. {NULL, no_argument, NULL, 0}
  73. };
  74. size_t md_longopts_size = sizeof (md_longopts);
  75. /* The target specific pseudo-ops which we support. */
  76. const pseudo_typeS md_pseudo_table[] =
  77. {
  78. { NULL, NULL, 0 }
  79. };
  80. /* Opcode hash table. */
  81. static struct hash_control *mn10200_hash;
  82. /* This table is sorted. Suitable for searching by a binary search. */
  83. static const struct reg_name data_registers[] =
  84. {
  85. { "d0", 0 },
  86. { "d1", 1 },
  87. { "d2", 2 },
  88. { "d3", 3 },
  89. };
  90. #define DATA_REG_NAME_CNT \
  91. (sizeof (data_registers) / sizeof (struct reg_name))
  92. static const struct reg_name address_registers[] =
  93. {
  94. { "a0", 0 },
  95. { "a1", 1 },
  96. { "a2", 2 },
  97. { "a3", 3 },
  98. };
  99. #define ADDRESS_REG_NAME_CNT \
  100. (sizeof (address_registers) / sizeof (struct reg_name))
  101. static const struct reg_name other_registers[] =
  102. {
  103. { "mdr", 0 },
  104. { "psw", 0 },
  105. };
  106. #define OTHER_REG_NAME_CNT \
  107. (sizeof (other_registers) / sizeof (struct reg_name))
  108. /* reg_name_search does a binary search of the given register table
  109. to see if "name" is a valid regiter name. Returns the register
  110. number from the array on success, or -1 on failure. */
  111. static int
  112. reg_name_search (const struct reg_name *regs,
  113. int regcount,
  114. const char *name)
  115. {
  116. int middle, low, high;
  117. int cmp;
  118. low = 0;
  119. high = regcount - 1;
  120. do
  121. {
  122. middle = (low + high) / 2;
  123. cmp = strcasecmp (name, regs[middle].name);
  124. if (cmp < 0)
  125. high = middle - 1;
  126. else if (cmp > 0)
  127. low = middle + 1;
  128. else
  129. return regs[middle].value;
  130. }
  131. while (low <= high);
  132. return -1;
  133. }
  134. /* Summary of register_name().
  135. in: Input_line_pointer points to 1st char of operand.
  136. out: An expressionS.
  137. The operand may have been a register: in this case, X_op == O_register,
  138. X_add_number is set to the register number, and truth is returned.
  139. Input_line_pointer->(next non-blank) char after operand, or is in
  140. its original state. */
  141. static bfd_boolean
  142. data_register_name (expressionS *expressionP)
  143. {
  144. int reg_number;
  145. char *name;
  146. char *start;
  147. char c;
  148. /* Find the spelling of the operand. */
  149. start = input_line_pointer;
  150. c = get_symbol_name (&name);
  151. reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
  152. /* Put back the delimiting char. */
  153. (void) restore_line_pointer (c);
  154. /* Look to see if it's in the register table. */
  155. if (reg_number >= 0)
  156. {
  157. expressionP->X_op = O_register;
  158. expressionP->X_add_number = reg_number;
  159. /* Make the rest nice. */
  160. expressionP->X_add_symbol = NULL;
  161. expressionP->X_op_symbol = NULL;
  162. return TRUE;
  163. }
  164. /* Reset the line as if we had not done anything. */
  165. input_line_pointer = start;
  166. return FALSE;
  167. }
  168. /* Summary of register_name().
  169. in: Input_line_pointer points to 1st char of operand.
  170. out: An expressionS.
  171. The operand may have been a register: in this case, X_op == O_register,
  172. X_add_number is set to the register number, and truth is returned.
  173. Input_line_pointer->(next non-blank) char after operand, or is in
  174. its original state. */
  175. static bfd_boolean
  176. address_register_name (expressionS *expressionP)
  177. {
  178. int reg_number;
  179. char *name;
  180. char *start;
  181. char c;
  182. /* Find the spelling of the operand. */
  183. start = input_line_pointer;
  184. c = get_symbol_name (&name);
  185. reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
  186. /* Put back the delimiting char. */
  187. (void) restore_line_pointer (c);
  188. /* Look to see if it's in the register table. */
  189. if (reg_number >= 0)
  190. {
  191. expressionP->X_op = O_register;
  192. expressionP->X_add_number = reg_number;
  193. /* Make the rest nice. */
  194. expressionP->X_add_symbol = NULL;
  195. expressionP->X_op_symbol = NULL;
  196. return TRUE;
  197. }
  198. /* Reset the line as if we had not done anything. */
  199. input_line_pointer = start;
  200. return FALSE;
  201. }
  202. /* Summary of register_name().
  203. in: Input_line_pointer points to 1st char of operand.
  204. out: An expressionS.
  205. The operand may have been a register: in this case, X_op == O_register,
  206. X_add_number is set to the register number, and truth is returned.
  207. Input_line_pointer->(next non-blank) char after operand, or is in
  208. its original state. */
  209. static bfd_boolean
  210. other_register_name (expressionS *expressionP)
  211. {
  212. int reg_number;
  213. char *name;
  214. char *start;
  215. char c;
  216. /* Find the spelling of the operand. */
  217. start = input_line_pointer;
  218. c = get_symbol_name (&name);
  219. reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
  220. /* Put back the delimiting char. */
  221. (void) restore_line_pointer (c);
  222. /* Look to see if it's in the register table. */
  223. if (reg_number >= 0)
  224. {
  225. expressionP->X_op = O_register;
  226. expressionP->X_add_number = reg_number;
  227. /* Make the rest nice. */
  228. expressionP->X_add_symbol = NULL;
  229. expressionP->X_op_symbol = NULL;
  230. return TRUE;
  231. }
  232. /* Reset the line as if we had not done anything. */
  233. input_line_pointer = start;
  234. return FALSE;
  235. }
  236. void
  237. md_show_usage (FILE *stream)
  238. {
  239. fprintf (stream, _("MN10200 options:\n\
  240. none yet\n"));
  241. }
  242. int
  243. md_parse_option (int c ATTRIBUTE_UNUSED,
  244. char *arg ATTRIBUTE_UNUSED)
  245. {
  246. return 0;
  247. }
  248. symbolS *
  249. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  250. {
  251. return 0;
  252. }
  253. char *
  254. md_atof (int type, char *litp, int *sizep)
  255. {
  256. return ieee_md_atof (type, litp, sizep, FALSE);
  257. }
  258. void
  259. md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
  260. asection *sec,
  261. fragS *fragP)
  262. {
  263. static unsigned long label_count = 0;
  264. char buf[40];
  265. subseg_change (sec, 0);
  266. if (fragP->fr_subtype == 0)
  267. {
  268. fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
  269. fragP->fr_offset, 1, BFD_RELOC_8_PCREL);
  270. fragP->fr_var = 0;
  271. fragP->fr_fix += 2;
  272. }
  273. else if (fragP->fr_subtype == 1)
  274. {
  275. /* Reverse the condition of the first branch. */
  276. int offset = fragP->fr_fix;
  277. int opcode = fragP->fr_literal[offset] & 0xff;
  278. switch (opcode)
  279. {
  280. case 0xe8:
  281. opcode = 0xe9;
  282. break;
  283. case 0xe9:
  284. opcode = 0xe8;
  285. break;
  286. case 0xe0:
  287. opcode = 0xe2;
  288. break;
  289. case 0xe2:
  290. opcode = 0xe0;
  291. break;
  292. case 0xe3:
  293. opcode = 0xe1;
  294. break;
  295. case 0xe1:
  296. opcode = 0xe3;
  297. break;
  298. case 0xe4:
  299. opcode = 0xe6;
  300. break;
  301. case 0xe6:
  302. opcode = 0xe4;
  303. break;
  304. case 0xe7:
  305. opcode = 0xe5;
  306. break;
  307. case 0xe5:
  308. opcode = 0xe7;
  309. break;
  310. default:
  311. abort ();
  312. }
  313. fragP->fr_literal[offset] = opcode;
  314. /* Create a fixup for the reversed conditional branch. */
  315. sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
  316. fix_new (fragP, fragP->fr_fix + 1, 1,
  317. symbol_new (buf, sec, 0, fragP->fr_next),
  318. fragP->fr_offset, 1, BFD_RELOC_8_PCREL);
  319. /* Now create the unconditional branch + fixup to the
  320. final target. */
  321. fragP->fr_literal[offset + 2] = 0xfc;
  322. fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
  323. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  324. fragP->fr_var = 0;
  325. fragP->fr_fix += 5;
  326. }
  327. else if (fragP->fr_subtype == 2)
  328. {
  329. /* Reverse the condition of the first branch. */
  330. int offset = fragP->fr_fix;
  331. int opcode = fragP->fr_literal[offset] & 0xff;
  332. switch (opcode)
  333. {
  334. case 0xe8:
  335. opcode = 0xe9;
  336. break;
  337. case 0xe9:
  338. opcode = 0xe8;
  339. break;
  340. case 0xe0:
  341. opcode = 0xe2;
  342. break;
  343. case 0xe2:
  344. opcode = 0xe0;
  345. break;
  346. case 0xe3:
  347. opcode = 0xe1;
  348. break;
  349. case 0xe1:
  350. opcode = 0xe3;
  351. break;
  352. case 0xe4:
  353. opcode = 0xe6;
  354. break;
  355. case 0xe6:
  356. opcode = 0xe4;
  357. break;
  358. case 0xe7:
  359. opcode = 0xe5;
  360. break;
  361. case 0xe5:
  362. opcode = 0xe7;
  363. break;
  364. default:
  365. abort ();
  366. }
  367. fragP->fr_literal[offset] = opcode;
  368. /* Create a fixup for the reversed conditional branch. */
  369. sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
  370. fix_new (fragP, fragP->fr_fix + 1, 1,
  371. symbol_new (buf, sec, 0, fragP->fr_next),
  372. fragP->fr_offset, 1, BFD_RELOC_8_PCREL);
  373. /* Now create the unconditional branch + fixup to the
  374. final target. */
  375. fragP->fr_literal[offset + 2] = 0xf4;
  376. fragP->fr_literal[offset + 3] = 0xe0;
  377. fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
  378. fragP->fr_offset, 1, BFD_RELOC_24_PCREL);
  379. fragP->fr_var = 0;
  380. fragP->fr_fix += 7;
  381. }
  382. else if (fragP->fr_subtype == 3)
  383. {
  384. fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
  385. fragP->fr_offset, 1, BFD_RELOC_8_PCREL);
  386. fragP->fr_var = 0;
  387. fragP->fr_fix += 3;
  388. }
  389. else if (fragP->fr_subtype == 4)
  390. {
  391. /* Reverse the condition of the first branch. */
  392. int offset = fragP->fr_fix;
  393. int opcode = fragP->fr_literal[offset + 1] & 0xff;
  394. switch (opcode)
  395. {
  396. case 0xfc:
  397. opcode = 0xfd;
  398. break;
  399. case 0xfd:
  400. opcode = 0xfc;
  401. break;
  402. case 0xfe:
  403. opcode = 0xff;
  404. break;
  405. case 0xff:
  406. opcode = 0xfe;
  407. break;
  408. case 0xe8:
  409. opcode = 0xe9;
  410. break;
  411. case 0xe9:
  412. opcode = 0xe8;
  413. break;
  414. case 0xe0:
  415. opcode = 0xe2;
  416. break;
  417. case 0xe2:
  418. opcode = 0xe0;
  419. break;
  420. case 0xe3:
  421. opcode = 0xe1;
  422. break;
  423. case 0xe1:
  424. opcode = 0xe3;
  425. break;
  426. case 0xe4:
  427. opcode = 0xe6;
  428. break;
  429. case 0xe6:
  430. opcode = 0xe4;
  431. break;
  432. case 0xe7:
  433. opcode = 0xe5;
  434. break;
  435. case 0xe5:
  436. opcode = 0xe7;
  437. break;
  438. case 0xec:
  439. opcode = 0xed;
  440. break;
  441. case 0xed:
  442. opcode = 0xec;
  443. break;
  444. case 0xee:
  445. opcode = 0xef;
  446. break;
  447. case 0xef:
  448. opcode = 0xee;
  449. break;
  450. default:
  451. abort ();
  452. }
  453. fragP->fr_literal[offset + 1] = opcode;
  454. /* Create a fixup for the reversed conditional branch. */
  455. sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
  456. fix_new (fragP, fragP->fr_fix + 2, 1,
  457. symbol_new (buf, sec, 0, fragP->fr_next),
  458. fragP->fr_offset, 1, BFD_RELOC_8_PCREL);
  459. /* Now create the unconditional branch + fixup to the
  460. final target. */
  461. fragP->fr_literal[offset + 3] = 0xfc;
  462. fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
  463. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  464. fragP->fr_var = 0;
  465. fragP->fr_fix += 6;
  466. }
  467. else if (fragP->fr_subtype == 5)
  468. {
  469. /* Reverse the condition of the first branch. */
  470. int offset = fragP->fr_fix;
  471. int opcode = fragP->fr_literal[offset + 1] & 0xff;
  472. switch (opcode)
  473. {
  474. case 0xfc:
  475. opcode = 0xfd;
  476. break;
  477. case 0xfd:
  478. opcode = 0xfc;
  479. break;
  480. case 0xfe:
  481. opcode = 0xff;
  482. break;
  483. case 0xff:
  484. opcode = 0xfe;
  485. break;
  486. case 0xe8:
  487. opcode = 0xe9;
  488. break;
  489. case 0xe9:
  490. opcode = 0xe8;
  491. break;
  492. case 0xe0:
  493. opcode = 0xe2;
  494. break;
  495. case 0xe2:
  496. opcode = 0xe0;
  497. break;
  498. case 0xe3:
  499. opcode = 0xe1;
  500. break;
  501. case 0xe1:
  502. opcode = 0xe3;
  503. break;
  504. case 0xe4:
  505. opcode = 0xe6;
  506. break;
  507. case 0xe6:
  508. opcode = 0xe4;
  509. break;
  510. case 0xe7:
  511. opcode = 0xe5;
  512. break;
  513. case 0xe5:
  514. opcode = 0xe7;
  515. break;
  516. case 0xec:
  517. opcode = 0xed;
  518. break;
  519. case 0xed:
  520. opcode = 0xec;
  521. break;
  522. case 0xee:
  523. opcode = 0xef;
  524. break;
  525. case 0xef:
  526. opcode = 0xee;
  527. break;
  528. default:
  529. abort ();
  530. }
  531. fragP->fr_literal[offset + 1] = opcode;
  532. /* Create a fixup for the reversed conditional branch. */
  533. sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
  534. fix_new (fragP, fragP->fr_fix + 2, 1,
  535. symbol_new (buf, sec, 0, fragP->fr_next),
  536. fragP->fr_offset, 1, BFD_RELOC_8_PCREL);
  537. /* Now create the unconditional branch + fixup to the
  538. final target. */
  539. fragP->fr_literal[offset + 3] = 0xf4;
  540. fragP->fr_literal[offset + 4] = 0xe0;
  541. fix_new (fragP, fragP->fr_fix + 5, 4, fragP->fr_symbol,
  542. fragP->fr_offset, 1, BFD_RELOC_24_PCREL);
  543. fragP->fr_var = 0;
  544. fragP->fr_fix += 8;
  545. }
  546. else if (fragP->fr_subtype == 6)
  547. {
  548. fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
  549. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  550. fragP->fr_var = 0;
  551. fragP->fr_fix += 3;
  552. }
  553. else if (fragP->fr_subtype == 7)
  554. {
  555. int offset = fragP->fr_fix;
  556. fragP->fr_literal[offset] = 0xf4;
  557. fragP->fr_literal[offset + 1] = 0xe1;
  558. fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
  559. fragP->fr_offset, 1, BFD_RELOC_24_PCREL);
  560. fragP->fr_var = 0;
  561. fragP->fr_fix += 5;
  562. }
  563. else if (fragP->fr_subtype == 8)
  564. {
  565. fragP->fr_literal[fragP->fr_fix] = 0xea;
  566. fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
  567. fragP->fr_offset, 1, BFD_RELOC_8_PCREL);
  568. fragP->fr_var = 0;
  569. fragP->fr_fix += 2;
  570. }
  571. else if (fragP->fr_subtype == 9)
  572. {
  573. int offset = fragP->fr_fix;
  574. fragP->fr_literal[offset] = 0xfc;
  575. fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
  576. fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
  577. fragP->fr_var = 0;
  578. fragP->fr_fix += 3;
  579. }
  580. else if (fragP->fr_subtype == 10)
  581. {
  582. int offset = fragP->fr_fix;
  583. fragP->fr_literal[offset] = 0xf4;
  584. fragP->fr_literal[offset + 1] = 0xe0;
  585. fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
  586. fragP->fr_offset, 1, BFD_RELOC_24_PCREL);
  587. fragP->fr_var = 0;
  588. fragP->fr_fix += 5;
  589. }
  590. else
  591. abort ();
  592. }
  593. valueT
  594. md_section_align (asection *seg, valueT addr)
  595. {
  596. int align = bfd_get_section_alignment (stdoutput, seg);
  597. return ((addr + (1 << align) - 1) & (-1 << align));
  598. }
  599. void
  600. md_begin (void)
  601. {
  602. char *prev_name = "";
  603. const struct mn10200_opcode *op;
  604. mn10200_hash = hash_new ();
  605. /* Insert unique names into hash table. The MN10200 instruction set
  606. has many identical opcode names that have different opcodes based
  607. on the operands. This hash table then provides a quick index to
  608. the first opcode with a particular name in the opcode table. */
  609. op = mn10200_opcodes;
  610. while (op->name)
  611. {
  612. if (strcmp (prev_name, op->name))
  613. {
  614. prev_name = (char *) op->name;
  615. hash_insert (mn10200_hash, op->name, (char *) op);
  616. }
  617. op++;
  618. }
  619. /* This is both a simplification (we don't have to write md_apply_fix)
  620. and support for future optimizations (branch shortening and similar
  621. stuff in the linker. */
  622. linkrelax = 1;
  623. }
  624. static unsigned long
  625. check_operand (unsigned long insn ATTRIBUTE_UNUSED,
  626. const struct mn10200_operand *operand,
  627. offsetT val)
  628. {
  629. /* No need to check 24bit or 32bit operands for a bit. */
  630. if (operand->bits < 24
  631. && (operand->flags & MN10200_OPERAND_NOCHECK) == 0)
  632. {
  633. long min, max;
  634. offsetT test;
  635. if ((operand->flags & MN10200_OPERAND_SIGNED) != 0)
  636. {
  637. max = (1 << (operand->bits - 1)) - 1;
  638. min = - (1 << (operand->bits - 1));
  639. }
  640. else
  641. {
  642. max = (1 << operand->bits) - 1;
  643. min = 0;
  644. }
  645. test = val;
  646. if (test < (offsetT) min || test > (offsetT) max)
  647. return 0;
  648. else
  649. return 1;
  650. }
  651. return 1;
  652. }
  653. /* If while processing a fixup, a reloc really needs to be created
  654. Then it is done here. */
  655. arelent *
  656. tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
  657. {
  658. arelent *reloc;
  659. reloc = xmalloc (sizeof (arelent));
  660. if (fixp->fx_subsy != NULL)
  661. {
  662. if (S_GET_SEGMENT (fixp->fx_addsy) == S_GET_SEGMENT (fixp->fx_subsy)
  663. && S_IS_DEFINED (fixp->fx_subsy))
  664. {
  665. fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy);
  666. fixp->fx_subsy = NULL;
  667. }
  668. else
  669. /* FIXME: We should try more ways to resolve difference expressions
  670. here. At least this is better than silently ignoring the
  671. subtrahend. */
  672. as_bad_where (fixp->fx_file, fixp->fx_line,
  673. _("can't resolve `%s' {%s section} - `%s' {%s section}"),
  674. fixp->fx_addsy ? S_GET_NAME (fixp->fx_addsy) : "0",
  675. segment_name (fixp->fx_addsy
  676. ? S_GET_SEGMENT (fixp->fx_addsy)
  677. : absolute_section),
  678. S_GET_NAME (fixp->fx_subsy),
  679. segment_name (S_GET_SEGMENT (fixp->fx_addsy)));
  680. }
  681. reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
  682. if (reloc->howto == NULL)
  683. {
  684. as_bad_where (fixp->fx_file, fixp->fx_line,
  685. _("reloc %d not supported by object file format"),
  686. (int) fixp->fx_r_type);
  687. return NULL;
  688. }
  689. reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  690. reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
  691. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  692. reloc->addend = fixp->fx_offset;
  693. return reloc;
  694. }
  695. int
  696. md_estimate_size_before_relax (fragS *fragp, asection *seg)
  697. {
  698. if (fragp->fr_subtype == 6
  699. && (!S_IS_DEFINED (fragp->fr_symbol)
  700. || seg != S_GET_SEGMENT (fragp->fr_symbol)))
  701. fragp->fr_subtype = 7;
  702. else if (fragp->fr_subtype == 8
  703. && (!S_IS_DEFINED (fragp->fr_symbol)
  704. || seg != S_GET_SEGMENT (fragp->fr_symbol)))
  705. fragp->fr_subtype = 10;
  706. if (fragp->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
  707. abort ();
  708. return md_relax_table[fragp->fr_subtype].rlx_length;
  709. }
  710. long
  711. md_pcrel_from (fixS *fixp)
  712. {
  713. return fixp->fx_frag->fr_address;
  714. }
  715. void
  716. md_apply_fix (fixS * fixP, valueT * valP ATTRIBUTE_UNUSED, segT seg ATTRIBUTE_UNUSED)
  717. {
  718. /* We shouldn't ever get here because linkrelax is nonzero. */
  719. abort ();
  720. fixP->fx_done = 1;
  721. }
  722. /* Insert an operand value into an instruction. */
  723. static void
  724. mn10200_insert_operand (unsigned long *insnp,
  725. unsigned long *extensionp,
  726. const struct mn10200_operand *operand,
  727. offsetT val,
  728. char *file,
  729. unsigned int line,
  730. unsigned int shift)
  731. {
  732. /* No need to check 24 or 32bit operands for a bit. */
  733. if (operand->bits < 24
  734. && (operand->flags & MN10200_OPERAND_NOCHECK) == 0)
  735. {
  736. long min, max;
  737. offsetT test;
  738. if ((operand->flags & MN10200_OPERAND_SIGNED) != 0)
  739. {
  740. max = (1 << (operand->bits - 1)) - 1;
  741. min = - (1 << (operand->bits - 1));
  742. }
  743. else
  744. {
  745. max = (1 << operand->bits) - 1;
  746. min = 0;
  747. }
  748. test = val;
  749. if (test < (offsetT) min || test > (offsetT) max)
  750. as_warn_value_out_of_range (_("operand"), test, (offsetT) min, (offsetT) max, file, line);
  751. }
  752. if ((operand->flags & MN10200_OPERAND_EXTENDED) == 0)
  753. {
  754. *insnp |= (((long) val & ((1 << operand->bits) - 1))
  755. << (operand->shift + shift));
  756. if ((operand->flags & MN10200_OPERAND_REPEATED) != 0)
  757. *insnp |= (((long) val & ((1 << operand->bits) - 1))
  758. << (operand->shift + shift + 2));
  759. }
  760. else
  761. {
  762. *extensionp |= (val >> 16) & 0xff;
  763. *insnp |= val & 0xffff;
  764. }
  765. }
  766. void
  767. md_assemble (char *str)
  768. {
  769. char *s;
  770. struct mn10200_opcode *opcode;
  771. struct mn10200_opcode *next_opcode;
  772. const unsigned char *opindex_ptr;
  773. int next_opindex, relaxable;
  774. unsigned long insn, extension, size = 0;
  775. char *f;
  776. int i;
  777. int match;
  778. /* Get the opcode. */
  779. for (s = str; *s != '\0' && !ISSPACE (*s); s++)
  780. ;
  781. if (*s != '\0')
  782. *s++ = '\0';
  783. /* Find the first opcode with the proper name. */
  784. opcode = (struct mn10200_opcode *) hash_find (mn10200_hash, str);
  785. if (opcode == NULL)
  786. {
  787. as_bad (_("Unrecognized opcode: `%s'"), str);
  788. return;
  789. }
  790. str = s;
  791. while (ISSPACE (*str))
  792. ++str;
  793. input_line_pointer = str;
  794. for (;;)
  795. {
  796. const char *errmsg = NULL;
  797. int op_idx;
  798. char *hold;
  799. int extra_shift = 0;
  800. relaxable = 0;
  801. fc = 0;
  802. match = 0;
  803. next_opindex = 0;
  804. insn = opcode->opcode;
  805. extension = 0;
  806. for (op_idx = 1, opindex_ptr = opcode->operands;
  807. *opindex_ptr != 0;
  808. opindex_ptr++, op_idx++)
  809. {
  810. const struct mn10200_operand *operand;
  811. expressionS ex;
  812. if (next_opindex == 0)
  813. {
  814. operand = &mn10200_operands[*opindex_ptr];
  815. }
  816. else
  817. {
  818. operand = &mn10200_operands[next_opindex];
  819. next_opindex = 0;
  820. }
  821. errmsg = NULL;
  822. while (*str == ' ' || *str == ',')
  823. ++str;
  824. if (operand->flags & MN10200_OPERAND_RELAX)
  825. relaxable = 1;
  826. /* Gather the operand. */
  827. hold = input_line_pointer;
  828. input_line_pointer = str;
  829. if (operand->flags & MN10200_OPERAND_PAREN)
  830. {
  831. if (*input_line_pointer != ')' && *input_line_pointer != '(')
  832. {
  833. input_line_pointer = hold;
  834. str = hold;
  835. goto error;
  836. }
  837. input_line_pointer++;
  838. goto keep_going;
  839. }
  840. /* See if we can match the operands. */
  841. else if (operand->flags & MN10200_OPERAND_DREG)
  842. {
  843. if (!data_register_name (&ex))
  844. {
  845. input_line_pointer = hold;
  846. str = hold;
  847. goto error;
  848. }
  849. }
  850. else if (operand->flags & MN10200_OPERAND_AREG)
  851. {
  852. if (!address_register_name (&ex))
  853. {
  854. input_line_pointer = hold;
  855. str = hold;
  856. goto error;
  857. }
  858. }
  859. else if (operand->flags & MN10200_OPERAND_PSW)
  860. {
  861. char *start;
  862. char c = get_symbol_name (&start);
  863. if (strcmp (start, "psw") != 0)
  864. {
  865. (void) restore_line_pointer (c);
  866. input_line_pointer = hold;
  867. str = hold;
  868. goto error;
  869. }
  870. (void) restore_line_pointer (c);
  871. goto keep_going;
  872. }
  873. else if (operand->flags & MN10200_OPERAND_MDR)
  874. {
  875. char *start;
  876. char c = get_symbol_name (&start);
  877. if (strcmp (start, "mdr") != 0)
  878. {
  879. (void) restore_line_pointer (c);
  880. input_line_pointer = hold;
  881. str = hold;
  882. goto error;
  883. }
  884. (void) restore_line_pointer (c);
  885. goto keep_going;
  886. }
  887. else if (data_register_name (&ex))
  888. {
  889. input_line_pointer = hold;
  890. str = hold;
  891. goto error;
  892. }
  893. else if (address_register_name (&ex))
  894. {
  895. input_line_pointer = hold;
  896. str = hold;
  897. goto error;
  898. }
  899. else if (other_register_name (&ex))
  900. {
  901. input_line_pointer = hold;
  902. str = hold;
  903. goto error;
  904. }
  905. else if (*str == ')' || *str == '(')
  906. {
  907. input_line_pointer = hold;
  908. str = hold;
  909. goto error;
  910. }
  911. else
  912. {
  913. expression (&ex);
  914. }
  915. switch (ex.X_op)
  916. {
  917. case O_illegal:
  918. errmsg = _("illegal operand");
  919. goto error;
  920. case O_absent:
  921. errmsg = _("missing operand");
  922. goto error;
  923. case O_register:
  924. if ((operand->flags
  925. & (MN10200_OPERAND_DREG | MN10200_OPERAND_AREG)) == 0)
  926. {
  927. input_line_pointer = hold;
  928. str = hold;
  929. goto error;
  930. }
  931. if (opcode->format == FMT_2 || opcode->format == FMT_5)
  932. extra_shift = 8;
  933. else if (opcode->format == FMT_3 || opcode->format == FMT_6
  934. || opcode->format == FMT_7)
  935. extra_shift = 16;
  936. else
  937. extra_shift = 0;
  938. mn10200_insert_operand (&insn, &extension, operand,
  939. ex.X_add_number, NULL,
  940. 0, extra_shift);
  941. break;
  942. case O_constant:
  943. /* If this operand can be promoted, and it doesn't
  944. fit into the allocated bitfield for this insn,
  945. then promote it (ie this opcode does not match). */
  946. if (operand->flags
  947. & (MN10200_OPERAND_PROMOTE | MN10200_OPERAND_RELAX)
  948. && !check_operand (insn, operand, ex.X_add_number))
  949. {
  950. input_line_pointer = hold;
  951. str = hold;
  952. goto error;
  953. }
  954. mn10200_insert_operand (&insn, &extension, operand,
  955. ex.X_add_number, NULL,
  956. 0, 0);
  957. break;
  958. default:
  959. /* If this operand can be promoted, then this opcode didn't
  960. match since we can't know if it needed promotion! */
  961. if (operand->flags & MN10200_OPERAND_PROMOTE)
  962. {
  963. input_line_pointer = hold;
  964. str = hold;
  965. goto error;
  966. }
  967. /* We need to generate a fixup for this expression. */
  968. if (fc >= MAX_INSN_FIXUPS)
  969. as_fatal (_("too many fixups"));
  970. fixups[fc].exp = ex;
  971. fixups[fc].opindex = *opindex_ptr;
  972. fixups[fc].reloc = BFD_RELOC_UNUSED;
  973. ++fc;
  974. break;
  975. }
  976. keep_going:
  977. str = input_line_pointer;
  978. input_line_pointer = hold;
  979. while (*str == ' ' || *str == ',')
  980. ++str;
  981. }
  982. /* Make sure we used all the operands! */
  983. if (*str != ',')
  984. match = 1;
  985. error:
  986. if (match == 0)
  987. {
  988. next_opcode = opcode + 1;
  989. if (!strcmp (next_opcode->name, opcode->name))
  990. {
  991. opcode = next_opcode;
  992. continue;
  993. }
  994. as_bad ("%s", errmsg);
  995. return;
  996. }
  997. break;
  998. }
  999. while (ISSPACE (*str))
  1000. ++str;
  1001. if (*str != '\0')
  1002. as_bad (_("junk at end of line: `%s'"), str);
  1003. input_line_pointer = str;
  1004. if (opcode->format == FMT_1)
  1005. size = 1;
  1006. else if (opcode->format == FMT_2 || opcode->format == FMT_4)
  1007. size = 2;
  1008. else if (opcode->format == FMT_3 || opcode->format == FMT_5)
  1009. size = 3;
  1010. else if (opcode->format == FMT_6)
  1011. size = 4;
  1012. else if (opcode->format == FMT_7)
  1013. size = 5;
  1014. else
  1015. abort ();
  1016. /* Write out the instruction. */
  1017. dwarf2_emit_insn (0);
  1018. if (relaxable && fc > 0)
  1019. {
  1020. /* On a 64-bit host the size of an 'int' is not the same
  1021. as the size of a pointer, so we need a union to convert
  1022. the opindex field of the fr_cgen structure into a char *
  1023. so that it can be stored in the frag. We do not have
  1024. to worry about loosing accuracy as we are not going to
  1025. be even close to the 32bit limit of the int. */
  1026. union
  1027. {
  1028. int opindex;
  1029. char * ptr;
  1030. }
  1031. opindex_converter;
  1032. int type;
  1033. /* bCC */
  1034. if (size == 2 && opcode->opcode != 0xfc0000)
  1035. {
  1036. /* Handle bra specially. Basically treat it like jmp so
  1037. that we automatically handle 8, 16 and 32 bit offsets
  1038. correctly as well as jumps to an undefined address.
  1039. It is also important to not treat it like other bCC
  1040. instructions since the long forms of bra is different
  1041. from other bCC instructions. */
  1042. if (opcode->opcode == 0xea00)
  1043. type = 8;
  1044. else
  1045. type = 0;
  1046. }
  1047. /* jsr */
  1048. else if (size == 3 && opcode->opcode == 0xfd0000)
  1049. type = 6;
  1050. /* jmp */
  1051. else if (size == 3 && opcode->opcode == 0xfc0000)
  1052. type = 8;
  1053. /* bCCx */
  1054. else
  1055. type = 3;
  1056. opindex_converter.opindex = fixups[0].opindex;
  1057. f = frag_var (rs_machine_dependent, 8, 8 - size, type,
  1058. fixups[0].exp.X_add_symbol,
  1059. fixups[0].exp.X_add_number,
  1060. opindex_converter.ptr);
  1061. number_to_chars_bigendian (f, insn, size);
  1062. if (8 - size > 4)
  1063. {
  1064. number_to_chars_bigendian (f + size, 0, 4);
  1065. number_to_chars_bigendian (f + size + 4, 0, 8 - size - 4);
  1066. }
  1067. else
  1068. number_to_chars_bigendian (f + size, 0, 8 - size);
  1069. }
  1070. else
  1071. {
  1072. f = frag_more (size);
  1073. /* Oh, what a mess. The instruction is in big endian format, but
  1074. 16 and 24bit immediates are little endian! */
  1075. if (opcode->format == FMT_3)
  1076. {
  1077. number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
  1078. number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
  1079. }
  1080. else if (opcode->format == FMT_6)
  1081. {
  1082. number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
  1083. number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
  1084. }
  1085. else if (opcode->format == FMT_7)
  1086. {
  1087. number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
  1088. number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
  1089. number_to_chars_littleendian (f + 4, extension & 0xff, 1);
  1090. }
  1091. else
  1092. number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
  1093. /* Create any fixups. */
  1094. for (i = 0; i < fc; i++)
  1095. {
  1096. const struct mn10200_operand *operand;
  1097. int reloc_size;
  1098. operand = &mn10200_operands[fixups[i].opindex];
  1099. if (fixups[i].reloc != BFD_RELOC_UNUSED)
  1100. {
  1101. reloc_howto_type *reloc_howto;
  1102. int offset;
  1103. fixS *fixP;
  1104. reloc_howto = bfd_reloc_type_lookup (stdoutput,
  1105. fixups[i].reloc);
  1106. if (!reloc_howto)
  1107. abort ();
  1108. reloc_size = bfd_get_reloc_size (reloc_howto);
  1109. if (reloc_size < 1 || reloc_size > 4)
  1110. abort ();
  1111. offset = 4 - reloc_size;
  1112. fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
  1113. reloc_size,
  1114. &fixups[i].exp,
  1115. reloc_howto->pc_relative,
  1116. fixups[i].reloc);
  1117. /* PC-relative offsets are from the first byte of the
  1118. next instruction, not from the start of the current
  1119. instruction. */
  1120. if (reloc_howto->pc_relative)
  1121. fixP->fx_offset += reloc_size;
  1122. }
  1123. else
  1124. {
  1125. int reloc, pcrel, offset;
  1126. fixS *fixP;
  1127. reloc = BFD_RELOC_NONE;
  1128. /* How big is the reloc? Remember SPLIT relocs are
  1129. implicitly 32bits. */
  1130. reloc_size = operand->bits;
  1131. offset = size - reloc_size / 8;
  1132. /* Is the reloc pc-relative? */
  1133. pcrel = (operand->flags & MN10200_OPERAND_PCREL) != 0;
  1134. /* Choose a proper BFD relocation type. */
  1135. if (pcrel)
  1136. {
  1137. if (reloc_size == 8)
  1138. reloc = BFD_RELOC_8_PCREL;
  1139. else if (reloc_size == 24)
  1140. reloc = BFD_RELOC_24_PCREL;
  1141. else
  1142. abort ();
  1143. }
  1144. else
  1145. {
  1146. if (reloc_size == 32)
  1147. reloc = BFD_RELOC_32;
  1148. else if (reloc_size == 16)
  1149. reloc = BFD_RELOC_16;
  1150. else if (reloc_size == 8)
  1151. reloc = BFD_RELOC_8;
  1152. else if (reloc_size == 24)
  1153. reloc = BFD_RELOC_24;
  1154. else
  1155. abort ();
  1156. }
  1157. /* Convert the size of the reloc into what fix_new_exp
  1158. wants. */
  1159. reloc_size = reloc_size / 8;
  1160. if (reloc_size == 8)
  1161. reloc_size = 0;
  1162. else if (reloc_size == 16)
  1163. reloc_size = 1;
  1164. else if (reloc_size == 32 || reloc_size == 24)
  1165. reloc_size = 2;
  1166. fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
  1167. reloc_size, &fixups[i].exp, pcrel,
  1168. ((bfd_reloc_code_real_type) reloc));
  1169. /* PC-relative offsets are from the first byte of the
  1170. next instruction, not from the start of the current
  1171. instruction. */
  1172. if (pcrel)
  1173. fixP->fx_offset += size;
  1174. }
  1175. }
  1176. }
  1177. }