Skip to content
Snippets Groups Projects
Commit 266959d2 authored by Martin Teichmann's avatar Martin Teichmann
Browse files

make better use of format characters

the format characters already include the information whether they
are signed or long, no need to add this information again.
parent fc2f6235
No related branches found
No related tags found
2 merge requests!9support Leybold turbo pump,!8Draft: move the index field to the end of packet
......@@ -19,7 +19,7 @@ from itertools import chain
from mmap import mmap
from struct import pack_into, unpack_from, calcsize
from .ebpf import FuncId, Map, Memory, MemoryDesc, Opcode, SubProgram
from .ebpf import FuncId, Map, MemoryDesc, Opcode, SubProgram
from .bpf import create_map, lookup_elem, MapType, MapFlags, update_elem
......
......@@ -721,14 +721,13 @@ class IAdd:
class Memory(Expression):
bits_to_opcode = {32: Opcode.W, 16: Opcode.H, 8: Opcode.B, 64: Opcode.DW}
fmt_to_opcode = {'I': Opcode.W, 'H': Opcode.H, 'B': Opcode.B, 'Q': Opcode.DW,
'i': Opcode.W, 'h': Opcode.H, 'b': Opcode.B, 'q': Opcode.DW}
'i': Opcode.W, 'h': Opcode.H, 'b': Opcode.B, 'q': Opcode.DW,
'A': Opcode.W}
def __init__(self, ebpf, fmt, address, signed=False, long=False):
def __init__(self, ebpf, fmt, address):
self.ebpf = ebpf
self.fmt = fmt
self.address = address
self.signed = signed
self.long = long
def __iadd__(self, value):
if self.fmt in "qQiI":
......@@ -748,10 +747,10 @@ class Memory(Expression):
with self.ebpf.get_free_register(dst) as dst:
self.ebpf.append(Opcode.LD + self.fmt_to_opcode[self.fmt], dst,
self.address.left.no, self.address.right, 0)
yield dst, self.long, self.signed
yield dst, self.fmt in "QqA", self.fmt.islower()
else:
with super().calculate(dst, long, signed, force) as (dst, _, _):
yield dst, self.long, self.signed
yield dst, self.fmt in "QqA", self.fmt.islower()
@contextmanager
def get_address(self, dst, long, signed, force=False):
......@@ -761,6 +760,10 @@ class Memory(Expression):
def contains(self, no):
return self.address.contains(no)
@property
def signed(self):
return isinstance(self.fmt, str) and self.fmt.islower()
class MemoryDesc:
"""A base class used by descriptors for memory
......@@ -774,8 +777,7 @@ class MemoryDesc:
return self
fmt, addr = self.fmt_addr(instance)
return Memory(instance.ebpf, fmt,
instance.ebpf.r[self.base_register] + addr,
fmt.islower())
instance.ebpf.r[self.base_register] + addr)
def __set__(self, instance, value):
ebpf = instance.ebpf
......@@ -822,11 +824,9 @@ class LocalVar(MemoryDesc):
class MemoryMap:
def __init__(self, ebpf, fmt, signed=False, long=False):
def __init__(self, ebpf, fmt):
self.ebpf = ebpf
self.fmt = fmt
self.long = long
self.signed = signed
def __setitem__(self, addr, value):
with ExitStack() as exitStack:
......@@ -860,7 +860,7 @@ class MemoryMap:
def __getitem__(self, addr):
if isinstance(addr, Register):
addr = addr + 0
return Memory(self.ebpf, self.fmt, addr, self.signed, self.long)
return Memory(self.ebpf, self.fmt, addr)
class Map(ABC):
......@@ -1017,8 +1017,8 @@ class EBPF:
self.mB = MemoryMap(self, "B")
self.mH = MemoryMap(self, "H")
self.mI = MemoryMap(self, "I")
self.mA = MemoryMap(self, "I", False, True)
self.mQ = MemoryMap(self, "Q", False, True)
self.mA = MemoryMap(self, "A") # actually I, but treat as Q
self.mQ = MemoryMap(self, "Q")
self.r = RegisterArray(self, True, False)
self.sr = RegisterArray(self, True, True)
......
......@@ -18,7 +18,7 @@
from contextlib import contextmanager
from struct import pack, unpack, unpack
from .ebpf import AssembleError, Expression, Opcode, Map, FuncId, Memory
from .ebpf import AssembleError, Expression, Opcode, Map, FuncId
from .bpf import create_map, lookup_elem, MapType, update_elem
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment