compiler: adjust init/_deploy method offsets during optimization

Because `_initialize` and `_deploy` methods encompass multiple
Go functions, their offsets are stored differently and need
special treatment.
This commit is contained in:
Evgeniy Stratonikov 2021-02-11 15:41:49 +03:00
parent 2ee755e09f
commit 560aff6155
3 changed files with 71 additions and 15 deletions

View file

@ -2011,28 +2011,41 @@ func (c *codegen) writeJumps(b []byte) ([]byte, error) {
}
}
}
if c.deployEndOffset >= 0 {
_, end := correctRange(uint16(c.initEndOffset+1), uint16(c.deployEndOffset), offsets)
c.deployEndOffset = int(end)
}
if c.initEndOffset > 0 {
_, end := correctRange(0, uint16(c.initEndOffset), offsets)
c.initEndOffset = int(end)
}
// Correct function ip range.
// Note: indices are sorted in increasing order.
for _, f := range c.funcs {
start, end := f.rng.Start, f.rng.End
loop:
for _, ind := range offsets {
switch {
case ind > int(f.rng.End):
break loop
case ind < int(f.rng.Start):
start -= longToShortRemoveCount
end -= longToShortRemoveCount
case ind >= int(f.rng.Start):
end -= longToShortRemoveCount
}
}
f.rng.Start = start
f.rng.End = end
f.rng.Start, f.rng.End = correctRange(f.rng.Start, f.rng.End, offsets)
}
return shortenJumps(b, offsets), nil
}
func correctRange(start, end uint16, offsets []int) (uint16, uint16) {
newStart, newEnd := start, end
loop:
for _, ind := range offsets {
switch {
case ind > int(end):
break loop
case ind < int(start):
newStart -= longToShortRemoveCount
newEnd -= longToShortRemoveCount
case ind >= int(start):
newEnd -= longToShortRemoveCount
}
}
return newStart, newEnd
}
func (c *codegen) replaceLabelWithOffset(ip int, arg []byte) (int, error) {
index := binary.LittleEndian.Uint16(arg)
if int(index) > len(c.l) {