github.com/cloudwego/frugal@v0.1.15/internal/atm/hir/regs.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package hir
    18  
    19  import (
    20      `fmt`
    21  )
    22  
    23  type Register interface {
    24      fmt.Stringer
    25      Z() bool
    26      A() uint8
    27  }
    28  
    29  type (
    30      GenericRegister uint8
    31      PointerRegister uint8
    32  )
    33  
    34  const (
    35      ArgMask    = 0x7f
    36      ArgGeneric = 0x00
    37      ArgPointer = 0x80
    38  )
    39  
    40  const (
    41      R0 GenericRegister = iota
    42      R1
    43      R2
    44      R3
    45      R4
    46      Rz
    47  )
    48  
    49  const (
    50      P0 PointerRegister = iota
    51      P1
    52      P2
    53      P3
    54      P4
    55      P5
    56      Pn
    57  )
    58  
    59  var GenericRegisters = map[GenericRegister]string {
    60      R0: "r0",
    61      R1: "r1",
    62      R2: "r2",
    63      R3: "r3",
    64      R4: "r4",
    65      Rz: "z",
    66  }
    67  
    68  var PointerRegisters = map[PointerRegister]string {
    69      P0: "p0",
    70      P1: "p1",
    71      P2: "p2",
    72      P3: "p3",
    73      P4: "p4",
    74      P5: "p5",
    75      Pn: "nil",
    76  }
    77  
    78  func (self GenericRegister) Z() bool { return self == Rz }
    79  func (self PointerRegister) Z() bool { return self == Pn }
    80  
    81  func (self GenericRegister) A() uint8 { return uint8(self) | ArgGeneric }
    82  func (self PointerRegister) A() uint8 { return uint8(self) | ArgPointer }
    83  
    84  func (self GenericRegister) String() string {
    85      if v := GenericRegisters[self]; v == "" {
    86          panic(fmt.Sprintf("invalid generic register: 0x%02x", uint8(self)))
    87      } else {
    88          return v
    89      }
    90  }
    91  
    92  func (self PointerRegister) String() string {
    93      if v := PointerRegisters[self]; v == "" {
    94          panic(fmt.Sprintf("invalid pointer register: 0x%02x", uint8(self)))
    95      } else {
    96          return v
    97      }
    98  }