-
Notifications
You must be signed in to change notification settings - Fork 10
/
instructions.go
66 lines (57 loc) · 1.8 KB
/
instructions.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
// Package instructions contains the comment instruction-definitions
// for the instructions that we understand.
//
// These are abstracted here, so that you only don't need to touch
// the parser/lexer to add new instructions.
//
// Just add the instructions here, and update the compiler to emit the
// appropriate code.
package instructions
var (
// InstructionLengths is a map that returns the number of operands
// the given assembly-language operation will accept.
//
// For example a `nop` argument requires zero arguments so the
// entry for that will be `0`.
InstructionLengths map[string]int
// Instructions is automatically generated from the InstructionLengths
// map, and contains the known instruction-types we can lex, parse, and
// compile.
Instructions []string
)
func init() {
// Setup our instruction-lengths
InstructionLengths = make(map[string]int)
InstructionLengths["add"] = 2
InstructionLengths["cmp"] = 2
InstructionLengths["dec"] = 1
InstructionLengths["inc"] = 1
InstructionLengths["int"] = 1
InstructionLengths["mov"] = 2
InstructionLengths["nop"] = 0
InstructionLengths["pop"] = 1
InstructionLengths["push"] = 1
InstructionLengths["ret"] = 0
InstructionLengths["sub"] = 2
InstructionLengths["xor"] = 2
// call
InstructionLengths["call"] = 1
// jump
InstructionLengths["je"] = 1
InstructionLengths["jmp"] = 1
InstructionLengths["jne"] = 1
InstructionLengths["jnz"] = 1
InstructionLengths["jz"] = 1
// Processor control instructions
InstructionLengths["clc"] = 0
InstructionLengths["cld"] = 0
InstructionLengths["cli"] = 0
InstructionLengths["cmc"] = 0
InstructionLengths["stc"] = 0
InstructionLengths["std"] = 0
InstructionLengths["sti"] = 0
// Now record the known-instructions
for k := range InstructionLengths {
Instructions = append(Instructions, k)
}
}