Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1134 lines
36 KiB

  1. /*
  2. * Copyright (c) 2000, Intel Corporation
  3. * All rights reserved.
  4. *
  5. * WARRANTY DISCLAIMER
  6. *
  7. * THESE MATERIALS ARE PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  10. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
  11. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  14. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  15. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
  16. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THESE
  17. * MATERIALS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. *
  19. * Intel Corporation is the author of the Materials, and requests that all
  20. * problem reports or change requests be submitted to it directly at
  21. * http://developer.intel.com/opensource.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <memory.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #define INT64
  29. #include "decfn_emdb.h"
  30. #include "decision_tree.h"
  31. #pragma function (memset)
  32. #include "decoder_priv.h"
  33. /***************************************************************************/
  34. #define STATIC
  35. #ifndef TRUE
  36. #define TRUE 1
  37. #endif
  38. #ifndef FALSE
  39. #define FALSE 0
  40. #endif
  41. #define PRED_SIZE EM_PREDICATE_BITS
  42. #define INIT_PSEUDO_TROLES_TAB_ENTRY(Entry,Slot0,Slot1,Slot2) \
  43. { \
  44. pseudo_troles_tab[Entry][0] = (Slot0); \
  45. pseudo_troles_tab[Entry][1] = (Slot1); \
  46. pseudo_troles_tab[Entry][2] = (Slot2); \
  47. }
  48. #define EM_DECODER_FLAGS__NO_MEMSET(Flags) ((Flags) & EM_DECODER_FLAG_NO_MEMSET)
  49. /***************************************************************************/
  50. U4byte IEL_t1, IEL_t2, IEL_t3, IEL_t4;
  51. U32 IEL_tempc;
  52. U64 IEL_et1, IEL_et2;
  53. U128 IEL_ext1, IEL_ext2, IEL_ext3, IEL_ext4, IEL_ext5;
  54. S128 IEL_ts1, IEL_ts2;
  55. extern struct EM_version_s deccpu_emdb_version;
  56. const U32 decoder_bundle_size = IEL_CONST32(EM_BUNDLE_SIZE);
  57. STATIC Temp_role_t pseudo_troles_tab[16][3];
  58. STATIC int troles_tab_initialized = FALSE;
  59. STATIC EM_Decoder_Err em_decoding(const EM_Decoder_Id, const unsigned char *,
  60. const int, const EM_IL, EM_Decoder_Info *);
  61. STATIC EM_Decoder_Err em_inst_decode(const EM_Decoder_Id, U64, const Temp_role_t,
  62. const U128 *, EM_Decoder_Info *);
  63. STATIC void em_decoder_init_decoder_info(EM_Decoder_Info *decoder_info);
  64. STATIC void em_decoder_init_bundle_info(EM_Decoder_Bundle_Info *bundle_info);
  65. STATIC void init_pseudo_troles_tab(void);
  66. /****************************************************************************
  67. * init_pseudo_troles_tab *
  68. * initalizes pseudo_troles_tab. If only template-# changes, update EM.h *
  69. * is enough to update the decoder. *
  70. ****************************************************************************/
  71. STATIC void init_pseudo_troles_tab(void)
  72. {
  73. /*** In the following table EM_TEMP_ROLE_MEM means M/A & same for _INT ***/
  74. int i;
  75. /*** initialize all entries as reserved ***/
  76. for (i = 0; i < EM_NUM_OF_TEMPLATES; i++)
  77. INIT_PSEUDO_TROLES_TAB_ENTRY(i, EM_TEMP_ROLE_NONE, EM_TEMP_ROLE_NONE,
  78. EM_TEMP_ROLE_NONE);
  79. /*** initialize specific entries ***/
  80. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mii , EM_TEMP_ROLE_MEM , \
  81. EM_TEMP_ROLE_INT , EM_TEMP_ROLE_INT );
  82. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mi_i , EM_TEMP_ROLE_MEM , \
  83. EM_TEMP_ROLE_INT , EM_TEMP_ROLE_INT );
  84. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mlx , EM_TEMP_ROLE_MEM , \
  85. EM_TEMP_ROLE_LONG, EM_TEMP_ROLE_LONG);
  86. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mmi , EM_TEMP_ROLE_MEM , \
  87. EM_TEMP_ROLE_MEM , EM_TEMP_ROLE_INT );
  88. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_m_mi , EM_TEMP_ROLE_MEM , \
  89. EM_TEMP_ROLE_MEM , EM_TEMP_ROLE_INT );
  90. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mfi , EM_TEMP_ROLE_MEM , \
  91. EM_TEMP_ROLE_FP , EM_TEMP_ROLE_INT );
  92. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mmf , EM_TEMP_ROLE_MEM , \
  93. EM_TEMP_ROLE_MEM , EM_TEMP_ROLE_FP );
  94. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mib , EM_TEMP_ROLE_MEM , \
  95. EM_TEMP_ROLE_INT , EM_TEMP_ROLE_BR );
  96. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mbb , EM_TEMP_ROLE_MEM , \
  97. EM_TEMP_ROLE_BR , EM_TEMP_ROLE_BR );
  98. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_bbb , EM_TEMP_ROLE_BR , \
  99. EM_TEMP_ROLE_BR , EM_TEMP_ROLE_BR );
  100. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mmb , EM_TEMP_ROLE_MEM , \
  101. EM_TEMP_ROLE_MEM , EM_TEMP_ROLE_BR );
  102. INIT_PSEUDO_TROLES_TAB_ENTRY(EM_template_mfb , EM_TEMP_ROLE_MEM , \
  103. EM_TEMP_ROLE_FP , EM_TEMP_ROLE_BR );
  104. /*** avoid multpiple initializations ***/
  105. troles_tab_initialized = TRUE;
  106. };
  107. #ifdef BIG_ENDIAN
  108. #define ENTITY_SWAP(E) entity_swap((unsigned char *)(&(E)), sizeof(E))
  109. /***************************** entity_swap *******************************/
  110. /* swap any number of bytes */
  111. /*************************************************************************/
  112. STATIC void entity_swap(unsigned char *entity_1st, unsigned int size)
  113. {
  114. unsigned char tmp8, *p, *q;
  115. for (q = (p = entity_1st) + (size-1);
  116. p < q;
  117. p++, q--)
  118. {
  119. tmp8 = *q;
  120. *q = *p;
  121. *p = tmp8;
  122. }
  123. }
  124. #else
  125. #define ENTITY_SWAP(E) {}
  126. #endif
  127. /*
  128. STATIC dec_2_emdb_trole[] =
  129. {
  130. EM_TEMP_ROLE_INT,
  131. EM_TEMP_ROLE_MEM,
  132. EM_TEMP_ROLE_FP,
  133. EM_TEMP_ROLE_BR,
  134. EM_TEMP_ROLE_LONG
  135. };
  136. */
  137. /********************************************************************************/
  138. /* em_decoder_open: opens a new entry in the em_clients_table and returns the */
  139. /* index of the entry. */
  140. /********************************************************************************/
  141. EM_Decoder_Id em_decoder_open(void)
  142. {
  143. int i;
  144. Client_Entry initiate_entry={1,
  145. DEFAULT_MACHINE_TYPE,
  146. DEFAULT_MACHINE_MODE,
  147. NULL
  148. };
  149. for (i=0 ; i < EM_DECODER_MAX_CLIENTS ; i++)
  150. {
  151. if ( !(em_clients_table[i].is_used) )
  152. {
  153. em_clients_table[i] = initiate_entry;
  154. if (!troles_tab_initialized)
  155. init_pseudo_troles_tab();
  156. return(i);
  157. }
  158. }
  159. return(-1);
  160. }
  161. /*****************************************************************************/
  162. /* legal_id: check whether a given id suits an active entry in the */
  163. /* clients table. */
  164. /*****************************************************************************/
  165. STATIC int legal_id(int id)
  166. {
  167. if ((id<0)||(id>=EM_DECODER_MAX_CLIENTS))
  168. {
  169. return(FALSE);
  170. }
  171. if (!em_clients_table[id].is_used)
  172. {
  173. return(FALSE);
  174. }
  175. return(TRUE);
  176. }
  177. /*****************************************************************************/
  178. /* em_decoder_close: closes an entry in the clients table for later use. */
  179. /*****************************************************************************/
  180. EM_Decoder_Err em_decoder_close(const EM_Decoder_Id id)
  181. {
  182. if (legal_id(id))
  183. {
  184. em_clients_table[id].is_used=0;
  185. if (em_clients_table[id].info_ptr != NULL)
  186. {
  187. free(em_clients_table[id].info_ptr);
  188. }
  189. return(EM_DECODER_NO_ERROR);
  190. }
  191. else
  192. {
  193. return(EM_DECODER_INVALID_CLIENT_ID);
  194. }
  195. }
  196. /*****************************************************************************/
  197. /* legal_type: */
  198. /*****************************************************************************/
  199. STATIC int legal_type(EM_Decoder_Machine_Type type)
  200. {
  201. if (type < EM_DECODER_CPU_LAST)
  202. {
  203. return(TRUE);
  204. }
  205. return(FALSE);
  206. }
  207. /*****************************************************************************/
  208. /* legal_mode: */
  209. /*****************************************************************************/
  210. STATIC int legal_mode(EM_Decoder_Machine_Type type, EM_Decoder_Machine_Mode mode)
  211. {
  212. if (mode == EM_DECODER_MODE_NO_CHANGE)
  213. {
  214. return(TRUE);
  215. }
  216. if ((mode > EM_DECODER_MODE_NO_CHANGE) && (mode < EM_DECODER_MODE_LAST))
  217. {
  218. if ((mode == EM_DECODER_MODE_EM) && (type != EM_DECODER_CPU_P7))
  219. {
  220. return(FALSE);
  221. }
  222. else
  223. {
  224. return(TRUE);
  225. }
  226. }
  227. return(FALSE);
  228. }
  229. /*****************************************************************************/
  230. /* legal_inst: */
  231. /*****************************************************************************/
  232. STATIC int legal_inst(EM_Decoder_Inst_Id inst, EM_Decoder_Machine_Type type)
  233. {
  234. if (inst < EM_INST_LAST)
  235. {
  236. /* unsigned int cpu_flag = deccpu_EMDB_info[inst].impls; */
  237. switch (type)
  238. {
  239. case EM_DECODER_CPU_P7:
  240. return TRUE;
  241. default:
  242. /*assert(0);*/
  243. break;
  244. }
  245. }
  246. return(FALSE);
  247. }
  248. /****************************************************************************/
  249. /* em_decoder_setenv: sets the machine type and machine mode variables. */
  250. /****************************************************************************/
  251. EM_Decoder_Err em_decoder_setenv(const EM_Decoder_Id id,
  252. const EM_Decoder_Machine_Type type,
  253. const EM_Decoder_Machine_Mode mode)
  254. {
  255. if (!legal_id(id))
  256. {
  257. return(EM_DECODER_INVALID_CLIENT_ID);
  258. }
  259. if (!legal_type(type))
  260. {
  261. return(EM_DECODER_INVALID_MACHINE_TYPE);
  262. }
  263. if (!legal_mode(type, mode))
  264. {
  265. return(EM_DECODER_INVALID_MACHINE_MODE);
  266. }
  267. if (type == EM_DECODER_CPU_DEFAULT)
  268. {
  269. em_clients_table[id].machine_type = DEFAULT_MACHINE_TYPE;
  270. }
  271. else if (type != EM_DECODER_CPU_NO_CHANGE)
  272. {
  273. em_clients_table[id].machine_type = type;
  274. }
  275. if (mode == EM_DECODER_MODE_DEFAULT)
  276. {
  277. em_clients_table[id].machine_mode = DEFAULT_MACHINE_MODE;
  278. }
  279. else if (mode != EM_DECODER_MODE_NO_CHANGE)
  280. {
  281. em_clients_table[id].machine_mode = mode;
  282. }
  283. return(EM_DECODER_NO_ERROR);
  284. }
  285. /******************************************************************************/
  286. /* em_decoder_setup: sets the machine type, machine mode variables and flags. */
  287. /******************************************************************************/
  288. EM_Decoder_Err em_decoder_setup(const EM_Decoder_Id id,
  289. const EM_Decoder_Machine_Type type,
  290. const EM_Decoder_Machine_Mode mode,
  291. unsigned long flags)
  292. {
  293. EM_Decoder_Err err;
  294. if ((err=em_decoder_setenv(id, type, mode)) != EM_DECODER_NO_ERROR)
  295. {
  296. return (err);
  297. }
  298. em_clients_table[id].flags = flags;
  299. return (EM_DECODER_NO_ERROR);
  300. }
  301. /********************************************************************************/
  302. /* em_decoder_init_decoder_info: initializes decoder_info in case of no memset. */
  303. /********************************************************************************/
  304. STATIC void em_decoder_init_decoder_info(EM_Decoder_Info *decoder_info)
  305. {
  306. decoder_info->pred.valid = FALSE;
  307. decoder_info->src1.type = EM_DECODER_NO_OPER;
  308. decoder_info->src1.oper_flags = 0;
  309. decoder_info->src2.type = EM_DECODER_NO_OPER;
  310. decoder_info->src2.oper_flags = 0;
  311. decoder_info->src3.type = EM_DECODER_NO_OPER;
  312. decoder_info->src3.oper_flags = 0;
  313. decoder_info->src4.type = EM_DECODER_NO_OPER;
  314. decoder_info->src4.oper_flags = 0;
  315. decoder_info->src5.type = EM_DECODER_NO_OPER;
  316. decoder_info->src5.oper_flags = 0;
  317. decoder_info->dst1.type = EM_DECODER_NO_OPER;
  318. decoder_info->dst1.oper_flags = 0;
  319. decoder_info->dst2.type = EM_DECODER_NO_OPER;
  320. decoder_info->dst1.oper_flags = 0;
  321. }
  322. /******************************************************************************/
  323. /* em_decoder_init_bundle_info: initializes bundle_info in case of no memset. */
  324. /******************************************************************************/
  325. STATIC void em_decoder_init_bundle_info(EM_Decoder_Bundle_Info *bundle_info)
  326. {
  327. unsigned int slot;
  328. for (slot=0; slot<3; slot++)
  329. {
  330. em_decoder_init_decoder_info(bundle_info->inst_info+slot);
  331. bundle_info->inst_info[slot].EM_info.em_flags = 0;
  332. }
  333. }
  334. /*******************************************************************************/
  335. /* em_decoder_associate_one: adds to the client's entry a pointer to an extra */
  336. /* information about a single instruction (inst). */
  337. /*******************************************************************************/
  338. EM_Decoder_Err em_decoder_associate_one(const EM_Decoder_Id id,
  339. const EM_Decoder_Inst_Id inst,
  340. const void * client_info)
  341. {
  342. int i;
  343. int n_insts;
  344. if (!legal_id(id))
  345. {
  346. return(EM_DECODER_INVALID_CLIENT_ID);
  347. }
  348. if (!legal_inst(inst, em_clients_table[id].machine_type))
  349. {
  350. return(EM_DECODER_INVALID_INST_ID);
  351. }
  352. {
  353. n_insts = EM_INST_LAST; /*** assume MAX. repair ***/
  354. }
  355. if (em_clients_table[id].info_ptr == NULL)
  356. {
  357. em_clients_table[id].info_ptr = calloc((size_t)n_insts, sizeof(void *));
  358. if (!em_clients_table[id].info_ptr)
  359. {
  360. return EM_DECODER_INTERNAL_ERROR;
  361. }
  362. for (i=0 ; i < n_insts ; i++)
  363. {
  364. em_clients_table[id].info_ptr[i] = NULL;
  365. }
  366. }
  367. em_clients_table[id].info_ptr[inst] = (void *)client_info;
  368. return(EM_DECODER_NO_ERROR);
  369. }
  370. /*****************************************************************************
  371. * em_decoder_associate_check - check the client's array of association *
  372. * valid for P7 cpu only *
  373. *****************************************************************************/
  374. EM_Decoder_Err em_decoder_associate_check(const EM_Decoder_Id id,
  375. EM_Decoder_Inst_Id * inst)
  376. {
  377. EM_Decoder_Inst_Id i;
  378. if(!legal_id(id))
  379. {
  380. return(EM_DECODER_INVALID_CLIENT_ID);
  381. }
  382. if (em_clients_table[id].machine_type == EM_DECODER_CPU_P7)
  383. {
  384. if (em_clients_table[id].machine_mode == EM_DECODER_MODE_EM)
  385. {
  386. if(em_clients_table[id].info_ptr == NULL)
  387. {
  388. *inst = 1;
  389. return(EM_DECODER_ASSOCIATE_MISS);
  390. }
  391. for (i = 1;
  392. (i < EM_INST_LAST) &&
  393. (em_clients_table[id].info_ptr[i] != NULL);
  394. i++);
  395. if (i < EM_INST_LAST)
  396. {
  397. *inst = i;
  398. return(EM_DECODER_ASSOCIATE_MISS);
  399. }
  400. }
  401. else /*** iA ***/
  402. {
  403. }
  404. *inst = EM_DECODER_INST_NONE;
  405. return(EM_DECODER_NO_ERROR);
  406. }
  407. else /* cpu is p5, p6 */
  408. {
  409. *inst = EM_DECODER_INST_NONE;
  410. return(EM_DECODER_NO_ERROR);
  411. }
  412. }
  413. /******************************************************************************
  414. * em_decoder_decode *
  415. * *
  416. * params: *
  417. * id - decoder client id *
  418. * code - pointer to instruction buffer *
  419. * max_code_size - instruction buffer size *
  420. * decoder_info - pointer to decoder_info to fill *
  421. * *
  422. * returns: *
  423. * EM_Decoder_Err *
  424. * *
  425. *****************************************************************************/
  426. EM_Decoder_Err em_decoder_decode(const EM_Decoder_Id id,
  427. const unsigned char * code,
  428. const int max_code_size,
  429. const EM_IL location,
  430. EM_Decoder_Info * decoder_info)
  431. {
  432. EM_Decoder_Err err = EM_DECODER_NO_ERROR;
  433. if (!legal_id(id))
  434. {
  435. return(EM_DECODER_INVALID_CLIENT_ID);
  436. }
  437. if (decoder_info == NULL)
  438. {
  439. return(EM_DECODER_NULL_PTR);
  440. }
  441. if (code == NULL)
  442. {
  443. return(EM_DECODER_TOO_SHORT_ERR);
  444. }
  445. if (EM_DECODER_FLAGS__NO_MEMSET(em_clients_table[id].flags))
  446. {
  447. em_decoder_init_decoder_info(decoder_info);
  448. }
  449. else
  450. {
  451. memset(decoder_info, 0, sizeof(EM_Decoder_Info));
  452. }
  453. if (em_clients_table[id].machine_mode == EM_DECODER_MODE_EM)
  454. {
  455. err = em_decoding(id, code, max_code_size, location, decoder_info);
  456. }
  457. else
  458. {
  459. err = EM_DECODER_INVALID_MACHINE_MODE;
  460. }
  461. return(err);
  462. }
  463. /*****************************************************************************/
  464. /* em_decoder_inst_static_info: return instruction static info (flags, */
  465. /* client_info pointer and static_info pointer) */
  466. /*****************************************************************************/
  467. EM_Decoder_Err em_decoder_inst_static_info(const EM_Decoder_Id id,
  468. const EM_Decoder_Inst_Id inst_id,
  469. EM_Decoder_Inst_Static_Info * static_info)
  470. {
  471. if (!legal_id(id))
  472. {
  473. return(EM_DECODER_INVALID_CLIENT_ID);
  474. }
  475. if (!legal_inst(inst_id, em_clients_table[id].machine_type))
  476. {
  477. return(EM_DECODER_INVALID_INST_ID);
  478. }
  479. if (static_info == NULL)
  480. {
  481. return(EM_DECODER_NULL_PTR);
  482. }
  483. if (em_clients_table[id].info_ptr != NULL)
  484. {
  485. static_info->client_info = em_clients_table[id].info_ptr[inst_id];
  486. }
  487. else
  488. {
  489. static_info->client_info = NULL;
  490. }
  491. static_info->static_info = em_decoder_static_info + inst_id;
  492. return(EM_DECODER_NO_ERROR);
  493. }
  494. /******************************************************************************
  495. * em_decoder_decode_bundle - decode em bundle *
  496. * *
  497. * params: *
  498. * id - decoder client id *
  499. * code - pointer to instruction buffer *
  500. * max_code_size - instruction buffer size(Should be at least 3*128 *
  501. * bundle_info - pointer to bundle_info to fill *
  502. * *
  503. * returns: *
  504. * EM_Decoder_Err *
  505. * *
  506. *****************************************************************************/
  507. EM_Decoder_Err em_decoder_decode_bundle(const EM_Decoder_Id id,
  508. const unsigned char* code,
  509. const int max_size,
  510. EM_Decoder_Bundle_Info* bundle_info)
  511. {
  512. unsigned int slot_no;
  513. U128 bundle;
  514. U64 instr;
  515. EM_template_t templt;
  516. Temp_role_t temp_role;
  517. EM_Decoder_Info *decoder_info;
  518. int bundle_stop;
  519. EM_Decoder_Err err, return_err = EM_DECODER_NO_ERROR;
  520. if (!legal_id(id))
  521. {
  522. return(EM_DECODER_INVALID_CLIENT_ID);
  523. }
  524. if (bundle_info == NULL)
  525. {
  526. return(EM_DECODER_NULL_PTR);
  527. }
  528. if (EM_DECODER_FLAGS__NO_MEMSET(em_clients_table[id].flags))
  529. {
  530. em_decoder_init_bundle_info(bundle_info);
  531. }
  532. else
  533. {
  534. memset(bundle_info, 0, sizeof(EM_Decoder_Bundle_Info));
  535. }
  536. bundle = *(const U128 *)code;
  537. ENTITY_SWAP(bundle);
  538. templt = EM_GET_TEMPLATE(bundle);
  539. if (max_size < EM_BUNDLE_SIZE)
  540. {
  541. return(EM_DECODER_TOO_SHORT_ERR);
  542. }
  543. bundle_info->em_bundle_info.flags = 0;
  544. if (bundle_stop = (IEL_GETDW0(bundle) & (1<<EM_SBIT_POS)))
  545. bundle_info->em_bundle_info.flags |= EM_DECODER_BIT_BUNDLE_STOP;
  546. if (EM_TEMPLATE_IS_RESERVED(templt))
  547. return(EM_DECODER_INVALID_TEMPLATE);
  548. bundle_info->em_bundle_info.b_template = templt;
  549. /*** Decode 3 instruction (unless long 2-slot instruction) ***/
  550. for(slot_no = 0; slot_no < 3; slot_no++)
  551. {
  552. decoder_info = &(bundle_info->inst_info[slot_no]);
  553. decoder_info->EM_info.slot_no = slot_no;
  554. temp_role = pseudo_troles_tab[templt][slot_no];
  555. /*** DECODER_NEXT should work even if error occurs ***/
  556. decoder_info->size = 1 + (temp_role == EM_TEMP_ROLE_LONG);
  557. if (temp_role == EM_TEMP_ROLE_LONG) /*** 2-slot instruction ***/
  558. {
  559. EM_GET_SYLLABLE(instr, bundle, slot_no+1); /* opcode is in slot 3 */
  560. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  561. if (err == EM_DECODER_INVALID_PRM_OPCODE)
  562. {
  563. /* try to find nop.i or break.i in this slot */
  564. temp_role = EM_TEMP_ROLE_INT;
  565. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  566. if (!err)
  567. {
  568. if (!EM_DECODER_CHECK_OK_IN_MLX(decoder_info))
  569. err = EM_DECODER_INVALID_PRM_OPCODE;
  570. }
  571. }
  572. }
  573. else
  574. {
  575. EM_GET_SYLLABLE(instr, bundle, slot_no);
  576. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  577. if (!err)
  578. {
  579. if ((EM_DECODER_CHECK_SLOT2_ONLY(decoder_info)) && (slot_no != 2))
  580. /*** intruction must be in slot 2 only, but... ***/
  581. err = EM_DECODER_INVALID_SLOT_BRANCH_INST;
  582. else if (EM_DECODER_CHECK_GROUP_LAST(decoder_info) &&
  583. (((slot_no != 2) || !bundle_stop) && ((slot_no != 0) || (templt != EM_template_m_mi))))
  584. /*** instruction fails to be the last in instruction group ***/
  585. err = EM_DECODER_MUST_BE_GROUP_LAST;
  586. }
  587. }
  588. /* return_err = the first worst error */
  589. if (err)
  590. {
  591. FILL_PREDICATE_INFO(instr, decoder_info);
  592. if ((!return_err) || (EM_DECODER_ERROR_IS_INST_FATAL(err)
  593. && !EM_DECODER_ERROR_IS_INST_FATAL(return_err)))
  594. return_err = err;
  595. }
  596. bundle_info->error[slot_no] = err;
  597. decoder_info->EM_info.em_bundle_info = bundle_info->em_bundle_info;
  598. if (decoder_info->size == 2) /*** 2-slot instruction - exit for loop ***/
  599. {
  600. slot_no++;
  601. break;
  602. }
  603. }
  604. /* if ((!err) && EM_DECODER_CHECK_GROUP_LAST(decoder_info) &&
  605. (!EM_DECODER_BUNDLE_STOP(decoder_info)))
  606. {
  607. *** instruction fails to be the last in instruction group ***
  608. bundle_info->error[slot_no-1] = EM_DECODER_MUST_BE_GROUP_LAST;
  609. if (!return_err)
  610. return_err = EM_DECODER_MUST_BE_GROUP_LAST;
  611. }*/
  612. bundle_info->inst_num = slot_no;
  613. bundle_info->inst_info[slot_no-1].EM_info.em_flags |=
  614. EM_DECODER_BIT_LAST_INST;
  615. if (bundle_stop)
  616. bundle_info->inst_info[slot_no-1].EM_info.em_flags |=
  617. EM_DECODER_BIT_CYCLE_BREAK;
  618. if (templt == EM_template_m_mi)
  619. bundle_info->inst_info[0].EM_info.em_flags |=
  620. EM_DECODER_BIT_CYCLE_BREAK;
  621. if (templt == EM_template_mi_i)
  622. bundle_info->inst_info[1].EM_info.em_flags |=
  623. EM_DECODER_BIT_CYCLE_BREAK;
  624. return(return_err);
  625. }
  626. /******************************************************************************
  627. * em_decoding - decode em (2.0- till ??) single instruction + bundle info *
  628. * *
  629. * params: *
  630. * id - decoder client id *
  631. * code - pointer to instruction buffer *
  632. * max_code_size - instruction buffer size *
  633. * location - syllable location, used to get slot # *
  634. * decoder_info - pointer to decoder_info to fill *
  635. * *
  636. * returns: *
  637. * EM_Decoder_Err *
  638. * *
  639. *****************************************************************************/
  640. STATIC EM_Decoder_Err em_decoding ( const EM_Decoder_Id id,
  641. const unsigned char * code,
  642. const int max_code_size,
  643. const EM_IL location,
  644. EM_Decoder_Info * decoder_info)
  645. {
  646. unsigned int slot_no = EM_IL_GET_SLOT_NO(location);
  647. U128 bundle;
  648. U64 instr;
  649. EM_template_t templt;
  650. Temp_role_t temp_role;
  651. int bundle_stop, cycle_break;
  652. EM_Decoder_Err err;
  653. bundle = *(const U128 *)code;
  654. ENTITY_SWAP(bundle);
  655. templt = EM_GET_TEMPLATE(bundle);
  656. /*** DECODER_NEXT should work even if error occurs ***/
  657. decoder_info->size = 1;
  658. if (max_code_size < EM_BUNDLE_SIZE)
  659. {
  660. return(EM_DECODER_TOO_SHORT_ERR);
  661. }
  662. /******************************************************************/
  663. /** fill EM_Info and check it ***/
  664. /******************************************************************/
  665. decoder_info->EM_info.em_flags =
  666. decoder_info->EM_info.em_bundle_info.flags = 0;
  667. if (bundle_stop = (IEL_GETDW0(bundle) & (1<<EM_SBIT_POS)))
  668. decoder_info->EM_info.em_bundle_info.flags |=
  669. EM_DECODER_BIT_BUNDLE_STOP;
  670. if (EM_TEMPLATE_IS_RESERVED(templt))
  671. return(EM_DECODER_INVALID_TEMPLATE);
  672. decoder_info->EM_info.em_bundle_info.b_template = templt;
  673. if (slot_no > EM_SLOT_2)
  674. {
  675. return(EM_DECODER_INVALID_INST_SLOT);
  676. }
  677. decoder_info->EM_info.slot_no = slot_no;
  678. /***********************/
  679. /*** decode syllable ***/
  680. /***********************/
  681. /*** get instruction binary. DON'T mask bits 41 and on ***/
  682. temp_role = pseudo_troles_tab[templt][slot_no];
  683. if (temp_role == EM_TEMP_ROLE_LONG) /*** 2-slot instruction ***/
  684. {
  685. decoder_info->size = 2;
  686. EM_GET_SYLLABLE(instr, bundle, slot_no+1); /* opcode is in slot 3 */
  687. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  688. if (err == EM_DECODER_INVALID_PRM_OPCODE)
  689. {
  690. /* try to find nop.i or break.i in this slot */
  691. temp_role = EM_TEMP_ROLE_INT;
  692. slot_no = EM_DECODER_SLOT_2;
  693. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  694. if (!err)
  695. {
  696. if (!EM_DECODER_CHECK_OK_IN_MLX(decoder_info))
  697. err = EM_DECODER_INVALID_PRM_OPCODE;
  698. }
  699. }
  700. }
  701. else
  702. {
  703. EM_GET_SYLLABLE(instr, bundle, slot_no);
  704. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  705. if (!err)
  706. {
  707. if ((EM_DECODER_CHECK_SLOT2_ONLY(decoder_info)) && (slot_no != 2))
  708. {
  709. /*** intruction must be in slot 2 only, but... ***/
  710. err = EM_DECODER_INVALID_SLOT_BRANCH_INST;
  711. }
  712. else if (EM_DECODER_CHECK_GROUP_LAST(decoder_info) &&
  713. (((slot_no != 2) || !bundle_stop) && ((slot_no != 0) || (templt != EM_template_m_mi))))
  714. {
  715. /* instruction fails to be the last in instruction group */
  716. err = EM_DECODER_MUST_BE_GROUP_LAST;
  717. }
  718. }
  719. }
  720. if (err) FILL_PREDICATE_INFO(instr, decoder_info);
  721. if ((slot_no == EM_DECODER_SLOT_2)||(decoder_info->size == 2) /* 2-slot instruction */)
  722. {
  723. cycle_break = (bundle_stop != 0);
  724. decoder_info->EM_info.em_flags |= EM_DECODER_BIT_LAST_INST;
  725. }
  726. else
  727. {
  728. cycle_break = ((slot_no==0) && (templt == EM_template_m_mi)) ||
  729. ((slot_no==1) && (templt == EM_template_mi_i));
  730. }
  731. decoder_info->EM_info.em_flags |= (cycle_break*EM_DECODER_BIT_CYCLE_BREAK);
  732. return(err);
  733. }
  734. /******************************************************************************
  735. * em_inst_decode - decode em (2.0- till ??) single syllable *
  736. * *
  737. * params: *
  738. * id - decoder client id *
  739. * instr - 64 bit, 0-40 are the syllable binary, 41-63 irrelevant *
  740. * pseudo_trole - M/A, I/A, FP, or BR *
  741. * bundle - original pointer to bundle, for 64-bit imm extraction *
  742. * decoder_info - pointer to decoder_info to fill *
  743. * *
  744. * returns: *
  745. * EM_Decoder_Err *
  746. * *
  747. *****************************************************************************/
  748. STATIC EM_Decoder_Err em_inst_decode( const EM_Decoder_Id id,
  749. U64 instr,
  750. const Temp_role_t pseudo_trole,
  751. const U128 * bundle_p,
  752. EM_Decoder_Info * decoder_info)
  753. {
  754. Inst_id_t inst_id;
  755. EM_Decoder_Err err;
  756. U64 tmp64;
  757. unsigned int major_opc, inst_center;
  758. Node_t node;
  759. int part_place, part_size, part_value, index;
  760. deccpu_EMDB_info_t * emdb_entry_p;
  761. EM_Decoder_static_info_t *static_entry_p;
  762. Template_role_t trole;
  763. /*** find major opcode ***/
  764. major_opc = IEL_GETDW1(instr); /*** assumes pos > 31 ***/
  765. major_opc >>= (EM_MAJOR_OPCODE_POS - 32);
  766. major_opc &= ((1<<EM_MAJOR_OPCODE_BITS)-1); /*** mask out bits 41-64 ***/
  767. /*** instruction bits 6-36 (without pred/major-opcode) to inst_center ***/
  768. /*** done to accelerate mask/shift in main loop (aviods IEL use). ***/
  769. IEL_SHR(tmp64, instr, PRED_SIZE); /*** bits 6-40 --> 0-34 ***/
  770. inst_center = IEL_GETDW0(tmp64); /*** original bits 6-37 ***/
  771. /*** walk through decoder decision tree ***/
  772. node = em_decision_tree[SQUARE(major_opc, pseudo_trole)];
  773. while (!NODE_IS_LEAF(node))
  774. {
  775. part_place = GET_NODE_POS(node) - PRED_SIZE;
  776. part_size = GET_NODE_SIZE(node);
  777. if (part_place < 0) /*** extensions in bits 0-5 ***/
  778. {
  779. part_place += PRED_SIZE;
  780. part_value = (IEL_GETDW0(instr) >> part_place) & ((1<<part_size)-1);
  781. }
  782. else
  783. part_value = (inst_center >> part_place) & ((1<<part_size)-1);
  784. index = GET_NEXT_NODE_INDEX(node) + part_value;
  785. node = em_decision_tree[index];
  786. }
  787. /*** leaf found - emdb line identified ***/
  788. inst_id = GET_NEXT_NODE_INDEX(node);
  789. decoder_info->flags = 0;
  790. /*define machine behaviour within illegal opcode */
  791. {
  792. Behaviour_ill_opcode machine_behaviour;
  793. /*Template_role_t em_trole = dec_2_emdb_trole[pseudo_trole];*/
  794. PRED_BEHAVIOUR(pseudo_trole, major_opc, machine_behaviour);
  795. if (machine_behaviour == BEHAVIOUR_UNDEF)
  796. {
  797. /* branch region with opcode 0 */
  798. /* check bit 32*/
  799. int decision_bit;
  800. GET_BRANCH_BEHAVIOUR_BIT(instr, decision_bit);
  801. if (decision_bit)
  802. {
  803. machine_behaviour = BEHAVIOUR_FAULT;
  804. }
  805. else
  806. {
  807. machine_behaviour = BEHAVIOUR_IGNORE_ON_FALSE_QP;
  808. }
  809. }
  810. if (machine_behaviour == BEHAVIOUR_FAULT)
  811. {
  812. EM_DECODER_SET_UNC_ILLEGAL_FAULT(decoder_info);
  813. }
  814. }
  815. if ((inst_id >= EM_INST_LAST) || inst_id == EM_ILLOP)
  816. {
  817. if (pseudo_trole == EM_TEMP_ROLE_BR)
  818. {
  819. /*** search for ignored fields ***/
  820. switch (major_opc)
  821. {
  822. case 0:
  823. {
  824. unsigned int x6_ext;
  825. U64 ext;
  826. /*** get extention in bits 27:32 ***/
  827. IEL_SHR(ext, instr, 27);
  828. x6_ext = IEL_GETDW0(ext) & ((1<<6) - 1);
  829. if (x6_ext == 1)
  830. {
  831. /*** nop.b has to be returned ***/
  832. inst_id = EM_NOP_B_IMM21;
  833. }
  834. else
  835. return(EM_DECODER_INVALID_PRM_OPCODE);
  836. break;
  837. }
  838. case 2:
  839. {
  840. unsigned int x6_ext;
  841. U64 ext;
  842. /*** get extention in bits 27:32 ***/
  843. IEL_SHR(ext, instr, 27);
  844. x6_ext = IEL_GETDW0(ext) & ((1<<6) - 1);
  845. switch (x6_ext)
  846. {
  847. case 0:
  848. case 16:
  849. case 17:
  850. return (EM_DECODER_INVALID_PRM_OPCODE);
  851. default:
  852. /*** nop.b has to be returned ***/
  853. inst_id = EM_NOP_B_IMM21;
  854. }
  855. break;
  856. }
  857. default:
  858. return(EM_DECODER_INVALID_PRM_OPCODE);
  859. }
  860. /*** zero the inst encoding: pred and operands extracted below will be 0 ***/
  861. IEL_ZERO(instr);
  862. }
  863. else return(EM_DECODER_INVALID_PRM_OPCODE);
  864. }
  865. if (! legal_inst(inst_id, em_clients_table[id].machine_type))
  866. { /*** inst does not belong to the specified machine IS ***/
  867. return(EM_DECODER_INVALID_PRM_OPCODE);
  868. }
  869. decoder_info->inst = inst_id;
  870. emdb_entry_p = deccpu_EMDB_info + inst_id;
  871. static_entry_p = (EM_Decoder_static_info_t *)em_decoder_static_info + inst_id;
  872. /*** get instruction static info ***/
  873. decoder_info->flags |= static_entry_p->flags;
  874. decoder_info->EM_info.eut = trole = static_entry_p->template_role;
  875. if (EM_DECODER_CHECK_TWO_SLOT(decoder_info))
  876. {
  877. /*** IMPORTANT: emdb flags already set from the static info !!! ***/
  878. decoder_info->EM_info.em_flags |= EM_DECODER_BIT_LONG_INST;
  879. /*** decoder_info->size = 2; *** should be already done ***/
  880. }
  881. /*** else *** should be already done ***
  882. {
  883. decoder_info->size = 1;
  884. }
  885. ***/
  886. /*** handle client and static info ***/
  887. if (em_clients_table[id].info_ptr != NULL)
  888. {
  889. decoder_info->client_info = em_clients_table[id].info_ptr[inst_id];
  890. }
  891. else
  892. {
  893. decoder_info->client_info = NULL;
  894. }
  895. decoder_info->static_info = static_entry_p;
  896. /*** Decode predicate register ***/
  897. if (static_entry_p->flags & EM_FLAG_PRED)
  898. {
  899. FILL_PREDICATE_INFO(instr, decoder_info)
  900. }
  901. /*** decode operands NYI ***/
  902. err = emdb_entry_p->format_function(emdb_entry_p, instr, bundle_p,
  903. decoder_info);
  904. return(err);
  905. }
  906. /************************ misc. API functions ********************************/
  907. const char* em_decoder_ver_str()
  908. {
  909. return(em_ver_string);
  910. }
  911. const char* em_decoder_err_msg(EM_Decoder_Err error)
  912. {
  913. if (error>=EM_DECODER_LAST_ERROR)
  914. {
  915. error = EM_DECODER_INTERNAL_ERROR;
  916. }
  917. return(em_err_msg[error]);
  918. }
  919. void em_decoder_get_version(EM_library_version_t *dec_version)
  920. {
  921. if (dec_version != NULL)
  922. {
  923. dec_version->xversion.major = XVER_MAJOR;
  924. dec_version->xversion.minor = XVER_MINOR;
  925. dec_version->api.major = API_MAJOR;
  926. dec_version->api.minor = API_MINOR;
  927. dec_version->emdb.major = deccpu_emdb_version.major;
  928. dec_version->emdb.minor = deccpu_emdb_version.minor;
  929. strcpy(dec_version->date, __DATE__);
  930. strcpy(dec_version->time, __TIME__);
  931. }
  932. }
  933. /****************************************************************************/