Implemented bitwise operations

This commit is contained in:
2024-07-25 14:17:51 +02:00
parent b8900b518a
commit d1a8f0d66c
12 changed files with 287 additions and 5 deletions

15
src/build/arch/x64/And.go Normal file
View File

@ -0,0 +1,15 @@
package x64
import (
"git.akyoto.dev/cli/q/src/build/cpu"
)
// AndRegisterNumber performs a bitwise AND using a register and a number.
func AndRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
return encodeNum(code, AddressDirect, 0b100, destination, number, 0x83, 0x81)
}
// AndRegisterRegister performs a bitwise AND using two registers.
func AndRegisterRegister(code []byte, destination cpu.Register, operand cpu.Register) []byte {
return encode(code, AddressDirect, operand, destination, 8, 0x21)
}

15
src/build/arch/x64/Or.go Normal file
View File

@ -0,0 +1,15 @@
package x64
import (
"git.akyoto.dev/cli/q/src/build/cpu"
)
// OrRegisterNumber performs a bitwise OR using a register and a number.
func OrRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
return encodeNum(code, AddressDirect, 0b001, destination, number, 0x83, 0x81)
}
// OrRegisterRegister performs a bitwise OR using two registers.
func OrRegisterRegister(code []byte, destination cpu.Register, operand cpu.Register) []byte {
return encode(code, AddressDirect, operand, destination, 8, 0x09)
}

15
src/build/arch/x64/Xor.go Normal file
View File

@ -0,0 +1,15 @@
package x64
import (
"git.akyoto.dev/cli/q/src/build/cpu"
)
// XorRegisterNumber performs a bitwise XOR using a register and a number.
func XorRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
return encodeNum(code, AddressDirect, 0b110, destination, number, 0x83, 0x81)
}
// XorRegisterRegister performs a bitwise XOR using two registers.
func XorRegisterRegister(code []byte, destination cpu.Register, operand cpu.Register) []byte {
return encode(code, AddressDirect, operand, destination, 8, 0x31)
}

View File

@ -26,6 +26,14 @@ func (a Assembler) Finalize() ([]byte, []byte) {
code = x64.AddRegisterRegister(code, operands.Destination, operands.Source)
}
case AND:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.AndRegisterNumber(code, operands.Register, operands.Number)
case *RegisterRegister:
code = x64.AndRegisterRegister(code, operands.Destination, operands.Source)
}
case SUB:
switch operands := x.Data.(type) {
case *RegisterNumber:
@ -153,6 +161,14 @@ func (a Assembler) Finalize() ([]byte, []byte) {
})
}
case OR:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.OrRegisterNumber(code, operands.Register, operands.Number)
case *RegisterRegister:
code = x64.OrRegisterRegister(code, operands.Destination, operands.Source)
}
case POP:
switch operands := x.Data.(type) {
case *Register:
@ -177,6 +193,14 @@ func (a Assembler) Finalize() ([]byte, []byte) {
case SYSCALL:
code = x64.Syscall(code)
case XOR:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.XorRegisterNumber(code, operands.Register, operands.Number)
case *RegisterRegister:
code = x64.XorRegisterRegister(code, operands.Destination, operands.Source)
}
default:
panic("unknown mnemonic: " + x.Mnemonic.String())
}

View File

@ -5,6 +5,7 @@ type Mnemonic uint8
const (
NONE Mnemonic = iota
ADD
AND
CALL
COMMENT
COMPARE
@ -21,12 +22,14 @@ const (
LOAD
MODULO
MOVE
OR
POP
PUSH
RETURN
STORE
SUB
SYSCALL
XOR
)
// String returns a human readable version.
@ -34,6 +37,8 @@ func (m Mnemonic) String() string {
switch m {
case ADD:
return "add"
case AND:
return "and"
case CALL:
return "call"
case COMMENT:
@ -66,6 +71,8 @@ func (m Mnemonic) String() string {
return "move"
case MUL:
return "mul"
case OR:
return "or"
case POP:
return "pop"
case PUSH:
@ -78,6 +85,8 @@ func (m Mnemonic) String() string {
return "store"
case SYSCALL:
return "syscall"
case XOR:
return "xor"
default:
return ""
}

View File

@ -25,6 +25,15 @@ func (f *Function) ExecuteRegisterNumber(operation token.Token, register cpu.Reg
case token.Mod, token.ModAssign:
f.RegisterNumber(asm.MODULO, register, number)
case token.And, token.AndAssign:
f.RegisterNumber(asm.AND, register, number)
case token.Or, token.OrAssign:
f.RegisterNumber(asm.OR, register, number)
case token.Xor, token.XorAssign:
f.RegisterNumber(asm.XOR, register, number)
case token.Equal, token.NotEqual, token.Less, token.LessEqual, token.Greater, token.GreaterEqual:
f.RegisterNumber(asm.COMPARE, register, number)

View File

@ -25,6 +25,15 @@ func (f *Function) ExecuteRegisterRegister(operation token.Token, destination cp
case token.Mod, token.ModAssign:
f.RegisterRegister(asm.MODULO, destination, source)
case token.And, token.AndAssign:
f.RegisterRegister(asm.AND, destination, source)
case token.Or, token.OrAssign:
f.RegisterRegister(asm.OR, destination, source)
case token.Xor, token.XorAssign:
f.RegisterRegister(asm.XOR, destination, source)
case token.Equal, token.NotEqual, token.Less, token.LessEqual, token.Greater, token.GreaterEqual:
f.RegisterRegister(asm.COMPARE, destination, source)