commit 4fe7cf18165d9cd7488f4b10bae3a9d67c6721cc
parent 29f1b5fc9aba4a9bba4e7e231bc85da28690d460
Author: Gabriel G. de Brito <gabrielgbrito@icloud.com>
Date: Sat, 30 Dec 2023 13:13:11 -0300
Documented assembler and machine packages
Diffstat:
2 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/assembler/assembler.go b/assembler/assembler.go
@@ -21,7 +21,8 @@ type Token struct {
Value string
}
-// Can be instruction or literal. Only instructions have Args.
+// Token with resolved labels and arguments (only instructions have arguments).
+// Can only be instruction or literal.
type ResolvedToken struct {
Type uint8
Size uint64
@@ -29,7 +30,8 @@ type ResolvedToken struct {
Args []uint64
}
-// Only instructions.
+// Token only for instructions. An array of DebuggerToken is passed to the
+// debugger.
type DebuggerToken struct {
Instruction string
Args string
@@ -37,6 +39,7 @@ type DebuggerToken struct {
Label string
}
+// Creates an array of DebuggerToken when instructions have always a fixed size.
func CreateDebugTokensFixedSize(tokens []Token, size uint64) []DebuggerToken {
var dt []DebuggerToken
addr := uint64(0)
@@ -75,7 +78,11 @@ func CreateDebugTokensFixedSize(tokens []Token, size uint64) []DebuggerToken {
type ArgTranslateFunction func(string) (uint64, error)
// Resolves labels and arguments for fixed-size instructions with size bytes.
-// ArgTranslateFunction translates arguments into numbers.
+// ArgTranslateFunction translates arguments into numbers. The arguments are
+// translating first resolving the labels, and them, if the argument is not a
+// label, calling ArgTranslateFunction with it. Labels are always changed by
+// their address, so the architeture-side should change it to an offset when
+// needed.
func ResolveTokensFixedSize(tokens []Token, size uint64, arg ArgTranslateFunction) ([]ResolvedToken, error) {
labels := make(map[string]uint64)
addr := uint64(0)
diff --git a/machine/machine.go b/machine/machine.go
@@ -9,19 +9,48 @@ import "github.com/gboncoffee/egg/assembler"
// need to be stored on the heap. Converting from any size less than and to 64
// bits can be done with a single x86 instruction.
type Machine interface {
+ // Generic function for loading the program into memory and initializing
+ // anything the machine may need. After this, the machine should be
+ // ready to receive sequences of NextInstruction().
LoadProgram([]uint8) error
+ // Executes the next instruction. The execution should be handled in a
+ // way that the instruction is executed and only them the instruction
+ // pointer is incremented, thus the pointer always points to the
+ // instruction to be performed next.
NextInstruction() (*Call, error)
+ // Retrieves a single byte from memory.
GetMemory(uint64) (uint8, error)
+ // Sets a single byte of memory.
SetMemory(uint64, uint8) error
- // Address than size.
+ // Retrieves multiple bytes from memory. First argument is the address
+ // and second argument is the size. Should return an error if
+ // address+size is not addressable.
GetMemoryChunk(uint64, uint64) ([]uint8, error)
+ // Same as GetMemoryChunk but for setting.
SetMemoryChunk(uint64, []uint8) error
+ // Gets the content of a register. When the ISA has registers with
+ // ambiguous numbers, the backend should define unambiguous numbers for
+ // them and only translate these numbers when needed on the instruction
+ // execution. Example: a RISC-V backend with the floating-point
+ // extension may define the floating-point registers as registers 32-63.
GetRegister(uint64) (uint64, error)
- // Address than content.
+ // Same as GetRegister but for setting. First argument is the register
+ // number, and second is the content. This may fail silently if trying
+ // to write to a forbidden register (as with x0 in RISC-V), so the
+ // caller should not expected it to work always.
SetRegister(uint64, uint64) error
+ // Translates a register name (as used in Assembly) to it's number.
+ // Registers may have more than a name (example: in the RISC-V backend,
+ // both "zero" and "x0" translates to the register number 0)
GetRegisterNumber(string) (uint64, error)
+ // Assembles a program. Usually a bunch of calls to functions on the
+ // egg/assembler package. If the second return value is nil, debugger
+ // support is disabled.
Assemble(string) ([]uint8, []assembler.DebuggerToken, error)
+ // Self-explanatory. Usually just a "return m.pc" or something like
+ // that.
GetCurrentInstructionAddress() uint64
+ // Self-explanatory. Usually just returns a string constant.
ArchitetureName() string
}
@@ -43,6 +72,7 @@ const (
SYS_WRITE = 3
)
+// Struct returned from NextInstruction() when a call is performed.
type Call struct {
Number uint64
Arg1 uint64