Constant rem

This commit is contained in:
David Gonzalez Martin 2025-02-21 12:52:33 -06:00
parent 1c8fd82e1f
commit 60b078caf3
4 changed files with 29 additions and 0 deletions

View File

@ -560,6 +560,14 @@ pub const Builder = opaque {
return api.LLVMBuildUDiv(builder, left, right, "");
}
pub fn create_srem(builder: *Builder, left: *Value, right: *Value) *Value {
return api.LLVMBuildSRem(builder, left, right, "");
}
pub fn create_urem(builder: *Builder, left: *Value, right: *Value) *Value {
return api.LLVMBuildURem(builder, left, right, "");
}
pub fn create_ret_void(builder: *Builder) void {
builder.create_ret(null);
}

View File

@ -296,6 +296,8 @@ const Converter = struct {
mul,
udiv,
sdiv,
urem,
srem,
};
fn parse_value(noalias converter: *Converter, noalias thread: *llvm.Thread, expected_type: Type) *llvm.Value {
@ -322,6 +324,8 @@ const Converter = struct {
.mul => thread.builder.create_mul(left, right),
.sdiv => thread.builder.create_sdiv(left, right),
.udiv => thread.builder.create_udiv(left, right),
.srem => thread.builder.create_srem(left, right),
.urem => thread.builder.create_urem(left, right),
};
const ch = converter.content[converter.offset];
@ -346,6 +350,13 @@ const Converter = struct {
false => break :blk .udiv,
}
},
'%' => blk: {
converter.offset += 1;
switch (expected_type.signedness) {
true => break :blk .srem,
false => break :blk .urem,
}
},
else => os.abort(),
};
@ -728,3 +739,7 @@ test "constant mul" {
test "constant div" {
try invoke("constant_div");
}
test "constant rem" {
try invoke("constant_rem");
}

View File

@ -24,6 +24,8 @@ pub extern fn LLVMBuildSub(builder: *llvm.Builder, left: *llvm.Value, right: *ll
pub extern fn LLVMBuildMul(builder: *llvm.Builder, left: *llvm.Value, right: *llvm.Value, name: [*:0]const u8) *llvm.Value;
pub extern fn LLVMBuildSDiv(builder: *llvm.Builder, left: *llvm.Value, right: *llvm.Value, name: [*:0]const u8) *llvm.Value;
pub extern fn LLVMBuildUDiv(builder: *llvm.Builder, left: *llvm.Value, right: *llvm.Value, name: [*:0]const u8) *llvm.Value;
pub extern fn LLVMBuildSRem(builder: *llvm.Builder, left: *llvm.Value, right: *llvm.Value, name: [*:0]const u8) *llvm.Value;
pub extern fn LLVMBuildURem(builder: *llvm.Builder, left: *llvm.Value, right: *llvm.Value, name: [*:0]const u8) *llvm.Value;
pub extern fn LLVMTypeOf(value: *llvm.Value) *llvm.Type;
pub extern fn LLVMGlobalGetValueType(value: *llvm.GlobalValue) *llvm.Type;

4
tests/constant_rem.bbb Normal file
View File

@ -0,0 +1,4 @@
[export] main = fn [cc(c)] () s32
{
return 5 % 5;
}