Merge pull request #194 from birth-software/xor

Implement 'xor'
This commit is contained in:
David 2024-05-25 18:35:15 -06:00 committed by GitHub
commit 09b6869ed0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 3 deletions

View File

@ -568,6 +568,8 @@ const Parser = struct{
and_assign,
@"or",
or_assign,
@"xor",
xor_assign,
};
fn parse_expression(parser: *Parser, analyzer: *Analyzer, thread: *Thread, file: *File, ty: ?*Type, side: Side) *Value {
@ -591,7 +593,7 @@ const Parser = struct{
.none => {
previous_value = current_value;
},
.add, .sub, .@"and", .@"or" => {
.add, .sub, .@"and", .@"or", .xor => {
const add = thread.integer_binary_operations.append(.{
.instruction = .{
.value = .{
@ -606,7 +608,7 @@ const Parser = struct{
.left = previous_value,
.right = current_value,
.id = switch (current_operation) {
.none, .add_assign, .sub_assign, .and_assign, .or_assign => unreachable,
.none, .add_assign, .sub_assign, .and_assign, .or_assign, .xor_assign => unreachable,
inline else => |co| @field(IntegerBinaryOperation.Id, @tagName(co)),
},
.type = if (ty) |t| t else current_value.get_type(),
@ -614,7 +616,7 @@ const Parser = struct{
_ = analyzer.current_basic_block.instructions.append(&add.instruction);
previous_value = &add.instruction.value;
},
.add_assign, .sub_assign, .and_assign, .or_assign => unreachable,
.add_assign, .sub_assign, .and_assign, .or_assign, .xor_assign => unreachable,
}
switch (src[parser.i]) {
@ -675,6 +677,20 @@ const Parser = struct{
parser.skip_space(src);
},
'^' => {
current_operation = .@"xor";
parser.i += 1;
switch (src[parser.i]) {
'=' => {
current_operation = .xor_assign;
parser.i += 1;
},
else => {},
}
parser.skip_space(src);
},
else => @panic((src.ptr + parser.i)[0..1]),
}
}
@ -1046,6 +1062,7 @@ const IntegerBinaryOperation = struct {
sub,
@"and",
@"or",
@"xor",
};
};
@ -2294,6 +2311,7 @@ fn worker_thread(thread_index: u32, cpu_count: *u32) void {
.sub => builder.createSub(left, right, name, name.len, no_unsigned_wrapping, no_signed_wrapping),
.@"and" => builder.createAnd(left, right, name, name.len),
.@"or" => builder.createOr(left, right, name, name.len),
.@"xor" => builder.createXor(left, right, name, name.len),
};
},
.call => block: {

View File

@ -0,0 +1,7 @@
fn [cc(.c)] main [export] () s32 {
>a: s32 = 561;
>b: s32 = 124;
>c = a ^ b;
>d = a ^ b;
return c ^ d;
}