github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/core/vm/common.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "math" 21 "math/big" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/logger/glog" 25 ) 26 27 // Global Debug flag indicating Debug VM (full logging) 28 var Debug bool 29 30 type Type byte 31 32 const ( 33 StdVmTy Type = iota 34 JitVmTy 35 MaxVmTy 36 37 LogTyPretty byte = 0x1 38 LogTyDiff byte = 0x2 39 ) 40 41 var ( 42 Pow256 = common.BigPow(2, 256) 43 44 U256 = common.U256 45 S256 = common.S256 46 47 Zero = common.Big0 48 One = common.Big1 49 50 max = big.NewInt(math.MaxInt64) 51 ) 52 53 func NewVm(env Environment) VirtualMachine { 54 switch env.VmType() { 55 case JitVmTy: 56 return NewJitVm(env) 57 default: 58 glog.V(0).Infoln("unsupported vm type %d", env.VmType()) 59 fallthrough 60 case StdVmTy: 61 return New(env) 62 } 63 } 64 65 func calcMemSize(off, l *big.Int) *big.Int { 66 if l.Cmp(common.Big0) == 0 { 67 return common.Big0 68 } 69 70 return new(big.Int).Add(off, l) 71 } 72 73 // Simple helper 74 func u256(n int64) *big.Int { 75 return big.NewInt(n) 76 } 77 78 // Mainly used for print variables and passing to Print* 79 func toValue(val *big.Int) interface{} { 80 // Let's assume a string on right padded zero's 81 b := val.Bytes() 82 if b[0] != 0 && b[len(b)-1] == 0x0 && b[len(b)-2] == 0x0 { 83 return string(b) 84 } 85 86 return val 87 } 88 89 func getData(data []byte, start, size *big.Int) []byte { 90 dlen := big.NewInt(int64(len(data))) 91 92 s := common.BigMin(start, dlen) 93 e := common.BigMin(new(big.Int).Add(s, size), dlen) 94 return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64())) 95 } 96 97 func UseGas(gas, amount *big.Int) bool { 98 if gas.Cmp(amount) < 0 { 99 return false 100 } 101 102 // Sub the amount of gas from the remaining 103 gas.Sub(gas, amount) 104 return true 105 }