Mips.java 17 KB

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