Attempt at including CodegenCx within Builder with Associated types

This commit is contained in:
Denis Merigoux 2018-09-06 18:31:42 -07:00 committed by Eduard-Mihai Burtescu
parent 1929ac2007
commit c487b825b0
4 changed files with 52 additions and 41 deletions

View file

@ -157,7 +157,7 @@ pub fn bin_op_to_fcmp_predicate(op: hir::BinOpKind) -> RealPredicate {
} }
} }
pub fn compare_simd_types<'a, 'll:'a, 'tcx:'ll, Builder: BuilderMethods<'a, 'll, 'tcx>>( pub fn compare_simd_types<'a, 'tcx: 'a, Builder: BuilderMethods<'a, 'tcx>>(
bx: &Builder, bx: &Builder,
lhs: Builder::Value, lhs: Builder::Value,
rhs: Builder::Value, rhs: Builder::Value,

View file

@ -12,7 +12,7 @@ use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
use llvm::{self, False, OperandBundleDef, BasicBlock}; use llvm::{self, False, OperandBundleDef, BasicBlock};
use common::{self, *}; use common::{self, *};
use context::CodegenCx; use context::CodegenCx;
use type_; use type_::Type;
use value::Value; use value::Value;
use libc::{c_uint, c_char}; use libc::{c_uint, c_char};
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
@ -57,14 +57,16 @@ bitflags! {
} }
impl Backend for Builder<'a, 'll, 'tcx> { impl Backend for Builder<'a, 'll, 'tcx> {
type Value = &'ll Value; type Value = &'ll Value;
type BasicBlock = &'ll BasicBlock; type BasicBlock = &'ll BasicBlock;
type Type = &'ll type_::Type; type Type = &'ll Type;
type TypeKind = llvm::TypeKind; type TypeKind = llvm::TypeKind;
type Context = &'ll llvm::Context; type Context = &'ll llvm::Context;
} }
impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> { impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
type CodegenCx = CodegenCx<'ll, 'tcx>;
fn new_block<'b>( fn new_block<'b>(
cx: &'a CodegenCx<'ll, 'tcx>, cx: &'a CodegenCx<'ll, 'tcx>,
llfn: &'ll Value, llfn: &'ll Value,
@ -199,7 +201,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
args: &[&'ll Value], args: &[&'ll Value],
then: &'ll BasicBlock, then: &'ll BasicBlock,
catch: &'ll BasicBlock, catch: &'ll BasicBlock,
bundle: Option<&common::OperandBundleDef<'ll, &'ll Value>>) -> &'ll Value { bundle: Option<&common::OperandBundleDef<&'ll Value>>) -> &'ll Value {
self.count_insn("invoke"); self.count_insn("invoke");
debug!("Invoke {:?} with args ({:?})", debug!("Invoke {:?} with args ({:?})",
@ -437,7 +439,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
} }
fn alloca(&self, ty: Self::Type, name: &str, align: Align) -> &'ll Value { fn alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
let bx = Builder::with_cx(self.cx); let bx = Builder::with_cx(self.cx);
bx.position_at_start(unsafe { bx.position_at_start(unsafe {
llvm::LLVMGetFirstBasicBlock(self.llfn()) llvm::LLVMGetFirstBasicBlock(self.llfn())
@ -445,7 +447,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
bx.dynamic_alloca(ty, name, align) bx.dynamic_alloca(ty, name, align)
} }
fn dynamic_alloca(&self, ty: Self::Type, name: &str, align: Align) -> &'ll Value { fn dynamic_alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
self.count_insn("alloca"); self.count_insn("alloca");
unsafe { unsafe {
let alloca = if name.is_empty() { let alloca = if name.is_empty() {
@ -461,7 +463,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
fn array_alloca(&self, fn array_alloca(&self,
ty: Self::Type, ty: &'ll Type,
len: &'ll Value, len: &'ll Value,
name: &str, name: &str,
align: Align) -> &'ll Value { align: Align) -> &'ll Value {
@ -620,77 +622,77 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
/* Casts */ /* Casts */
fn trunc(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn trunc(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("trunc"); self.count_insn("trunc");
unsafe { unsafe {
llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, noname())
} }
} }
fn sext(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn sext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("sext"); self.count_insn("sext");
unsafe { unsafe {
llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, noname())
} }
} }
fn fptoui(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn fptoui(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("fptoui"); self.count_insn("fptoui");
unsafe { unsafe {
llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, noname())
} }
} }
fn fptosi(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn fptosi(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("fptosi"); self.count_insn("fptosi");
unsafe { unsafe {
llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,noname()) llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,noname())
} }
} }
fn uitofp(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn uitofp(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("uitofp"); self.count_insn("uitofp");
unsafe { unsafe {
llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, noname())
} }
} }
fn sitofp(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn sitofp(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("sitofp"); self.count_insn("sitofp");
unsafe { unsafe {
llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, noname())
} }
} }
fn fptrunc(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn fptrunc(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("fptrunc"); self.count_insn("fptrunc");
unsafe { unsafe {
llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, noname())
} }
} }
fn fpext(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn fpext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("fpext"); self.count_insn("fpext");
unsafe { unsafe {
llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, noname())
} }
} }
fn ptrtoint(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn ptrtoint(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("ptrtoint"); self.count_insn("ptrtoint");
unsafe { unsafe {
llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, noname())
} }
} }
fn inttoptr(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn inttoptr(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("inttoptr"); self.count_insn("inttoptr");
unsafe { unsafe {
llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, noname())
} }
} }
fn bitcast(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn bitcast(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("bitcast"); self.count_insn("bitcast");
unsafe { unsafe {
llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, noname())
@ -698,14 +700,14 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
fn intcast(&self, val: &'ll Value, dest_ty: Self::Type, is_signed: bool) -> &'ll Value { fn intcast(&self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
self.count_insn("intcast"); self.count_insn("intcast");
unsafe { unsafe {
llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed) llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed)
} }
} }
fn pointercast(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn pointercast(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("pointercast"); self.count_insn("pointercast");
unsafe { unsafe {
llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, noname())
@ -729,14 +731,14 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
/* Miscellaneous instructions */ /* Miscellaneous instructions */
fn empty_phi(&self, ty: Self::Type) -> &'ll Value { fn empty_phi(&self, ty: &'ll Type) -> &'ll Value {
self.count_insn("emptyphi"); self.count_insn("emptyphi");
unsafe { unsafe {
llvm::LLVMBuildPhi(self.llbuilder, ty, noname()) llvm::LLVMBuildPhi(self.llbuilder, ty, noname())
} }
} }
fn phi(&self, ty: Self::Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value { fn phi(&self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
assert_eq!(vals.len(), bbs.len()); assert_eq!(vals.len(), bbs.len());
let phi = self.empty_phi(ty); let phi = self.empty_phi(ty);
self.count_insn("addincoming"); self.count_insn("addincoming");
@ -749,7 +751,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char, fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
inputs: &[&'ll Value], output: Self::Type, inputs: &[&'ll Value], output: &'ll Type,
volatile: bool, alignstack: bool, volatile: bool, alignstack: bool,
dia: syntax::ast::AsmDialect) -> Option<&'ll Value> { dia: syntax::ast::AsmDialect) -> Option<&'ll Value> {
self.count_insn("inlineasm"); self.count_insn("inlineasm");
@ -826,7 +828,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn va_arg(&self, list: &'ll Value, ty: Self::Type) -> &'ll Value { fn va_arg(&self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
self.count_insn("vaarg"); self.count_insn("vaarg");
unsafe { unsafe {
llvm::LLVMBuildVAArg(self.llbuilder, list, ty, noname()) llvm::LLVMBuildVAArg(self.llbuilder, list, ty, noname())
@ -961,7 +963,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
} }
fn landing_pad(&self, ty: Self::Type, pers_fn: &'ll Value, fn landing_pad(&self, ty: &'ll Type, pers_fn: &'ll Value,
num_clauses: usize) -> &'ll Value { num_clauses: usize) -> &'ll Value {
self.count_insn("landingpad"); self.count_insn("landingpad");
unsafe { unsafe {
@ -1232,7 +1234,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
fn call(&self, llfn: &'ll Value, args: &[&'ll Value], fn call(&self, llfn: &'ll Value, args: &[&'ll Value],
bundle: Option<&common::OperandBundleDef<'ll, &'ll Value>>) -> &'ll Value { bundle: Option<&common::OperandBundleDef<&'ll Value>>) -> &'ll Value {
self.count_insn("call"); self.count_insn("call");
debug!("Call {:?} with args ({:?})", debug!("Call {:?} with args ({:?})",
@ -1254,7 +1256,7 @@ impl BuilderMethods<'a, 'll, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
} }
fn zext(&self, val: &'ll Value, dest_ty: Self::Type) -> &'ll Value { fn zext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
self.count_insn("zext"); self.count_insn("zext");
unsafe { unsafe {
llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, noname()) llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, noname())

View file

@ -50,13 +50,13 @@ pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bo
ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP) ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP)
} }
pub struct OperandBundleDef<'a, Value: 'a> { pub struct OperandBundleDef<'a, Value> {
pub name: &'a str, pub name: &'a str,
pub val: Value pub val: Value
} }
impl OperandBundleDef<'ll, &'ll Value> { impl<'a, Value> OperandBundleDef<'a, Value> {
pub fn new(name: &'ll str, val: &'ll Value) -> Self { pub fn new(name: &'a str, val: Value) -> Self {
OperandBundleDef { OperandBundleDef {
name, name,
val val

View file

@ -15,6 +15,8 @@ use rustc::ty::layout::{Align, Size};
use rustc::session::Session; use rustc::session::Session;
use builder::MemFlags; use builder::MemFlags;
use super::backend::Backend; use super::backend::Backend;
use super::type_::TypeMethods;
use super::consts::ConstMethods;
use std::borrow::Cow; use std::borrow::Cow;
use std::ops::Range; use std::ops::Range;
@ -22,17 +24,24 @@ use syntax::ast::AsmDialect;
pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll>: Backend { pub trait BuilderMethods<'a, 'tcx: 'a>: Backend {
type CodegenCx: TypeMethods + ConstMethods + Backend<
Value = Self::Value,
BasicBlock = Self::BasicBlock,
Type = Self::Type,
TypeKind = Self::TypeKind,
Context = Self::Context,
>;
fn new_block<'b>( fn new_block<'b>(
cx: &'a CodegenCx<'ll, 'tcx, Self::Value>, cx: &'a Self::CodegenCx,
llfn: Self::Value, llfn: Self::Value,
name: &'b str name: &'b str
) -> Self; ) -> Self;
fn with_cx(cx: &'a CodegenCx<'ll, 'tcx, Self::Value>) -> Self; fn with_cx(cx: &'a Self::CodegenCx) -> Self;
fn build_sibling_block<'b>(&self, name: &'b str) -> Self; fn build_sibling_block<'b>(&self, name: &'b str) -> Self;
fn sess(&self) -> &Session; fn sess(&self) -> &Session;
fn cx(&self) -> &'a CodegenCx<'ll, 'tcx, Self::Value>; fn cx(&self) -> &'a Self::CodegenCx;
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx>; fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx>;
fn llfn(&self) -> Self::Value; fn llfn(&self) -> Self::Value;
fn llbb(&self) -> Self::BasicBlock; fn llbb(&self) -> Self::BasicBlock;
@ -62,7 +71,7 @@ pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll>: Backend {
args: &[Self::Value], args: &[Self::Value],
then: Self::BasicBlock, then: Self::BasicBlock,
catch: Self::BasicBlock, catch: Self::BasicBlock,
bundle: Option<&OperandBundleDef<'ll, Self::Value>> bundle: Option<&OperandBundleDef<Self::Value>>
) -> Self::Value; ) -> Self::Value;
fn unreachable(&self); fn unreachable(&self);
fn add(&self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; fn add(&self, lhs: Self::Value, rhs: Self::Value) -> Self::Value;
@ -278,6 +287,6 @@ pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll>: Backend {
fn call_lifetime_intrinsic(&self, intrinsic: &str, ptr: Self::Value, size: Size); fn call_lifetime_intrinsic(&self, intrinsic: &str, ptr: Self::Value, size: Size);
fn call(&self, llfn: Self::Value, args: &[Self::Value], fn call(&self, llfn: Self::Value, args: &[Self::Value],
bundle: Option<&OperandBundleDef<'ll, Self::Value>>) -> Self::Value; bundle: Option<&OperandBundleDef<Self::Value>>) -> Self::Value;
fn zext(&self, val: Self::Value, dest_ty: Self::Type) -> Self::Value; fn zext(&self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
} }