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

     1  package emulator
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dylandreimerink/gobpfld/ebpf"
     7  )
     8  
     9  var _ Instruction = (*Neg32)(nil)
    10  
    11  type Neg32 struct {
    12  	ebpf.Neg32
    13  }
    14  
    15  func (i *Neg32) Clone() Instruction {
    16  	c := *i
    17  	return &c
    18  }
    19  
    20  func (i *Neg32) Execute(vm *VM) error {
    21  	dv, dr, err := readReg(vm, i.Dest)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	err = dr.Assign(int64(-int32(dv)))
    27  	if err != nil {
    28  		return fmt.Errorf("assign value %s: %w", i.Dest.String(), err)
    29  	}
    30  
    31  	return nil
    32  }
    33  
    34  var _ Instruction = (*Neg64)(nil)
    35  
    36  type Neg64 struct {
    37  	ebpf.Neg64
    38  }
    39  
    40  func (i *Neg64) Clone() Instruction {
    41  	c := *i
    42  	return &c
    43  }
    44  
    45  func (i *Neg64) Execute(vm *VM) error {
    46  	dv, dr, err := readReg(vm, i.Dest)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	err = dr.Assign(-dv)
    52  	if err != nil {
    53  		return fmt.Errorf("assign value %s: %w", i.Dest.String(), err)
    54  	}
    55  
    56  	return nil
    57  }