Skip to content

Commit

Permalink
stub comparators
Browse files Browse the repository at this point in the history
refactor and implement comparisons

s/comp/cmp
  • Loading branch information
marctrem committed Sep 10, 2024
1 parent 18bbf83 commit fa1862e
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions contrib/codegen/generate_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ def eval_(expr, filt, label_t, label_f):

elif expr0_str == 'eq':
eval_equal(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '<':
eval_less(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '<=':
eval_less_or_equal(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '>':
eval_greater(filt, expr[1], expr[2], label_t, label_f)

elif expr0_str == '>=':
eval_greater_or_equal(filt, expr[1], expr[2], label_t, label_f)

else:
print(expr0_str)
raise("unknown fn")
Expand Down Expand Up @@ -239,7 +252,7 @@ def eval_bit_and(filt, op1, op2, label_t, label_f):
if label_t and label_f:
filt.append(ReloCondJump("BPF_JMP | BPF_JEQ | BPF_K, 0", label_f, label_t))

def eval_equal(filt, op1, op2, label_t, label_f):
def gen_cmp(filt, op1, op2, label_t, label_f, cmp_instr):
op1_type, op2_type = type(op1), type(op2)

if op1_type is edn_format.Symbol and op2_type is edn_format.Symbol:
Expand All @@ -253,18 +266,32 @@ def eval_equal(filt, op1, op2, label_t, label_f):
# eval op1 and do operation with op2 imm
eval_(op1, filt, None, None)
# accu now contains the eval res of op1
filt.append(ReloCondJump("BPF_JMP | BPF_JEQ | BPF_K, %s" % str(op2), label_t, label_f))
filt.append(ReloCondJump(f"BPF_JMP | {cmp_instr} | BPF_K, %s" % str(op2), label_t, label_f))

elif op2_type is tuple and op1_type is not tuple:
# eval op2 and do operation with op1 imm
eval_(op2, None, None)
# accu now contains the eval res of op2
filt.append(ReloCondJump("BPF_JMP | BPF_JEQ | BPF_K, %s" % str(op1), label_t, label_f))
filt.append(ReloCondJump(f"BPF_JMP | {cmp_instr} | BPF_K, %s" % str(op1), label_t, label_f))
else:
# This is unsupported because I didn't pick a calling convention and this means that accu and x should be saved to scratch.
# It's very easy to achieve but there's no need for it yet. It's basically register allocation over BPF scratch.
raise("unsupported")
raise("unsupported")

def eval_equal(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JEQ")

def eval_less(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JLT")

def eval_less_or_equal(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JLE")

def eval_greater(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JGT")

def eval_greater_or_equal(filt, op1, op2, label_t, label_f):
gen_cmp(filt, op1, op2, label_t, label_f, "BPF_JGE")

def resplit_lines(lines):
i = 0
Expand Down

0 comments on commit fa1862e

Please sign in to comment.