@@ -36,15 +36,16 @@ static bool cmp_opaque_ptr(OpaqueRef* a, OpaqueRef* b) {
3636KeyHash hash_node (Node * * );
3737bool compare_node (Node * * , Node * * );
3838
39- static const Node * write_bb_tail (Parser * p , Node * fn_or_bb , BodyBuilder * b , LLVMBasicBlockRef bb , LLVMValueRef first_instr ) {
39+ static const Node * write_bb_tail (Parser * p , FnParseCtx * fn_ctx , Node * fn_or_bb , LLVMBasicBlockRef bb , LLVMValueRef first_instr ) {
40+ BodyBuilder * b = begin_body (fn_or_bb -> arena );
4041 LLVMValueRef instr ;
4142 for (instr = first_instr ; instr ; instr = LLVMGetNextInstruction (instr )) {
4243 bool last = instr == LLVMGetLastInstruction (bb );
4344 if (last )
4445 assert (LLVMGetBasicBlockTerminator (bb ) == instr );
4546 // LLVMDumpValue(instr);
4647 // printf("\n");
47- EmittedInstr emitted = convert_instruction (p , fn_or_bb , b , instr );
48+ EmittedInstr emitted = convert_instruction (p , fn_ctx , fn_or_bb , b , instr );
4849 if (emitted .terminator )
4950 return finish_body (b , emitted .terminator );
5051 if (!emitted .instruction )
@@ -65,7 +66,7 @@ typedef struct {
6566 Node * nbb ;
6667} TodoBB ;
6768
68- static TodoBB prepare_bb (Parser * p , Node * fn , LLVMBasicBlockRef bb ) {
69+ static TodoBB prepare_bb (Parser * p , FnParseCtx * fn_ctx , LLVMBasicBlockRef bb ) {
6970 IrArena * a = get_module_arena (p -> dst );
7071 debug_print ("l2s: preparing BB %s %d\n" , LLVMGetBasicBlockName (bb ), bb );
7172 if (get_log_level () <= DEBUG )
@@ -92,9 +93,9 @@ static TodoBB prepare_bb(Parser* p, Node* fn, LLVMBasicBlockRef bb) {
9293 String name = LLVMGetBasicBlockName (bb );
9394 if (!name || strlen (name ) == 0 )
9495 name = unique_name (a , "bb" );
95- Node * nbb = basic_block (a , fn , params , name );
96+ Node * nbb = basic_block (a , fn_ctx -> fn , params , name );
9697 insert_dict (LLVMValueRef , const Node * , p -> map , bb , nbb );
97- insert_dict (const Node * , struct List * , p -> phis , nbb , phis );
98+ insert_dict (const Node * , struct List * , fn_ctx -> phis , nbb , phis );
9899 TodoBB todo = {
99100 .bb = bb ,
100101 .instr = instr ,
@@ -106,15 +107,14 @@ static TodoBB prepare_bb(Parser* p, Node* fn, LLVMBasicBlockRef bb) {
106107 }
107108}
108109
109- const Node * convert_basic_block (Parser * p , Node * fn , LLVMBasicBlockRef bb ) {
110+ const Node * convert_basic_block (Parser * p , FnParseCtx * fn_ctx , LLVMBasicBlockRef bb ) {
110111 IrArena * a = get_module_arena (p -> dst );
111112 const Node * * found = find_value_dict (LLVMValueRef , const Node * , p -> map , bb );
112113 if (found ) return * found ;
113114 // assert(false);
114115
115- TodoBB todo = prepare_bb (p , fn , bb );
116- BodyBuilder * bb_bb = begin_body (a );
117- todo .nbb -> payload .basic_block .body = write_bb_tail (p , todo .nbb , bb_bb , todo .bb , todo .instr );
116+ TodoBB todo = prepare_bb (p , fn_ctx , bb );
117+ todo .nbb -> payload .basic_block .body = write_bb_tail (p , fn_ctx , todo .nbb , todo .bb , todo .instr );
118118 return todo .nbb ;
119119}
120120
@@ -157,20 +157,33 @@ const Node* convert_function(Parser* p, LLVMValueRef fn) {
157157 break ;
158158 }
159159 Node * f = function (p -> dst , params , LLVMGetValueName (fn ), annotations , fn_type -> payload .fn_type .return_types );
160+ FnParseCtx fn_parse_ctx = {
161+ .fn = f ,
162+ .phis = new_dict (const Node * , struct List * , (HashFn ) hash_node , (CmpFn ) compare_node ),
163+ .jumps_todo = new_list (JumpTodo ),
164+ };
160165 const Node * r = fn_addr_helper (a , f );
161166 insert_dict (LLVMValueRef , const Node * , p -> map , fn , r );
162167
163168 if (LLVMCountBasicBlocks (fn ) > 0 ) {
164169 LLVMBasicBlockRef first_bb = LLVMGetEntryBasicBlock (fn );
165170 insert_dict (LLVMValueRef , const Node * , p -> map , first_bb , f );
166- BodyBuilder * b = begin_body (a );
167- f -> payload .fun .body = write_bb_tail (p , f , b , first_bb , LLVMGetFirstInstruction (first_bb ));
171+ f -> payload .fun .body = write_bb_tail (p , & fn_parse_ctx , f , first_bb , LLVMGetFirstInstruction (first_bb ));
168172 }
169173
170- while (entries_count_list (p -> jumps_todo ) > 0 ) {
171- JumpTodo todo = pop_last_list (JumpTodo , p -> jumps_todo );
172- convert_jump_finish (p , f , todo );
174+ while (entries_count_list (fn_parse_ctx .jumps_todo ) > 0 ) {
175+ JumpTodo todo = pop_last_list (JumpTodo , fn_parse_ctx .jumps_todo );
176+ convert_jump_finish (p , & fn_parse_ctx , todo );
177+ }
178+ {
179+ size_t i = 0 ;
180+ struct List * phis_list ;
181+ while (dict_iter (fn_parse_ctx .phis , & i , NULL , & phis_list )) {
182+ destroy_list (phis_list );
183+ }
173184 }
185+ destroy_dict (fn_parse_ctx .phis );
186+ destroy_list (fn_parse_ctx .jumps_todo );
174187
175188 return r ;
176189}
@@ -247,8 +260,6 @@ bool parse_llvm_into_shady(const CompilerConfig* config, size_t len, const char*
247260 .map = new_dict (LLVMValueRef , const Node * , (HashFn ) hash_opaque_ptr , (CmpFn ) cmp_opaque_ptr ),
248261 .annotations = new_dict (LLVMValueRef , ParsedAnnotation , (HashFn ) hash_opaque_ptr , (CmpFn ) cmp_opaque_ptr ),
249262 .scopes = new_dict (const Node * , Nodes , (HashFn ) hash_node , (CmpFn ) compare_node ),
250- .phis = new_dict (const Node * , struct List * , (HashFn ) hash_node , (CmpFn ) compare_node ),
251- .jumps_todo = new_list (JumpTodo ),
252263 .annotations_arena = new_arena (),
253264 .src = src ,
254265 .dst = dirty ,
@@ -283,15 +294,6 @@ bool parse_llvm_into_shady(const CompilerConfig* config, size_t len, const char*
283294 destroy_dict (p .map );
284295 destroy_dict (p .annotations );
285296 destroy_dict (p .scopes );
286- {
287- size_t i = 0 ;
288- struct List * phis_list ;
289- while (dict_iter (p .phis , & i , NULL , & phis_list )) {
290- destroy_list (phis_list );
291- }
292- }
293- destroy_dict (p .phis );
294- destroy_list (p .jumps_todo );
295297 destroy_arena (p .annotations_arena );
296298
297299 LLVMContextDispose (context );
0 commit comments