github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/emulator/inst_sub.go (about) 1 package emulator 2 3 import ( 4 "fmt" 5 6 "github.com/dylandreimerink/gobpfld/ebpf" 7 ) 8 9 var _ Instruction = (*Sub32)(nil) 10 11 type Sub32 struct { 12 ebpf.Sub32 13 } 14 15 func (i *Sub32) Clone() Instruction { 16 c := *i 17 return &c 18 } 19 20 func (i *Sub32) 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 = (*Sub64)(nil) 35 36 type Sub64 struct { 37 ebpf.Sub64 38 } 39 40 func (i *Sub64) Clone() Instruction { 41 c := *i 42 return &c 43 } 44 45 func (i *Sub64) 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 = (*Sub32Register)(nil) 60 61 type Sub32Register struct { 62 ebpf.Sub32Register 63 } 64 65 func (i *Sub32Register) Clone() Instruction { 66 c := *i 67 return &c 68 } 69 70 func (i *Sub32Register) 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 = (*Sub64Register)(nil) 90 91 type Sub64Register struct { 92 ebpf.Sub64Register 93 } 94 95 func (i *Sub64Register) Clone() Instruction { 96 c := *i 97 return &c 98 } 99 100 func (i *Sub64Register) 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 }