/* * MRustC - Rust Compiler * - By John Hodge (Mutabah/thePowersGang) * * mir/mir.cpp * - MIR (Middle Intermediate Representation) definitions */ #include namespace MIR { ::std::ostream& operator<<(::std::ostream& os, const Constant& v) { TU_MATCHA( (v), (e), (Int, os << (e < 0 ? "-" : "+"); os << (e < 0 ? -e : e); ), (Uint, os << e; ), (Float, os << e; ), (Bool, os << (e ? "true" : "false"); ), (Bytes, os << "["; os << ::std::hex; for(auto v : e) os << static_cast(v) << " "; os << ::std::dec; os << "]"; ), (StaticString, os << "\"" << e << "\""; ), (Const, os << e.p; ), (ItemAddr, os << "&" << e; ) ) return os; } ::std::ostream& operator<<(::std::ostream& os, const LValue& x) { TU_MATCHA( (x), (e), (Variable, os << "Variable(" << e << ")"; ), (Temporary, os << "Temporary(" << e.idx << ")"; ), (Argument, os << "Argument(" << e.idx << ")"; ), (Static, os << "Static(" << e << ")"; ), (Return, os << "Return"; ), (Field, os << "Field(" << e.field_index << ", " << *e.val << ")"; ), (Deref, os << "Deref(" << *e.val << ")"; ), (Index, os << "Deref(" << *e.val << ", " << *e.idx << ")"; ), (Downcast, os << "Downcast(" << e.variant_index << ", " << *e.val << ")"; ) ) return os; } ::std::ostream& operator<<(::std::ostream& os, const RValue& x) { TU_MATCHA( (x), (e), (Use, os << "Use(" << e << ")"; ), (Constant, os << "Constant(" << e << ")"; ), (SizedArray, os << "SizedArray(" << e.val << "; " << e.count << ")"; ), (Borrow, os << "Borrow(" << e.region << ", " << e.type << ", " << e.val << ")"; ), (Cast, os << "Cast(" << e.val << " as " << e.type << ")"; ), (BinOp, os << "BinOp(" << e.val_l << " " << static_cast(e.op) << " " << e.val_r << ")"; ), (UniOp, os << "UniOp(" << e.val << " " << static_cast(e.op) << ")"; ), (DstMeta, os << "DstMeta(" << e.val << ")"; ), (DstPtr, os << "DstPtr(" << e.val << ")"; ), (MakeDst, os << "MakeDst(" << e.ptr_val << ", " << e.meta_val << ")"; ), (Tuple, os << "Tuple(" << e.vals << ")"; ), (Array, os << "Array(" << e.vals << ")"; ), (Variant, os << "Variant(" << e.path << " #" << e.index << ", " << e.val << ")"; ), (Struct, os << "Struct(" << e.path << ", {" << e.vals << "})"; ) ) return os; } ::std::ostream& operator<<(::std::ostream& os, const Terminator& x) { TU_MATCHA( (x), (e), (Incomplete, os << "Invalid"; ), (Return, os << "Return"; ), (Diverge, os << "Diverge"; ), (Goto, os << "Goto(" << e << ")"; ), (Panic, os << "Panic(" << e.dst << ";)"; ), (If, os << "If( " << e.cond << " : " << e.bb0 << ", " << e.bb1 << ")"; ), (Switch, os << "Switch( " << e.val << " : "; for(unsigned int j = 0; j < e.targets.size(); j ++) os << j << " => bb" << e.targets[j] << ", "; os << ")"; ), (Call, os << "Call( " << e.ret_val << " = " << e.fcn_val << "( "; for(const auto& arg : e.args) os << arg << ", "; os << "), bb" << e.ret_block << ", bb" << e.panic_block << ")"; ) ) return os; } } ::MIR::LValue MIR::LValue::clone() const { TU_MATCHA( (*this), (e), (Variable, return LValue(e); ), (Temporary, return LValue(e); ), (Argument, return LValue(e); ), (Static, return LValue(e.clone()); ), (Return, return LValue(e); ), (Field, return LValue::make_Field({ box$( e.val->clone() ), e.field_index }); ), (Deref, return LValue::make_Deref({ box$( e.val->clone() ) }); ), (Index, return LValue::make_Index({ box$( e.val->clone() ), box$( e.idx->clone() ) }); ), (Downcast, return LValue::make_Downcast({ box$( e.val->clone() ), e.variant_index }); ) ) throw ""; }