commit 0053a177b91ca6804dc08e416d9b319be703bef6
Author: bsandro <email@bsandro.tech>
Date: Sun, 3 Jul 2022 19:48:44 +0300
initial commit: basic data structures and stringify methods
Diffstat:
7 files changed, 129 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+umx_asm
diff --git a/asm/instruction.go b/asm/instruction.go
@@ -0,0 +1,41 @@
+package asm
+
+import (
+ "bytes"
+)
+
+type Instruction struct {
+ Opcode Opcode
+ RegA, RegB, RegC Register
+ Value uint32
+}
+
+func (i *Instruction) String() string {
+ var buf bytes.Buffer
+ buf.WriteString(i.Opcode.String())
+ buf.WriteString(" ")
+
+ switch i.Opcode {
+ case CMOV, ARRI, ARRA, ADD, MUL, DIV, NOTA:
+ dumpRegisters(&buf, i.RegA, i.RegB, i.RegC)
+ case HALT:
+ // no operands
+ case ALLO:
+ dumpRegisters(&buf, i.RegB, i.RegC)
+ case ABAN, OUTP, INP:
+ dumpRegisters(&buf, i.RegC)
+ case LOAD:
+ dumpRegisters(&buf, i.RegB, i.RegC)
+ case ORTH:
+ dumpRegisters(&buf, i.RegA)
+ }
+
+ return buf.String()
+}
+
+func dumpRegisters(buf *bytes.Buffer, regs ...Register) {
+ for _, reg := range regs {
+ buf.WriteString(reg.String())
+ buf.WriteString(" ")
+ }
+}
diff --git a/asm/opcode.go b/asm/opcode.go
@@ -0,0 +1,21 @@
+package asm
+
+//go:generate go run golang.org/x/tools/cmd/stringer@latest -type=Opcode
+type Opcode uint8
+
+const (
+ CMOV Opcode = iota
+ ARRI
+ ARRA
+ ADD
+ MUL
+ DIV
+ NOTA
+ HALT
+ ALLO
+ ABAN
+ OUTP
+ INP
+ LOAD
+ ORTH
+)
diff --git a/asm/opcode_string.go b/asm/opcode_string.go
@@ -0,0 +1,36 @@
+// Code generated by "stringer -type=Opcode"; DO NOT EDIT.
+
+package asm
+
+import "strconv"
+
+func _() {
+ // An "invalid array index" compiler error signifies that the constant values have changed.
+ // Re-run the stringer command to generate them again.
+ var x [1]struct{}
+ _ = x[CMOV-0]
+ _ = x[ARRI-1]
+ _ = x[ARRA-2]
+ _ = x[ADD-3]
+ _ = x[MUL-4]
+ _ = x[DIV-5]
+ _ = x[NOTA-6]
+ _ = x[HALT-7]
+ _ = x[ALLO-8]
+ _ = x[ABAN-9]
+ _ = x[OUTP-10]
+ _ = x[INP-11]
+ _ = x[LOAD-12]
+ _ = x[ORTH-13]
+}
+
+const _Opcode_name = "CMOVARRIARRAADDMULDIVNOTAHALTALLOABANOUTPINPLOADORTH"
+
+var _Opcode_index = [...]uint8{0, 4, 8, 12, 15, 18, 21, 25, 29, 33, 37, 41, 44, 48, 52}
+
+func (i Opcode) String() string {
+ if i >= Opcode(len(_Opcode_index)-1) {
+ return "Opcode(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _Opcode_name[_Opcode_index[i]:_Opcode_index[i+1]]
+}
diff --git a/asm/register.go b/asm/register.go
@@ -0,0 +1,11 @@
+package asm
+
+import (
+ "strconv"
+)
+
+type Register uint8
+
+func (r Register) String() string {
+ return strconv.Itoa(int(r))
+}
diff --git a/go.mod b/go.mod
@@ -0,0 +1,3 @@
+module umx_asm
+
+go 1.18
diff --git a/main.go b/main.go
@@ -0,0 +1,16 @@
+package main
+
+import (
+ "umx_asm/asm"
+ "fmt"
+)
+
+func main() {
+ fmt.Println("umx_asm")
+ instruction := &asm.Instruction{
+ asm.CMOV,
+ 10, 20, 30,
+ 500,
+ }
+ fmt.Println(instruction.String())
+}