axe_gencode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_gencode.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include "axe_gencode.h"
  10. #include "axe_errors.h"
  11. static t_axe_instruction * gen_unary_instruction (t_program_infos *program
  12. , int opcode, int r_dest, t_axe_label *label, int addr);
  13. static t_axe_instruction * gen_binary_instruction (t_program_infos *program
  14. , int opcode, int r_dest, int r_source1, int immediate);
  15. static t_axe_instruction * gen_ternary_instruction (t_program_infos *program
  16. , int opcode, int r_dest, int r_source1, int r_source2, int flags);
  17. static t_axe_instruction * gen_jump_instruction (t_program_infos *program
  18. , int opcode, t_axe_label *label, int addr);
  19. t_axe_instruction * gen_bt_instruction
  20. (t_program_infos *program, t_axe_label *label, int addr)
  21. {
  22. return gen_jump_instruction (program, BT, label, addr);
  23. }
  24. t_axe_instruction * gen_bf_instruction
  25. (t_program_infos *program, t_axe_label *label, int addr)
  26. {
  27. return gen_jump_instruction (program, BF, label, addr);
  28. }
  29. t_axe_instruction * gen_bhi_instruction
  30. (t_program_infos *program, t_axe_label *label, int addr)
  31. {
  32. return gen_jump_instruction (program, BHI, label, addr);
  33. }
  34. t_axe_instruction * gen_bls_instruction
  35. (t_program_infos *program, t_axe_label *label, int addr)
  36. {
  37. return gen_jump_instruction (program, BLS, label, addr);
  38. }
  39. t_axe_instruction * gen_bcc_instruction
  40. (t_program_infos *program, t_axe_label *label, int addr)
  41. {
  42. return gen_jump_instruction (program, BCC, label, addr);
  43. }
  44. t_axe_instruction * gen_bcs_instruction
  45. (t_program_infos *program, t_axe_label *label, int addr)
  46. {
  47. return gen_jump_instruction (program, BCS, label, addr);
  48. }
  49. t_axe_instruction * gen_bne_instruction
  50. (t_program_infos *program, t_axe_label *label, int addr)
  51. {
  52. return gen_jump_instruction (program, BNE, label, addr);
  53. }
  54. t_axe_instruction * gen_beq_instruction
  55. (t_program_infos *program, t_axe_label *label, int addr)
  56. {
  57. return gen_jump_instruction (program, BEQ, label, addr);
  58. }
  59. t_axe_instruction * gen_bvc_instruction
  60. (t_program_infos *program, t_axe_label *label, int addr)
  61. {
  62. return gen_jump_instruction (program, BVC, label, addr);
  63. }
  64. t_axe_instruction * gen_bvs_instruction
  65. (t_program_infos *program, t_axe_label *label, int addr)
  66. {
  67. return gen_jump_instruction (program, BVS, label, addr);
  68. }
  69. t_axe_instruction * gen_bpl_instruction
  70. (t_program_infos *program, t_axe_label *label, int addr)
  71. {
  72. return gen_jump_instruction (program, BPL, label, addr);
  73. }
  74. t_axe_instruction * gen_bmi_instruction
  75. (t_program_infos *program, t_axe_label *label, int addr)
  76. {
  77. return gen_jump_instruction (program, BMI, label, addr);
  78. }
  79. t_axe_instruction * gen_bge_instruction
  80. (t_program_infos *program, t_axe_label *label, int addr)
  81. {
  82. return gen_jump_instruction (program, BGE, label, addr);
  83. }
  84. t_axe_instruction * gen_blt_instruction
  85. (t_program_infos *program, t_axe_label *label, int addr)
  86. {
  87. return gen_jump_instruction (program, BLT, label, addr);
  88. }
  89. t_axe_instruction * gen_bgt_instruction
  90. (t_program_infos *program, t_axe_label *label, int addr)
  91. {
  92. return gen_jump_instruction (program, BGT, label, addr);
  93. }
  94. t_axe_instruction * gen_ble_instruction
  95. (t_program_infos *program, t_axe_label *label, int addr)
  96. {
  97. return gen_jump_instruction (program, BLE, label, addr);
  98. }
  99. t_axe_instruction * gen_add_instruction (t_program_infos *program
  100. , int r_dest, int r_source1, int r_source2, int flags)
  101. {
  102. return gen_ternary_instruction
  103. (program, ADD, r_dest, r_source1, r_source2, flags);
  104. }
  105. t_axe_instruction * gen_sub_instruction (t_program_infos *program
  106. , int r_dest, int r_source1, int r_source2, int flags)
  107. {
  108. return gen_ternary_instruction
  109. (program, SUB, r_dest, r_source1, r_source2, flags);
  110. }
  111. t_axe_instruction * gen_andl_instruction (t_program_infos *program
  112. , int r_dest, int r_source1, int r_source2, int flags)
  113. {
  114. return gen_ternary_instruction
  115. (program, ANDL, r_dest, r_source1, r_source2, flags);
  116. }
  117. t_axe_instruction * gen_orl_instruction (t_program_infos *program
  118. , int r_dest, int r_source1, int r_source2, int flags)
  119. {
  120. return gen_ternary_instruction
  121. (program, ORL, r_dest, r_source1, r_source2, flags);
  122. }
  123. t_axe_instruction * gen_eorl_instruction (t_program_infos *program
  124. , int r_dest, int r_source1, int r_source2, int flags)
  125. {
  126. return gen_ternary_instruction
  127. (program, EORL, r_dest, r_source1, r_source2, flags);
  128. }
  129. t_axe_instruction * gen_andb_instruction (t_program_infos *program
  130. , int r_dest, int r_source1, int r_source2, int flags)
  131. {
  132. return gen_ternary_instruction
  133. (program, ANDB, r_dest, r_source1, r_source2, flags);
  134. }
  135. t_axe_instruction * gen_orb_instruction (t_program_infos *program
  136. , int r_dest, int r_source1, int r_source2, int flags)
  137. {
  138. return gen_ternary_instruction
  139. (program, ORB, r_dest, r_source1, r_source2, flags);
  140. }
  141. t_axe_instruction * gen_eorb_instruction (t_program_infos *program
  142. , int r_dest, int r_source1, int r_source2, int flags)
  143. {
  144. return gen_ternary_instruction
  145. (program, EORB, r_dest, r_source1, r_source2, flags);
  146. }
  147. t_axe_instruction * gen_mul_instruction (t_program_infos *program
  148. , int r_dest, int r_source1, int r_source2, int flags)
  149. {
  150. return gen_ternary_instruction
  151. (program, MUL, r_dest, r_source1, r_source2, flags);
  152. }
  153. t_axe_instruction * gen_div_instruction (t_program_infos *program
  154. , int r_dest, int r_source1, int r_source2, int flags)
  155. {
  156. return gen_ternary_instruction
  157. (program, DIV, r_dest, r_source1, r_source2, flags);
  158. }
  159. t_axe_instruction * gen_shl_instruction (t_program_infos *program
  160. , int r_dest, int r_source1, int r_source2, int flags)
  161. {
  162. /* The SHL operational semantic is: source2 << source1. Because of this we
  163. * need to put the value to shift in source2, and the shift-amount in source1
  164. */
  165. return gen_ternary_instruction
  166. (program, SHL, r_dest, r_source2, r_source1, flags);
  167. }
  168. t_axe_instruction * gen_shr_instruction (t_program_infos *program
  169. , int r_dest, int r_source1, int r_source2, int flags)
  170. {
  171. /* The SHR operational semantic is: source2 >> source1. Because of this we
  172. * need to put the value to shift in source2, and the shift-amount in source1
  173. */
  174. return gen_ternary_instruction
  175. (program, SHR, r_dest, r_source2, r_source1, flags);
  176. }
  177. t_axe_instruction * gen_neg_instruction (t_program_infos *program
  178. , int r_dest, int r_source, int flags)
  179. {
  180. return gen_ternary_instruction
  181. (program, SUB, r_dest, REG_0, r_source, flags);
  182. }
  183. t_axe_instruction * gen_spcl_instruction (t_program_infos *program
  184. , int r_dest, int r_source1, int r_source2, int flags)
  185. {
  186. return gen_ternary_instruction
  187. (program, SPCL, r_dest, r_source1, r_source2, flags);
  188. }
  189. t_axe_instruction * gen_addi_instruction
  190. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  191. {
  192. return gen_binary_instruction(program, ADDI
  193. , r_dest, r_source1, immediate);
  194. }
  195. t_axe_instruction * gen_subi_instruction
  196. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  197. {
  198. return gen_binary_instruction(program, SUBI
  199. , r_dest, r_source1, immediate);
  200. }
  201. t_axe_instruction * gen_andli_instruction
  202. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  203. {
  204. return gen_binary_instruction(program, ANDLI
  205. , r_dest, r_source1, immediate);
  206. }
  207. t_axe_instruction * gen_orli_instruction
  208. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  209. {
  210. return gen_binary_instruction(program, ORLI
  211. , r_dest, r_source1, immediate);
  212. }
  213. t_axe_instruction * gen_eorli_instruction
  214. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  215. {
  216. return gen_binary_instruction(program, EORLI
  217. , r_dest, r_source1, immediate);
  218. }
  219. t_axe_instruction * gen_andbi_instruction
  220. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  221. {
  222. return gen_binary_instruction(program, ANDBI
  223. , r_dest, r_source1, immediate);
  224. }
  225. t_axe_instruction * gen_muli_instruction
  226. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  227. {
  228. return gen_binary_instruction(program, MULI
  229. , r_dest, r_source1, immediate);
  230. }
  231. t_axe_instruction * gen_orbi_instruction
  232. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  233. {
  234. return gen_binary_instruction(program, ORBI
  235. , r_dest, r_source1, immediate);
  236. }
  237. t_axe_instruction * gen_eorbi_instruction
  238. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  239. {
  240. return gen_binary_instruction(program, EORBI
  241. , r_dest, r_source1, immediate);
  242. }
  243. t_axe_instruction * gen_divi_instruction
  244. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  245. {
  246. return gen_binary_instruction(program, DIVI
  247. , r_dest, r_source1, immediate);
  248. }
  249. t_axe_instruction * gen_shli_instruction
  250. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  251. {
  252. return gen_binary_instruction(program, SHLI
  253. , r_dest, r_source1, immediate);
  254. }
  255. t_axe_instruction * gen_shri_instruction
  256. (t_program_infos *program, int r_dest, int r_source1, int immediate)
  257. {
  258. return gen_binary_instruction(program, SHRI
  259. , r_dest, r_source1, immediate);
  260. }
  261. t_axe_instruction * gen_notl_instruction
  262. (t_program_infos *program, int r_dest, int r_source1)
  263. {
  264. return gen_binary_instruction(program, NOTL
  265. , r_dest, r_source1, 0);
  266. }
  267. t_axe_instruction * gen_notb_instruction
  268. (t_program_infos *program, int r_dest, int r_source1)
  269. {
  270. return gen_binary_instruction(program, NOTB
  271. , r_dest, r_source1, 0);
  272. }
  273. t_axe_instruction * gen_read_instruction
  274. (t_program_infos *program, int r_dest)
  275. {
  276. return gen_unary_instruction(program, AXE_READ, r_dest, NULL, 0);
  277. }
  278. t_axe_instruction * gen_write_instruction
  279. (t_program_infos *program, int r_dest)
  280. {
  281. return gen_unary_instruction(program, AXE_WRITE, r_dest, NULL, 0);
  282. }
  283. t_axe_instruction * gen_load_instruction
  284. (t_program_infos *program, int r_dest, t_axe_label *label, int address)
  285. {
  286. return gen_unary_instruction(program, LOAD, r_dest, label, address);
  287. }
  288. t_axe_instruction * gen_store_instruction
  289. (t_program_infos *program, int r_dest, t_axe_label *label, int address)
  290. {
  291. return gen_unary_instruction(program, STORE, r_dest, label, address);
  292. }
  293. t_axe_instruction * gen_mova_instruction
  294. (t_program_infos *program, int r_dest, t_axe_label *label, int address)
  295. {
  296. return gen_unary_instruction(program, MOVA, r_dest, label, address);
  297. }
  298. t_axe_instruction * gen_sge_instruction
  299. (t_program_infos *program, int r_dest)
  300. {
  301. return gen_unary_instruction(program, SGE, r_dest, NULL, 0);
  302. }
  303. t_axe_instruction * gen_seq_instruction
  304. (t_program_infos *program, int r_dest)
  305. {
  306. return gen_unary_instruction(program, SEQ, r_dest, NULL, 0);
  307. }
  308. t_axe_instruction * gen_sgt_instruction
  309. (t_program_infos *program, int r_dest)
  310. {
  311. return gen_unary_instruction(program, SGT, r_dest, NULL, 0);
  312. }
  313. t_axe_instruction * gen_sle_instruction
  314. (t_program_infos *program, int r_dest)
  315. {
  316. return gen_unary_instruction(program, SLE, r_dest, NULL, 0);
  317. }
  318. t_axe_instruction * gen_slt_instruction
  319. (t_program_infos *program, int r_dest)
  320. {
  321. return gen_unary_instruction(program, SLT, r_dest, NULL, 0);
  322. }
  323. t_axe_instruction * gen_sne_instruction
  324. (t_program_infos *program, int r_dest)
  325. {
  326. return gen_unary_instruction(program, SNE, r_dest, NULL, 0);
  327. }
  328. t_axe_instruction * gen_halt_instruction
  329. (t_program_infos *program)
  330. {
  331. t_axe_instruction *instr;
  332. /* test if program is initialized */
  333. if (program == NULL)
  334. notifyError(AXE_PROGRAM_NOT_INITIALIZED);
  335. /* create an instance of `t_axe_instruction' */
  336. instr = alloc_instruction(HALT);
  337. if (instr == NULL)
  338. notifyError(AXE_OUT_OF_MEMORY);
  339. /* add the newly created instruction to the current program */
  340. addInstruction(program, instr);
  341. /* return the load instruction */
  342. return instr;
  343. }
  344. t_axe_instruction * gen_nop_instruction(t_program_infos *program)
  345. {
  346. t_axe_instruction *instr;
  347. /* test if program is initialized */
  348. if (program == NULL)
  349. notifyError(AXE_PROGRAM_NOT_INITIALIZED);
  350. /* create an instance of `t_axe_instruction' */
  351. instr = alloc_instruction(NOP);
  352. if (instr == NULL)
  353. notifyError(AXE_OUT_OF_MEMORY);
  354. /* add the newly created instruction to the current program */
  355. addInstruction(program, instr);
  356. /* return the load instruction */
  357. return instr;
  358. }
  359. t_axe_instruction * gen_unary_instruction (t_program_infos *program
  360. , int opcode, int r_dest, t_axe_label *label, int addr)
  361. {
  362. t_axe_instruction *instr;
  363. t_axe_register *reg;
  364. t_axe_address *address;
  365. int addressType;
  366. /* test if program is initialized */
  367. if (program == NULL)
  368. notifyError(AXE_PROGRAM_NOT_INITIALIZED);
  369. if (r_dest == REG_INVALID)
  370. notifyError(AXE_INVALID_REGISTER_INFO);
  371. /* test if value is correctly initialized */
  372. if (label != NULL)
  373. {
  374. if (label->labelID == LABEL_UNSPECIFIED)
  375. {
  376. notifyError(AXE_INVALID_LABEL);
  377. }
  378. /* address type is a label type */
  379. addressType = LABEL_TYPE;
  380. }
  381. else
  382. {
  383. if (addr < 0)
  384. notifyError(AXE_INVALID_ADDRESS);
  385. /* address type is a label type */
  386. addressType = ADDRESS_TYPE;
  387. }
  388. /* test if the opcode is a valid opcode */
  389. if (opcode == INVALID_OPCODE)
  390. notifyError(AXE_INVALID_OPCODE);
  391. /* create an instance of `t_axe_instruction' */
  392. instr = alloc_instruction(opcode);
  393. if (instr == NULL)
  394. notifyError(AXE_OUT_OF_MEMORY);
  395. /* initialize a register info */
  396. reg = alloc_register(r_dest, 0);
  397. if (reg == NULL)
  398. {
  399. free_Instruction(instr);
  400. notifyError(AXE_OUT_OF_MEMORY);
  401. }
  402. /* update the reg_1 info */
  403. instr->reg_1 = reg;
  404. /* initialize an address info */
  405. address = alloc_address(addressType, addr, label);
  406. if (address == NULL)
  407. {
  408. free_Instruction(instr);
  409. notifyError(AXE_OUT_OF_MEMORY);
  410. }
  411. /* update the instruction address info */
  412. instr->address = address;
  413. /* add the newly created instruction to the current program */
  414. addInstruction(program, instr);
  415. /* return the load instruction */
  416. return instr;
  417. }
  418. t_axe_instruction * gen_binary_instruction (t_program_infos *program
  419. , int opcode, int r_dest, int r_source1, int immediate)
  420. {
  421. t_axe_instruction *instr;
  422. t_axe_register *reg_dest;
  423. t_axe_register *reg_source1;
  424. /* test if program is initialized */
  425. if (program == NULL)
  426. notifyError(AXE_PROGRAM_NOT_INITIALIZED);
  427. /* test if value is correctly initialized */
  428. if ( (r_dest == REG_INVALID)
  429. || (r_source1 == REG_INVALID) )
  430. {
  431. notifyError(AXE_INVALID_REGISTER_INFO);
  432. }
  433. /* create an instance of `t_axe_instruction' */
  434. instr = alloc_instruction(opcode);
  435. if (instr == NULL)
  436. notifyError(AXE_OUT_OF_MEMORY);
  437. /* initialize a register info */
  438. reg_dest = alloc_register(r_dest, 0);
  439. if (reg_dest == NULL)
  440. {
  441. free_Instruction(instr);
  442. notifyError(AXE_OUT_OF_MEMORY);
  443. }
  444. /* update the reg_1 info */
  445. instr->reg_1 = reg_dest;
  446. reg_source1 = alloc_register(r_source1, 0);
  447. if (reg_source1 == NULL)
  448. {
  449. free_Instruction(instr);
  450. notifyError(AXE_OUT_OF_MEMORY);
  451. return NULL;
  452. }
  453. /* update the reg_1 info */
  454. instr->reg_2 = reg_source1;
  455. /* assign an immediate value */
  456. instr->immediate = immediate;
  457. /* add the newly created instruction to the current program */
  458. addInstruction(program, instr);
  459. /* return the load instruction */
  460. return instr;
  461. }
  462. t_axe_instruction * gen_ternary_instruction (t_program_infos *program
  463. , int opcode, int r_dest, int r_source1, int r_source2, int flags)
  464. {
  465. t_axe_instruction *instr;
  466. t_axe_register *reg_dest;
  467. t_axe_register *reg_source1;
  468. t_axe_register *reg_source2;
  469. /* test if program is initialized */
  470. if (program == NULL)
  471. notifyError(AXE_PROGRAM_NOT_INITIALIZED);
  472. /* test if value is correctly initialized */
  473. if ( (r_dest == REG_INVALID)
  474. || (r_source1 == REG_INVALID)
  475. || (r_source2 == REG_INVALID))
  476. {
  477. notifyError(AXE_INVALID_REGISTER_INFO);
  478. }
  479. /* create an instance of `t_axe_instruction' */
  480. instr = alloc_instruction(opcode);
  481. if (instr == NULL)
  482. notifyError(AXE_OUT_OF_MEMORY);
  483. /* initialize a register info */
  484. reg_dest = alloc_register(r_dest, ((flags & CG_INDIRECT_DEST)? 1 : 0) );
  485. if (reg_dest == NULL)
  486. {
  487. free_Instruction(instr);
  488. notifyError(AXE_OUT_OF_MEMORY);
  489. }
  490. /* update the reg_1 info */
  491. instr->reg_1 = reg_dest;
  492. reg_source1 = alloc_register(r_source1, 0);
  493. if (reg_source1 == NULL)
  494. {
  495. free_Instruction(instr);
  496. notifyError(AXE_OUT_OF_MEMORY);
  497. }
  498. /* update the reg_2 info */
  499. instr->reg_2 = reg_source1;
  500. reg_source2 = alloc_register(r_source2, ((flags & CG_INDIRECT_SOURCE)? 1 : 0));
  501. if (reg_source1 == NULL)
  502. {
  503. free_Instruction(instr);
  504. notifyError(AXE_OUT_OF_MEMORY);
  505. }
  506. /* update the reg_3 info */
  507. instr->reg_3 = reg_source2;
  508. /* add the newly created instruction to the current program */
  509. addInstruction(program, instr);
  510. /* return the load instruction */
  511. return instr;
  512. }
  513. t_axe_instruction * gen_jump_instruction (t_program_infos *program
  514. , int opcode, t_axe_label *label, int addr)
  515. {
  516. t_axe_instruction *instr;
  517. t_axe_address * address;
  518. int addressType;
  519. /* test if program is initialized */
  520. if (program == NULL)
  521. notifyError(AXE_PROGRAM_NOT_INITIALIZED);
  522. /* test if value is correctly initialized */
  523. if (label != NULL)
  524. {
  525. if (label->labelID == LABEL_UNSPECIFIED)
  526. notifyError(AXE_INVALID_LABEL);
  527. addressType = LABEL_TYPE;
  528. }
  529. else
  530. {
  531. if (addr < 0)
  532. notifyError(AXE_INVALID_ADDRESS);
  533. addressType = ADDRESS_TYPE;
  534. }
  535. /* test if the opcode is a valid opcode */
  536. if (opcode == INVALID_OPCODE)
  537. notifyError(AXE_INVALID_OPCODE);
  538. /* create an instance of `t_axe_instruction' */
  539. instr = alloc_instruction(opcode);
  540. if (instr == NULL)
  541. notifyError(AXE_OUT_OF_MEMORY);
  542. /* initialize an address info */
  543. address = alloc_address(addressType, addr, label);
  544. if (address == NULL)
  545. {
  546. free_Instruction(instr);
  547. notifyError(AXE_OUT_OF_MEMORY);
  548. }
  549. /* update the instruction address info */
  550. instr->address = address;
  551. /* add the newly created instruction to the current program */
  552. addInstruction(program, instr);
  553. /* return the load instruction */
  554. return instr;
  555. }