-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpTable.c
55 lines (48 loc) · 1.89 KB
/
OpTable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "OpTable.h"
void OpTableTest() {
OpTableNew();
HashTableEach(opTable, (FuncPtr1) OpPrintln);
OpTableFree();
}
// opList每個element都是指向string pointer (name, code, type)
char *opList[] = {"LD 00 L", "ST 01 L", "LDB 02 L", "STB 03 L",
"LDR 04 L", "STR 05 L", "LBR 06 L", "SBR 07 L", "LDI 08 L",
"CMP 10 A", "MOV 12 A", "ADD 13 A", "SUB 14 A", "MUL 15 A",
"DIV 16 A", "AND 18 A", "OR 19 A", "XOR 1A A", "ROL 1C A",
"ROR 1D A", "SHL 1E A", "SHR 1F A", "JEQ 20 J", "JNE 21 J",
"JLT 22 J", "JGT 23 J", "JLE 24 J", "JGE 25 J", "JMP 26 J",
"SWI 2A J", "JSUB 2B J", "RET 2C J", "PUSH 30 J", "POP 31 J",
"PUSHB 32 J", "POPB 33 J", "RESW F0 D", "RESB F1 D", "WORD F2 D", "BYTE F3 D"};
HashTable *opTable = NULL;
//create optable(hashtable) stored the op "name, code, type"
HashTable *OpTableNew() {
if (opTable != NULL) return opTable; //check opTable is already initialized
opTable = HashTableNew(127);
int i;
for (i=0; i<sizeof(opList)/sizeof(char*); i++) { //the numbers of strings stored in opList
Op *op = OpNew(opList[i]); //將opList 轉換成new Op object "*name, code, type"
HashTablePut(opTable, op->name, op);//put (key, new data) into hashtable
}
return opTable;
}
void OpTableFree() {
if (opTable != NULL) {
HashTableEach(opTable, (FuncPtr1) OpFree);
HashTableFree(opTable);
opTable = NULL;
}
}
Op* OpNew(char *opLine) { //opLine 需要轉換的string
Op *op = ObjNew(Op, 1); //allocate Op type *1 count memory
char opName[100];
sscanf(opLine, "%s %x %c", opName, &op->code, &op->type);//將opLine string轉成特定type (string, hex, singel char)
op->name = newStr(opName);// allocates memory on the heap (dynamiclly)
return op;
}
void OpFree(Op *op) {
freeMemory(op->name);
ObjFree(op);
}
int OpPrintln(Op *op) {
printf("%s %2x %c\n", op->name, op->code, op->type);
}