Skip to content

Commit

Permalink
Cleaned up the IR type system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Goubermouche committed Dec 21, 2023
1 parent b52eda0 commit db7b2dc
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 84 deletions.
3 changes: 2 additions & 1 deletion source/compiler/test/main.s
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ i32 test(i32 a) {

i32 main() {
bool value = true;
value = false;

if(value) {
printf("true\n");
Expand All @@ -25,5 +24,7 @@ i32 main() {
printf("false\n");
}

puts("Hello, world!\n");

ret 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace sigma::ir {
active_control_node = nullptr;

// just add the edge directly.
ASSERT(control->dt.ty == data_type::CONTROL, "invalid edge");
ASSERT(control->dt == data_type::base::CONTROL, "invalid edge");
add_input_late(target, control);
add_memory_edge(control, memory, target);
}
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace sigma::ir {

auto function::create_signed_integer(i64 value, u8 bit_width) -> handle<node> {
const handle<node> integer = create_node<ir::integer>(node::type::INTEGER_CONSTANT, 1);
integer->dt = data_type(data_type::INTEGER, bit_width);
integer->dt = data_type(data_type::base::INTEGER, bit_width);

auto& property = integer->get<ir::integer>();
property.bit_width = bit_width;
Expand Down Expand Up @@ -256,7 +256,7 @@ namespace sigma::ir {
}

auto function::create_projection(data_type dt, handle<node> source, u64 index) -> handle<node> {
ASSERT(source->dt.ty == data_type::TUPLE, "projections must be of type tuple");
ASSERT(source->dt == data_type::base::TUPLE, "projections must be of type tuple");
const handle<node> projection = create_node<ir::projection>(node::type::PROJECTION, 1);

projection->get<ir::projection>().index = index;
Expand Down
10 changes: 5 additions & 5 deletions source/intermediate_representation/node_hierarchy/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace sigma::ir {
auto node::get_fallthrough() -> handle<node> {
if(
m_type == type::PROJECTION &&
dt.ty == data_type::CONTROL &&
dt == data_type::base::CONTROL &&
inputs[0] != type::ENTRY
) {
// if it's single user and that user is the terminator we can skip it in the fallthrough logic
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace sigma::ir {
case type::MEMSET:
return true;
case type::PROJECTION:
return dt.ty == data_type::CONTROL;
return dt == data_type::base::CONTROL;
// control flow
case type::ENTRY:
case type::REGION:
Expand All @@ -91,11 +91,11 @@ namespace sigma::ir {

auto node::is_control() const -> bool {
// easy case
if (dt.ty == data_type::CONTROL) {
if (dt == data_type::base::CONTROL) {
return true;
}

if (dt.ty != data_type::TUPLE) {
if (dt != data_type::base::TUPLE) {
return false;
}

Expand Down Expand Up @@ -180,7 +180,7 @@ namespace sigma::ir {

auto node::is_mem_out_op() const -> bool {
return
dt.ty == data_type::MEMORY ||
dt == data_type::base::MEMORY ||
(m_type >= type::STORE && m_type < type::ATOMIC_CAS) ||
(m_type >= type::CALL && m_type <= type::SAFE_POINT_POLL);
}
Expand Down
59 changes: 44 additions & 15 deletions source/intermediate_representation/node_hierarchy/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,61 @@
#include <utility/macros.h>

namespace sigma::ir {
data_type::data_type() : ty(UNKNOWN), bit_width(0) {}
data_type::data_type(type t, u8 bw) : ty(t), bit_width(bw) {}
data_type::data_type() : m_base(base::UNKNOWN), m_bit_width(0) {}
data_type::data_type(base base, u8 bit_width) : m_base(base), m_bit_width(bit_width) {}

auto data_type::to_string() const -> std::string {
switch (ty) {
case INTEGER:
return "i" + std::to_string(bit_width);
case POINTER:
return "ptr" + std::to_string(bit_width);
case FLOAT:
return "f" + std::to_string(bit_width);
case TUPLE:
switch (m_base) {
case base::INTEGER:
return "i" + std::to_string(m_bit_width);
case base::POINTER:
return "ptr" + std::to_string(m_bit_width);
case base::FLOAT:
return "f" + std::to_string(m_bit_width);
case base::TUPLE:
return "tuple";
case CONTROL:
case base::CONTROL:
return "control";
case MEMORY:
case base::MEMORY:
return "memory";
case UNKNOWN:
return "unknown";
case base::UNKNOWN:
return "unknown";
default:
NOT_IMPLEMENTED();
return "";
}
}

auto data_type::operator==(const data_type& other) const -> bool {
return ty == other.ty && bit_width == other.bit_width;
return m_base.get_underlying() == other.m_base.get_underlying() && m_bit_width == other.m_bit_width;
}

auto data_type::get_base() const -> base {
return m_base;
}

auto data_type::get_bit_width() const -> u8 {
return m_bit_width;
}

void data_type::set_bit_width(u8 bit_width) {
m_bit_width = bit_width;
}

data_type::base::base() : m_type(UNKNOWN) {}

data_type::base::base(underlying type) : m_type(type) {}

data_type::base::operator underlying() const{
return m_type;
}

auto data_type::base::get_underlying() const -> underlying {
return m_type;
}

bool operator==(data_type type_a, data_type::base::underlying type_b) {
return type_a.get_base().get_underlying() == type_b;
}

} // namespace sigma::ir
69 changes: 43 additions & 26 deletions source/intermediate_representation/node_hierarchy/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,44 @@ namespace sigma::ir {
// TODO: rework the IR type system

struct data_type {
enum type : u8 {
INTEGER, // void is an i0 and bool is an i1
FLOAT, // f{32, 64}
POINTER, // pointer{0-2047}
TUPLE, // tuples, can only be accessed by projections
CONTROL, // represents control flow for regions and branches
MEMORY, // represents memory and I/O
CONTINUATION, // continuation (usually just the return addresses)
UNKNOWN
struct base {
enum underlying : u8 {
INTEGER, // void is an i0 and bool is an i1
FLOAT, // f{32, 64}
POINTER, // pointer{0-2047}
TUPLE, // tuples, can only be accessed by projections
CONTROL, // represents control flow for regions and branches
MEMORY, // represents memory and I/O
CONTINUATION, // continuation (usually just the return addresses)
UNKNOWN
};

base();
base(underlying type);

operator underlying() const;
auto get_underlying() const -> underlying;
private:
underlying m_type;
};

data_type();
data_type(type t, u8 bw = 0);
data_type(base base, u8 bit_width = 0);

auto to_string() const -> std::string;
auto operator==(const data_type& other) const -> bool;

type ty;
u8 bit_width;
auto to_string() const->std::string;

auto get_base() const -> base;
auto get_bit_width() const -> u8;
void set_bit_width(u8 bit_width);
private:
base m_base;
u8 m_bit_width;
};

bool operator==(data_type type_a, data_type::base::underlying type_b);

struct function_signature {
std::string identifier;

Expand All @@ -39,26 +56,26 @@ namespace sigma::ir {
} // namespace sigma::ir

// integral types
#define VOID_TYPE sigma::ir::data_type(sigma::ir::data_type::INTEGER, 0)
#define BOOL_TYPE sigma::ir::data_type(sigma::ir::data_type::INTEGER, 1)
#define PTR_TYPE sigma::ir::data_type(sigma::ir::data_type::POINTER, 0)
#define I8_TYPE sigma::ir::data_type(sigma::ir::data_type::INTEGER, 8)
#define I16_TYPE sigma::ir::data_type(sigma::ir::data_type::INTEGER, 16)
#define I32_TYPE sigma::ir::data_type(sigma::ir::data_type::INTEGER, 32)
#define I64_TYPE sigma::ir::data_type(sigma::ir::data_type::INTEGER, 64)
#define VOID_TYPE sigma::ir::data_type(sigma::ir::data_type::base::INTEGER, 0)
#define BOOL_TYPE sigma::ir::data_type(sigma::ir::data_type::base::INTEGER, 1)
#define PTR_TYPE sigma::ir::data_type(sigma::ir::data_type::base::POINTER, 0)
#define I8_TYPE sigma::ir::data_type(sigma::ir::data_type::base::INTEGER, 8)
#define I16_TYPE sigma::ir::data_type(sigma::ir::data_type::base::INTEGER, 16)
#define I32_TYPE sigma::ir::data_type(sigma::ir::data_type::base::INTEGER, 32)
#define I64_TYPE sigma::ir::data_type(sigma::ir::data_type::base::INTEGER, 64)

// floating point types
#define F32_TYPE sigma::ir::data_type( \
sigma::ir::data_type::FLOAT, \
sigma::ir::data_type::base::FLOAT, \
static_cast<u8>(sigma::ir::float_format::f32) \
)
#define F64_TYPE sigma::ir::data_type( \
sigma::ir::data_type::FLOAT, \
sigma::ir::data_type::base::FLOAT, \
static_cast<u8>(sigma::ir::float_format::f64) \
)

// misc types
#define CONTROL_TYPE sigma::ir::data_type(sigma::ir::data_type::CONTROL)
#define TUPLE_TYPE sigma::ir::data_type(sigma::ir::data_type::TUPLE)
#define MEMORY_TYPE sigma::ir::data_type(sigma::ir::data_type::MEMORY)
#define CONTINUATION_TYPE sigma::ir::data_type(sigma::ir::data_type::MEMORY)
#define CONTROL_TYPE sigma::ir::data_type(sigma::ir::data_type::base::CONTROL)
#define TUPLE_TYPE sigma::ir::data_type(sigma::ir::data_type::base::TUPLE)
#define MEMORY_TYPE sigma::ir::data_type(sigma::ir::data_type::base::MEMORY)
#define CONTINUATION_TYPE sigma::ir::data_type(sigma::ir::data_type::base::MEMORY)
Loading

0 comments on commit db7b2dc

Please sign in to comment.