-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.go
387 lines (347 loc) · 9.39 KB
/
assembler.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Package hack implements an assembler for the hack assembly language as documented in
// https://www.nand2tetris.org/project04.
package hack
import (
"bufio"
"errors"
"fmt"
"io"
"strconv"
"strings"
"unicode"
)
type instruction interface {
Instruction()
}
// A-instruction represents a constant or symbol which can be pre- or user-defined.
type aInstruction struct {
Literal string
IsSymbol bool
Value uint16
}
func (a aInstruction) Instruction() {}
// C-instruction represents a computation in the form of dest=comp;jump.
type cInstruction struct {
Dest string
Comp string
Jump string
}
func (c cInstruction) Instruction() {}
// label represents a label declaration. It is a pseudo-instruction that will not be translated into
// machine code. It is used as a reference to instruction memory location holding the next command
// in the program.
type label struct {
Literal string
}
func (l label) Instruction() {}
// Assemble translates hack assembly into machine code for the hack CPU. The machine code is written
// as text instead of binary as that is what was required in https://www.nand2tetris.org/project06.
func Assemble(r io.Reader, w io.Writer) error {
instructions, err := parse(r)
if err != nil {
return err
}
err = code(instructions, w)
if err != nil {
return err
}
return nil
}
// parse parses hack assembly into instructions including the pseudo-instruction label. Symbolic
// declarations in labels or symbolic references in A-instructions will not have been resolved at
// this stage.
func parse(r io.Reader) ([]instruction, error) {
var instructions []instruction
s := bufio.NewScanner(r)
for s.Scan() {
line, _, _ := strings.Cut(s.Text(), "//")
command := strings.TrimSpace(line)
if len(command) == 0 {
continue
}
if command[0] == '@' {
ins, err := parseAInstruction(command)
if err != nil {
return nil, err
}
instructions = append(instructions, ins)
} else if command[0] == '(' {
ins, err := parseLabel(command)
if err != nil {
return nil, err
}
instructions = append(instructions, ins)
} else {
ins, err := parseCInstruction(command)
if err != nil {
return nil, err
}
instructions = append(instructions, ins)
}
}
// TODO error handling of s.Error()
return instructions, nil
}
func parseAInstruction(in string) (*aInstruction, error) {
if len(in) < 2 {
return nil, errors.New("failed to parse A-instruction: @ needs to be followed by a constant or symbol")
}
in = in[1:] // drop the @
// symbols cannot start with a digit; as a starting digit indicates a constant
if unicode.IsDigit(rune(in[0])) {
v, err := strconv.ParseUint(in, 10, 15)
if err != nil {
return nil, fmt.Errorf("failed to parse A-instruction: expected unsigned 15-bit value: %v", err)
}
return &aInstruction{Literal: in, Value: uint16(v)}, nil
}
ok := containsOnly(in, validSymbolChars)
if !ok {
return nil, errors.New(`failed to parse A-instruction: literal contains illegal character. A user-defined symbol can be any sequence of letters, digits, underscore ( _ ),
dot (.), dollar sign ($), and colon (:) that does not begin with a digit`)
}
return &aInstruction{Literal: in, IsSymbol: true}, nil
}
// validSymbolChars ensures that user-defined symbol can only be any sequence of letters, digits,
// underscore ( _ ), dot (.), dollar sign ($), and colon (:).
func validSymbolChars(r rune) bool {
return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '.' || r == '$' || r == ':'
}
// containsOnly returns true if every rune in s satisfies given f.
func containsOnly(s string, f func(rune) bool) bool {
for _, r := range s {
ok := f(r)
if !ok {
return false
}
}
return true
}
func parseCInstruction(in string) (*cInstruction, error) {
var dest, comp, jump string
dest, rest, foundEquals := strings.Cut(in, "=")
if !foundEquals {
// this is to accommodate for Cut behavior
dest = ""
rest = in
}
comp, jump, foundSemicolon := strings.Cut(rest, ";")
if !foundEquals && !foundSemicolon {
// TODO this is illegal; add a test and implement
}
if !foundSemicolon {
}
return &cInstruction{
Dest: strings.TrimSpace(dest),
Comp: strings.TrimSpace(comp),
Jump: strings.TrimSpace(jump),
}, nil
}
func parseLabel(in string) (*label, error) {
if len(in) < 3 {
return nil, errors.New("failed to parse label: label definitions need to define symbols enclosed in ().")
}
if in[0] != '(' {
return nil, errors.New("failed to parse label: label definitions need to be enclosed in (). Missing leading (")
}
if in[len(in)-1] != ')' {
return nil, errors.New("failed to parse label: label definitions need to be enclosed in (). Missing closing )")
}
in = strings.Trim(in, "()")
ok := containsOnly(in, validSymbolChars)
if !ok {
return nil, errors.New(`failed to parse A-instruction: literal contains illegal character. A user-defined symbol can be any sequence of letters, digits, underscore ( _ ),
dot (.), dollar sign ($), and colon (:) that does not begin with a digit`)
}
return &label{Literal: in}, nil
}
var predefinedSymbols map[string]uint16 = map[string]uint16{
"SP": 0,
"LCL": 1,
"ARG": 2,
"THIS": 3,
"THAT": 4,
"R0": 0,
"R1": 1,
"R2": 2,
"R3": 3,
"R4": 4,
"R5": 5,
"R6": 6,
"R7": 7,
"R8": 8,
"R9": 9,
"R10": 10,
"R11": 11,
"R12": 12,
"R13": 13,
"R14": 14,
"R15": 15,
"SCREEN": 16384,
"KBD": 24576,
}
// code translates instructions into machine code. Labels do not result in an instruction in machine
// code. Symbolic references in A-instructions are resolved into memory addresses at this stage.
func code(instructions []instruction, w io.Writer) error {
var nextVariableAddress uint16 = 16
var pc uint16
symbolTable := make(map[string]uint16)
for _, instruction := range instructions {
switch v := instruction.(type) {
case *label:
if _, ok := symbolTable[v.Literal]; ok {
return fmt.Errorf("failed to encode label %q: label re-declared", v.Literal)
}
if _, ok := predefinedSymbols[v.Literal]; ok {
return fmt.Errorf("failed to encode label: %q is a pre-defined symbol which cannot be used as a label", v.Literal)
}
symbolTable[v.Literal] = pc
default:
pc++
}
}
for k, v := range predefinedSymbols {
symbolTable[k] = v
}
for _, instruction := range instructions {
switch ins := instruction.(type) {
case *aInstruction:
ains := ins
if ins.IsSymbol {
v, ok := symbolTable[ins.Literal]
if !ok {
v = nextVariableAddress
symbolTable[ins.Literal] = v
nextVariableAddress++
}
ains = &aInstruction{Value: v}
}
n, err := fmt.Fprintf(w, "%016b\n", codeAInstruction(ains))
if n != 17 {
return fmt.Errorf("failed to write entire a-instruction %v: wrote %d instead of 17 bytes/chars", ins, n)
}
if err != nil {
return fmt.Errorf("failed to write a-instruction %v: %v", ins, err)
}
case *cInstruction:
code, err := codeCInstruction(ins)
if err != nil {
return fmt.Errorf("failed to encode c-instruction %v: %v", ins, err)
}
n, err := fmt.Fprintf(w, "%s\n", code)
if n != 17 {
return fmt.Errorf("failed to write entire c-instruction %v: wrote %d instead of 16 bytes/chars", ins, n)
}
if err != nil {
return fmt.Errorf("failed to write c-instruction %v: %v", ins, err)
}
}
}
return nil
}
func codeAInstruction(instruction *aInstruction) uint16 {
return instruction.Value
}
var compToA map[string]string = map[string]string{
"0": "0",
"1": "0",
"-1": "0",
"D": "0",
"A": "0",
"!D": "0",
"!A": "0",
"-D": "0",
"-A": "0",
"D+1": "0",
"A+1": "0",
"D-1": "0",
"A-1": "0",
"D+A": "0",
"D-A": "0",
"A-D": "0",
"D&A": "0",
"D|A": "0",
"M": "1",
"!M": "1",
"-M": "1",
"M+1": "1",
"M-1": "1",
"D+M": "1",
"D-M": "1",
"M-D": "1",
"D&M": "1",
"D|M": "1",
}
var compToC map[string]string = map[string]string{
"0": "101010",
"1": "111111",
"-1": "111010",
"D": "001100",
"A": "110000",
"!D": "001101",
"!A": "110001",
"-D": "001111",
"-A": "110011",
"D+1": "011111",
"A+1": "110111",
"D-1": "001110",
"A-1": "110010",
"D+A": "000010",
"D-A": "010011",
"A-D": "000111",
"D&A": "000000",
"D|A": "010101",
"M": "110000",
"!M": "110001",
"-M": "110011",
"M+1": "110111",
"M-1": "110010",
"D+M": "000010",
"D-M": "010011",
"M-D": "000111",
"D&M": "000000",
"D|M": "010101",
}
var destToD map[string]string = map[string]string{
"M": "001",
"D": "010",
"MD": "011",
"A": "100",
"AM": "101",
"AD": "110",
"AMD": "111",
}
var jumpToJ map[string]string = map[string]string{
"JGT": "001",
"JEQ": "010",
"JGE": "011",
"JLT": "100",
"JNE": "101",
"JLE": "110",
"JMP": "111",
}
func codeCInstruction(instruction *cInstruction) ([]byte, error) {
aBit, ok := compToA[instruction.Comp]
if !ok {
return nil, fmt.Errorf("failed to encode a-bit from comp field %q", instruction.Comp)
}
cBits, ok := compToC[instruction.Comp]
if !ok {
return nil, fmt.Errorf("failed to encode c-bits from comp field %q", instruction.Comp)
}
dBits := "000"
if instruction.Dest != "" {
dBits, ok = destToD[instruction.Dest]
if !ok {
return nil, fmt.Errorf("failed to encode d-bits from dest field %q", instruction.Dest)
}
}
jBits := "000"
if instruction.Jump != "" {
jBits, ok = jumpToJ[instruction.Jump]
if !ok {
return nil, fmt.Errorf("failed to encode j-bits from jump field %q", instruction.Jump)
}
}
return []byte("111" + aBit + cBits + dBits + jBits), nil
}