Mips.java 17 KB

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