instructions.go 21 KB

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