Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sdiv and smod instruction support to bpf2c #3165

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/isa-support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ opcode src imm off description
0x32 any any any (deprecated, implementation-specific) no no no (none)
0x33 any any any (deprecated, implementation-specific) no no no (none)
0x34 0x0 any 0 dst = (u32)((imm != 0) ? ((u32)dst / (u32)imm) : 0) Y Y Y alu-arith
0x34 0x0 any 1 dst = (u32)((imm != 0) ? ((s32)dst s/ imm) : 0) Y no no ???
0x34 0x0 any 1 dst = (u32)((imm != 0) ? ((s32)dst s/ imm) : 0) Y no Y ???
0x35 0x0 any any if dst >= imm goto +offset Y Y Y jge-imm
0x36 0x0 any any if (u32)dst >= imm goto +offset Y Y Y jge32-imm
0x37 0x0 any 0 dst = (imm != 0) ? (dst / (u32)imm) : 0 Y Y Y alu64-arith
0x37 0x0 any 1 dst = (imm != 0) ? (dst s/ imm) : 0 Y no no ???
0x37 0x0 any 1 dst = (imm != 0) ? (dst s/ imm) : 0 Y no Y ???
0x38 any any any (deprecated, implementation-specific) no no no (none)
0x39 any any any (deprecated, implementation-specific) no no no (none)
0x3a any any any (deprecated, implementation-specific) no no no (none)
0x3b any any any (deprecated, implementation-specific) no no no (none)
0x3c any 0x00 0 dst = (u32)((src != 0) ? ((u32)dst / (u32)src) : 0) Y Y Y alu-arith
0x3c any 0x00 1 dst = (u32)((src != 0) ? ((s32)dst s/ (s32)src) : 0) Y no no ???
0x3c any 0x00 1 dst = (u32)((src != 0) ? ((s32)dst s/ (s32)src) : 0) Y no Y ???
0x3d any 0x00 any if dst >= src goto +offset Y Y Y prime
0x3e any 0x00 any if (u32)dst >= (u32)src goto +offset Y Y Y jge32-reg
0x3f any 0x00 0 dst = (src !+ 0) ? (dst / src) : 0 Y Y Y alu64-arith
0x3f any 0x00 1 dst = (src !+ 0) ? (dst s/ src) : 0 Y no no ???
0x3f any 0x00 1 dst = (src !+ 0) ? (dst s/ src) : 0 Y no Y ???
0x40 any any any (deprecated, implementation-specific) no no no (none)
0x41 any any any (deprecated, implementation-specific) no no no (none)
0x42 any any any (deprecated, implementation-specific) no no no (none)
Expand Down
13 changes: 6 additions & 7 deletions tools/bpf2c/bpf_code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ bpf_code_generator::encode_instructions(const bpf_code_generator::unsafe_string&
bool is64bit = (inst.opcode & EBPF_CLS_MASK) == EBPF_CLS_ALU64;
AluOperations operation = static_cast<AluOperations>(inst.opcode >> 4);
std::string swap_function;
std::string type;
switch (operation) {
case AluOperations::Add:
output.lines.push_back(std::format("{} += {};", destination, source));
Expand All @@ -892,15 +893,13 @@ bpf_code_generator::encode_instructions(const bpf_code_generator::unsafe_string&
break;
case AluOperations::Div:
if (is64bit) {
output.lines.push_back(
std::format("{} = {} ? ({} / {}) : 0;", destination, source, destination, source));
type = (inst.offset == 1) ? "(int64_t)" : "";
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
output.lines.push_back(std::format(
"{} = {} ? ({}{} / {}{}) : 0;", destination, source, type, destination, type, source));
} else {
type = (inst.offset == 1) ? "(int32_t)" : "(uint32_t)";
output.lines.push_back(std::format(
"{} = (uint32_t){} ? (uint32_t){} / (uint32_t){} : 0;",
destination,
source,
destination,
source));
"{} = (uint32_t){} ? {}{} / {}{} : 0;", destination, source, type, destination, type, source));
}
break;
case AluOperations::Or:
Expand Down
Loading