Constant mul

This commit is contained in:
David Gonzalez Martin 2025-02-21 11:34:20 -06:00
parent 892360a084
commit 7bd79cb741
4 changed files with 20 additions and 1 deletions

View File

@ -548,6 +548,10 @@ pub const Builder = opaque {
return api.LLVMBuildSub(builder, left, right, "");
}
pub fn create_mul(builder: *Builder, left: *Value, right: *Value) *Value {
return api.LLVMBuildMul(builder, left, right, "");
}
pub fn create_ret_void(builder: *Builder) void {
builder.create_ret(null);
}

View File

@ -268,8 +268,9 @@ const Converter = struct {
const ExpressionState = enum {
none,
sub,
add,
sub,
mul,
};
fn parse_value(noalias converter: *Converter, noalias thread: *llvm.Thread, expected_type: *llvm.Type) *llvm.Value {
@ -293,6 +294,7 @@ const Converter = struct {
.none => current_value,
.sub => thread.builder.create_sub(left, right),
.add => thread.builder.create_add(left, right),
.mul => thread.builder.create_mul(left, right),
};
const ch = converter.content[converter.offset];
@ -306,6 +308,10 @@ const Converter = struct {
converter.offset += 1;
break :blk .add;
},
'*' => blk: {
converter.offset += 1;
break :blk .mul;
},
else => os.abort(),
};
@ -693,3 +699,7 @@ test "constant add" {
test "constant sub" {
try invoke("constant_sub");
}
test "constant mul" {
try invoke("constant_mul");
}

View File

@ -21,6 +21,7 @@ pub extern fn LLVMPositionBuilderAtEnd(builder: *llvm.Builder, basic_block: *llv
pub extern fn LLVMBuildRet(builder: *llvm.Builder, value: ?*llvm.Value) void;
pub extern fn LLVMBuildAdd(builder: *llvm.Builder, left: *llvm.Value, right: *llvm.Value, name: [*:0]const u8) *llvm.Value;
pub extern fn LLVMBuildSub(builder: *llvm.Builder, left: *llvm.Value, right: *llvm.Value, name: [*:0]const u8) *llvm.Value;
pub extern fn LLVMBuildMul(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_mul.bbb Normal file
View File

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