Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/internal/sys/arch.go

Documentation: cmd/internal/sys

     1  // Copyright 2016 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 sys
     6  
     7  import "encoding/binary"
     8  
     9  // ArchFamily represents a family of one or more related architectures.
    10  // For example, ppc64 and ppc64le are both members of the PPC64 family.
    11  type ArchFamily byte
    12  
    13  const (
    14  	NoArch ArchFamily = iota
    15  	AMD64
    16  	ARM
    17  	ARM64
    18  	I386
    19  	MIPS
    20  	MIPS64
    21  	PPC64
    22  	RISCV64
    23  	S390X
    24  	Wasm
    25  )
    26  
    27  // Arch represents an individual architecture.
    28  type Arch struct {
    29  	Name   string
    30  	Family ArchFamily
    31  
    32  	ByteOrder binary.ByteOrder
    33  
    34  	// PtrSize is the size in bytes of pointers and the
    35  	// predeclared "int", "uint", and "uintptr" types.
    36  	PtrSize int
    37  
    38  	// RegSize is the size in bytes of general purpose registers.
    39  	RegSize int
    40  
    41  	// MinLC is the minimum length of an instruction code.
    42  	MinLC int
    43  
    44  	// Alignment is maximum alignment required by the architecture
    45  	// for any (compiler-generated) load or store instruction.
    46  	// Loads or stores smaller than Alignment must be naturally aligned.
    47  	// Loads or stores larger than Alignment need only be Alignment-aligned.
    48  	Alignment int8
    49  }
    50  
    51  // InFamily reports whether a is a member of any of the specified
    52  // architecture families.
    53  func (a *Arch) InFamily(xs ...ArchFamily) bool {
    54  	for _, x := range xs {
    55  		if a.Family == x {
    56  			return true
    57  		}
    58  	}
    59  	return false
    60  }
    61  
    62  var Arch386 = &Arch{
    63  	Name:      "386",
    64  	Family:    I386,
    65  	ByteOrder: binary.LittleEndian,
    66  	PtrSize:   4,
    67  	RegSize:   4,
    68  	MinLC:     1,
    69  	Alignment: 1,
    70  }
    71  
    72  var ArchAMD64 = &Arch{
    73  	Name:      "amd64",
    74  	Family:    AMD64,
    75  	ByteOrder: binary.LittleEndian,
    76  	PtrSize:   8,
    77  	RegSize:   8,
    78  	MinLC:     1,
    79  	Alignment: 1,
    80  }
    81  
    82  var ArchARM = &Arch{
    83  	Name:      "arm",
    84  	Family:    ARM,
    85  	ByteOrder: binary.LittleEndian,
    86  	PtrSize:   4,
    87  	RegSize:   4,
    88  	MinLC:     4,
    89  	Alignment: 4, // TODO: just for arm5?
    90  }
    91  
    92  var ArchARM64 = &Arch{
    93  	Name:      "arm64",
    94  	Family:    ARM64,
    95  	ByteOrder: binary.LittleEndian,
    96  	PtrSize:   8,
    97  	RegSize:   8,
    98  	MinLC:     4,
    99  	Alignment: 1,
   100  }
   101  
   102  var ArchMIPS = &Arch{
   103  	Name:      "mips",
   104  	Family:    MIPS,
   105  	ByteOrder: binary.BigEndian,
   106  	PtrSize:   4,
   107  	RegSize:   4,
   108  	MinLC:     4,
   109  	Alignment: 4,
   110  }
   111  
   112  var ArchMIPSLE = &Arch{
   113  	Name:      "mipsle",
   114  	Family:    MIPS,
   115  	ByteOrder: binary.LittleEndian,
   116  	PtrSize:   4,
   117  	RegSize:   4,
   118  	MinLC:     4,
   119  	Alignment: 4,
   120  }
   121  
   122  var ArchMIPS64 = &Arch{
   123  	Name:      "mips64",
   124  	Family:    MIPS64,
   125  	ByteOrder: binary.BigEndian,
   126  	PtrSize:   8,
   127  	RegSize:   8,
   128  	MinLC:     4,
   129  	Alignment: 8,
   130  }
   131  
   132  var ArchMIPS64LE = &Arch{
   133  	Name:      "mips64le",
   134  	Family:    MIPS64,
   135  	ByteOrder: binary.LittleEndian,
   136  	PtrSize:   8,
   137  	RegSize:   8,
   138  	MinLC:     4,
   139  	Alignment: 8,
   140  }
   141  
   142  var ArchPPC64 = &Arch{
   143  	Name:      "ppc64",
   144  	Family:    PPC64,
   145  	ByteOrder: binary.BigEndian,
   146  	PtrSize:   8,
   147  	RegSize:   8,
   148  	MinLC:     4,
   149  	Alignment: 1,
   150  }
   151  
   152  var ArchPPC64LE = &Arch{
   153  	Name:      "ppc64le",
   154  	Family:    PPC64,
   155  	ByteOrder: binary.LittleEndian,
   156  	PtrSize:   8,
   157  	RegSize:   8,
   158  	MinLC:     4,
   159  	Alignment: 1,
   160  }
   161  
   162  var ArchRISCV64 = &Arch{
   163  	Name:      "riscv64",
   164  	Family:    RISCV64,
   165  	ByteOrder: binary.LittleEndian,
   166  	PtrSize:   8,
   167  	RegSize:   8,
   168  	MinLC:     4,
   169  	Alignment: 8, // riscv unaligned loads work, but are really slow (trap + simulated by OS)
   170  }
   171  
   172  var ArchS390X = &Arch{
   173  	Name:      "s390x",
   174  	Family:    S390X,
   175  	ByteOrder: binary.BigEndian,
   176  	PtrSize:   8,
   177  	RegSize:   8,
   178  	MinLC:     2,
   179  	Alignment: 1,
   180  }
   181  
   182  var ArchWasm = &Arch{
   183  	Name:      "wasm",
   184  	Family:    Wasm,
   185  	ByteOrder: binary.LittleEndian,
   186  	PtrSize:   8,
   187  	RegSize:   8,
   188  	MinLC:     1,
   189  	Alignment: 1,
   190  }
   191  
   192  var Archs = [...]*Arch{
   193  	Arch386,
   194  	ArchAMD64,
   195  	ArchARM,
   196  	ArchARM64,
   197  	ArchMIPS,
   198  	ArchMIPSLE,
   199  	ArchMIPS64,
   200  	ArchMIPS64LE,
   201  	ArchPPC64,
   202  	ArchPPC64LE,
   203  	ArchRISCV64,
   204  	ArchS390X,
   205  	ArchWasm,
   206  }
   207  

View as plain text