@@ -33,11 +33,14 @@ impl<'a> CodegenEngine<'a> {
3333 }
3434 }
3535
36+ /// # Safety
37+ ///
38+ /// Caller must ensure LLVM context is valid.
3639 pub unsafe fn compile_module ( & mut self , ir_module : & Module ) -> Result < ( ) , String > {
3740 self . global_names . clear ( ) ;
3841
3942 // 1. Declare globals
40- for & ( ref global_id, ref name, ref ty) in & ir_module. globals {
43+ for ( global_id, name, ty) in & ir_module. globals {
4144 let llvm_ty = to_llvm_type ( self . ctx . context , ty) ;
4245 let global_var = LLVMAddGlobal (
4346 self . ctx . module ,
@@ -124,8 +127,6 @@ impl<'a> CodegenEngine<'a> {
124127 let dest_block = self . ctx . get_block ( * dest) . unwrap ( ) ;
125128 LLVMBuildBr ( self . ctx . builder , dest_block) ;
126129 }
127- <
128- }
129130 TerminatorKind :: ConditionalJump {
130131 cond,
131132 then_block,
@@ -199,6 +200,25 @@ impl<'a> CodegenEngine<'a> {
199200 TerminatorKind :: Unreachable => {
200201 LLVMBuildUnreachable ( self . ctx . builder ) ;
201202 }
203+ TerminatorKind :: Throw ( val) => {
204+ let thrown_val = self . codegen_val ( val) ?;
205+ let boxed_val = self . box_val ( thrown_val) ?;
206+ let i8_ptr_ty = LLVMPointerType ( LLVMInt8TypeInContext ( self . ctx . context ) , 0 ) ;
207+ let fn_throw = self . get_or_declare_runtime_fn (
208+ "ts_throw" ,
209+ LLVMVoidTypeInContext ( self . ctx . context ) ,
210+ & [ i8_ptr_ty] ,
211+ ) ;
212+ LLVMBuildCall2 (
213+ self . ctx . builder ,
214+ LLVMTypeOf ( fn_throw) ,
215+ fn_throw,
216+ [ boxed_val] . as_mut_ptr ( ) ,
217+ 1 ,
218+ c"" . as_ptr ( ) ,
219+ ) ;
220+ LLVMBuildUnreachable ( self . ctx . builder ) ;
221+ }
202222 }
203223 }
204224 }
@@ -1056,7 +1076,10 @@ impl<'a> CodegenEngine<'a> {
10561076 CString :: new ( "cast" ) . unwrap ( ) . as_ptr ( ) ,
10571077 )
10581078 }
1059- Op :: Try { catch_block, catch_var } => {
1079+ Op :: Try {
1080+ catch_block,
1081+ catch_var,
1082+ } => {
10601083 let i8_ptr_ty = LLVMPointerType ( LLVMInt8TypeInContext ( context) , 0 ) ;
10611084 let fn_push = self . get_or_declare_runtime_fn ( "ts_try_push" , i8_ptr_ty, & [ ] ) ;
10621085 let buf_ptr = LLVMBuildCall2 (
@@ -1120,7 +1143,11 @@ impl<'a> CodegenEngine<'a> {
11201143 LLVMPositionBuilderAtEnd ( self . ctx . builder , dispatch_block) ;
11211144
11221145 // Clean up the jmp_buf since we arrived here via longjmp and ts_try_pop wasn't called
1123- let fn_free_buf = self . get_or_declare_runtime_fn( "ts_try_free" , LLVMVoidTypeInContext ( context) , & [ i8_ptr_ty] ) ;
1146+ let fn_free_buf = self . get_or_declare_runtime_fn (
1147+ "ts_try_free" ,
1148+ LLVMVoidTypeInContext ( context) ,
1149+ & [ i8_ptr_ty] ,
1150+ ) ;
11241151 LLVMBuildCall2 (
11251152 self . ctx . builder ,
11261153 LLVMTypeOf ( fn_free_buf) ,
@@ -1151,7 +1178,11 @@ impl<'a> CodegenEngine<'a> {
11511178 return Ok ( ( ) ) ;
11521179 }
11531180 Op :: EndTry => {
1154- let fn_pop = self . get_or_declare_runtime_fn( "ts_try_pop" , LLVMVoidTypeInContext ( context) , & [ ] ) ;
1181+ let fn_pop = self . get_or_declare_runtime_fn (
1182+ "ts_try_pop" ,
1183+ LLVMVoidTypeInContext ( context) ,
1184+ & [ ] ,
1185+ ) ;
11551186 LLVMBuildCall2 (
11561187 self . ctx . builder ,
11571188 LLVMTypeOf ( fn_pop) ,
0 commit comments