-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgalistinggo.cpp
More file actions
110 lines (88 loc) · 3.22 KB
/
galistinggo.cpp
File metadata and controls
110 lines (88 loc) · 3.22 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "galistinggo.h"
#include "goodasm.h"
#include "gainstruction.h"
GAListingGo::GAListingGo() {
name="go";
}
QString GAListingGo::renderSymbols(GoodASM *goodasm){
QString res;
QString modulename=moduleName(goodasm);
auto table=goodasm->symbols.table;
res="var "+modulename+"Symbols = map[string]int{\n";
foreach(auto s, table.keys()){
uint64_t val=table[s]->absval;
QString hex=QString::asprintf("0x%04lx",(unsigned long) val);
res+=" \""+s+"\": "+hex+",\n";
}
res+="}\n";
return res;
}
QString GAListingGo::render(GoodASM *goodasm){
GAInstruction ins=goodasm->at(0);
QString modulename=moduleName(goodasm);
QString src="// Rendered by GoodASM\n"
"package main\n"
+renderSymbols(goodasm)+
"var "+modulename+" = []byte {\n";
//Assembly comes from source, must match data.
foreach(auto ins, goodasm->instructions){
src+=render(&ins);
}
src+="}\n";
return src;
}
QString GAListingGo::render(GAInstruction *instruction){
// label, instruction, comments
QString label="";
QString ins="";
GoodASM *gasm=instruction->gasm;
switch(instruction->type){
case GAInstruction::DIRECTIVE:
if(instruction->override!="")
return instruction->override+"\n";
else
return "// Unknown directive.\n";
break;
case GAInstruction::COMMENT:
return "//"+instruction->comment+"\n";
case GAInstruction::NONE:
return "// No instruction type.\n";
break;
case GAInstruction::DATA:
//Address comes first, if we show it.
if(gasm->listadr)
label=QString::asprintf("%04x: ",
(int) gasm->lang->nativeAdr(instruction->adr));
//Then the data bytes. We always print these when they aren't
//a known instruction.
assert(instruction->len==instruction->data.length());
ins="";
for(int i=0; i<instruction->data.length(); i++){
ins+=QString::asprintf("0x%02x, ", instruction->data[i] & 0xFF);
}
//Finally the instruction and parameters.
ins+=instruction->override;
return gasm->formatSource("", ins, "// "+label+instruction->comment);
break;
case GAInstruction::MNEMONIC:
//Bad things happen if the length and the data disagree.
assert(instruction->len==instruction->data.length());
assert(instruction->len);
//Address comes first, if we show it.
if(gasm->listadr)
label+=QString::asprintf("%04x: ",
(int) gasm->lang->nativeAdr(instruction->adr));
//Then the data bytes, if we show them.
for(int i=0; i<gasm->lang->maxbytes && i<instruction->len; i++){
ins+=QString::asprintf("0x%02x, ",
instruction->data[i] & 0xFF);
}
//Finally the instruction and parameters.
return formatSource("",
ins,
"// "+label+""+instruction->verb+" "+instruction->params,
instruction->comment);
break;
}
return "// unimplemented\n";
}