github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/compile/internal/ssa/op.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa
     6  
     7  import (
     8  	"cmd/internal/obj"
     9  	"fmt"
    10  )
    11  
    12  // An Op encodes the specific operation that a Value performs.
    13  // Opcodes' semantics can be modified by the type and aux fields of the Value.
    14  // For instance, OpAdd can be 32 or 64 bit, signed or unsigned, float or complex, depending on Value.Type.
    15  // Semantics of each op are described in the opcode files in gen/*Ops.go.
    16  // There is one file for generic (architecture-independent) ops and one file
    17  // for each architecture.
    18  type Op int32
    19  
    20  type opInfo struct {
    21  	name              string
    22  	reg               regInfo
    23  	auxType           auxType
    24  	argLen            int32 // the number of arugments, -1 if variable length
    25  	asm               obj.As
    26  	generic           bool // this is a generic (arch-independent) opcode
    27  	rematerializeable bool // this op is rematerializeable
    28  	commutative       bool // this operation is commutative (e.g. addition)
    29  	resultInArg0      bool // prefer v and v.Args[0] to be allocated to the same register
    30  }
    31  
    32  type inputInfo struct {
    33  	idx  int     // index in Args array
    34  	regs regMask // allowed input registers
    35  }
    36  
    37  type regInfo struct {
    38  	inputs   []inputInfo // ordered in register allocation order
    39  	clobbers regMask
    40  	outputs  []regMask // NOTE: values can only have 1 output for now.
    41  }
    42  
    43  type auxType int8
    44  
    45  const (
    46  	auxNone         auxType = iota
    47  	auxBool                 // auxInt is 0/1 for false/true
    48  	auxInt8                 // auxInt is an 8-bit integer
    49  	auxInt16                // auxInt is a 16-bit integer
    50  	auxInt32                // auxInt is a 32-bit integer
    51  	auxInt64                // auxInt is a 64-bit integer
    52  	auxFloat32              // auxInt is a float32 (encoded with math.Float64bits)
    53  	auxFloat64              // auxInt is a float64 (encoded with math.Float64bits)
    54  	auxString               // auxInt is a string
    55  	auxSym                  // aux is a symbol
    56  	auxSymOff               // aux is a symbol, auxInt is an offset
    57  	auxSymValAndOff         // aux is a symbol, auxInt is a ValAndOff
    58  
    59  	auxSymInt32 // aux is a symbol, auxInt is a 32-bit integer
    60  )
    61  
    62  // A ValAndOff is used by the several opcodes. It holds
    63  // both a value and a pointer offset.
    64  // A ValAndOff is intended to be encoded into an AuxInt field.
    65  // The zero ValAndOff encodes a value of 0 and an offset of 0.
    66  // The high 32 bits hold a value.
    67  // The low 32 bits hold a pointer offset.
    68  type ValAndOff int64
    69  
    70  func (x ValAndOff) Val() int64 {
    71  	return int64(x) >> 32
    72  }
    73  func (x ValAndOff) Off() int64 {
    74  	return int64(int32(x))
    75  }
    76  func (x ValAndOff) Int64() int64 {
    77  	return int64(x)
    78  }
    79  func (x ValAndOff) String() string {
    80  	return fmt.Sprintf("val=%d,off=%d", x.Val(), x.Off())
    81  }
    82  
    83  // validVal reports whether the value can be used
    84  // as an argument to makeValAndOff.
    85  func validVal(val int64) bool {
    86  	return val == int64(int32(val))
    87  }
    88  
    89  // validOff reports whether the offset can be used
    90  // as an argument to makeValAndOff.
    91  func validOff(off int64) bool {
    92  	return off == int64(int32(off))
    93  }
    94  
    95  // validValAndOff reports whether we can fit the value and offset into
    96  // a ValAndOff value.
    97  func validValAndOff(val, off int64) bool {
    98  	if !validVal(val) {
    99  		return false
   100  	}
   101  	if !validOff(off) {
   102  		return false
   103  	}
   104  	return true
   105  }
   106  
   107  // makeValAndOff encodes a ValAndOff into an int64 suitable for storing in an AuxInt field.
   108  func makeValAndOff(val, off int64) int64 {
   109  	if !validValAndOff(val, off) {
   110  		panic("invalid makeValAndOff")
   111  	}
   112  	return ValAndOff(val<<32 + int64(uint32(off))).Int64()
   113  }
   114  
   115  func (x ValAndOff) canAdd(off int64) bool {
   116  	newoff := x.Off() + off
   117  	return newoff == int64(int32(newoff))
   118  }
   119  
   120  func (x ValAndOff) add(off int64) int64 {
   121  	if !x.canAdd(off) {
   122  		panic("invalid ValAndOff.add")
   123  	}
   124  	return makeValAndOff(x.Val(), x.Off()+off)
   125  }