Descriprion.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. // Inicio da definicao base das instruções I, R e J
  145. Instruction I = new Instruction()
  146. .Set("type", "I")
  147. .Set("rs", "zero")
  148. .Set("rt", "zero")
  149. .Set("offset", "0")
  150. .Set("format", "I0");
  151. Instruction R = new Instruction()
  152. .Set("type", "R")
  153. .Set("sa", "0")
  154. .Set("rs", "zero")
  155. .Set("rt", "zero")
  156. .Set("rd", "zero")
  157. .Set("func", "0")
  158. .Set("format", "R0");
  159. Instruction J = new Instruction()
  160. .Set("type", "J")
  161. .Set("format", "J");
  162. // Fim da definicao base das instruções I, R e J
  163. put("nop", new Instruction("sll")
  164. .Set("rs", "zero")
  165. .Set("rt", "zero")
  166. .Set("rd", "zero")
  167. .Set("comment", "Nop"));
  168. put("stop", new Instruction()
  169. .Set("type", "S")
  170. .Set("inst", "stop")
  171. .Set("codop", "111111")
  172. .Set("txt", "11111111111111111111111111111111")
  173. .Set("format", "S")
  174. .Set("comment", "End of programa"));
  175. put("add", R.copy()
  176. .Set("inst", "add")
  177. .Set("codop", "000000")
  178. .Set("func", "100000"));
  179. put("addu", R.copy()
  180. .Set("inst", "addu")
  181. .Set("codop", "000000")
  182. .Set("func", "100001"));
  183. put("and", R.copy()
  184. .Set("inst", "and")
  185. .Set("codop", "000000")
  186. .Set("func", "100100"));
  187. put("div", R.copy()
  188. .Set("inst", "div")
  189. .Set("overflow", "mflo")
  190. .Set("codop", "000000")
  191. .Set("func", "011010")
  192. .Set("format", "R1"));
  193. put("divu", R.copy()
  194. .Set("inst", "divu")
  195. .Set("/", "mflo")
  196. .Set("%", "mfhi")
  197. .Set("codop", "000000")
  198. .Set("func", "011011")
  199. .Set("format", "R1"));
  200. put("jr", R.copy()
  201. .Set("inst", "jr")
  202. .Set("codop", "000000")
  203. .Set("func", "001000")
  204. .Set("format", "R2"));
  205. put("mfhi", R.copy()
  206. .Set("inst", "mfhi")
  207. .Set("codop", "000000")
  208. .Set("func", "010000")
  209. .Set("format", "R5"));
  210. put("mflo", R.copy()
  211. .Set("inst", "mflo")
  212. .Set("codop", "000000")
  213. .Set("func", "010010")
  214. .Set("format", "R5"));
  215. put("mthi", R.copy()
  216. .Set("inst", "mthi")
  217. .Set("codop", "000000")
  218. .Set("func", "010001")
  219. .Set("format", "R5"));
  220. put("mtlo", R.copy()
  221. .Set("inst", "mtlo")
  222. .Set("codop", "000000")
  223. .Set("func", "010011")
  224. .Set("format", "R5"));
  225. put("mult", R.copy()
  226. .Set("inst", "mult")
  227. .Set("overflow", "mflo")
  228. .Set("codop", "000000")
  229. .Set("func", "011000")
  230. .Set("format", "R1"));
  231. put("multu", R.copy()
  232. .Set("inst", "multu")
  233. .Set("overflow", "mflo")
  234. .Set("codop", "000000")
  235. .Set("func", "011001")
  236. .Set("format", "R1"));
  237. put("nor", R.copy()
  238. .Set("inst", "nor")
  239. .Set("codop", "000000")
  240. .Set("func", "100111"));
  241. put("or", R.copy()
  242. .Set("inst", "or")
  243. .Set("codop", "000000")
  244. .Set("func", "100101"));
  245. put("sll", R.copy()
  246. .Set("inst", "sll")
  247. .Set("codop", "000000")
  248. .Set("func", "000000")
  249. .Set("rs", "zero")
  250. .Set("rt", "zero")
  251. .Set("rd", "zero")
  252. .Set("format", "R4"));
  253. put("slt", R.copy()
  254. .Set("inst", "slt")
  255. .Set("codop", "000000")
  256. .Set("func", "101010"));
  257. put("sra", R.copy()
  258. .Set("inst", "sra")
  259. .Set("codop", "000000")
  260. .Set("func", "000011")
  261. .Set("rs", "zero")
  262. .Set("rt", "zero")
  263. .Set("rd", "zero")
  264. .Set("format", "R4"));
  265. put("srl", R.copy()
  266. .Set("inst", "srl")
  267. .Set("codop", "000000")
  268. .Set("func", "000010")
  269. .Set("format", "R4"));
  270. put("sub", R.copy()
  271. .Set("inst", "sub")
  272. .Set("codop", "000000")
  273. .Set("func", "100010"));
  274. put("subu", R.copy()
  275. .Set("inst", "subu")
  276. .Set("codop", "000000")
  277. .Set("func", "100011"));
  278. put("xor", R.copy()
  279. .Set("inst", "xor")
  280. .Set("codop", "000000")
  281. .Set("func", "100110"));
  282. //
  283. // I instructions
  284. //
  285. put("addi", I.copy()
  286. .Set("inst", "addi")
  287. .Set("codop", "001000")
  288. .Set("format", "I0"));
  289. put("addiu", I.copy()
  290. .Set("inst", "addiu")
  291. .Set("codop", "001001")
  292. .Set("format", "I0"));
  293. put("andi", I.copy() //r<0
  294. .Set("inst", "andi")
  295. .Set("codop", "001100")
  296. .Set("format", "I0"));
  297. put("beq", I.copy()//r = Set
  298. .Set("inst", "beq")
  299. .Set("codop", "000100")
  300. .Set("format", "I0"));
  301. put("bgez", I.copy()//r >= 0
  302. .Set("inst", "bgez")
  303. .Set("codop", "000001")
  304. .Set("rt", "00001")
  305. .Set("format", "I2"));
  306. // .Set("format", "I0"));
  307. put("bgtz", I.copy()//r>0
  308. .Set("inst", "bgtz")
  309. .Set("codop", "000111")
  310. .Set("rt", "zero")
  311. .Set("format", "I2"));
  312. // .Set("format", "I0"));
  313. put("blez", I.copy()//r<=0
  314. .Set("inst", "blez")
  315. .Set("codop", "000110")
  316. .Set("rt", "zero")
  317. .Set("format", "I2"));
  318. // .Set("format", "I0"));
  319. put("bltz", I.copy()//r<0
  320. .Set("inst", "bltz")
  321. .Set("codop", "000001")
  322. .Set("rt", "00000")
  323. // .Set("format", "I0"));
  324. .Set("format", "I2"));
  325. put("bne", I.copy()//r != Set
  326. .Set("inst", "bne")
  327. .Set("codop", "000101")
  328. .Set("format", "I0"));
  329. // put("bnez", I.copy()//r = Set
  330. // .Set("inst", "bnez")
  331. // .Set("codop", "000101")
  332. // .Set("format", "I0"));
  333. put("lb", I.copy()
  334. .Set("inst", "lb")
  335. .Set("codop", "100000c")
  336. .Set("format", "I2"));
  337. put("lbu", I.copy()
  338. .Set("inst", "lbu")
  339. .Set("codop", "100100")
  340. .Set("format", "I2"));
  341. put("lh", I.copy()
  342. .Set("inst", "lh")
  343. .Set("codop", "100001")
  344. .Set("format", "I2"));
  345. put("lhu", I.copy()
  346. .Set("inst", "lhu")
  347. .Set("codop", "100101")
  348. .Set("format", "I2"));
  349. put("lui", I.copy()
  350. .Set("inst", "lui")
  351. .Set("codop", "001111")
  352. .Set("format", "I0"));
  353. put("lw", I.copy()
  354. .Set("inst", "lw")
  355. .Set("codop", "100011")
  356. .Set("format", "I1"));
  357. put("ori", I.copy() //r<0
  358. .Set("inst", "ori")
  359. .Set("codop", "001101")
  360. .Set("format", "I0"));
  361. put("slti", I.copy()
  362. .Set("inst", "slti")
  363. .Set("codop", "001010")
  364. .Set("format", "I0"));
  365. put("sltiu", I.copy()
  366. .Set("inst", "sltiu")
  367. .Set("codop", "001011")
  368. .Set("format", "I2"));
  369. put("sltiu", I.copy()
  370. .Set("inst", "sltiu")
  371. .Set("codop", "001011")
  372. .Set("format", "I0"));
  373. put("sw", I.copy()
  374. .Set("inst", "sw")
  375. .Set("codop", "101011")
  376. .Set("format", "I1"));
  377. put("sb", I.copy()
  378. .Set("inst", "sb")
  379. .Set("codop", "101000")
  380. .Set("format", "I0"));
  381. put("xori", I.copy()
  382. .Set("inst", "xori")
  383. .Set("codop", "001110")
  384. .Set("format", "I0"));
  385. //
  386. // J instructions
  387. //
  388. put("j", J.copy()
  389. .Set("inst", "j")
  390. .Set("codop", "000010"));
  391. put("jal", J.copy()
  392. .Set("inst", "jal")
  393. .Set("codop", "000011"));
  394. }
  395. };
  396. public static Instruction Instruction(String inst) throws Exception {
  397. if (!Codops.containsKey(inst)) {
  398. throw new Exception(String.format("Instruction '%s' not defined", inst));
  399. }
  400. return Codops.get(inst).copy();
  401. }
  402. public static String InstructionByOperator(Instruction inst) throws Exception {
  403. Instruction r = null;
  404. String sentence = inst.Get("type"), type = "std";
  405. String op = inst.Get("op");
  406. if (!op.equals("")) { // Se é um expr
  407. if (!instOperators.containsKey(op)) {
  408. throw new Exception("Operador {" + op + "} não definido.");
  409. }
  410. r = instOperators.get(op);
  411. if (r.Has("ime")
  412. && (inst.eq("p1value", "true") || inst.eq("p2value", "true"))) {
  413. type = "ime";
  414. }
  415. }
  416. if (r == null) {
  417. throw new Exception(String.format("Sentenca '%s' não definida", sentence));
  418. }
  419. return r.Get(type);
  420. }
  421. public static String Register(String reg) {
  422. return registers.get(reg);
  423. }
  424. }