Descriprion.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package targets.mips;
  7. import common.Instruction;
  8. import java.util.LinkedHashMap;
  9. /**
  10. *
  11. * @author EUGENIO CARVALHO
  12. */
  13. public class Descriprion {
  14. public static LinkedHashMap<String, Instruction> instOperators = new LinkedHashMap<String, Instruction>() {
  15. {
  16. put("+", new Instruction()
  17. // .Set("std", "add");
  18. // .Set("ime", "addi");
  19. .Set("std", "addu")
  20. .Set("ime", "addiu")
  21. );
  22. put("-", new Instruction()
  23. // .Set("std", "sub")
  24. // .Set("uns", "subu")
  25. .Set("ime", "addiu")
  26. .Set("std", "subu")
  27. );
  28. put("*", new Instruction()
  29. // .Set("std", "mult")
  30. // .Set("uns", "multu")
  31. .Set("std", "mult"));
  32. put("/", new Instruction()
  33. // .Set("std", "div")
  34. // .Set("uns", "divu")
  35. .Set("std", "divu")
  36. );
  37. put("%", new Instruction()
  38. // .Set("std", "div")
  39. // .Set("uns", "divu")
  40. .Set("std", "divu")
  41. );
  42. put("&&", new Instruction()
  43. .Set("std", "and")
  44. .Set("ime", "andi")
  45. .Set("assign", "and")
  46. .Set("atribuicao_ime", "andi")
  47. );
  48. put("||", new Instruction()
  49. .Set("std", "or")
  50. .Set("ime", "ori")
  51. .Set("assign", "or")
  52. .Set("atribuicao_ime", "ori")
  53. );
  54. put("<", new Instruction()
  55. .Set("std", "bltz")
  56. .Set("assign", "sltu")
  57. .Set("atribuicao_ime", "sltiu")
  58. );
  59. put(">", new Instruction()
  60. .Set("std", "bgtz")
  61. .Set("assign", "sgtu")
  62. );
  63. put("<=", new Instruction()
  64. .Set("std", "blez")
  65. .Set("assign", "sleu"));
  66. put(">=", new Instruction()
  67. .Set("std", "bgez")
  68. .Set("assign", "sgeu")
  69. );
  70. put("==", new Instruction()
  71. .Set("std", "beq")
  72. .Set("assign", "seq")
  73. );
  74. put("!=", new Instruction()
  75. .Set("std", "bne")
  76. .Set("assign", "sne")
  77. );
  78. put("<<", new Instruction()
  79. .Set("std", "sllv")
  80. .Set("ime", "sll")
  81. );
  82. put(">>", new Instruction()
  83. .Set("std", "srlv")
  84. .Set("ime", "srl")
  85. );
  86. put("|", new Instruction()
  87. .Set("std", "or")
  88. .Set("ime", "ori")
  89. .Set("assign", "or")
  90. .Set("atribuicao_ime", "ori")
  91. );
  92. put("^", new Instruction()
  93. .Set("std", "xor")
  94. .Set("ime", "xori")
  95. .Set("assign", "xor")
  96. .Set("atribuicao_ime", "xori")
  97. );
  98. put("&", new Instruction()
  99. .Set("std", "and")
  100. .Set("ime", "andi")
  101. .Set("assign", "and")
  102. .Set("atribuicao_ime", "andi")
  103. );
  104. }
  105. };
  106. public static LinkedHashMap<String, String> registers = new LinkedHashMap<String, String>() {
  107. {
  108. put("zero", "0");
  109. put("at", "1");
  110. put("v0", "2");
  111. put("v1", "3");
  112. put("a0", "4");
  113. put("a1", "5");
  114. put("a2", "6");
  115. put("a3", "7");
  116. put("t0", "8");
  117. put("t1", "9");
  118. put("t2", "10");
  119. put("t3", "11");
  120. put("t4", "12");
  121. put("t5", "13");
  122. put("t6", "14");
  123. put("t7", "15");
  124. put("s0", "16");
  125. put("s1", "17");
  126. put("s2", "18");
  127. put("s3", "19");
  128. put("s4", "20");
  129. put("s5", "21");
  130. put("s6", "22");
  131. put("s7", "23");
  132. put("t8", "24");
  133. put("t9", "25");
  134. put("k0", "26");
  135. put("k1", "27");
  136. put("gp", "28");
  137. put("sp", "29");
  138. put("fp", "30");
  139. put("ra", "31");
  140. }
  141. };
  142. public static LinkedHashMap<String, Instruction> Codops = new LinkedHashMap<String, Instruction>() {
  143. {
  144. put("nop", new Instruction("sll")
  145. .Set("rs", "zero")
  146. .Set("rt", "zero")
  147. .Set("rd", "zero")
  148. .Set("comment", "Nop"));
  149. put("stop", new Instruction()
  150. .Set("type", "S")
  151. .Set("inst", "stop")
  152. .Set("codop", "111111")
  153. .Set("txt", "11111111111111111111111111111111")
  154. .Set("format", "S")
  155. .Set("comment", "End of programa"));
  156. put("add", new Instruction()
  157. .Set("type", "R")
  158. .Set("sa", "0")
  159. .Set("inst", "add")
  160. .Set("codop", "000000")
  161. .Set("func", "100000")
  162. .Set("format", "R0"));
  163. put("addu", new Instruction()
  164. .Set("type", "R")
  165. .Set("sa", "0")
  166. .Set("inst", "addu")
  167. .Set("codop", "000000")
  168. .Set("func", "100001")
  169. .Set("format", "R0"));
  170. put("and", new Instruction()
  171. .Set("type", "R")
  172. .Set("sa", "0")
  173. .Set("inst", "and")
  174. .Set("codop", "000000")
  175. .Set("func", "100100")
  176. .Set("format", "R0"));
  177. put("div", new Instruction()
  178. .Set("type", "R")
  179. .Set("sa", "0")
  180. .Set("inst", "div")
  181. .Set("overflow", "mflo")
  182. .Set("codop", "000000")
  183. .Set("func", "011010")
  184. .Set("format", "R1"));
  185. put("divu", new Instruction()
  186. .Set("type", "R")
  187. .Set("sa", "0")
  188. .Set("inst", "divu")
  189. .Set("/", "mflo")
  190. .Set("%", "mfhi")
  191. .Set("codop", "000000")
  192. .Set("func", "011011")
  193. .Set("format", "R1"));
  194. put("jr", new Instruction()
  195. .Set("type", "R")
  196. .Set("sa", "0")
  197. .Set("inst", "jr")
  198. .Set("codop", "000000")
  199. .Set("func", "001000")
  200. .Set("format", "R2"));
  201. put("mfhi", new Instruction()
  202. .Set("type", "R")
  203. .Set("sa", "0")
  204. .Set("inst", "mfhi")
  205. .Set("codop", "000000")
  206. .Set("func", "010000")
  207. .Set("format", "R5"));
  208. put("mflo", new Instruction()
  209. .Set("type", "R")
  210. .Set("sa", "0")
  211. .Set("inst", "mflo")
  212. .Set("codop", "000000")
  213. .Set("func", "010010")
  214. .Set("format", "R5"));
  215. put("mthi", new Instruction()
  216. .Set("type", "R")
  217. .Set("sa", "0")
  218. .Set("inst", "mthi")
  219. .Set("codop", "000000")
  220. .Set("func", "010001")
  221. .Set("format", "R5"));
  222. put("mtlo", new Instruction()
  223. .Set("type", "R")
  224. .Set("sa", "0")
  225. .Set("inst", "mtlo")
  226. .Set("codop", "000000")
  227. .Set("func", "010011")
  228. .Set("format", "R5"));
  229. put("mult", new Instruction()
  230. .Set("type", "R")
  231. .Set("sa", "0")
  232. .Set("inst", "mult")
  233. .Set("overflow", "mflo")
  234. .Set("codop", "000000")
  235. .Set("func", "011000")
  236. .Set("format", "R1"));
  237. put("multu", new Instruction()
  238. .Set("type", "R")
  239. .Set("sa", "0")
  240. .Set("inst", "multu")
  241. .Set("overflow", "mflo")
  242. .Set("codop", "000000")
  243. .Set("func", "011001")
  244. .Set("format", "R1"));
  245. put("nor", new Instruction()
  246. .Set("type", "R")
  247. .Set("sa", "0")
  248. .Set("inst", "nor")
  249. .Set("codop", "000000")
  250. .Set("func", "100111")
  251. .Set("format", "R0"));
  252. put("or", new Instruction()
  253. .Set("type", "R")
  254. .Set("sa", "0")
  255. .Set("inst", "or")
  256. .Set("codop", "000000")
  257. .Set("func", "100101")
  258. .Set("format", "R0"));
  259. put("sll", new Instruction()
  260. .Set("type", "R")
  261. .Set("sa", "0")
  262. .Set("inst", "sll")
  263. .Set("codop", "000000")
  264. .Set("func", "000000")
  265. .Set("rs", "zero")
  266. .Set("rt", "zero")
  267. .Set("rd", "zero")
  268. .Set("offset", "0")
  269. .Set("format", "R4"));
  270. put("slt", new Instruction()
  271. .Set("type", "R")
  272. .Set("sa", "0")
  273. .Set("inst", "slt")
  274. .Set("codop", "000000")
  275. .Set("func", "101010")
  276. .Set("format", "R0"));
  277. put("sra", new Instruction()
  278. .Set("type", "R")
  279. .Set("sa", "0")
  280. .Set("inst", "sra")
  281. .Set("codop", "000000")
  282. .Set("func", "000011")
  283. .Set("rs", "zero")
  284. .Set("rt", "zero")
  285. .Set("rd", "zero")
  286. .Set("offset", "0")
  287. .Set("format", "R4"));
  288. put("srl", new Instruction()
  289. .Set("type", "R")
  290. .Set("sa", "0")
  291. .Set("inst", "srl")
  292. .Set("codop", "000000")
  293. .Set("func", "000010")
  294. .Set("format", "R4"));
  295. put("sub", new Instruction()
  296. .Set("type", "R")
  297. .Set("sa", "0")
  298. .Set("inst", "sub")
  299. .Set("codop", "000000")
  300. .Set("func", "100010")
  301. .Set("format", "R0"));
  302. put("subu", new Instruction()
  303. .Set("type", "R")
  304. .Set("sa", "0")
  305. .Set("inst", "subu")
  306. .Set("codop", "000000")
  307. .Set("func", "100011")
  308. .Set("format", "R0"));
  309. put("xor", new Instruction()
  310. .Set("type", "R")
  311. .Set("sa", "0")
  312. .Set("inst", "xor")
  313. .Set("codop", "000000")
  314. .Set("func", "100110")
  315. .Set("format", "R0"));
  316. //
  317. // I instructions
  318. //
  319. put("addi", new Instruction()
  320. .Set("type", "I")
  321. .Set("inst", "addi")
  322. .Set("codop", "001000")
  323. .Set("format", "I0"));
  324. put("addiu", new Instruction()
  325. .Set("type", "I")
  326. .Set("inst", "addiu")
  327. .Set("codop", "001001")
  328. .Set("format", "I0"));
  329. put("andi", new Instruction() //r<0
  330. .Set("type", "I")
  331. .Set("inst", "andi")
  332. .Set("codop", "001100")
  333. .Set("format", "I0"));
  334. put("beq", new Instruction()//r = Set
  335. .Set("type", "I")
  336. .Set("inst", "beq")
  337. .Set("codop", "000100")
  338. .Set("format", "I0"));
  339. put("bgez", new Instruction()//r >= 0
  340. .Set("type", "I")
  341. .Set("inst", "bgez")
  342. .Set("codop", "000001")
  343. .Set("rt", "00001")
  344. .Set("format", "I0"));
  345. put("bgtz", new Instruction()//r>0
  346. .Set("type", "I")
  347. .Set("inst", "bgtz")
  348. .Set("codop", "000111")
  349. .Set("rt", "zero")
  350. .Set("format", "I0"));
  351. put("blez", new Instruction()//r<=0
  352. .Set("type", "I")
  353. .Set("inst", "blez")
  354. .Set("codop", "000110")
  355. .Set("rt", "zero")
  356. .Set("format", "I0"));
  357. put("bltz", new Instruction()//r<0
  358. .Set("type", "I")
  359. .Set("inst", "bltz")
  360. .Set("codop", "000001")
  361. .Set("rt", "00000")
  362. .Set("format", "I0"));
  363. put("bne", new Instruction()//r != Set
  364. .Set("type", "I")
  365. .Set("inst", "bne")
  366. .Set("codop", "000101")
  367. .Set("format", "I0"));
  368. put("lb", new Instruction()
  369. .Set("type", "I")
  370. .Set("inst", "lb")
  371. .Set("codop", "100000c")
  372. .Set("format", "I2"));
  373. put("lbu", new Instruction()
  374. .Set("type", "I")
  375. .Set("inst", "lbu")
  376. .Set("codop", "100100")
  377. .Set("format", "I2"));
  378. put("lh", new Instruction()
  379. .Set("type", "I")
  380. .Set("inst", "lh")
  381. .Set("codop", "100001")
  382. .Set("format", "I2"));
  383. put("lhu", new Instruction()
  384. .Set("type", "I")
  385. .Set("inst", "lhu")
  386. .Set("codop", "100101")
  387. .Set("format", "I2"));
  388. put("lui", new Instruction()
  389. .Set("type", "I")
  390. .Set("inst", "lui")
  391. .Set("codop", "001111")
  392. .Set("format", "I0"));
  393. put("lw", new Instruction()
  394. .Set("type", "I")
  395. .Set("inst", "lw")
  396. .Set("codop", "100011")
  397. .Set("format", "I0"));
  398. put("ori", new Instruction() //r<0
  399. .Set("type", "I")
  400. .Set("inst", "ori")
  401. .Set("codop", "001101")
  402. .Set("format", "I1"));
  403. put("slti", new Instruction()
  404. .Set("type", "I")
  405. .Set("inst", "slti")
  406. .Set("codop", "001010")
  407. .Set("format", "I0"));
  408. put("sltiu", new Instruction()
  409. .Set("type", "I")
  410. .Set("inst", "sltiu")
  411. .Set("codop", "001011")
  412. .Set("format", "I2"));
  413. put("sltiu", new Instruction()
  414. .Set("type", "I")
  415. .Set("inst", "sltiu")
  416. .Set("codop", "001011")
  417. .Set("format", "I0"));
  418. put("sw", new Instruction()
  419. .Set("type", "I")
  420. .Set("inst", "sw")
  421. .Set("codop", "101011")
  422. .Set("format", "I0"));
  423. put("sb", new Instruction()
  424. .Set("type", "I")
  425. .Set("inst", "sb")
  426. .Set("codop", "101000")
  427. .Set("format", "I0"));
  428. put("xori", new Instruction()
  429. .Set("type", "I")
  430. .Set("inst", "xori")
  431. .Set("codop", "001110")
  432. .Set("format", "I0"));
  433. //
  434. // J instructions
  435. //
  436. put("j", new Instruction()
  437. .Set("type", "J")
  438. .Set("inst", "j")
  439. .Set("codop", "000010")
  440. .Set("format", "J"));
  441. put("jal", new Instruction()
  442. .Set("type", "J")
  443. .Set("inst", "jal")
  444. .Set("codop", "000011")
  445. .Set("format", "J"));
  446. //
  447. //
  448. //
  449. }
  450. };
  451. public static Instruction Instruction(String inst) throws Exception {
  452. if (!Codops.containsKey(inst)) {
  453. throw new Exception(String.format("Instruction '%s' not defined", inst));
  454. }
  455. return Codops.get(inst).copy();
  456. }
  457. public static String InstructionByOperator(Instruction inst) throws Exception {
  458. Instruction r = null;
  459. String sentence = inst.Get("type"), type = "std";
  460. String op = inst.Get("op");
  461. if (!op.equals("")) { // Se é um expr
  462. if (!instOperators.containsKey(op)) {
  463. throw new Exception("Operador {" + op + "} não definido.");
  464. }
  465. r = instOperators.get(op);
  466. if (r.Has("ime")
  467. && (inst.eq("p1value", "true") || inst.eq("p2value", "true"))) {
  468. type = "ime";
  469. }
  470. }
  471. if (r == null) {
  472. throw new Exception(String.format("Sentenca '%s' não definida", sentence));
  473. }
  474. return r.Get(type);
  475. }
  476. public static String Register(String reg) {
  477. return registers.get(reg);
  478. }
  479. }