github.com/annchain/OG@v0.0.9/vm/code/analysis.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 code
    18  
    19  import (
    20  	"github.com/annchain/OG/vm/common"
    21  	"github.com/annchain/OG/vm/instruction"
    22  )
    23  
    24  // codeBitmap collects data locations in code.
    25  func CodeBitmap(code []byte) common.Bitvec {
    26  	// The bitmap is 4 bytes longer than necessary, in case the code
    27  	// ends with a PUSH32, the algorithm will push zeroes onto the
    28  	// Bitvector outside the bounds of the actual code.
    29  	bits := make(common.Bitvec, len(code)/8+1+4)
    30  	for pc := uint64(0); pc < uint64(len(code)); {
    31  		op := instruction.OpCode(code[pc])
    32  
    33  		if op >= instruction.PUSH1 && op <= instruction.PUSH32 {
    34  			numbits := op - instruction.PUSH1 + 1
    35  			pc++
    36  			for ; numbits >= 8; numbits -= 8 {
    37  				bits.Set8(pc) // 8
    38  				pc += 8
    39  			}
    40  			for ; numbits > 0; numbits-- {
    41  				bits.Set(pc)
    42  				pc++
    43  			}
    44  		} else {
    45  			pc++
    46  		}
    47  	}
    48  	return bits
    49  }