Fixed jump address calculation
This commit is contained in:
parent
bf80551a15
commit
d001e4e55f
@ -57,23 +57,25 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
code = x64.Call(code, 0x00_00_00_00)
|
||||
size := 4
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, &Pointer{
|
||||
pointer := &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: 1,
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
}
|
||||
|
||||
pointer.Resolve = func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
|
||||
if !exists {
|
||||
panic("unknown call label")
|
||||
panic("unknown jump label")
|
||||
}
|
||||
|
||||
distance := destination - nextInstructionAddress
|
||||
distance := destination - (pointer.Position + Address(pointer.Size))
|
||||
return Address(distance)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pointers = append(pointers, pointer)
|
||||
|
||||
case COMMENT:
|
||||
continue
|
||||
@ -106,23 +108,25 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
|
||||
size := 1
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, &Pointer{
|
||||
pointer := &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: 1,
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
}
|
||||
|
||||
pointer.Resolve = func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
|
||||
if !exists {
|
||||
panic("unknown jump label")
|
||||
}
|
||||
|
||||
distance := destination - nextInstructionAddress
|
||||
distance := destination - (pointer.Position + Address(pointer.Size))
|
||||
return Address(distance)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pointers = append(pointers, pointer)
|
||||
|
||||
case LABEL:
|
||||
labels[x.Data.(*Label).Name] = Address(len(code))
|
||||
@ -269,8 +273,16 @@ restart:
|
||||
following.Position += offset
|
||||
}
|
||||
|
||||
code = append(left, jump...)
|
||||
code = append(code, right...)
|
||||
for key, address := range labels {
|
||||
if address > pointer.Position {
|
||||
labels[key] += offset
|
||||
}
|
||||
}
|
||||
|
||||
code = make([]byte, len(left)+len(jump)+len(right))
|
||||
copy(code, left)
|
||||
copy(code[len(left):], jump)
|
||||
copy(code[len(left)+len(jump):], right)
|
||||
goto restart
|
||||
}
|
||||
|
||||
|
16
tests/programs/param-order.q
Normal file
16
tests/programs/param-order.q
Normal file
@ -0,0 +1,16 @@
|
||||
main() {
|
||||
f(1, 2, 3, 4, 5, 6)
|
||||
}
|
||||
|
||||
f(a, b, c, d, e, f) {
|
||||
return g(f, e, d, c, b, a)
|
||||
}
|
||||
|
||||
g(a, b, c, d, e, f) {
|
||||
assert a == 6
|
||||
assert b == 5
|
||||
assert c == 4
|
||||
assert d == 3
|
||||
assert e == 2
|
||||
assert f == 1
|
||||
}
|
@ -25,6 +25,7 @@ var programs = []struct {
|
||||
{"nested-calls", "", "", 4},
|
||||
{"param", "", "", 3},
|
||||
{"param-multi", "", "", 21},
|
||||
{"param-order", "", "", 0},
|
||||
{"reuse", "", "", 3},
|
||||
{"return", "", "", 6},
|
||||
{"reassign", "", "", 2},
|
||||
|
Loading…
Reference in New Issue
Block a user