To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

gen_mov_phi.rs 10.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use ast::ir::*;
use ast::ptr::*;
use ast::inst::*;
use vm::VM;

use compiler::CompilerPass;
use std::any::Any;
use std::sync::RwLock;

pub struct GenMovPhi {
    name: &'static str,
}

impl GenMovPhi {
    pub fn new() -> GenMovPhi {
        GenMovPhi{name: "Generate Phi Moves"}
    }
}

struct IntermediateBlockInfo {
    blk_id: MuID,
    target: MuID,
    from_args : Vec<P<TreeNode>>
}

impl CompilerPass for GenMovPhi {
    fn name(&self) -> &'static str {
        self.name
    }

    fn as_any(&self) -> &Any {
        self
    }

    fn visit_function(&mut self, vm: &VM, func: &mut MuFunctionVersion) {
        let mut f_content = func.content.take().unwrap();

        let mut new_blocks_to_insert : Vec<IntermediateBlockInfo> = vec![];

        // iteratio blocks
        for (blk_id, mut block) in f_content.blocks.iter_mut() {
            trace!("block: {}", blk_id);

            // old block content
qinsoon's avatar
qinsoon committed
45
            let block_content = block.content.as_ref().unwrap().clone();
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

            let mut new_body = vec![];

            let mut i = 0;
            let i_last = block_content.body.len() - 1;
            for node in block_content.body.iter() {
                // check if this is the last element
                if i != i_last {
                    new_body.push(node.clone());
                } else {
                    trace!("last instruction is {}", node);
                    let last_inst = node.clone();

                    match last_inst.v {
                        TreeNode_::Instruction(inst) => {
qinsoon's avatar
qinsoon committed
61
                            let ops = inst.ops.read().unwrap();
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

                            match inst.v {
                                Instruction_::Branch2{cond, true_dest, false_dest, true_prob} => {
                                    let true_dest  = process_dest(true_dest,  &mut new_blocks_to_insert, &ops, vm);
                                    let false_dest = process_dest(false_dest, &mut new_blocks_to_insert, &ops, vm);

                                    let new_inst = func.new_inst(Instruction{
                                        hdr: inst.hdr.clone(),
                                        value: inst.value.clone(),
                                        ops: RwLock::new(ops.to_vec()),
                                        v: Instruction_::Branch2 {
                                            cond: cond,
                                            true_dest: true_dest,
                                            false_dest: false_dest,
                                            true_prob: true_prob
                                        }
                                    });

                                    trace!("rewrite to {}", new_inst);
                                    new_body.push(new_inst);
                                }
                                Instruction_::Call{data, resume} => {
                                    let norm_dest = process_dest(resume.normal_dest, &mut new_blocks_to_insert, &ops, vm);
                                    let exn_dest  = process_dest(resume.exn_dest,    &mut new_blocks_to_insert, &ops, vm);

                                    let new_inst = func.new_inst(Instruction{
                                        hdr: inst.hdr.clone(),
                                        value: inst.value.clone(),
                                        ops: RwLock::new(ops.to_vec()),
                                        v: Instruction_::Call {
                                            data: data.clone(),
                                            resume: ResumptionData{
                                                normal_dest: norm_dest,
                                                exn_dest: exn_dest
                                            }
                                        }
                                    });

                                    trace!("rewrite to {}", new_inst);
                                    new_body.push(new_inst);
                                }
                                Instruction_::CCall{data, resume} => {
                                    let norm_dest = process_dest(resume.normal_dest, &mut new_blocks_to_insert, &ops, vm);
                                    let exn_dest  = process_dest(resume.exn_dest,    &mut new_blocks_to_insert, &ops, vm);

                                    let new_inst = func.new_inst(Instruction{
                                        hdr: inst.hdr.clone(),
                                        value: inst.value.clone(),
                                        ops: RwLock::new(ops.to_vec()),
                                        v: Instruction_::Call {
                                            data: data.clone(),
                                            resume: ResumptionData{
                                                normal_dest: norm_dest,
                                                exn_dest: exn_dest
                                            }
                                        }
                                    });

                                    trace!("rewrite to {}", new_inst);
                                    new_body.push(new_inst);
                                },
                                Instruction_::Switch{cond, default, mut branches} => {
                                    let default_dest = process_dest(default, &mut new_blocks_to_insert, &ops, vm);

                                    let new_branches = branches.drain(..).map(|pair| {
                                        let dest = process_dest(pair.1, &mut new_blocks_to_insert, &ops, vm);
                                        (pair.0, dest)
                                    }).collect();

                                    let new_inst = func.new_inst(Instruction{
                                        hdr: inst.hdr.clone(),
                                        value: inst.value.clone(),
                                        ops: RwLock::new(ops.to_vec()),
                                        v: Instruction_::Switch {
                                            cond: cond,
                                            default: default_dest,
                                            branches: new_branches
                                        }
                                    });

                                    trace!("rewrite to {}", new_inst);
                                    new_body.push(new_inst);
                                }
                                Instruction_::Watchpoint{..} => {
                                    unimplemented!()
                                },
                                Instruction_::WPBranch{..} => {
                                    unimplemented!()
                                },
                                Instruction_::SwapStack{..} => {
                                    unimplemented!()
                                },
                                Instruction_::ExnInstruction{..} => {
                                    unimplemented!()
                                },
                                _ => {
                                    trace!("no rewrite");
                                    new_body.push(node.clone())
                                }
                            }
                        }
                        _ => panic!("expect a terminal instruction")
                    }
                }

                i += 1;
            }

            block.content = Some(BlockContent{
                args      : block_content.args.to_vec(),
                exn_arg   : block_content.exn_arg.clone(),
                body      : new_body,
                keepalives: block_content.keepalives.clone()
            });
        }

        // insert new blocks here
        for block_info in new_blocks_to_insert {
            let block = {
                let mut ret = Block::new(block_info.blk_id);
                let target_id = block_info.target;

                let name = format!("intermediate_block_{}_to_{}", block_info.blk_id, target_id);
                vm.set_name(ret.as_entity(), name);


                let target_block = f_content.get_block(target_id);

                assert!(target_block.content.is_some());

                let ref target_args = target_block.content.as_ref().unwrap().args;

                ret.content = Some(BlockContent{
                    args: vec![],
                    exn_arg: None,
                    body: {
                        let mut vec = vec![];

                        // move every from_arg to target_arg
                        let mut i = 0;
                        for arg in block_info.from_args.iter() {
                            let m = func.new_inst(Instruction{
                                hdr: MuEntityHeader::unnamed(vm.next_id()),
                                value: Some(vec![target_args[i].clone()]),
                                ops: RwLock::new(vec![arg.clone()]),
                                v: Instruction_::Move(0)
                            });

                            vec.push(m);

                            i += 1;
                        }

                        // branch to target
                        let b = func.new_inst(Instruction{
                            hdr: MuEntityHeader::unnamed(vm.next_id()),
                            value: None,
                            ops: RwLock::new(vec![]),
                            v: Instruction_::Branch1(Destination{
                                target: target_id,
                                args: vec![]
                            })
                        });
                        vec.push(b);

                        vec
                    },
                    keepalives: None
                });

                trace!("inserting new intermediate block: {:?}", ret);

                ret
            };

            f_content.blocks.insert(block.id(), block);
        }

        func.define(f_content);
    }
}

fn process_dest(dest: Destination, blocks_to_insert: &mut Vec<IntermediateBlockInfo>, ops: &Vec<P<TreeNode>>, vm: &VM) -> Destination {
    if dest.args.is_empty() {
        dest
    } else {
        let target = dest.target;

        let mut from_args = vec![];
        for arg in dest.args.iter() {
            let from_arg = match arg {
                &DestArg::Normal(i) => ops[i].clone(),
                &DestArg::Freshbound(_) => unimplemented!()
            };

            from_args.push(from_arg);
        };

        let new_blk_id = vm.next_id();

        let dest = Destination {
            target: new_blk_id,
            args: vec![]
        };

        blocks_to_insert.push(IntermediateBlockInfo {
            blk_id: new_blk_id,
            target: target,
            from_args: from_args
        });

        dest
    }
}