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

     1  package emulator
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dylandreimerink/gobpfld/ebpf"
     7  )
     8  
     9  var _ Instruction = (*Mul32)(nil)
    10  
    11  type Mul32 struct {
    12  	ebpf.Mul32
    13  }
    14  
    15  func (i *Mul32) Clone() Instruction {
    16  	c := *i
    17  	return &c
    18  }
    19  
    20  func (i *Mul32) Execute(vm *VM) error {
    21  	rv, r, err := readReg(vm, i.Dest)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	err = r.Assign(int64(int32(rv) * i.Value))
    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 = (*Mul64)(nil)
    35  
    36  type Mul64 struct {
    37  	ebpf.Mul64
    38  }
    39  
    40  func (i *Mul64) Clone() Instruction {
    41  	c := *i
    42  	return &c
    43  }
    44  
    45  func (i *Mul64) Execute(vm *VM) error {
    46  	rv, r, err := readReg(vm, i.Dest)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	err = r.Assign(rv * int64(i.Value))
    52  	if err != nil {
    53  		return fmt.Errorf("assign value  %s: %w", i.Dest.String(), err)
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  var _ Instruction = (*Mul32Register)(nil)
    60  
    61  type Mul32Register struct {
    62  	ebpf.Mul32Register
    63  }
    64  
    65  func (i *Mul32Register) Clone() Instruction {
    66  	c := *i
    67  	return &c
    68  }
    69  
    70  func (i *Mul32Register) Execute(vm *VM) error {
    71  	dv, dr, err := readReg(vm, i.Dest)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	sv, _, err := readReg(vm, i.Src)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	err = dr.Assign(int64(int32(dv) * int32(sv)))
    82  	if err != nil {
    83  		return fmt.Errorf("assign value %s: %w", i.Dest.String(), err)
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  var _ Instruction = (*Mul64Register)(nil)
    90  
    91  type Mul64Register struct {
    92  	ebpf.Mul64Register
    93  }
    94  
    95  func (i *Mul64Register) Clone() Instruction {
    96  	c := *i
    97  	return &c
    98  }
    99  
   100  func (i *Mul64Register) Execute(vm *VM) error {
   101  	dv, dr, err := readReg(vm, i.Dest)
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	sv, _, err := readReg(vm, i.Src)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	err = dr.Assign(dv * sv)
   112  	if err != nil {
   113  		return fmt.Errorf("assign value %s: %w", i.Dest.String(), err)
   114  	}
   115  
   116  	return nil
   117  }