1 #include "adt/util.h"
2 #include "jump_target.h"
3 
jump_from_block_to_target(jump_target * const tgt,ir_node * const block)4 void jump_from_block_to_target(jump_target *const tgt, ir_node *const block)
5 {
6 	if (!tgt->block) {
7 		tgt->block = block;
8 		tgt->first = true;
9 		return;
10 	} else if (tgt->first) {
11 		ir_node *const jmp = new_r_Jmp(tgt->block);
12 		tgt->block = new_immBlock();
13 		tgt->first = false;
14 		add_immBlock_pred(tgt->block, jmp);
15 	}
16 	ir_node *const jmp = new_r_Jmp(block);
17 	add_immBlock_pred(tgt->block, jmp);
18 }
19 
jump_to_target(jump_target * const tgt)20 void jump_to_target(jump_target *const tgt)
21 {
22 	ir_node *const block = get_cur_block();
23 	if (block)
24 		jump_from_block_to_target(tgt, block);
25 }
26 
add_pred_to_jump_target(jump_target * const tgt,ir_node * const pred)27 void add_pred_to_jump_target(jump_target *const tgt, ir_node *const pred)
28 {
29 	if (!tgt->block) {
30 		tgt->block = new_immBlock();
31 	} else if (tgt->first) {
32 		ir_node *const jmp = new_r_Jmp(tgt->block);
33 		tgt->block = new_immBlock();
34 		tgt->first = false;
35 		add_immBlock_pred(tgt->block, jmp);
36 	}
37 	add_immBlock_pred(tgt->block, pred);
38 }
39 
enter_jump_target(jump_target * const tgt)40 ir_node *enter_jump_target(jump_target *const tgt)
41 {
42 	ir_node *const block = tgt->block;
43 	if (block && !tgt->first)
44 		mature_immBlock(block);
45 	set_cur_block(block);
46 	return block;
47 }
48 
enter_immature_jump_target(jump_target * const tgt)49 void enter_immature_jump_target(jump_target *const tgt)
50 {
51 	ir_node *jmp;
52 	ir_node *block = tgt->block;
53 	if (!block) {
54 		/* Avoid unreachable loops by adding a Bad entry. */
55 		jmp = new_Bad(mode_X);
56 		goto new_block;
57 	} else if (tgt->first) {
58 		tgt->first = false;
59 		jmp = new_r_Jmp(block);
60 new_block:
61 		tgt->block = block = new_immBlock();
62 		add_immBlock_pred(block, jmp);
63 	}
64 	set_cur_block(block);
65 }
66