Skip to content

Commit

Permalink
Added a basic interface for IR bitwise binops.
Browse files Browse the repository at this point in the history
  • Loading branch information
Goubermouche committed Jan 29, 2024
1 parent 3011310 commit b75701e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
4 changes: 1 addition & 3 deletions source/compiler/test/main.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
// - numerical literals throw cast warnings, which shouldnt happen

// TODO:
// - check for memory oversteps in the parser
// - add namespaces to error messages, whenever applicable (ie. x::y::test)
// - namespace directives should probably be a part of the function signature?

// - cleanup ir gen alignment sizes (u64 vs u32 vs u16)
// - conjunction & disjunction operators

// CHECK:
// - bool b = 100 > 200;
Expand Down
15 changes: 15 additions & 0 deletions source/intermediate_representation/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ namespace sigma::ir {
return get_insert_point_checked()->create_cmp_ige(a, b, is_signed);
}

auto builder::create_not(handle<node> value) const -> handle<node> {
DEBUG_PRINT("creating not");
return get_insert_point_checked()->create_not(value);
}

auto builder::create_and(handle<node> a, handle<node> b) const -> handle<node> {
DEBUG_PRINT("creating and");
return get_insert_point_checked()->create_and(a, b);
}

auto builder::create_or(handle<node> a, handle<node> b) const -> handle<node> {
DEBUG_PRINT("creating or");
return get_insert_point_checked()->create_or(a, b);
}

auto builder::create_sxt(handle<node> src, data_type dt) const -> handle<node> {
DEBUG_PRINT("creating sxt");
return get_insert_point_checked()->create_sxt(src, dt);
Expand Down
5 changes: 5 additions & 0 deletions source/intermediate_representation/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ namespace sigma::ir {
auto create_cmp_igt(handle<node> a, handle<node> b, bool is_signed) const -> handle<node>;
auto create_cmp_ige(handle<node> a, handle<node> b, bool is_signed) const -> handle<node>;

// bitwise operations
auto create_not(handle<node> value) const -> handle<node>;
auto create_and(handle<node> a, handle<node> b) const -> handle<node>;
auto create_or(handle<node> a, handle<node> b) const -> handle<node>;

// casting
auto create_sxt(handle<node> src, data_type dt) const -> handle<node>;
auto create_zxt(handle<node> src, data_type dt) const -> handle<node>;
Expand Down
41 changes: 28 additions & 13 deletions source/intermediate_representation/node_hierarchy/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,42 @@ namespace sigma::ir {
}

auto function::create_cmp_eq(handle<node> a, handle<node> b) -> handle<node> {
return create_cmp(node::type::CMP_EQ, a, b);
return create_cmp_operation(node::type::CMP_EQ, a, b);
}

auto function::create_cmp_ne(handle<node> a, handle<node> b) -> handle<node> {
return create_cmp(node::type::CMP_NE, a, b);
return create_cmp_operation(node::type::CMP_NE, a, b);
}

auto function::create_cmp_ilt(handle<node> a, handle<node> b, bool is_signed) -> handle<node> {
return create_cmp(is_signed ? node::type::CMP_SLT : node::type::CMP_ULT, a, b);
return create_cmp_operation(is_signed ? node::type::CMP_SLT : node::type::CMP_ULT, a, b);
}

auto function::create_cmp_ile(handle<node> a, handle<node> b, bool is_signed) -> handle<node> {
return create_cmp(is_signed ? node::type::CMP_SLE : node::type::CMP_ULE, a, b);
return create_cmp_operation(is_signed ? node::type::CMP_SLE : node::type::CMP_ULE, a, b);
}

auto function::create_cmp_igt(handle<node> a, handle<node> b, bool is_signed) -> handle<node> {
return create_cmp(is_signed ? node::type::CMP_SLT : node::type::CMP_ULT, a, b);
return create_cmp_operation(is_signed ? node::type::CMP_SLT : node::type::CMP_ULT, a, b);
}

auto function::create_cmp_ige(handle<node> a, handle<node> b, bool is_signed) -> handle<node> {
return create_cmp(is_signed ? node::type::CMP_SLE : node::type::CMP_ULE, a, b);
return create_cmp_operation(is_signed ? node::type::CMP_SLE : node::type::CMP_ULE, a, b);
}

auto function::create_not(handle<node> value) -> handle<node> {
const handle<node> n = create_node<utility::empty_property>(node::type::NOT, 2);
n->inputs[1] = value;
return n;
}

auto function::create_and(handle<node> a, handle<node> b) -> handle<node> {
// bitwise operators can't wrap
return create_binary_arithmetic_operation(node::type::AND, a, b, arithmetic_behaviour::NONE);
}

auto function::create_or(handle<node> a, handle<node> b) -> handle<node> {
return create_binary_arithmetic_operation(node::type::OR, a, b, arithmetic_behaviour::NONE);
}

void function::create_store(handle<node> destination, handle<node> value, u32 alignment, bool is_volatile) {
Expand Down Expand Up @@ -306,14 +321,14 @@ namespace sigma::ir {

auto function::create_binary_arithmetic_operation(node::type type, handle<node> left, handle<node> right, arithmetic_behaviour behaviour) -> handle<node> {
ASSERT(left->dt == right->dt, "data types of the two operands do not match");
const handle<node> operation = create_node<binary_integer_op>(type, 3);
const handle<node> op = create_node<binary_integer_op>(type, 3);

operation->get<binary_integer_op>().behaviour = behaviour;
operation->inputs[1] = left;
operation->inputs[2] = right;
operation->dt = left->dt;
op->get<binary_integer_op>().behaviour = behaviour;
op->inputs[1] = left;
op->inputs[2] = right;
op->dt = left->dt;

return operation;
return op;
}

auto function::create_unary_operation(node::type type, data_type dt, handle<node> src) -> handle<node> {
Expand All @@ -323,7 +338,7 @@ namespace sigma::ir {
return operation;
}

auto function::create_cmp(node::type type, handle<node> a, handle<node> b) -> handle<node> {
auto function::create_cmp_operation(node::type type, handle<node> a, handle<node> b) -> handle<node> {
ASSERT(a->dt == b->dt, "data types of the two operands do not match");
const handle<node> cmp = create_node<compare_op>(type, 3);

Expand Down
8 changes: 7 additions & 1 deletion source/intermediate_representation/node_hierarchy/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ namespace sigma::ir {
auto create_cmp_igt(handle<node> a, handle<node> b, bool is_signed) -> handle<node>;
auto create_cmp_ige(handle<node> a, handle<node> b, bool is_signed) -> handle<node>;

// bitwise operations
auto create_not(handle<node> value) -> handle<node>;
auto create_and(handle<node> a, handle<node> b) -> handle<node>;
auto create_or(handle<node> a, handle<node> b) -> handle<node>;

void create_store(handle<node> destination, handle<node> value, u32 alignment, bool is_volatile);
auto create_load(handle<node> value_to_load, data_type data_type, u32 alignment, bool is_volatile) -> handle<node>;
auto create_array_access(handle<node> base, handle<node> index, i64 stride) -> handle <node>;
Expand All @@ -82,8 +87,9 @@ namespace sigma::ir {
auto create_call(const function_signature& function_sig, handle<node> callee_symbol_address,const std::vector<handle<node>>& arguments) -> handle<node>;

auto create_binary_arithmetic_operation(node::type type, handle<node> left, handle<node> right, arithmetic_behaviour behaviour) -> handle<node>;

auto create_unary_operation(node::type type, data_type dt, handle<node> src) -> handle<node>;
auto create_cmp(node::type type, handle<node> a, handle<node> b) -> handle<node>;
auto create_cmp_operation(node::type type, handle<node> a, handle<node> b) -> handle<node>;

auto create_projection(data_type dt, handle<node> source, u64 index) -> handle<node>;
auto append_memory(handle<node> memory) const -> handle<node>;
Expand Down

0 comments on commit b75701e

Please sign in to comment.