Reorganized file structure
This commit is contained in:
@ -1,4 +0,0 @@
|
||||
package asm
|
||||
|
||||
// Address represents a memory address.
|
||||
type Address = uint32
|
@ -1,93 +0,0 @@
|
||||
package asm
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/arch/x64"
|
||||
"git.akyoto.dev/cli/q/src/config"
|
||||
"git.akyoto.dev/cli/q/src/log"
|
||||
"git.akyoto.dev/cli/q/src/register"
|
||||
)
|
||||
|
||||
// Assembler contains a list of instructions.
|
||||
type Assembler struct {
|
||||
Instructions []Instruction
|
||||
}
|
||||
|
||||
// New creates a new assembler.
|
||||
func New() *Assembler {
|
||||
return &Assembler{
|
||||
Instructions: make([]Instruction, 0, 8),
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize generates the final machine code.
|
||||
func (a *Assembler) Finalize() ([]byte, []byte) {
|
||||
code := make([]byte, 0, len(a.Instructions)*8)
|
||||
data := make(Data, 0, 16)
|
||||
pointers := []Pointer{}
|
||||
|
||||
for _, x := range a.Instructions {
|
||||
switch x.Mnemonic {
|
||||
case MOV:
|
||||
code = x64.MoveRegNum32(code, uint8(x.Destination), uint32(x.Number))
|
||||
|
||||
case MOVDATA:
|
||||
code = x64.MoveRegNum32(code, uint8(x.Destination), 0)
|
||||
|
||||
pointers = append(pointers, Pointer{
|
||||
Position: Address(len(code) - 4),
|
||||
Address: data.Add(x.Data),
|
||||
})
|
||||
|
||||
case SYSCALL:
|
||||
code = x64.Syscall(code)
|
||||
}
|
||||
}
|
||||
|
||||
if config.Verbose {
|
||||
for _, x := range a.Instructions {
|
||||
log.Info.Println(x.String())
|
||||
}
|
||||
}
|
||||
|
||||
dataStart := config.BaseAddress + config.CodeOffset + Address(len(code))
|
||||
|
||||
for _, pointer := range pointers {
|
||||
slice := code[pointer.Position : pointer.Position+4]
|
||||
address := dataStart + pointer.Address
|
||||
binary.LittleEndian.PutUint32(slice, address)
|
||||
}
|
||||
|
||||
return code, data
|
||||
}
|
||||
|
||||
// Merge combines the contents of this assembler with another one.
|
||||
func (a *Assembler) Merge(b *Assembler) {
|
||||
a.Instructions = append(a.Instructions, b.Instructions...)
|
||||
}
|
||||
|
||||
// MoveRegisterData moves a data section address into the given register.
|
||||
func (a *Assembler) MoveRegisterData(reg register.ID, data []byte) {
|
||||
a.Instructions = append(a.Instructions, Instruction{
|
||||
Mnemonic: MOVDATA,
|
||||
Destination: reg,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// MoveRegisterNumber moves a number into the given register.
|
||||
func (a *Assembler) MoveRegisterNumber(reg register.ID, number uint64) {
|
||||
a.Instructions = append(a.Instructions, Instruction{
|
||||
Mnemonic: MOV,
|
||||
Destination: reg,
|
||||
Number: number,
|
||||
})
|
||||
}
|
||||
|
||||
// Syscall executes a kernel function.
|
||||
func (a *Assembler) Syscall() {
|
||||
a.Instructions = append(a.Instructions, Instruction{
|
||||
Mnemonic: SYSCALL,
|
||||
})
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package asm_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/arch/x64"
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/os/linux"
|
||||
"git.akyoto.dev/go/assert"
|
||||
)
|
||||
|
||||
func TestHello(t *testing.T) {
|
||||
a := asm.New()
|
||||
|
||||
hello := []byte("Hello\n")
|
||||
a.MoveRegisterNumber(x64.SyscallNumber, linux.Write)
|
||||
a.MoveRegisterNumber(x64.SyscallArgs[0], 1)
|
||||
a.MoveRegisterData(x64.SyscallArgs[1], hello)
|
||||
a.MoveRegisterNumber(x64.SyscallArgs[2], uint64(len(hello)))
|
||||
a.Syscall()
|
||||
|
||||
a.MoveRegisterNumber(x64.SyscallNumber, linux.Exit)
|
||||
a.MoveRegisterNumber(x64.SyscallArgs[0], 0)
|
||||
a.Syscall()
|
||||
|
||||
code, data := a.Finalize()
|
||||
|
||||
assert.DeepEqual(t, code, []byte{
|
||||
0xb8, 0x01, 0x00, 0x00, 0x00,
|
||||
0xbf, 0x01, 0x00, 0x00, 0x00,
|
||||
0xbe, 0xa2, 0x00, 0x40, 0x00,
|
||||
0xba, 0x06, 0x00, 0x00, 0x00,
|
||||
0x0f, 0x05,
|
||||
|
||||
0xb8, 0x3c, 0x00, 0x00, 0x00,
|
||||
0xbf, 0x00, 0x00, 0x00, 0x00,
|
||||
0x0f, 0x05,
|
||||
})
|
||||
|
||||
assert.DeepEqual(t, data, hello)
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package asm
|
||||
|
||||
import "bytes"
|
||||
|
||||
// Data represents the static read-only data.
|
||||
type Data []byte
|
||||
|
||||
// Add adds the given bytes to the data block if this sequence of bytes doesn't exist yet.
|
||||
// It returns the address relative to the start of the data section.
|
||||
func (data *Data) Add(block []byte) Address {
|
||||
position := bytes.Index(*data, block)
|
||||
|
||||
if position != -1 {
|
||||
return Address(position)
|
||||
}
|
||||
|
||||
address := Address(len(*data))
|
||||
*data = append(*data, block...)
|
||||
return address
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package asm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/register"
|
||||
)
|
||||
|
||||
// Instruction represents a single instruction which can be converted to machine code.
|
||||
type Instruction struct {
|
||||
Mnemonic Mnemonic
|
||||
Source register.ID
|
||||
Destination register.ID
|
||||
Number uint64
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// String returns the assembler representation of the instruction.
|
||||
func (x *Instruction) String() string {
|
||||
switch x.Mnemonic {
|
||||
case MOV:
|
||||
return fmt.Sprintf("%s %s, %x", x.Mnemonic, x.Destination, x.Number)
|
||||
case MOVDATA:
|
||||
return fmt.Sprintf("%s %s, %v", x.Mnemonic, x.Destination, x.Data)
|
||||
case SYSCALL:
|
||||
return x.Mnemonic.String()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package asm
|
||||
|
||||
type Mnemonic uint8
|
||||
|
||||
const (
|
||||
NONE Mnemonic = iota
|
||||
MOV
|
||||
MOVDATA
|
||||
SYSCALL
|
||||
)
|
||||
|
||||
func (m Mnemonic) String() string {
|
||||
switch m {
|
||||
case MOV:
|
||||
return "mov"
|
||||
|
||||
case MOVDATA:
|
||||
return "mov"
|
||||
|
||||
case SYSCALL:
|
||||
return "syscall"
|
||||
}
|
||||
|
||||
return "NONE"
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package asm
|
||||
|
||||
// Pointer stores a relative memory address that we can later turn into an absolute one.
|
||||
// Position: The machine code offset where the address was inserted.
|
||||
// Address: The offset inside the section.
|
||||
type Pointer struct {
|
||||
Position uint32
|
||||
Address uint32
|
||||
}
|
Reference in New Issue
Block a user