This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
/
arch.go
235 lines (212 loc) · 5.15 KB
/
arch.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// Copyright 2017 Bryan T. Meyers <bmeyers@datadrake.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implie$
// See the License for the specific language governing permissions and
// limitations under the License.
//
package machine
import (
"bytes"
"debug/elf"
"encoding/binary"
"encoding/gob"
"errors"
"github.com/boltdb/bolt"
"gopkg.in/yaml.v2"
"strings"
"sync/atomic"
)
var archSequence int64
// ArchYml is the YAML representation of an Architecture
type ArchYml struct {
Name string `yaml:"name"`
MTypes []elf.Machine `yaml:"types"`
}
// ReadArchYml deserializes an ArchYml from a []byte
func ReadArchYml(raw []byte) (ay *ArchYml, err error) {
ay = &ArchYml{}
err = yaml.Unmarshal(raw, ay)
return
}
// ToArch converts an ArchYml to an Arch and assigns an ID
func (ay *ArchYml) ToArch(tx *bolt.Tx) *Arch {
a := &Arch{}
id := atomic.AddInt64(&archSequence, 1)
buf := bytes.NewBuffer(make([]byte, 0))
err := binary.Write(buf, binary.LittleEndian, id)
if err != nil {
panic(err.Error())
}
a.id = buf.Bytes()
a.Name = ay.Name
a.mtypes = ay.MTypes
a.tx = tx
return a
}
// Arch represents an Architecture, with its instructions and registers
type Arch struct {
id []byte
Name string
insts *bolt.Bucket
regs *bolt.Bucket
tx *bolt.Tx
mtypes []elf.Machine
}
// ReadArchElf deserializes an Arch by ELF type from a BoltDB and retrieves its Buckets
func ReadArchElf(tx *bolt.Tx, mtype elf.Machine) (a *Arch, err error) {
b := tx.Bucket([]byte("elf"))
if b == nil {
panic("Bucket 'elf' does not exist")
}
buf := bytes.NewBuffer(make([]byte, 0))
err = binary.Write(buf, binary.LittleEndian, mtype)
if err != nil {
panic(err.Error())
}
mID := buf.Bytes()
aID := b.Get(mID)
a, err = ReadArch(tx, aID)
return
}
// ReadArch deserializes an Arch from a BoltDB and retrieves its Buckets
func ReadArch(tx *bolt.Tx, id []byte) (a *Arch, err error) {
b := tx.Bucket([]byte("arch"))
if b == nil {
err = errors.New("Bucket 'arch' does not exist")
return
}
as := b.Get(id)
if as == nil {
err = errors.New("Architecture not found")
return
}
bbuf := bytes.NewBuffer(as)
dec := gob.NewDecoder(bbuf)
err = dec.Decode(&a)
if err != nil {
return
}
a.id = id
b = tx.Bucket([]byte("insts"))
if b == nil {
err = errors.New("Bucket 'insts' does not exist")
return
}
a.insts = b.Bucket(id)
if a.insts == nil {
err = errors.New("'insts' arch-specific subbucket does not exist")
return
}
b = tx.Bucket([]byte("regs"))
if b == nil {
err = errors.New("Bucket 'regs' does not exist")
return
}
a.regs = b.Bucket(id)
if a.regs == nil {
err = errors.New("'regs' arch-specific subbucket does not exist")
return
}
a.tx = tx
return
}
// Put serializes an Arch into a BoltDB and creates its Buckets
func (a *Arch) Put() (err error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(a)
if err != nil {
return
}
b := a.tx.Bucket([]byte("arch"))
if b == nil {
err = errors.New("Bucket 'arch' does not exist")
return
}
err = b.Put(a.id, buf.Bytes())
if err != nil {
return
}
b = a.tx.Bucket([]byte("elf"))
if b == nil {
err = errors.New("Bucket 'elfs' does not exist")
return
}
for _, e := range a.mtypes {
buf := bytes.NewBuffer(make([]byte, 0))
err = binary.Write(buf, binary.LittleEndian, e)
if err != nil {
return
}
err = b.Put(buf.Bytes(), a.id)
if err != nil {
return
}
}
b = a.tx.Bucket([]byte("insts"))
if b == nil {
err = errors.New("Bucket 'insts' does not exist")
return
}
a.insts, err = b.CreateBucket(a.id)
if err != nil {
return
}
b = a.tx.Bucket([]byte("regs"))
if b == nil {
err = errors.New("Bucket 'regs' does not exist")
return
}
a.regs, err = b.CreateBucket(a.id)
if err != nil {
return
}
return
}
// AddInst adds an instruction to an Arch
func (a *Arch) AddInst(iname string, isaID []byte) (err error) {
iname = strings.ToLower(iname)
err = a.insts.Put([]byte(iname), isaID)
return
}
// InstToISA retrieves the ID for an Instruction's ISA
func (a *Arch) InstToISA(iname string) (id []byte, err error) {
id = a.insts.Get([]byte(iname))
if id == nil {
err = errors.New("Instruction not found")
return
}
return
}
// NInst gets the total number of instructions for an Arch
func (a *Arch) NInst() int {
return a.insts.Stats().KeyN
}
// AddReg adds a register to an Arch
func (a *Arch) AddReg(rname string, isaID []byte) (err error) {
rname = strings.ToLower(rname)
err = a.regs.Put([]byte(rname), isaID)
return
}
// RegToISA retrieves the ID for a Register's ISA
func (a *Arch) RegToISA(rname string) (id []byte, err error) {
id = a.regs.Get([]byte(rname))
if id == nil {
err = errors.New("Register not found")
return
}
return
}
// NReg gets the total number of registers for an Arch
func (a *Arch) NReg() int {
return a.regs.Stats().KeyN
}