2014-10-18 14:31:20 +03:00
|
|
|
package vm
|
2014-07-22 12:54:48 +03:00
|
|
|
|
|
|
|
import (
|
2015-03-13 14:44:15 +02:00
|
|
|
"math"
|
2014-10-14 12:48:52 +03:00
|
|
|
"math/big"
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-10-31 13:56:05 +02:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2014-07-22 12:54:48 +03:00
|
|
|
)
|
|
|
|
|
2014-10-31 13:56:05 +02:00
|
|
|
var vmlogger = logger.NewLogger("VM")
|
2014-07-22 12:54:48 +03:00
|
|
|
|
2015-03-06 17:58:52 +02:00
|
|
|
// Global Debug flag indicating Debug VM (full logging)
|
|
|
|
var Debug bool
|
|
|
|
|
2015-01-19 12:18:34 +02:00
|
|
|
type Type byte
|
2014-10-14 12:48:52 +03:00
|
|
|
|
|
|
|
const (
|
2015-01-19 12:18:34 +02:00
|
|
|
StdVmTy Type = iota
|
2015-01-12 20:40:14 +02:00
|
|
|
JitVmTy
|
2014-10-14 12:48:52 +03:00
|
|
|
MaxVmTy
|
2015-03-16 22:46:47 +02:00
|
|
|
|
|
|
|
MaxCallDepth = 1025
|
|
|
|
|
|
|
|
LogTyPretty byte = 0x1
|
|
|
|
LogTyDiff byte = 0x2
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Pow256 = common.BigPow(2, 256)
|
|
|
|
|
|
|
|
U256 = common.U256
|
|
|
|
S256 = common.S256
|
|
|
|
|
|
|
|
Zero = common.Big0
|
|
|
|
|
|
|
|
max = big.NewInt(math.MaxInt64)
|
2014-10-14 12:48:52 +03:00
|
|
|
)
|
|
|
|
|
2015-02-01 16:30:29 +02:00
|
|
|
func NewVm(env Environment) VirtualMachine {
|
|
|
|
switch env.VmType() {
|
|
|
|
case JitVmTy:
|
|
|
|
return NewJitVm(env)
|
|
|
|
default:
|
|
|
|
vmlogger.Infoln("unsupported vm type %d", env.VmType())
|
|
|
|
fallthrough
|
|
|
|
case StdVmTy:
|
|
|
|
return New(env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 12:48:52 +03:00
|
|
|
func calcMemSize(off, l *big.Int) *big.Int {
|
2015-03-16 12:27:38 +02:00
|
|
|
if l.Cmp(common.Big0) == 0 {
|
|
|
|
return common.Big0
|
2014-10-14 12:48:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new(big.Int).Add(off, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple helper
|
|
|
|
func u256(n int64) *big.Int {
|
|
|
|
return big.NewInt(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mainly used for print variables and passing to Print*
|
|
|
|
func toValue(val *big.Int) interface{} {
|
|
|
|
// Let's assume a string on right padded zero's
|
|
|
|
b := val.Bytes()
|
|
|
|
if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
2015-03-13 14:44:15 +02:00
|
|
|
|
|
|
|
func getCode(code []byte, start, size uint64) []byte {
|
|
|
|
x := uint64(math.Min(float64(start), float64(len(code))))
|
|
|
|
y := uint64(math.Min(float64(x+size), float64(len(code))))
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
return common.RightPadBytes(code[x:y], int(size))
|
2015-03-13 14:44:15 +02:00
|
|
|
}
|