Created
December 12, 2014 14:51
-
-
Save nyuichi/abca742dadc5e56e8739 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdint.h> | |
| typedef uint32_t uint; | |
| uint | |
| read_ins() | |
| { | |
| uint u = 0; | |
| u = (u << 8) + (uint)getchar(); | |
| u = (u << 8) + (uint)getchar(); | |
| u = (u << 8) + (uint)getchar(); | |
| u = (u << 8) + (uint)getchar(); | |
| return u; | |
| } | |
| #define SLICE(u, i, j) \ | |
| (((u) << (31 - i)) >> ((31 - i) + j)) | |
| char reg[32][4]; | |
| char *alu_op[] = { | |
| [0] = "add", | |
| [1] = "sub", | |
| [2] = "shl", | |
| [3] = "shr", | |
| [4] = "sar", | |
| [5] = "and", | |
| [6] = "or", | |
| [7] = "xor", | |
| [24] = "cmpne", | |
| [25] = "cmpeq", | |
| [26] = "cmplt", | |
| [27] = "cmple", | |
| [28] = "fcmpne", | |
| [29] = "fcmpeq", | |
| [30] = "fcmplt", | |
| [31] = "fcmple", | |
| }; | |
| char *fpu_op[] = { | |
| [0] = "fadd", | |
| [1] = "fsub", | |
| [2] = "fmul", | |
| [3] = "fdiv", | |
| [4] = "finv", | |
| [5] = "fsqrt", | |
| [6] = "ftoi", | |
| [7] = "itof", | |
| [8] = "floor", | |
| }; | |
| char *fpu_sig[] = { | |
| [0] = "", | |
| [1] = ".neg", | |
| [2] = ".abs", | |
| [3] = ".abs.neg", | |
| }; | |
| char *misc_op[] = { | |
| [2] = "ldl", | |
| [3] = "ldh", | |
| [4] = "sysenter", | |
| [5] = "sysexit", | |
| [6] = "st", | |
| [8] = "ld", | |
| [11] = "jl", | |
| [12] = "jr", | |
| [13] = "bne", | |
| [15] = "beq" | |
| }; | |
| char *misc_pre[] = { | |
| [0] = "-", | |
| [3] = "+" | |
| }; | |
| void | |
| print_ins_i(uint rx, uint ra, uint rb, uint lit, uint tag) | |
| { | |
| printf("%s %s, %s, %s, %d\n", alu_op[tag], reg[rx], reg[ra], reg[rb], lit); | |
| } | |
| void | |
| print_ins_f(uint rx, uint ra, uint rb, uint sig, uint tag) | |
| { | |
| printf("%s%s %s, %s, %s\n", fpu_op[tag], fpu_sig[sig], reg[rx], reg[ra], reg[rb]); | |
| } | |
| void | |
| print_ins_m(uint op, uint rx, uint ra, uint pre, uint disp) | |
| { | |
| printf("%s%s %s, %s, %d\n", misc_op[op], misc_pre[pre], reg[rx], reg[ra], disp); | |
| } | |
| void | |
| print_ins(uint ins) | |
| { | |
| uint op, rx, ra, rb, lit, tag, sig, pre, disp; | |
| op = SLICE(ins, 31, 28); | |
| rx = SLICE(ins, 27, 23); | |
| ra = SLICE(ins, 22, 18); | |
| rb = SLICE(ins, 17, 13); | |
| lit = SLICE(ins, 12, 5); | |
| tag = SLICE(ins, 4, 0); | |
| sig = SLICE(ins, 6, 5); | |
| pre = SLICE(ins, 17, 16); | |
| disp = SLICE(ins, 15, 0); | |
| switch (op) { | |
| case 0x0: | |
| print_ins_i(rx, ra, rb, lit, tag); | |
| break; | |
| case 0x1: | |
| print_ins_f(rx, ra, rb, sig, tag); | |
| break; | |
| default: | |
| print_ins_m(op, rx, ra, pre, disp); | |
| break; | |
| } | |
| } | |
| int | |
| main(int argc, char *argv[]) | |
| { | |
| int i; | |
| uint32_t u; | |
| if (! freopen(argv[1], "r", stdin)) { | |
| fprintf(stderr, "cannot open file %s\n", argv[1]); | |
| return 1; | |
| } | |
| for (i = 0; i < 32; ++i) { | |
| sprintf(reg[i], "r%d", i); | |
| } | |
| do { | |
| u = read_ins(); | |
| print_ins(u); | |
| fflush(stdout); | |
| } while (! feof(stdin)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment