Implement 'mul'

This commit is contained in:
David Gonzalez Martin 2024-05-26 11:10:21 -06:00
parent e66fa4f70d
commit 48db2c9265
2 changed files with 26 additions and 3 deletions

View File

@ -564,6 +564,8 @@ const Parser = struct{
add_assign, add_assign,
sub, sub,
sub_assign, sub_assign,
mul,
mul_assign,
@"and", @"and",
and_assign, and_assign,
@"or", @"or",
@ -599,7 +601,7 @@ const Parser = struct{
.none => { .none => {
previous_value = current_value; previous_value = current_value;
}, },
.add, .sub, .@"and", .@"or", .xor, .shift_left, .arithmetic_shift_right, .logical_shift_right => { .add, .sub, .mul, .@"and", .@"or", .xor, .shift_left, .arithmetic_shift_right, .logical_shift_right => {
const add = thread.integer_binary_operations.append(.{ const add = thread.integer_binary_operations.append(.{
.instruction = .{ .instruction = .{
.value = .{ .value = .{
@ -614,7 +616,7 @@ const Parser = struct{
.left = previous_value, .left = previous_value,
.right = current_value, .right = current_value,
.id = switch (current_operation) { .id = switch (current_operation) {
.none, .add_assign, .sub_assign, .and_assign, .or_assign, .xor_assign, .shift_left_assign, .arithmetic_shift_right_assign, .logical_shift_right_assign => unreachable, .none, .add_assign, .sub_assign, .mul_assign, .and_assign, .or_assign, .xor_assign, .shift_left_assign, .arithmetic_shift_right_assign, .logical_shift_right_assign => unreachable,
inline else => |co| @field(IntegerBinaryOperation.Id, @tagName(co)), inline else => |co| @field(IntegerBinaryOperation.Id, @tagName(co)),
}, },
.type = if (ty) |t| t else current_value.get_type(), .type = if (ty) |t| t else current_value.get_type(),
@ -622,7 +624,7 @@ const Parser = struct{
_ = analyzer.current_basic_block.instructions.append(&add.instruction); _ = analyzer.current_basic_block.instructions.append(&add.instruction);
previous_value = &add.instruction.value; previous_value = &add.instruction.value;
}, },
.add_assign, .sub_assign, .and_assign, .or_assign, .xor_assign, .shift_left_assign, .logical_shift_right_assign, .arithmetic_shift_right_assign => unreachable, .add_assign, .sub_assign, .mul_assign, .and_assign, .or_assign, .xor_assign, .shift_left_assign, .logical_shift_right_assign, .arithmetic_shift_right_assign => unreachable,
} }
switch (src[parser.i]) { switch (src[parser.i]) {
@ -655,6 +657,20 @@ const Parser = struct{
parser.skip_space(src); parser.skip_space(src);
}, },
'*' => {
current_operation = .mul;
parser.i += 1;
switch (src[parser.i]) {
'=' => {
current_operation = .mul_assign;
parser.i += 1;
},
else => {},
}
parser.skip_space(src);
},
'&' => { '&' => {
current_operation = .@"and"; current_operation = .@"and";
parser.i += 1; parser.i += 1;
@ -1121,6 +1137,7 @@ const IntegerBinaryOperation = struct {
const Id = enum{ const Id = enum{
add, add,
sub, sub,
mul,
@"and", @"and",
@"or", @"or",
@"xor", @"xor",
@ -2374,6 +2391,7 @@ fn worker_thread(thread_index: u32, cpu_count: *u32) void {
break :block switch (integer_binary_operation.id) { break :block switch (integer_binary_operation.id) {
.add => builder.createAdd(left, right, name, name.len, no_unsigned_wrapping, no_signed_wrapping), .add => builder.createAdd(left, right, name, name.len, no_unsigned_wrapping, no_signed_wrapping),
.sub => builder.createSub(left, right, name, name.len, no_unsigned_wrapping, no_signed_wrapping), .sub => builder.createSub(left, right, name, name.len, no_unsigned_wrapping, no_signed_wrapping),
.mul => builder.createMultiply(left, right, name, name.len, no_unsigned_wrapping, no_signed_wrapping),
.@"and" => builder.createAnd(left, right, name, name.len), .@"and" => builder.createAnd(left, right, name, name.len),
.@"or" => builder.createOr(left, right, name, name.len), .@"or" => builder.createOr(left, right, name, name.len),
.@"xor" => builder.createXor(left, right, name, name.len), .@"xor" => builder.createXor(left, right, name, name.len),

View File

@ -0,0 +1,5 @@
fn [cc(.c)] main [export] () s32 {
>a: s32 = 5;
>b: s32 = 4;
return (a * b) - (a * b);
}