Leaked source code of windows server 2003
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.

1149 lines
37 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. if (slot_no >= EM_SLOT_LAST - 1)
  560. {
  561. err = EM_DECODER_INVALID_INST_SLOT;
  562. }
  563. else
  564. {
  565. EM_GET_SYLLABLE(instr, bundle, slot_no+1); /* opcode is in slot 3 */
  566. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  567. }
  568. if (err == EM_DECODER_INVALID_PRM_OPCODE)
  569. {
  570. /* try to find nop.i or break.i in this slot */
  571. temp_role = EM_TEMP_ROLE_INT;
  572. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  573. if (!err)
  574. {
  575. if (!EM_DECODER_CHECK_OK_IN_MLX(decoder_info))
  576. err = EM_DECODER_INVALID_PRM_OPCODE;
  577. }
  578. }
  579. }
  580. else
  581. {
  582. EM_GET_SYLLABLE(instr, bundle, slot_no);
  583. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  584. if (!err)
  585. {
  586. if ((EM_DECODER_CHECK_SLOT2_ONLY(decoder_info)) && (slot_no != 2))
  587. /*** intruction must be in slot 2 only, but... ***/
  588. err = EM_DECODER_INVALID_SLOT_BRANCH_INST;
  589. else if (EM_DECODER_CHECK_GROUP_LAST(decoder_info) &&
  590. (((slot_no != 2) || !bundle_stop) && ((slot_no != 0) || (templt != EM_template_m_mi))))
  591. /*** instruction fails to be the last in instruction group ***/
  592. err = EM_DECODER_MUST_BE_GROUP_LAST;
  593. }
  594. }
  595. /* return_err = the first worst error */
  596. if (err)
  597. {
  598. FILL_PREDICATE_INFO(instr, decoder_info);
  599. if ((!return_err) || (EM_DECODER_ERROR_IS_INST_FATAL(err)
  600. && !EM_DECODER_ERROR_IS_INST_FATAL(return_err)))
  601. return_err = err;
  602. }
  603. bundle_info->error[slot_no] = err;
  604. decoder_info->EM_info.em_bundle_info = bundle_info->em_bundle_info;
  605. if (decoder_info->size == 2) /*** 2-slot instruction - exit for loop ***/
  606. {
  607. slot_no++;
  608. break;
  609. }
  610. }
  611. /* if ((!err) && EM_DECODER_CHECK_GROUP_LAST(decoder_info) &&
  612. (!EM_DECODER_BUNDLE_STOP(decoder_info)))
  613. {
  614. *** instruction fails to be the last in instruction group ***
  615. bundle_info->error[slot_no-1] = EM_DECODER_MUST_BE_GROUP_LAST;
  616. if (!return_err)
  617. return_err = EM_DECODER_MUST_BE_GROUP_LAST;
  618. }*/
  619. bundle_info->inst_num = slot_no;
  620. bundle_info->inst_info[slot_no-1].EM_info.em_flags |=
  621. EM_DECODER_BIT_LAST_INST;
  622. if (bundle_stop)
  623. bundle_info->inst_info[slot_no-1].EM_info.em_flags |=
  624. EM_DECODER_BIT_CYCLE_BREAK;
  625. if (templt == EM_template_m_mi)
  626. bundle_info->inst_info[0].EM_info.em_flags |=
  627. EM_DECODER_BIT_CYCLE_BREAK;
  628. if (templt == EM_template_mi_i)
  629. bundle_info->inst_info[1].EM_info.em_flags |=
  630. EM_DECODER_BIT_CYCLE_BREAK;
  631. return(return_err);
  632. }
  633. /******************************************************************************
  634. * em_decoding - decode em (2.0- till ??) single instruction + bundle info *
  635. * *
  636. * params: *
  637. * id - decoder client id *
  638. * code - pointer to instruction buffer *
  639. * max_code_size - instruction buffer size *
  640. * location - syllable location, used to get slot # *
  641. * decoder_info - pointer to decoder_info to fill *
  642. * *
  643. * returns: *
  644. * EM_Decoder_Err *
  645. * *
  646. *****************************************************************************/
  647. STATIC EM_Decoder_Err em_decoding ( const EM_Decoder_Id id,
  648. const unsigned char * code,
  649. const int max_code_size,
  650. const EM_IL location,
  651. EM_Decoder_Info * decoder_info)
  652. {
  653. unsigned int slot_no = EM_IL_GET_SLOT_NO(location);
  654. U128 bundle;
  655. U64 instr;
  656. EM_template_t templt;
  657. Temp_role_t temp_role;
  658. int bundle_stop, cycle_break;
  659. EM_Decoder_Err err;
  660. IEL_ZERO(instr);
  661. bundle = *(const U128 *)code;
  662. ENTITY_SWAP(bundle);
  663. templt = EM_GET_TEMPLATE(bundle);
  664. /*** DECODER_NEXT should work even if error occurs ***/
  665. decoder_info->size = 1;
  666. if (max_code_size < EM_BUNDLE_SIZE)
  667. {
  668. return(EM_DECODER_TOO_SHORT_ERR);
  669. }
  670. /******************************************************************/
  671. /** fill EM_Info and check it ***/
  672. /******************************************************************/
  673. decoder_info->EM_info.em_flags =
  674. decoder_info->EM_info.em_bundle_info.flags = 0;
  675. if (bundle_stop = (IEL_GETDW0(bundle) & (1<<EM_SBIT_POS)))
  676. decoder_info->EM_info.em_bundle_info.flags |=
  677. EM_DECODER_BIT_BUNDLE_STOP;
  678. if (EM_TEMPLATE_IS_RESERVED(templt))
  679. return(EM_DECODER_INVALID_TEMPLATE);
  680. decoder_info->EM_info.em_bundle_info.b_template = templt;
  681. if (slot_no > EM_SLOT_2)
  682. {
  683. return(EM_DECODER_INVALID_INST_SLOT);
  684. }
  685. decoder_info->EM_info.slot_no = slot_no;
  686. /***********************/
  687. /*** decode syllable ***/
  688. /***********************/
  689. /*** get instruction binary. DON'T mask bits 41 and on ***/
  690. temp_role = pseudo_troles_tab[templt][slot_no];
  691. if (temp_role == EM_TEMP_ROLE_LONG) /*** 2-slot instruction ***/
  692. {
  693. if (slot_no >= EM_SLOT_LAST - 1)
  694. {
  695. err = EM_DECODER_INVALID_INST_SLOT;
  696. }
  697. else
  698. {
  699. decoder_info->size = 2;
  700. EM_GET_SYLLABLE(instr, bundle, slot_no+1); /* opcode is in slot 3 */
  701. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  702. }
  703. if (err == EM_DECODER_INVALID_PRM_OPCODE)
  704. {
  705. /* try to find nop.i or break.i in this slot */
  706. temp_role = EM_TEMP_ROLE_INT;
  707. slot_no = EM_DECODER_SLOT_2;
  708. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  709. if (!err)
  710. {
  711. if (!EM_DECODER_CHECK_OK_IN_MLX(decoder_info))
  712. err = EM_DECODER_INVALID_PRM_OPCODE;
  713. }
  714. }
  715. }
  716. else
  717. {
  718. EM_GET_SYLLABLE(instr, bundle, slot_no);
  719. err = em_inst_decode(id, instr, temp_role, &bundle, decoder_info);
  720. if (!err)
  721. {
  722. if ((EM_DECODER_CHECK_SLOT2_ONLY(decoder_info)) && (slot_no != 2))
  723. {
  724. /*** intruction must be in slot 2 only, but... ***/
  725. err = EM_DECODER_INVALID_SLOT_BRANCH_INST;
  726. }
  727. else if (EM_DECODER_CHECK_GROUP_LAST(decoder_info) &&
  728. (((slot_no != 2) || !bundle_stop) && ((slot_no != 0) || (templt != EM_template_m_mi))))
  729. {
  730. /* instruction fails to be the last in instruction group */
  731. err = EM_DECODER_MUST_BE_GROUP_LAST;
  732. }
  733. }
  734. }
  735. if (err) FILL_PREDICATE_INFO(instr, decoder_info);
  736. if ((slot_no == EM_DECODER_SLOT_2)||(decoder_info->size == 2) /* 2-slot instruction */)
  737. {
  738. cycle_break = (bundle_stop != 0);
  739. decoder_info->EM_info.em_flags |= EM_DECODER_BIT_LAST_INST;
  740. }
  741. else
  742. {
  743. cycle_break = ((slot_no==0) && (templt == EM_template_m_mi)) ||
  744. ((slot_no==1) && (templt == EM_template_mi_i));
  745. }
  746. decoder_info->EM_info.em_flags |= (cycle_break*EM_DECODER_BIT_CYCLE_BREAK);
  747. return(err);
  748. }
  749. /******************************************************************************
  750. * em_inst_decode - decode em (2.0- till ??) single syllable *
  751. * *
  752. * params: *
  753. * id - decoder client id *
  754. * instr - 64 bit, 0-40 are the syllable binary, 41-63 irrelevant *
  755. * pseudo_trole - M/A, I/A, FP, or BR *
  756. * bundle - original pointer to bundle, for 64-bit imm extraction *
  757. * decoder_info - pointer to decoder_info to fill *
  758. * *
  759. * returns: *
  760. * EM_Decoder_Err *
  761. * *
  762. *****************************************************************************/
  763. STATIC EM_Decoder_Err em_inst_decode( const EM_Decoder_Id id,
  764. U64 instr,
  765. const Temp_role_t pseudo_trole,
  766. const U128 * bundle_p,
  767. EM_Decoder_Info * decoder_info)
  768. {
  769. Inst_id_t inst_id;
  770. EM_Decoder_Err err;
  771. U64 tmp64;
  772. unsigned int major_opc, inst_center;
  773. Node_t node;
  774. int part_place, part_size, part_value, index;
  775. deccpu_EMDB_info_t * emdb_entry_p;
  776. EM_Decoder_static_info_t *static_entry_p;
  777. Template_role_t trole;
  778. /*** find major opcode ***/
  779. major_opc = IEL_GETDW1(instr); /*** assumes pos > 31 ***/
  780. major_opc >>= (EM_MAJOR_OPCODE_POS - 32);
  781. major_opc &= ((1<<EM_MAJOR_OPCODE_BITS)-1); /*** mask out bits 41-64 ***/
  782. /*** instruction bits 6-36 (without pred/major-opcode) to inst_center ***/
  783. /*** done to accelerate mask/shift in main loop (aviods IEL use). ***/
  784. IEL_SHR(tmp64, instr, PRED_SIZE); /*** bits 6-40 --> 0-34 ***/
  785. inst_center = IEL_GETDW0(tmp64); /*** original bits 6-37 ***/
  786. /*** walk through decoder decision tree ***/
  787. node = em_decision_tree[SQUARE(major_opc, pseudo_trole)];
  788. while (!NODE_IS_LEAF(node))
  789. {
  790. part_place = GET_NODE_POS(node) - PRED_SIZE;
  791. part_size = GET_NODE_SIZE(node);
  792. if (part_place < 0) /*** extensions in bits 0-5 ***/
  793. {
  794. part_place += PRED_SIZE;
  795. part_value = (IEL_GETDW0(instr) >> part_place) & ((1<<part_size)-1);
  796. }
  797. else
  798. part_value = (inst_center >> part_place) & ((1<<part_size)-1);
  799. index = GET_NEXT_NODE_INDEX(node) + part_value;
  800. node = em_decision_tree[index];
  801. }
  802. /*** leaf found - emdb line identified ***/
  803. inst_id = GET_NEXT_NODE_INDEX(node);
  804. decoder_info->flags = 0;
  805. /*define machine behaviour within illegal opcode */
  806. {
  807. Behaviour_ill_opcode machine_behaviour;
  808. /*Template_role_t em_trole = dec_2_emdb_trole[pseudo_trole];*/
  809. PRED_BEHAVIOUR(pseudo_trole, major_opc, machine_behaviour);
  810. if (machine_behaviour == BEHAVIOUR_UNDEF)
  811. {
  812. /* branch region with opcode 0 */
  813. /* check bit 32*/
  814. int decision_bit;
  815. GET_BRANCH_BEHAVIOUR_BIT(instr, decision_bit);
  816. if (decision_bit)
  817. {
  818. machine_behaviour = BEHAVIOUR_FAULT;
  819. }
  820. else
  821. {
  822. machine_behaviour = BEHAVIOUR_IGNORE_ON_FALSE_QP;
  823. }
  824. }
  825. if (machine_behaviour == BEHAVIOUR_FAULT)
  826. {
  827. EM_DECODER_SET_UNC_ILLEGAL_FAULT(decoder_info);
  828. }
  829. }
  830. if ((inst_id >= EM_INST_LAST) || inst_id == EM_ILLOP)
  831. {
  832. if (pseudo_trole == EM_TEMP_ROLE_BR)
  833. {
  834. /*** search for ignored fields ***/
  835. switch (major_opc)
  836. {
  837. case 0:
  838. {
  839. unsigned int x6_ext;
  840. U64 ext;
  841. /*** get extention in bits 27:32 ***/
  842. IEL_SHR(ext, instr, 27);
  843. x6_ext = IEL_GETDW0(ext) & ((1<<6) - 1);
  844. if (x6_ext == 1)
  845. {
  846. /*** nop.b has to be returned ***/
  847. inst_id = EM_NOP_B_IMM21;
  848. }
  849. else
  850. return(EM_DECODER_INVALID_PRM_OPCODE);
  851. break;
  852. }
  853. case 2:
  854. {
  855. unsigned int x6_ext;
  856. U64 ext;
  857. /*** get extention in bits 27:32 ***/
  858. IEL_SHR(ext, instr, 27);
  859. x6_ext = IEL_GETDW0(ext) & ((1<<6) - 1);
  860. switch (x6_ext)
  861. {
  862. case 0:
  863. case 16:
  864. case 17:
  865. return (EM_DECODER_INVALID_PRM_OPCODE);
  866. default:
  867. /*** nop.b has to be returned ***/
  868. inst_id = EM_NOP_B_IMM21;
  869. }
  870. break;
  871. }
  872. default:
  873. return(EM_DECODER_INVALID_PRM_OPCODE);
  874. }
  875. /*** zero the inst encoding: pred and operands extracted below will be 0 ***/
  876. IEL_ZERO(instr);
  877. }
  878. else return(EM_DECODER_INVALID_PRM_OPCODE);
  879. }
  880. if (! legal_inst(inst_id, em_clients_table[id].machine_type))
  881. { /*** inst does not belong to the specified machine IS ***/
  882. return(EM_DECODER_INVALID_PRM_OPCODE);
  883. }
  884. decoder_info->inst = inst_id;
  885. emdb_entry_p = deccpu_EMDB_info + inst_id;
  886. static_entry_p = (EM_Decoder_static_info_t *)em_decoder_static_info + inst_id;
  887. /*** get instruction static info ***/
  888. decoder_info->flags |= static_entry_p->flags;
  889. decoder_info->EM_info.eut = trole = static_entry_p->template_role;
  890. if (EM_DECODER_CHECK_TWO_SLOT(decoder_info))
  891. {
  892. /*** IMPORTANT: emdb flags already set from the static info !!! ***/
  893. decoder_info->EM_info.em_flags |= EM_DECODER_BIT_LONG_INST;
  894. /*** decoder_info->size = 2; *** should be already done ***/
  895. }
  896. /*** else *** should be already done ***
  897. {
  898. decoder_info->size = 1;
  899. }
  900. ***/
  901. /*** handle client and static info ***/
  902. if (em_clients_table[id].info_ptr != NULL)
  903. {
  904. decoder_info->client_info = em_clients_table[id].info_ptr[inst_id];
  905. }
  906. else
  907. {
  908. decoder_info->client_info = NULL;
  909. }
  910. decoder_info->static_info = static_entry_p;
  911. /*** Decode predicate register ***/
  912. if (static_entry_p->flags & EM_FLAG_PRED)
  913. {
  914. FILL_PREDICATE_INFO(instr, decoder_info)
  915. }
  916. /*** decode operands NYI ***/
  917. err = emdb_entry_p->format_function(emdb_entry_p, instr, bundle_p,
  918. decoder_info);
  919. return(err);
  920. }
  921. /************************ misc. API functions ********************************/
  922. const char* em_decoder_ver_str()
  923. {
  924. return(em_ver_string);
  925. }
  926. const char* em_decoder_err_msg(EM_Decoder_Err error)
  927. {
  928. if (error>=EM_DECODER_LAST_ERROR)
  929. {
  930. error = EM_DECODER_INTERNAL_ERROR;
  931. }
  932. return(em_err_msg[error]);
  933. }
  934. void em_decoder_get_version(EM_library_version_t *dec_version)
  935. {
  936. if (dec_version != NULL)
  937. {
  938. dec_version->xversion.major = XVER_MAJOR;
  939. dec_version->xversion.minor = XVER_MINOR;
  940. dec_version->api.major = API_MAJOR;
  941. dec_version->api.minor = API_MINOR;
  942. dec_version->emdb.major = deccpu_emdb_version.major;
  943. dec_version->emdb.minor = deccpu_emdb_version.minor;
  944. strcpy(dec_version->date, __DATE__);
  945. strcpy(dec_version->time, __TIME__);
  946. }
  947. }
  948. /****************************************************************************/