@@ -36,15 +36,16 @@ static bool cmp_opaque_ptr(OpaqueRef* a, OpaqueRef* b) {
36
36
KeyHash hash_node (Node * * );
37
37
bool compare_node (Node * * , Node * * );
38
38
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 );
40
41
LLVMValueRef instr ;
41
42
for (instr = first_instr ; instr ; instr = LLVMGetNextInstruction (instr )) {
42
43
bool last = instr == LLVMGetLastInstruction (bb );
43
44
if (last )
44
45
assert (LLVMGetBasicBlockTerminator (bb ) == instr );
45
46
// LLVMDumpValue(instr);
46
47
// 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 );
48
49
if (emitted .terminator )
49
50
return finish_body (b , emitted .terminator );
50
51
if (!emitted .instruction )
@@ -65,7 +66,7 @@ typedef struct {
65
66
Node * nbb ;
66
67
} TodoBB ;
67
68
68
- static TodoBB prepare_bb (Parser * p , Node * fn , LLVMBasicBlockRef bb ) {
69
+ static TodoBB prepare_bb (Parser * p , FnParseCtx * fn_ctx , LLVMBasicBlockRef bb ) {
69
70
IrArena * a = get_module_arena (p -> dst );
70
71
debug_print ("l2s: preparing BB %s %d\n" , LLVMGetBasicBlockName (bb ), bb );
71
72
if (get_log_level () <= DEBUG )
@@ -92,9 +93,9 @@ static TodoBB prepare_bb(Parser* p, Node* fn, LLVMBasicBlockRef bb) {
92
93
String name = LLVMGetBasicBlockName (bb );
93
94
if (!name || strlen (name ) == 0 )
94
95
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 );
96
97
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 );
98
99
TodoBB todo = {
99
100
.bb = bb ,
100
101
.instr = instr ,
@@ -106,15 +107,14 @@ static TodoBB prepare_bb(Parser* p, Node* fn, LLVMBasicBlockRef bb) {
106
107
}
107
108
}
108
109
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 ) {
110
111
IrArena * a = get_module_arena (p -> dst );
111
112
const Node * * found = find_value_dict (LLVMValueRef , const Node * , p -> map , bb );
112
113
if (found ) return * found ;
113
114
// assert(false);
114
115
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 );
118
118
return todo .nbb ;
119
119
}
120
120
@@ -157,20 +157,33 @@ const Node* convert_function(Parser* p, LLVMValueRef fn) {
157
157
break ;
158
158
}
159
159
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
+ };
160
165
const Node * r = fn_addr_helper (a , f );
161
166
insert_dict (LLVMValueRef , const Node * , p -> map , fn , r );
162
167
163
168
if (LLVMCountBasicBlocks (fn ) > 0 ) {
164
169
LLVMBasicBlockRef first_bb = LLVMGetEntryBasicBlock (fn );
165
170
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 ));
168
172
}
169
173
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
+ }
173
184
}
185
+ destroy_dict (fn_parse_ctx .phis );
186
+ destroy_list (fn_parse_ctx .jumps_todo );
174
187
175
188
return r ;
176
189
}
@@ -247,8 +260,6 @@ bool parse_llvm_into_shady(const CompilerConfig* config, size_t len, const char*
247
260
.map = new_dict (LLVMValueRef , const Node * , (HashFn ) hash_opaque_ptr , (CmpFn ) cmp_opaque_ptr ),
248
261
.annotations = new_dict (LLVMValueRef , ParsedAnnotation , (HashFn ) hash_opaque_ptr , (CmpFn ) cmp_opaque_ptr ),
249
262
.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 ),
252
263
.annotations_arena = new_arena (),
253
264
.src = src ,
254
265
.dst = dirty ,
@@ -283,15 +294,6 @@ bool parse_llvm_into_shady(const CompilerConfig* config, size_t len, const char*
283
294
destroy_dict (p .map );
284
295
destroy_dict (p .annotations );
285
296
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 );
295
297
destroy_arena (p .annotations_arena );
296
298
297
299
LLVMContextDispose (context );
0 commit comments