Implemented multiplication
This commit is contained in:
parent
625e8d5e2e
commit
34af20d7a0
@ -11,7 +11,8 @@ hello() {
|
|||||||
address += 4194304
|
address += 4194304
|
||||||
address += 1
|
address += 1
|
||||||
length += 5
|
length += 5
|
||||||
length -= 2
|
length *= 2
|
||||||
|
length -= 7
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
syscall(write, stdout, address, length)
|
syscall(write, stdout, address, length)
|
||||||
|
@ -29,8 +29,8 @@ func Finalize(functions map[string]*Function) ([]byte, []byte) {
|
|||||||
func entry() *asm.Assembler {
|
func entry() *asm.Assembler {
|
||||||
entry := asm.New()
|
entry := asm.New()
|
||||||
entry.Call("main")
|
entry.Call("main")
|
||||||
entry.MoveRegisterNumber(x64.SyscallRegisters[0], linux.Exit)
|
entry.RegisterNumber(asm.MOVE, x64.SyscallRegisters[0], linux.Exit)
|
||||||
entry.MoveRegisterNumber(x64.SyscallRegisters[1], 0)
|
entry.RegisterNumber(asm.MOVE, x64.SyscallRegisters[1], 0)
|
||||||
entry.Syscall()
|
entry.Syscall()
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
@ -120,11 +120,15 @@ func (f *Function) CompileInstruction(line token.List) error {
|
|||||||
|
|
||||||
switch expr.Token.Text() {
|
switch expr.Token.Text() {
|
||||||
case "+=":
|
case "+=":
|
||||||
f.Assembler.AddRegisterNumber(register, number)
|
f.Assembler.RegisterNumber(asm.ADD, register, number)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case "-=":
|
case "-=":
|
||||||
f.Assembler.SubRegisterNumber(register, number)
|
f.Assembler.RegisterNumber(asm.SUB, register, number)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case "*=":
|
||||||
|
f.Assembler.RegisterNumber(asm.MUL, register, number)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,7 +168,7 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Assembler.MoveRegisterNumber(register, n)
|
f.Assembler.RegisterNumber(asm.MOVE, register, n)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case token.String:
|
case token.String:
|
||||||
|
@ -14,22 +14,23 @@ func TestAddRegisterNumber(t *testing.T) {
|
|||||||
Number int
|
Number int
|
||||||
Code []byte
|
Code []byte
|
||||||
}{
|
}{
|
||||||
{x64.RAX, 1, []byte{0x48, 0x83, 0xc0, 0x01}},
|
{x64.RAX, 1, []byte{0x48, 0x83, 0xC0, 0x01}},
|
||||||
{x64.RCX, 1, []byte{0x48, 0x83, 0xc1, 0x01}},
|
{x64.RCX, 1, []byte{0x48, 0x83, 0xC1, 0x01}},
|
||||||
{x64.RDX, 1, []byte{0x48, 0x83, 0xc2, 0x01}},
|
{x64.RDX, 1, []byte{0x48, 0x83, 0xC2, 0x01}},
|
||||||
{x64.RBX, 1, []byte{0x48, 0x83, 0xc3, 0x01}},
|
{x64.RBX, 1, []byte{0x48, 0x83, 0xC3, 0x01}},
|
||||||
{x64.RSP, 1, []byte{0x48, 0x83, 0xc4, 0x01}},
|
{x64.RSP, 1, []byte{0x48, 0x83, 0xC4, 0x01}},
|
||||||
{x64.RBP, 1, []byte{0x48, 0x83, 0xc5, 0x01}},
|
{x64.RBP, 1, []byte{0x48, 0x83, 0xC5, 0x01}},
|
||||||
{x64.RSI, 1, []byte{0x48, 0x83, 0xc6, 0x01}},
|
{x64.RSI, 1, []byte{0x48, 0x83, 0xC6, 0x01}},
|
||||||
{x64.RDI, 1, []byte{0x48, 0x83, 0xc7, 0x01}},
|
{x64.RDI, 1, []byte{0x48, 0x83, 0xC7, 0x01}},
|
||||||
{x64.R8, 1, []byte{0x49, 0x83, 0xc0, 0x01}},
|
{x64.R8, 1, []byte{0x49, 0x83, 0xC0, 0x01}},
|
||||||
{x64.R9, 1, []byte{0x49, 0x83, 0xc1, 0x01}},
|
{x64.R9, 1, []byte{0x49, 0x83, 0xC1, 0x01}},
|
||||||
{x64.R10, 1, []byte{0x49, 0x83, 0xc2, 0x01}},
|
{x64.R10, 1, []byte{0x49, 0x83, 0xC2, 0x01}},
|
||||||
{x64.R11, 1, []byte{0x49, 0x83, 0xc3, 0x01}},
|
{x64.R11, 1, []byte{0x49, 0x83, 0xC3, 0x01}},
|
||||||
{x64.R12, 1, []byte{0x49, 0x83, 0xc4, 0x01}},
|
{x64.R12, 1, []byte{0x49, 0x83, 0xC4, 0x01}},
|
||||||
{x64.R13, 1, []byte{0x49, 0x83, 0xc5, 0x01}},
|
{x64.R13, 1, []byte{0x49, 0x83, 0xC5, 0x01}},
|
||||||
{x64.R14, 1, []byte{0x49, 0x83, 0xc6, 0x01}},
|
{x64.R14, 1, []byte{0x49, 0x83, 0xC6, 0x01}},
|
||||||
{x64.R15, 1, []byte{0x49, 0x83, 0xc7, 0x01}},
|
{x64.R15, 1, []byte{0x49, 0x83, 0xC7, 0x01}},
|
||||||
|
|
||||||
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}},
|
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC1, 0xFF, 0xFF, 0xFF, 0x7F}},
|
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC1, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC2, 0xFF, 0xFF, 0xFF, 0x7F}},
|
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC2, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
8
src/build/arch/x64/Mul.go
Normal file
8
src/build/arch/x64/Mul.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package x64
|
||||||
|
|
||||||
|
import "git.akyoto.dev/cli/q/src/build/cpu"
|
||||||
|
|
||||||
|
// MulRegNum multiplies a register with a number.
|
||||||
|
func MulRegNum(code []byte, destination cpu.Register, number int) []byte {
|
||||||
|
return numRegReg(code, 0x6B, 0x69, byte(destination), byte(destination), number)
|
||||||
|
}
|
57
src/build/arch/x64/Mul_test.go
Normal file
57
src/build/arch/x64/Mul_test.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package x64_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.akyoto.dev/cli/q/src/build/arch/x64"
|
||||||
|
"git.akyoto.dev/cli/q/src/build/cpu"
|
||||||
|
"git.akyoto.dev/go/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMulRegisterNumber(t *testing.T) {
|
||||||
|
usagePatterns := []struct {
|
||||||
|
Register cpu.Register
|
||||||
|
Number int
|
||||||
|
Code []byte
|
||||||
|
}{
|
||||||
|
{x64.RAX, 1, []byte{0x48, 0x6B, 0xC0, 0x01}},
|
||||||
|
{x64.RCX, 1, []byte{0x48, 0x6B, 0xC9, 0x01}},
|
||||||
|
{x64.RDX, 1, []byte{0x48, 0x6B, 0xD2, 0x01}},
|
||||||
|
{x64.RBX, 1, []byte{0x48, 0x6B, 0xDB, 0x01}},
|
||||||
|
{x64.RSP, 1, []byte{0x48, 0x6B, 0xE4, 0x01}},
|
||||||
|
{x64.RBP, 1, []byte{0x48, 0x6B, 0xED, 0x01}},
|
||||||
|
{x64.RSI, 1, []byte{0x48, 0x6B, 0xF6, 0x01}},
|
||||||
|
{x64.RDI, 1, []byte{0x48, 0x6B, 0xFF, 0x01}},
|
||||||
|
{x64.R8, 1, []byte{0x4D, 0x6B, 0xC0, 0x01}},
|
||||||
|
{x64.R9, 1, []byte{0x4D, 0x6B, 0xC9, 0x01}},
|
||||||
|
{x64.R10, 1, []byte{0x4D, 0x6B, 0xD2, 0x01}},
|
||||||
|
{x64.R11, 1, []byte{0x4D, 0x6B, 0xDB, 0x01}},
|
||||||
|
{x64.R12, 1, []byte{0x4D, 0x6B, 0xE4, 0x01}},
|
||||||
|
{x64.R13, 1, []byte{0x4D, 0x6B, 0xED, 0x01}},
|
||||||
|
{x64.R14, 1, []byte{0x4D, 0x6B, 0xF6, 0x01}},
|
||||||
|
{x64.R15, 1, []byte{0x4D, 0x6B, 0xFF, 0x01}},
|
||||||
|
|
||||||
|
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xC9, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xD2, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.RBX, 0x7FFFFFFF, []byte{0x48, 0x69, 0xDB, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.RSP, 0x7FFFFFFF, []byte{0x48, 0x69, 0xE4, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.RBP, 0x7FFFFFFF, []byte{0x48, 0x69, 0xED, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.RSI, 0x7FFFFFFF, []byte{0x48, 0x69, 0xF6, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.RDI, 0x7FFFFFFF, []byte{0x48, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R8, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R9, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xC9, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R10, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xD2, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R11, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xDB, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R12, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xE4, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R13, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xED, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R14, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xF6, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
{x64.R15, 0x7FFFFFFF, []byte{0x4D, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pattern := range usagePatterns {
|
||||||
|
t.Logf("mul %s, %x", pattern.Register, pattern.Number)
|
||||||
|
code := x64.MulRegNum(nil, pattern.Register, pattern.Number)
|
||||||
|
assert.DeepEqual(t, code, pattern.Code)
|
||||||
|
}
|
||||||
|
}
|
@ -30,6 +30,7 @@ func TestSubRegisterNumber(t *testing.T) {
|
|||||||
{x64.R13, 1, []byte{0x49, 0x83, 0xED, 0x01}},
|
{x64.R13, 1, []byte{0x49, 0x83, 0xED, 0x01}},
|
||||||
{x64.R14, 1, []byte{0x49, 0x83, 0xEE, 0x01}},
|
{x64.R14, 1, []byte{0x49, 0x83, 0xEE, 0x01}},
|
||||||
{x64.R15, 1, []byte{0x49, 0x83, 0xEF, 0x01}},
|
{x64.R15, 1, []byte{0x49, 0x83, 0xEF, 0x01}},
|
||||||
|
|
||||||
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE8, 0xFF, 0xFF, 0xFF, 0x7F}},
|
{x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE8, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE9, 0xFF, 0xFF, 0xFF, 0x7F}},
|
{x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE9, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEA, 0xFF, 0xFF, 0xFF, 0x7F}},
|
{x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEA, 0xFF, 0xFF, 0xFF, 0x7F}},
|
||||||
|
@ -39,6 +39,12 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
|
|||||||
code = x64.SubRegNum(code, operands.Register, operands.Number)
|
code = x64.SubRegNum(code, operands.Register, operands.Number)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case MUL:
|
||||||
|
switch operands := x.Data.(type) {
|
||||||
|
case *RegisterNumber:
|
||||||
|
code = x64.MulRegNum(code, operands.Register, operands.Number)
|
||||||
|
}
|
||||||
|
|
||||||
case CALL:
|
case CALL:
|
||||||
code = x64.Call(code, 0x00_00_00_00)
|
code = x64.Call(code, 0x00_00_00_00)
|
||||||
size := 4
|
size := 4
|
||||||
|
@ -2,32 +2,10 @@ package asm
|
|||||||
|
|
||||||
import "git.akyoto.dev/cli/q/src/build/cpu"
|
import "git.akyoto.dev/cli/q/src/build/cpu"
|
||||||
|
|
||||||
// AddRegisterNumber adds a number to the given register.
|
// RegisterNumber adds an instruction with a register and a number.
|
||||||
func (a *Assembler) AddRegisterNumber(reg cpu.Register, number int) {
|
func (a *Assembler) RegisterNumber(mnemonic Mnemonic, reg cpu.Register, number int) {
|
||||||
a.Instructions = append(a.Instructions, Instruction{
|
a.Instructions = append(a.Instructions, Instruction{
|
||||||
Mnemonic: ADD,
|
Mnemonic: mnemonic,
|
||||||
Data: &RegisterNumber{
|
|
||||||
Register: reg,
|
|
||||||
Number: number,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// SubRegisterNumber subtracts a number from the given register.
|
|
||||||
func (a *Assembler) SubRegisterNumber(reg cpu.Register, number int) {
|
|
||||||
a.Instructions = append(a.Instructions, Instruction{
|
|
||||||
Mnemonic: SUB,
|
|
||||||
Data: &RegisterNumber{
|
|
||||||
Register: reg,
|
|
||||||
Number: number,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// MoveRegisterNumber moves a number into the given register.
|
|
||||||
func (a *Assembler) MoveRegisterNumber(reg cpu.Register, number int) {
|
|
||||||
a.Instructions = append(a.Instructions, Instruction{
|
|
||||||
Mnemonic: MOVE,
|
|
||||||
Data: &RegisterNumber{
|
Data: &RegisterNumber{
|
||||||
Register: reg,
|
Register: reg,
|
||||||
Number: number,
|
Number: number,
|
||||||
|
@ -7,6 +7,7 @@ const (
|
|||||||
ADD
|
ADD
|
||||||
CALL
|
CALL
|
||||||
JUMP
|
JUMP
|
||||||
|
MUL
|
||||||
LABEL
|
LABEL
|
||||||
MOVE
|
MOVE
|
||||||
RETURN
|
RETURN
|
||||||
@ -32,6 +33,9 @@ func (m Mnemonic) String() string {
|
|||||||
case MOVE:
|
case MOVE:
|
||||||
return "move"
|
return "move"
|
||||||
|
|
||||||
|
case MUL:
|
||||||
|
return "mul"
|
||||||
|
|
||||||
case RETURN:
|
case RETURN:
|
||||||
return "return"
|
return "return"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user