summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--src/mir/from_hir.cpp43
-rw-r--r--src/mir/mir.hpp171
-rw-r--r--src/mir/mir_ptr.cpp9
-rw-r--r--src/mir/mir_ptr.hpp23
5 files changed, 247 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index bff5efef..520b8007 100644
--- a/Makefile
+++ b/Makefile
@@ -55,6 +55,7 @@ OBJ += hir_typeck/expr_visit.o
OBJ += hir_typeck/expr_simple.o hir_typeck/expr_cs.o
OBJ += hir_typeck/expr_check.o
OBJ += hir_expand/closures.o hir_expand/ufcs_everything.o
+OBJ += mir/mir_ptr.o mir/from_hir.o
PCHS := ast/ast.hpp
diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp
new file mode 100644
index 00000000..7bc25385
--- /dev/null
+++ b/src/mir/from_hir.cpp
@@ -0,0 +1,43 @@
+/*
+ * MRustC - Rust Compiler
+ * - By John Hodge (Mutabah/thePowersGang)
+ *
+ * mir/from_hir.hpp
+ * - Construction of MIR from the HIR expression tree
+ */
+#include "mir.hpp"
+#include "mir_ptr.hpp"
+#include <hir/expr.hpp>
+
+namespace {
+ class ExprVisitor_Conv:
+ public ::HIR::ExprVisitor
+ {
+ ::MIR::Function& m_output;
+
+ unsigned int m_result_tmp_idx;
+
+ public:
+ ExprVisitor_Conv(::MIR::Function& output):
+ m_output(output)
+ {}
+
+
+ void visit(::HIR::ExprNode_Block& node) override
+ {
+ // Creates a BB, all expressions end up as part of it (with all but the final expression having their results dropped)
+ }
+ };
+}
+
+
+::MIR::FunctionPointer LowerMIR(const ::HIR::ExprPtr& ptr, const ::std::vector< ::std::pair< ::HIR::Pattern, ::HIR::TypeRef> >& args)
+{
+ ::MIR::Function fcn;
+
+ // 1. Apply destructuring to arguments
+ // 2. Destructure code
+
+ return ::MIR::FunctionPointer();
+}
+
diff --git a/src/mir/mir.hpp b/src/mir/mir.hpp
new file mode 100644
index 00000000..04b7d0a0
--- /dev/null
+++ b/src/mir/mir.hpp
@@ -0,0 +1,171 @@
+/*
+ * MRustC - Rust Compiler
+ * - By John Hodge (Mutabah/thePowersGang)
+ *
+ * mir/mir.hpp
+ * - MIR (Middle Intermediate Representation) definitions
+ */
+#pragma once
+#include <tagged_union.hpp>
+#include <vector>
+#include <string>
+#include <hir/type.hpp>
+
+namespace MIR {
+
+typedef unsigned int RegionId;
+typedef unsigned int BasicBlockId;
+
+// "LVALUE" - Assignable values
+TAGGED_UNION(LValue, Variable,
+ // User-named variable
+ (Variable, unsigned int),
+ // Temporary with no user-defined name
+ (Temporary, struct {
+ unsigned int idx;
+ }),
+ // Function argument (matters for destructuring)
+ (Argument, struct {
+ unsigned int idx;
+ }),
+ // `static` or `static mut`
+ (Static, ::HIR::Path),
+ // Function return
+ (Return, struct{}),
+ (Field, struct {
+ ::std::unique_ptr<LValue> val;
+ unsigned int field_index;
+ }),
+ (Deref, struct {
+ ::std::unique_ptr<LValue> val;
+ }),
+ (Downcast, struct {
+ ::std::unique_ptr<LValue> val;
+ unsigned int variant_index;
+ })
+ );
+
+enum class eBinOp
+{
+ ADD, ADD_OV,
+ SUB, SUB_OV,
+ MUL, MUL_OV,
+ DIV, DIV_OV,
+ MOD, MOD_OV,
+
+ EQ, NE,
+ GT, GE,
+ LT, LE,
+};
+enum class eUniOp
+{
+ INV,
+ NEG
+};
+
+// Compile-time known values
+TAGGED_UNION(Constant, Int,
+ (Int, ::std::int64_t),
+ (Uint, ::std::uint64_t),
+ (Float, double),
+ (Bool, bool),
+ (Bytes, ::std::vector< ::std::uint8_t>),
+ (StaticString, ::std::string),
+ (ItemAddr, ::HIR::Path)
+ );
+
+TAGGED_UNION(RValue, Use,
+ (Use, LValue),
+ (Constant, Constant),
+ (SizedArray, struct {
+ LValue val;
+ unsigned int count;
+ }),
+ (Borrow, struct {
+ RegionId region;
+ ::HIR::BorrowType type;
+ LValue val;
+ }),
+ (Cast, struct {
+ LValue val;
+ ::HIR::TypeRef type;
+ }),
+ (BinOp, struct {
+ LValue val_l;
+ eBinOp op;
+ LValue val_r;
+ }),
+ (UniOp, struct {
+ LValue val;
+ eUniOp op;
+ }),
+ (DstMeta, struct {
+ LValue val;
+ }),
+ (Tuple, struct {
+ ::std::vector<LValue> vals;
+ }),
+ (Array, struct {
+ ::std::vector<LValue> vals;
+ }),
+ (Struct, struct {
+ ::HIR::GenericPath path;
+ ::std::vector<LValue> vals;
+ })
+);
+
+TAGGED_UNION(Terminator, Return,
+ (Return, struct {}),
+ (Diverge, struct {}),
+ (Goto, BasicBlockId),
+ (Panic, struct { BasicBlockId dst; }),
+ (If, struct {
+ LValue cond;
+ BasicBlockId bb0;
+ BasicBlockId bb1;
+ }),
+ (Switch, struct {
+ LValue enum_val;
+ ::std::vector<BasicBlockId> targets;
+ }),
+ (Call, struct {
+ BasicBlockId ret_block;
+ BasicBlockId panic_block;
+ LValue ret_val;
+ LValue fcn_val;
+ ::std::vector<LValue> args;
+ })
+ );
+
+enum class eDropKind {
+ SHALLOW,
+ DEEP,
+};
+TAGGED_UNION(Statement, Assign,
+ (Assign, struct {
+ LValue dst;
+ RValue src;
+ }),
+ (Drop, struct {
+ eDropKind kind;
+ LValue slot;
+ })
+ );
+
+struct BasicBlock
+{
+ ::std::vector<Statement> statements;
+ Terminator terminator;
+};
+
+
+struct Function
+{
+ ::std::vector< ::HIR::TypeRef> named_variables;
+ ::std::vector< ::HIR::TypeRef> temporaries;
+
+ ::std::vector<BasicBlock> blocks;
+};
+
+};
+
diff --git a/src/mir/mir_ptr.cpp b/src/mir/mir_ptr.cpp
new file mode 100644
index 00000000..a31e12ab
--- /dev/null
+++ b/src/mir/mir_ptr.cpp
@@ -0,0 +1,9 @@
+/*
+ */
+#include "mir_ptr.hpp"
+
+
+::MIR::FunctionPointer::~FunctionPointer()
+{
+}
+
diff --git a/src/mir/mir_ptr.hpp b/src/mir/mir_ptr.hpp
new file mode 100644
index 00000000..d1c879c3
--- /dev/null
+++ b/src/mir/mir_ptr.hpp
@@ -0,0 +1,23 @@
+/*
+ */
+#pragma once
+
+
+namespace MIR {
+
+class Function;
+
+class FunctionPointer
+{
+ ::MIR::Function* ptr;
+public:
+ FunctionPointer(): ptr(nullptr) {}
+ FunctionPointer(::MIR::Function* p): ptr(p) {}
+ ~FunctionPointer();
+
+ ::MIR::Function& operator->() { return *ptr; }
+ ::MIR::Function& operator*() { return *ptr; }
+};
+
+}
+