3
3
use std:: ops:: Deref ;
4
4
5
5
use rustc_abi:: { HasDataLayout , TargetDataLayout } ;
6
+ use rustc_codegen_c_ast:: func:: CFunc ;
7
+ use rustc_codegen_c_ast:: r#type:: CTy ;
6
8
use rustc_codegen_ssa:: traits:: { BackendTypes , BuilderMethods , HasCodegen } ;
7
9
use rustc_middle:: ty:: layout:: {
8
10
FnAbiError , FnAbiOfHelpers , FnAbiRequest , HasParamEnv , HasTyCtxt , LayoutError , LayoutOfHelpers ,
@@ -12,10 +14,7 @@ use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
12
14
use rustc_target:: abi:: call:: FnAbi ;
13
15
use rustc_target:: spec:: { HasTargetSpec , Target } ;
14
16
15
- use crate :: c_expr;
16
- use crate :: context:: { CFunctionBuilder , CodegenCx } ;
17
- use crate :: module:: { CDecl , CExpr , CStmt , CType } ;
18
- use crate :: utils:: slab:: Id ;
17
+ use crate :: context:: CodegenCx ;
19
18
20
19
mod abi;
21
20
mod asm;
@@ -24,68 +23,68 @@ mod debug_info;
24
23
mod intrinsic_call;
25
24
mod r#static;
26
25
27
- pub struct Builder < ' a , ' tcx > {
28
- pub cx : & ' a CodegenCx < ' tcx > ,
29
- bb : Id < CFunctionBuilder > ,
26
+ pub struct Builder < ' a , ' tcx , ' mx > {
27
+ pub cx : & ' a CodegenCx < ' tcx , ' mx > ,
28
+ bb : CFunc < ' mx > ,
30
29
}
31
30
32
- impl < ' a , ' tcx > Deref for Builder < ' a , ' tcx > {
33
- type Target = CodegenCx < ' tcx > ;
31
+ impl < ' a , ' tcx , ' mx > Deref for Builder < ' a , ' tcx , ' mx > {
32
+ type Target = CodegenCx < ' tcx , ' mx > ;
34
33
35
34
fn deref < ' b > ( & ' b self ) -> & ' a Self :: Target {
36
35
self . cx
37
36
}
38
37
}
39
38
40
- impl < ' tcx > HasCodegen < ' tcx > for Builder < ' _ , ' tcx > {
41
- type CodegenCx = CodegenCx < ' tcx > ;
39
+ impl < ' tcx , ' mx > HasCodegen < ' tcx > for Builder < ' _ , ' tcx , ' mx > {
40
+ type CodegenCx = CodegenCx < ' tcx , ' mx > ;
42
41
}
43
42
44
- impl < ' tcx > HasDataLayout for Builder < ' _ , ' tcx > {
43
+ impl < ' tcx , ' mx > HasDataLayout for Builder < ' _ , ' tcx , ' mx > {
45
44
fn data_layout ( & self ) -> & TargetDataLayout {
46
45
todo ! ( )
47
46
}
48
47
}
49
48
50
- impl < ' tcx > HasTyCtxt < ' tcx > for Builder < ' _ , ' tcx > {
49
+ impl < ' tcx , ' mx > HasTyCtxt < ' tcx > for Builder < ' _ , ' tcx , ' mx > {
51
50
fn tcx ( & self ) -> TyCtxt < ' tcx > {
52
51
self . cx . tcx ( )
53
52
}
54
53
}
55
54
56
- impl < ' tcx > HasParamEnv < ' tcx > for Builder < ' _ , ' tcx > {
55
+ impl < ' tcx , ' mx > HasParamEnv < ' tcx > for Builder < ' _ , ' tcx , ' mx > {
57
56
fn param_env ( & self ) -> ParamEnv < ' tcx > {
58
57
self . cx . param_env ( )
59
58
}
60
59
}
61
60
62
- impl < ' tcx > BackendTypes for Builder < ' _ , ' tcx > {
63
- type Value = <CodegenCx < ' tcx > as BackendTypes >:: Value ;
64
- type Function = <CodegenCx < ' tcx > as BackendTypes >:: Function ;
65
- type BasicBlock = <CodegenCx < ' tcx > as BackendTypes >:: BasicBlock ;
66
- type Type = <CodegenCx < ' tcx > as BackendTypes >:: Type ;
67
- type Funclet = <CodegenCx < ' tcx > as BackendTypes >:: Funclet ;
61
+ impl < ' tcx , ' mx > BackendTypes for Builder < ' _ , ' tcx , ' mx > {
62
+ type Value = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: Value ;
63
+ type Function = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: Function ;
64
+ type BasicBlock = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: BasicBlock ;
65
+ type Type = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: Type ;
66
+ type Funclet = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: Funclet ;
68
67
69
- type DIScope = <CodegenCx < ' tcx > as BackendTypes >:: DIScope ;
70
- type DILocation = <CodegenCx < ' tcx > as BackendTypes >:: DILocation ;
71
- type DIVariable = <CodegenCx < ' tcx > as BackendTypes >:: DIVariable ;
68
+ type DIScope = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: DIScope ;
69
+ type DILocation = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: DILocation ;
70
+ type DIVariable = <CodegenCx < ' tcx , ' mx > as BackendTypes >:: DIVariable ;
72
71
}
73
72
74
- impl < ' tcx > HasTargetSpec for Builder < ' _ , ' tcx > {
73
+ impl < ' tcx , ' mx > HasTargetSpec for Builder < ' _ , ' tcx , ' mx > {
75
74
fn target_spec ( & self ) -> & Target {
76
75
todo ! ( )
77
76
}
78
77
}
79
78
80
- impl < ' tcx > LayoutOfHelpers < ' tcx > for Builder < ' _ , ' tcx > {
79
+ impl < ' tcx , ' mx > LayoutOfHelpers < ' tcx > for Builder < ' _ , ' tcx , ' mx > {
81
80
type LayoutOfResult = TyAndLayout < ' tcx > ;
82
81
83
82
fn handle_layout_err ( & self , err : LayoutError < ' tcx > , span : rustc_span:: Span , ty : Ty < ' tcx > ) -> ! {
84
83
todo ! ( )
85
84
}
86
85
}
87
86
88
- impl < ' tcx > FnAbiOfHelpers < ' tcx > for Builder < ' _ , ' tcx > {
87
+ impl < ' tcx , ' mx > FnAbiOfHelpers < ' tcx > for Builder < ' _ , ' tcx , ' mx > {
89
88
type FnAbiOfResult = & ' tcx FnAbi < ' tcx , Ty < ' tcx > > ;
90
89
91
90
fn handle_fn_abi_err (
@@ -98,7 +97,7 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, 'tcx> {
98
97
}
99
98
}
100
99
101
- impl < ' a , ' tcx > BuilderMethods < ' a , ' tcx > for Builder < ' a , ' tcx > {
100
+ impl < ' a , ' tcx , ' mx > BuilderMethods < ' a , ' tcx > for Builder < ' a , ' tcx , ' mx > {
102
101
fn build ( cx : & ' a Self :: CodegenCx , llbb : Self :: BasicBlock ) -> Self {
103
102
Self { cx, bb : llbb }
104
103
}
@@ -128,13 +127,11 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
128
127
}
129
128
130
129
fn ret_void ( & mut self ) {
131
- let mut functions = self . cx . functions . borrow_mut ( ) ;
132
- functions[ self . bb ] . push_stmt ( CStmt :: Return ( None ) ) ;
130
+ self . bb . 0 . push_stmt ( self . cx . mcx . ret ( None ) ) ;
133
131
}
134
132
135
133
fn ret ( & mut self , v : Self :: Value ) {
136
- let mut functions = self . cx . functions . borrow_mut ( ) ;
137
- functions[ self . bb ] . push_stmt ( CStmt :: Return ( Some ( Box :: new ( CExpr :: Value ( v) ) ) ) ) ;
134
+ self . bb . 0 . push_stmt ( self . cx . mcx . ret ( Some ( self . cx . mcx . value ( v) ) ) )
138
135
}
139
136
140
137
fn br ( & mut self , dest : Self :: BasicBlock ) {
@@ -510,33 +507,23 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
510
507
/// integer via explicit conversion. Then, use a helper function to cast the
511
508
/// result to a signed integer.
512
509
fn intcast ( & mut self , val : Self :: Value , dest_ty : Self :: Type , is_signed : bool ) -> Self :: Value {
513
- let mut functions = self . cx . functions . borrow_mut ( ) ;
514
- let function = & mut functions[ self . bb ] ;
515
- let ret = function. next_value ( ) ;
516
-
517
- let dest = if let CType :: Primitive ( ty) = dest_ty { ty } else { unreachable ! ( ) } ;
518
- let cast = if !dest. is_signed ( ) {
519
- CExpr :: Cast { ty : CType :: Primitive ( dest) , expr : Box :: new ( CExpr :: Value ( val) ) }
520
- } else {
521
- let cast = CExpr :: Cast {
522
- ty : CType :: Primitive ( dest. to_unsigned ( ) ) ,
523
- expr : Box :: new ( CExpr :: Value ( val) ) ,
524
- } ;
525
- CExpr :: Call {
526
- callee : c_expr ! ( "__rust_utos" ) ,
527
- args : vec ! [
528
- c_expr!( dest. to_unsigned( ) . to_string( ) ) ,
529
- c_expr!( dest. to_string( ) ) ,
510
+ let mcx = self . cx . mcx ;
511
+ let ret = self . bb . 0 . next_local_var ( ) ;
512
+
513
+ let dest = if let CTy :: Primitive ( ty) = dest_ty { ty } else { unreachable ! ( ) } ;
514
+ let mut cast = mcx. cast ( CTy :: Primitive ( dest) , mcx. value ( val) ) ;
515
+ if dest. is_signed ( ) {
516
+ cast = mcx. call (
517
+ mcx. raw ( "__rust_utos" ) ,
518
+ vec ! [
519
+ mcx. raw( dest. to_unsigned( ) . to_str( ) ) ,
520
+ mcx. raw( dest. to_str( ) ) ,
530
521
cast,
531
- c_expr! ( dest. max_value( ) ) ,
522
+ mcx . raw ( dest. max_value( ) ) ,
532
523
] ,
533
- }
534
- } ;
535
- function. push_stmt ( CStmt :: Decl ( Box :: new ( CDecl :: Var {
536
- name : ret,
537
- ty : dest_ty,
538
- init : Some ( cast) ,
539
- } ) ) ) ;
524
+ ) ;
525
+ }
526
+ self . bb . 0 . push_stmt ( mcx. decl_stmt ( mcx. var ( ret, dest_ty, Some ( cast) ) ) ) ;
540
527
ret
541
528
}
542
529
0 commit comments