github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/ebpf/div.go (about)

     1  package ebpf
     2  
     3  import "fmt"
     4  
     5  var _ Instruction = (*Div32)(nil)
     6  
     7  type Div32 struct {
     8  	Dest  Register
     9  	Value int32
    10  }
    11  
    12  func (a *Div32) Raw() ([]RawInstruction, error) {
    13  	return []RawInstruction{
    14  		{Op: BPF_ALU | BPF_K | BPF_DIV, Reg: NewReg(0, a.Dest), Imm: a.Value},
    15  	}, nil
    16  }
    17  
    18  func (a *Div32) String() string {
    19  	return fmt.Sprintf("w%s /= %d", a.Dest, a.Value)
    20  }
    21  
    22  var _ Instruction = (*Div64)(nil)
    23  
    24  type Div64 struct {
    25  	Dest  Register
    26  	Value int32
    27  }
    28  
    29  func (a *Div64) Raw() ([]RawInstruction, error) {
    30  	return []RawInstruction{
    31  		{Op: BPF_ALU64 | BPF_K | BPF_DIV, Reg: NewReg(0, a.Dest), Imm: a.Value},
    32  	}, nil
    33  }
    34  
    35  func (a *Div64) String() string {
    36  	return fmt.Sprintf("r%s /= %d", a.Dest, a.Value)
    37  }
    38  
    39  var _ Instruction = (*Div32Register)(nil)
    40  
    41  type Div32Register struct {
    42  	Dest Register
    43  	Src  Register
    44  }
    45  
    46  func (a *Div32Register) Raw() ([]RawInstruction, error) {
    47  	return []RawInstruction{
    48  		{Op: BPF_ALU | BPF_X | BPF_DIV, Reg: NewReg(a.Src, a.Dest)},
    49  	}, nil
    50  }
    51  
    52  func (a *Div32Register) String() string {
    53  	return fmt.Sprintf("w%s /= w%d", a.Dest, a.Src)
    54  }
    55  
    56  var _ Instruction = (*Div64Register)(nil)
    57  
    58  type Div64Register struct {
    59  	Dest Register
    60  	Src  Register
    61  }
    62  
    63  func (a *Div64Register) Raw() ([]RawInstruction, error) {
    64  	return []RawInstruction{
    65  		{Op: BPF_ALU64 | BPF_X | BPF_DIV, Reg: NewReg(a.Src, a.Dest)},
    66  	}, nil
    67  }
    68  
    69  func (a *Div64Register) String() string {
    70  	return fmt.Sprintf("r%s /= r%s", a.Dest, a.Src)
    71  }