instructions.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // Copyright 2015 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. package vm
  17. import (
  18. "fmt"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/math"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/params"
  25. )
  26. var bigZero = new(big.Int)
  27. func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  28. x, y := stack.pop(), stack.pop()
  29. stack.push(math.U256(x.Add(x, y)))
  30. evm.interpreter.intPool.put(y)
  31. return nil, nil
  32. }
  33. func opSub(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  34. x, y := stack.pop(), stack.pop()
  35. stack.push(math.U256(x.Sub(x, y)))
  36. evm.interpreter.intPool.put(y)
  37. return nil, nil
  38. }
  39. func opMul(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  40. x, y := stack.pop(), stack.pop()
  41. stack.push(math.U256(x.Mul(x, y)))
  42. evm.interpreter.intPool.put(y)
  43. return nil, nil
  44. }
  45. func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  46. x, y := stack.pop(), stack.pop()
  47. if y.Cmp(common.Big0) != 0 {
  48. stack.push(math.U256(x.Div(x, y)))
  49. } else {
  50. stack.push(new(big.Int))
  51. }
  52. evm.interpreter.intPool.put(y)
  53. return nil, nil
  54. }
  55. func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  56. x, y := math.S256(stack.pop()), math.S256(stack.pop())
  57. if y.Cmp(common.Big0) == 0 {
  58. stack.push(new(big.Int))
  59. return nil, nil
  60. } else {
  61. n := new(big.Int)
  62. if evm.interpreter.intPool.get().Mul(x, y).Cmp(common.Big0) < 0 {
  63. n.SetInt64(-1)
  64. } else {
  65. n.SetInt64(1)
  66. }
  67. res := x.Div(x.Abs(x), y.Abs(y))
  68. res.Mul(res, n)
  69. stack.push(math.U256(res))
  70. }
  71. evm.interpreter.intPool.put(y)
  72. return nil, nil
  73. }
  74. func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  75. x, y := stack.pop(), stack.pop()
  76. if y.Cmp(common.Big0) == 0 {
  77. stack.push(new(big.Int))
  78. } else {
  79. stack.push(math.U256(x.Mod(x, y)))
  80. }
  81. evm.interpreter.intPool.put(y)
  82. return nil, nil
  83. }
  84. func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  85. x, y := math.S256(stack.pop()), math.S256(stack.pop())
  86. if y.Cmp(common.Big0) == 0 {
  87. stack.push(new(big.Int))
  88. } else {
  89. n := new(big.Int)
  90. if x.Cmp(common.Big0) < 0 {
  91. n.SetInt64(-1)
  92. } else {
  93. n.SetInt64(1)
  94. }
  95. res := x.Mod(x.Abs(x), y.Abs(y))
  96. res.Mul(res, n)
  97. stack.push(math.U256(res))
  98. }
  99. evm.interpreter.intPool.put(y)
  100. return nil, nil
  101. }
  102. func opExp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  103. base, exponent := stack.pop(), stack.pop()
  104. stack.push(math.Exp(base, exponent))
  105. evm.interpreter.intPool.put(base, exponent)
  106. return nil, nil
  107. }
  108. func opSignExtend(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  109. back := stack.pop()
  110. if back.Cmp(big.NewInt(31)) < 0 {
  111. bit := uint(back.Uint64()*8 + 7)
  112. num := stack.pop()
  113. mask := back.Lsh(common.Big1, bit)
  114. mask.Sub(mask, common.Big1)
  115. if num.Bit(int(bit)) > 0 {
  116. num.Or(num, mask.Not(mask))
  117. } else {
  118. num.And(num, mask)
  119. }
  120. stack.push(math.U256(num))
  121. }
  122. evm.interpreter.intPool.put(back)
  123. return nil, nil
  124. }
  125. func opNot(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  126. x := stack.pop()
  127. stack.push(math.U256(x.Not(x)))
  128. return nil, nil
  129. }
  130. func opLt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  131. x, y := stack.pop(), stack.pop()
  132. if x.Cmp(y) < 0 {
  133. stack.push(evm.interpreter.intPool.get().SetUint64(1))
  134. } else {
  135. stack.push(new(big.Int))
  136. }
  137. evm.interpreter.intPool.put(x, y)
  138. return nil, nil
  139. }
  140. func opGt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  141. x, y := stack.pop(), stack.pop()
  142. if x.Cmp(y) > 0 {
  143. stack.push(evm.interpreter.intPool.get().SetUint64(1))
  144. } else {
  145. stack.push(new(big.Int))
  146. }
  147. evm.interpreter.intPool.put(x, y)
  148. return nil, nil
  149. }
  150. func opSlt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  151. x, y := math.S256(stack.pop()), math.S256(stack.pop())
  152. if x.Cmp(math.S256(y)) < 0 {
  153. stack.push(evm.interpreter.intPool.get().SetUint64(1))
  154. } else {
  155. stack.push(new(big.Int))
  156. }
  157. evm.interpreter.intPool.put(x, y)
  158. return nil, nil
  159. }
  160. func opSgt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  161. x, y := math.S256(stack.pop()), math.S256(stack.pop())
  162. if x.Cmp(y) > 0 {
  163. stack.push(evm.interpreter.intPool.get().SetUint64(1))
  164. } else {
  165. stack.push(new(big.Int))
  166. }
  167. evm.interpreter.intPool.put(x, y)
  168. return nil, nil
  169. }
  170. func opEq(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  171. x, y := stack.pop(), stack.pop()
  172. if x.Cmp(y) == 0 {
  173. stack.push(evm.interpreter.intPool.get().SetUint64(1))
  174. } else {
  175. stack.push(new(big.Int))
  176. }
  177. evm.interpreter.intPool.put(x, y)
  178. return nil, nil
  179. }
  180. func opIszero(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  181. x := stack.pop()
  182. if x.Cmp(common.Big0) > 0 {
  183. stack.push(new(big.Int))
  184. } else {
  185. stack.push(evm.interpreter.intPool.get().SetUint64(1))
  186. }
  187. evm.interpreter.intPool.put(x)
  188. return nil, nil
  189. }
  190. func opAnd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  191. x, y := stack.pop(), stack.pop()
  192. stack.push(x.And(x, y))
  193. evm.interpreter.intPool.put(y)
  194. return nil, nil
  195. }
  196. func opOr(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  197. x, y := stack.pop(), stack.pop()
  198. stack.push(x.Or(x, y))
  199. evm.interpreter.intPool.put(y)
  200. return nil, nil
  201. }
  202. func opXor(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  203. x, y := stack.pop(), stack.pop()
  204. stack.push(x.Xor(x, y))
  205. evm.interpreter.intPool.put(y)
  206. return nil, nil
  207. }
  208. func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  209. th, val := stack.pop(), stack.pop()
  210. if th.Cmp(big.NewInt(32)) < 0 {
  211. byte := evm.interpreter.intPool.get().SetInt64(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
  212. stack.push(byte)
  213. } else {
  214. stack.push(new(big.Int))
  215. }
  216. evm.interpreter.intPool.put(th, val)
  217. return nil, nil
  218. }
  219. func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  220. x, y, z := stack.pop(), stack.pop(), stack.pop()
  221. if z.Cmp(bigZero) > 0 {
  222. add := x.Add(x, y)
  223. add.Mod(add, z)
  224. stack.push(math.U256(add))
  225. } else {
  226. stack.push(new(big.Int))
  227. }
  228. evm.interpreter.intPool.put(y, z)
  229. return nil, nil
  230. }
  231. func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  232. x, y, z := stack.pop(), stack.pop(), stack.pop()
  233. if z.Cmp(bigZero) > 0 {
  234. mul := x.Mul(x, y)
  235. mul.Mod(mul, z)
  236. stack.push(math.U256(mul))
  237. } else {
  238. stack.push(new(big.Int))
  239. }
  240. evm.interpreter.intPool.put(y, z)
  241. return nil, nil
  242. }
  243. func opSha3(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  244. offset, size := stack.pop(), stack.pop()
  245. data := memory.Get(offset.Int64(), size.Int64())
  246. hash := crypto.Keccak256(data)
  247. if evm.vmConfig.EnablePreimageRecording {
  248. evm.StateDB.AddPreimage(common.BytesToHash(hash), data)
  249. }
  250. stack.push(new(big.Int).SetBytes(hash))
  251. evm.interpreter.intPool.put(offset, size)
  252. return nil, nil
  253. }
  254. func opAddress(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  255. stack.push(contract.Address().Big())
  256. return nil, nil
  257. }
  258. func opBalance(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  259. addr := common.BigToAddress(stack.pop())
  260. balance := evm.StateDB.GetBalance(addr)
  261. stack.push(new(big.Int).Set(balance))
  262. return nil, nil
  263. }
  264. func opOrigin(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  265. stack.push(evm.Origin.Big())
  266. return nil, nil
  267. }
  268. func opCaller(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  269. stack.push(contract.Caller().Big())
  270. return nil, nil
  271. }
  272. func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  273. stack.push(evm.interpreter.intPool.get().Set(contract.value))
  274. return nil, nil
  275. }
  276. func opCalldataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  277. stack.push(new(big.Int).SetBytes(getData(contract.Input, stack.pop(), common.Big32)))
  278. return nil, nil
  279. }
  280. func opCalldataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  281. stack.push(evm.interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
  282. return nil, nil
  283. }
  284. func opCalldataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  285. var (
  286. mOff = stack.pop()
  287. cOff = stack.pop()
  288. l = stack.pop()
  289. )
  290. memory.Set(mOff.Uint64(), l.Uint64(), getData(contract.Input, cOff, l))
  291. evm.interpreter.intPool.put(mOff, cOff, l)
  292. return nil, nil
  293. }
  294. func opExtCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  295. a := stack.pop()
  296. addr := common.BigToAddress(a)
  297. a.SetInt64(int64(evm.StateDB.GetCodeSize(addr)))
  298. stack.push(a)
  299. return nil, nil
  300. }
  301. func opCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  302. l := evm.interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
  303. stack.push(l)
  304. return nil, nil
  305. }
  306. func opCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  307. var (
  308. mOff = stack.pop()
  309. cOff = stack.pop()
  310. l = stack.pop()
  311. )
  312. codeCopy := getData(contract.Code, cOff, l)
  313. memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
  314. evm.interpreter.intPool.put(mOff, cOff, l)
  315. return nil, nil
  316. }
  317. func opExtCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  318. var (
  319. addr = common.BigToAddress(stack.pop())
  320. mOff = stack.pop()
  321. cOff = stack.pop()
  322. l = stack.pop()
  323. )
  324. codeCopy := getData(evm.StateDB.GetCode(addr), cOff, l)
  325. memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
  326. evm.interpreter.intPool.put(mOff, cOff, l)
  327. return nil, nil
  328. }
  329. func opGasprice(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  330. stack.push(evm.interpreter.intPool.get().Set(evm.GasPrice))
  331. return nil, nil
  332. }
  333. func opBlockhash(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  334. num := stack.pop()
  335. n := evm.interpreter.intPool.get().Sub(evm.BlockNumber, common.Big257)
  336. if num.Cmp(n) > 0 && num.Cmp(evm.BlockNumber) < 0 {
  337. stack.push(evm.GetHash(num.Uint64()).Big())
  338. } else {
  339. stack.push(new(big.Int))
  340. }
  341. evm.interpreter.intPool.put(num, n)
  342. return nil, nil
  343. }
  344. func opCoinbase(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  345. stack.push(evm.Coinbase.Big())
  346. return nil, nil
  347. }
  348. func opTimestamp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  349. stack.push(math.U256(new(big.Int).Set(evm.Time)))
  350. return nil, nil
  351. }
  352. func opNumber(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  353. stack.push(math.U256(new(big.Int).Set(evm.BlockNumber)))
  354. return nil, nil
  355. }
  356. func opDifficulty(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  357. stack.push(math.U256(new(big.Int).Set(evm.Difficulty)))
  358. return nil, nil
  359. }
  360. func opGasLimit(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  361. stack.push(math.U256(new(big.Int).Set(evm.GasLimit)))
  362. return nil, nil
  363. }
  364. func opPop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  365. evm.interpreter.intPool.put(stack.pop())
  366. return nil, nil
  367. }
  368. func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  369. offset := stack.pop()
  370. val := new(big.Int).SetBytes(memory.Get(offset.Int64(), 32))
  371. stack.push(val)
  372. evm.interpreter.intPool.put(offset)
  373. return nil, nil
  374. }
  375. func opMstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  376. // pop value of the stack
  377. mStart, val := stack.pop(), stack.pop()
  378. memory.Set(mStart.Uint64(), 32, math.PaddedBigBytes(val, 32))
  379. evm.interpreter.intPool.put(mStart, val)
  380. return nil, nil
  381. }
  382. func opMstore8(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  383. off, val := stack.pop().Int64(), stack.pop().Int64()
  384. memory.store[off] = byte(val & 0xff)
  385. return nil, nil
  386. }
  387. func opSload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  388. loc := common.BigToHash(stack.pop())
  389. val := evm.StateDB.GetState(contract.Address(), loc).Big()
  390. stack.push(val)
  391. return nil, nil
  392. }
  393. func opSstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  394. loc := common.BigToHash(stack.pop())
  395. val := stack.pop()
  396. evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
  397. evm.interpreter.intPool.put(val)
  398. return nil, nil
  399. }
  400. func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  401. pos := stack.pop()
  402. if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
  403. nop := contract.GetOp(pos.Uint64())
  404. return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
  405. }
  406. *pc = pos.Uint64()
  407. evm.interpreter.intPool.put(pos)
  408. return nil, nil
  409. }
  410. func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  411. pos, cond := stack.pop(), stack.pop()
  412. if cond.BitLen() > 0 {
  413. if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) {
  414. nop := contract.GetOp(pos.Uint64())
  415. return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos)
  416. }
  417. *pc = pos.Uint64()
  418. } else {
  419. *pc++
  420. }
  421. evm.interpreter.intPool.put(pos, cond)
  422. return nil, nil
  423. }
  424. func opJumpdest(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  425. return nil, nil
  426. }
  427. func opPc(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  428. stack.push(evm.interpreter.intPool.get().SetUint64(*pc))
  429. return nil, nil
  430. }
  431. func opMsize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  432. stack.push(evm.interpreter.intPool.get().SetInt64(int64(memory.Len())))
  433. return nil, nil
  434. }
  435. func opGas(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  436. stack.push(evm.interpreter.intPool.get().SetUint64(contract.Gas))
  437. return nil, nil
  438. }
  439. func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  440. var (
  441. value = stack.pop()
  442. offset, size = stack.pop(), stack.pop()
  443. input = memory.Get(offset.Int64(), size.Int64())
  444. gas = contract.Gas
  445. )
  446. if evm.ChainConfig().IsEIP150(evm.BlockNumber) {
  447. gas -= gas / 64
  448. }
  449. contract.UseGas(gas)
  450. _, addr, returnGas, suberr := evm.Create(contract, input, gas, value)
  451. // Push item on the stack based on the returned error. If the ruleset is
  452. // homestead we must check for CodeStoreOutOfGasError (homestead only
  453. // rule) and treat as an error, if the ruleset is frontier we must
  454. // ignore this error and pretend the operation was successful.
  455. if evm.ChainConfig().IsHomestead(evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
  456. stack.push(new(big.Int))
  457. } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
  458. stack.push(new(big.Int))
  459. } else {
  460. stack.push(addr.Big())
  461. }
  462. contract.Gas += returnGas
  463. evm.interpreter.intPool.put(value, offset, size)
  464. return nil, nil
  465. }
  466. func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  467. gas := stack.pop().Uint64()
  468. // pop gas and value of the stack.
  469. addr, value := stack.pop(), stack.pop()
  470. value = math.U256(value)
  471. // pop input size and offset
  472. inOffset, inSize := stack.pop(), stack.pop()
  473. // pop return size and offset
  474. retOffset, retSize := stack.pop(), stack.pop()
  475. address := common.BigToAddress(addr)
  476. // Get the arguments from the memory
  477. args := memory.Get(inOffset.Int64(), inSize.Int64())
  478. if value.BitLen() > 0 {
  479. gas += params.CallStipend
  480. }
  481. ret, returnGas, err := evm.Call(contract, address, args, gas, value)
  482. if err != nil {
  483. stack.push(new(big.Int))
  484. } else {
  485. stack.push(big.NewInt(1))
  486. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  487. }
  488. contract.Gas += returnGas
  489. evm.interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  490. return nil, nil
  491. }
  492. func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  493. gas := stack.pop().Uint64()
  494. // pop gas and value of the stack.
  495. addr, value := stack.pop(), stack.pop()
  496. value = math.U256(value)
  497. // pop input size and offset
  498. inOffset, inSize := stack.pop(), stack.pop()
  499. // pop return size and offset
  500. retOffset, retSize := stack.pop(), stack.pop()
  501. address := common.BigToAddress(addr)
  502. // Get the arguments from the memory
  503. args := memory.Get(inOffset.Int64(), inSize.Int64())
  504. if value.BitLen() > 0 {
  505. gas += params.CallStipend
  506. }
  507. ret, returnGas, err := evm.CallCode(contract, address, args, gas, value)
  508. if err != nil {
  509. stack.push(new(big.Int))
  510. } else {
  511. stack.push(big.NewInt(1))
  512. memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
  513. }
  514. contract.Gas += returnGas
  515. evm.interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
  516. return nil, nil
  517. }
  518. func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  519. // if not homestead return an error. DELEGATECALL is not supported
  520. // during pre-homestead.
  521. if !evm.ChainConfig().IsHomestead(evm.BlockNumber) {
  522. return nil, fmt.Errorf("invalid opcode %x", DELEGATECALL)
  523. }
  524. gas, to, inOffset, inSize, outOffset, outSize := stack.pop().Uint64(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
  525. toAddr := common.BigToAddress(to)
  526. args := memory.Get(inOffset.Int64(), inSize.Int64())
  527. ret, returnGas, err := evm.DelegateCall(contract, toAddr, args, gas)
  528. if err != nil {
  529. stack.push(new(big.Int))
  530. } else {
  531. stack.push(big.NewInt(1))
  532. memory.Set(outOffset.Uint64(), outSize.Uint64(), ret)
  533. }
  534. contract.Gas += returnGas
  535. evm.interpreter.intPool.put(to, inOffset, inSize, outOffset, outSize)
  536. return nil, nil
  537. }
  538. func opReturn(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  539. offset, size := stack.pop(), stack.pop()
  540. ret := memory.GetPtr(offset.Int64(), size.Int64())
  541. evm.interpreter.intPool.put(offset, size)
  542. return ret, nil
  543. }
  544. func opStop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  545. return nil, nil
  546. }
  547. func opSuicide(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  548. balance := evm.StateDB.GetBalance(contract.Address())
  549. evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
  550. evm.StateDB.Suicide(contract.Address())
  551. return nil, nil
  552. }
  553. // following functions are used by the instruction jump table
  554. // make log instruction function
  555. func makeLog(size int) executionFunc {
  556. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  557. topics := make([]common.Hash, size)
  558. mStart, mSize := stack.pop(), stack.pop()
  559. for i := 0; i < size; i++ {
  560. topics[i] = common.BigToHash(stack.pop())
  561. }
  562. d := memory.Get(mStart.Int64(), mSize.Int64())
  563. evm.StateDB.AddLog(&types.Log{
  564. Address: contract.Address(),
  565. Topics: topics,
  566. Data: d,
  567. // This is a non-consensus field, but assigned here because
  568. // core/state doesn't know the current block number.
  569. BlockNumber: evm.BlockNumber.Uint64(),
  570. })
  571. evm.interpreter.intPool.put(mStart, mSize)
  572. return nil, nil
  573. }
  574. }
  575. // make push instruction function
  576. func makePush(size uint64, bsize *big.Int) executionFunc {
  577. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  578. byts := getData(contract.Code, evm.interpreter.intPool.get().SetUint64(*pc+1), bsize)
  579. stack.push(new(big.Int).SetBytes(byts))
  580. *pc += size
  581. return nil, nil
  582. }
  583. }
  584. // make push instruction function
  585. func makeDup(size int64) executionFunc {
  586. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  587. stack.dup(int(size))
  588. return nil, nil
  589. }
  590. }
  591. // make swap instruction function
  592. func makeSwap(size int64) executionFunc {
  593. // switch n + 1 otherwise n would be swapped with n
  594. size += 1
  595. return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
  596. stack.swap(int(size))
  597. return nil, nil
  598. }
  599. }