Implemented 32-bit jumps
This commit is contained in:
@ -2,6 +2,7 @@ package asm
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/arch/x64"
|
||||
)
|
||||
@ -16,7 +17,7 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
code := make([]byte, 0, len(a.Instructions)*8)
|
||||
data := make([]byte, 0, 16)
|
||||
labels := map[string]Address{}
|
||||
pointers := []Pointer{}
|
||||
pointers := []*Pointer{}
|
||||
|
||||
for _, x := range a.Instructions {
|
||||
switch x.Mnemonic {
|
||||
@ -53,8 +54,9 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, Pointer{
|
||||
pointers = append(pointers, &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: 1,
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
@ -101,8 +103,9 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, Pointer{
|
||||
pointers = append(pointers, &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: 1,
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
@ -153,10 +156,55 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
|
||||
// dataStart := config.BaseAddress + config.CodeOffset + Address(len(code))
|
||||
|
||||
for _, pointer := range pointers {
|
||||
slice := code[pointer.Position : pointer.Position+Address(pointer.Size)]
|
||||
restart:
|
||||
for i, pointer := range pointers {
|
||||
address := pointer.Resolve()
|
||||
|
||||
if x64.SizeOf(int64(address)) > int(pointer.Size) {
|
||||
left := code[:pointer.Position-Address(pointer.OpSize)]
|
||||
right := code[pointer.Position+Address(pointer.Size):]
|
||||
size := pointer.Size + pointer.OpSize
|
||||
opCode := code[pointer.Position-Address(pointer.OpSize)]
|
||||
|
||||
var jump []byte
|
||||
|
||||
switch opCode {
|
||||
case 0x74: // JE
|
||||
jump = []byte{0x0F, 0x84}
|
||||
case 0x75: // JNE
|
||||
jump = []byte{0x0F, 0x85}
|
||||
case 0x7C: // JL
|
||||
jump = []byte{0x0F, 0x8C}
|
||||
case 0x7D: // JGE
|
||||
jump = []byte{0x0F, 0x8D}
|
||||
case 0x7E: // JLE
|
||||
jump = []byte{0x0F, 0x8E}
|
||||
case 0x7F: // JG
|
||||
jump = []byte{0x0F, 0x8F}
|
||||
case 0xEB: // JMP
|
||||
jump = []byte{0xE9}
|
||||
|
||||
default:
|
||||
panic(fmt.Errorf("failed to increase pointer size for instruction 0x%x", opCode))
|
||||
}
|
||||
|
||||
pointer.Position += Address(len(jump) - int(pointer.OpSize))
|
||||
pointer.OpSize = uint8(len(jump))
|
||||
pointer.Size = 4
|
||||
jump = binary.LittleEndian.AppendUint32(jump, uint32(address))
|
||||
offset := Address(len(jump)) - Address(size)
|
||||
|
||||
for _, following := range pointers[i+1:] {
|
||||
following.Position += offset
|
||||
}
|
||||
|
||||
code = append(left, jump...)
|
||||
code = append(code, right...)
|
||||
goto restart
|
||||
}
|
||||
|
||||
slice := code[pointer.Position : pointer.Position+Address(pointer.Size)]
|
||||
|
||||
switch pointer.Size {
|
||||
case 1:
|
||||
slice[0] = uint8(address)
|
||||
|
Reference in New Issue
Block a user