github.com/amazechain/amc@v0.1.3/internal/vm/analysis.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  package vm
    17  
    18  // codeBitmap collects data locations in code.
    19  func codeBitmap(code []byte) []uint64 {
    20  	// The bitmap is 4 bytes longer than necessary, in case the code
    21  	// ends with a PUSH32, the algorithm will push zeroes onto the
    22  	// bitvector outside the bounds of the actual code.
    23  	bits := make([]uint64, (len(code)+32+63)/64)
    24  
    25  	for pc := 0; pc < len(code); {
    26  		op := OpCode(code[pc])
    27  		pc++
    28  		if op >= PUSH1 && op <= PUSH32 {
    29  			numbits := int(op - PUSH1 + 1)
    30  			x := uint64(1) << (op - PUSH1)
    31  			x = x | (x - 1) // Smear the bit to the right
    32  			idx := pc / 64
    33  			shift := pc & 63
    34  			bits[idx] |= x << shift
    35  			if shift+shift > 64 {
    36  				bits[idx+1] |= x >> (64 - shift)
    37  			}
    38  			pc += numbits
    39  		}
    40  	}
    41  	return bits
    42  }