github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/bootstrap6g/reg.go (about)

     1  // Do not edit. Bootstrap copy of /Users/rsc/g/go/src/cmd/6g/reg.go
     2  
     3  // Derived from Inferno utils/6c/reg.c
     4  // http://code.google.com/p/inferno-os/source/browse/utils/6c/reg.c
     5  //
     6  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     7  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     8  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     9  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
    10  //	Portions Copyright © 2004,2006 Bruce Ellis
    11  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    12  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    13  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    14  //
    15  // Permission is hereby granted, free of charge, to any person obtaining a copy
    16  // of this software and associated documentation files (the "Software"), to deal
    17  // in the Software without restriction, including without limitation the rights
    18  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    19  // copies of the Software, and to permit persons to whom the Software is
    20  // furnished to do so, subject to the following conditions:
    21  //
    22  // The above copyright notice and this permission notice shall be included in
    23  // all copies or substantial portions of the Software.
    24  //
    25  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    26  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    27  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    28  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    29  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    30  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    31  // THE SOFTWARE.
    32  
    33  package main
    34  
    35  import (
    36  	"rsc.io/tmp/bootstrap/internal/gc"
    37  	"rsc.io/tmp/bootstrap/internal/obj"
    38  	"rsc.io/tmp/bootstrap/internal/obj/x86"
    39  )
    40  
    41  const (
    42  	NREGVAR = 32
    43  )
    44  
    45  var reg [x86.MAXREG]uint8
    46  
    47  var regname = []string{
    48  	".AX",
    49  	".CX",
    50  	".DX",
    51  	".BX",
    52  	".SP",
    53  	".BP",
    54  	".SI",
    55  	".DI",
    56  	".R8",
    57  	".R9",
    58  	".R10",
    59  	".R11",
    60  	".R12",
    61  	".R13",
    62  	".R14",
    63  	".R15",
    64  	".X0",
    65  	".X1",
    66  	".X2",
    67  	".X3",
    68  	".X4",
    69  	".X5",
    70  	".X6",
    71  	".X7",
    72  	".X8",
    73  	".X9",
    74  	".X10",
    75  	".X11",
    76  	".X12",
    77  	".X13",
    78  	".X14",
    79  	".X15",
    80  }
    81  
    82  func regnames(n *int) []string {
    83  	*n = NREGVAR
    84  	return regname
    85  }
    86  
    87  func excludedregs() uint64 {
    88  	return RtoB(x86.REG_SP)
    89  }
    90  
    91  func doregbits(r int) uint64 {
    92  	b := uint64(0)
    93  	if r >= x86.REG_AX && r <= x86.REG_R15 {
    94  		b |= RtoB(r)
    95  	} else if r >= x86.REG_AL && r <= x86.REG_R15B {
    96  		b |= RtoB(r - x86.REG_AL + x86.REG_AX)
    97  	} else if r >= x86.REG_AH && r <= x86.REG_BH {
    98  		b |= RtoB(r - x86.REG_AH + x86.REG_AX)
    99  	} else if r >= x86.REG_X0 && r <= x86.REG_X0+15 {
   100  		b |= FtoB(r)
   101  	}
   102  	return b
   103  }
   104  
   105  // For ProgInfo.
   106  const (
   107  	AX  = 1 << (x86.REG_AX - x86.REG_AX)
   108  	BX  = 1 << (x86.REG_BX - x86.REG_AX)
   109  	CX  = 1 << (x86.REG_CX - x86.REG_AX)
   110  	DX  = 1 << (x86.REG_DX - x86.REG_AX)
   111  	DI  = 1 << (x86.REG_DI - x86.REG_AX)
   112  	SI  = 1 << (x86.REG_SI - x86.REG_AX)
   113  	R15 = 1 << (x86.REG_R15 - x86.REG_AX)
   114  )
   115  
   116  func RtoB(r int) uint64 {
   117  	if r < x86.REG_AX || r > x86.REG_R15 {
   118  		return 0
   119  	}
   120  	return 1 << uint(r-x86.REG_AX)
   121  }
   122  
   123  func BtoR(b uint64) int {
   124  	b &= 0xffff
   125  	if gc.Nacl {
   126  		b &^= (1<<(x86.REG_BP-x86.REG_AX) | 1<<(x86.REG_R15-x86.REG_AX))
   127  	} else if obj.Framepointer_enabled != 0 {
   128  		// BP is part of the calling convention if framepointer_enabled.
   129  		b &^= (1 << (x86.REG_BP - x86.REG_AX))
   130  	}
   131  	if b == 0 {
   132  		return 0
   133  	}
   134  	return gc.Bitno(b) + x86.REG_AX
   135  }
   136  
   137  /*
   138   *	bit	reg
   139   *	16	X0
   140   *	...
   141   *	31	X15
   142   */
   143  func FtoB(f int) uint64 {
   144  	if f < x86.REG_X0 || f > x86.REG_X15 {
   145  		return 0
   146  	}
   147  	return 1 << uint(f-x86.REG_X0+16)
   148  }
   149  
   150  func BtoF(b uint64) int {
   151  	b &= 0xFFFF0000
   152  	if b == 0 {
   153  		return 0
   154  	}
   155  	return gc.Bitno(b) - 16 + x86.REG_X0
   156  }