ret_sink.rs 4.54 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2017 The Australian National University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use ast::ir::*;
use ast::inst::*;
use ast::ptr::*;
use vm::VM;
use compiler::CompilerPass;
20
use compiler::EPILOGUE_BLOCK_NAME;
qinsoon's avatar
qinsoon committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use std::any::Any;

/// Mu IR the client gives us may contain several RET instructions. However,
/// internally we want a single exit point for a function. In this pass, we
/// create a return sink (a block), and rewrite all the RET instruction into
/// a BRANCH with return values.
pub struct RetSink {
    name: &'static str
}

impl RetSink {
    pub fn new() -> RetSink {
        RetSink {
            name: "Creating Return Sink"
        }
    }
}

impl CompilerPass for RetSink {
    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();

        // create a return sink
        let return_sink = {
53
            let block_name = format!("{}:{}", func.name(), EPILOGUE_BLOCK_NAME);
qinsoon's avatar
qinsoon committed
54 55 56
            trace!("created return sink {}", block_name);

            let mut block = Block::new(MuEntityHeader::named(vm.next_id(), block_name));
57 58
            // tell the compiler this is the return sink
            block.trace_hint = TraceHint::ReturnSink;
qinsoon's avatar
qinsoon committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            vm.set_name(block.as_entity());

            let sig = func.sig.clone();
            let args : Vec<P<Value>> = sig.ret_tys.iter()
                .map(|ty| func.new_ssa(MuEntityHeader::unnamed(vm.next_id()), ty.clone()).clone_value()).collect();

            block.content = Some(BlockContent {
                args: args.clone(),
                exn_arg: None,
                body: vec![
                    func.new_inst(Instruction {
                        hdr: MuEntityHeader::unnamed(vm.next_id()),
                        value: None,
                        ops: args.iter().map(|val| TreeNode::new_value(val.clone())).collect(),
                        v: Instruction_::Return((0..args.len()).collect())
                    })
                ],
                keepalives: None
            });

            block
        };

        // rewrite existing RET instruction to a BRANCH
        // use RET values as BRANCH's goto values
84
        let mut has_ret : bool = false;
qinsoon's avatar
qinsoon committed
85 86 87 88 89 90 91 92 93
        for (blk_id, mut block) in f_content.blocks.iter_mut() {
            trace!("block: {}", blk_id);

            // old block content
            let block_content = block.content.as_ref().unwrap().clone();

            let mut new_body = vec![];

            for node in block_content.body.iter() {
94
                trace!("{}", node);
qinsoon's avatar
qinsoon committed
95 96 97 98 99 100 101 102 103 104 105
                match node.v {
                    TreeNode_::Instruction(Instruction {ref ops, v: Instruction_::Return(ref arg_index), ..}) => {
                        let branch_to_sink = func.new_inst(Instruction {
                            hdr: MuEntityHeader::unnamed(vm.next_id()),
                            value: None,
                            ops: ops.clone(),
                            v: Instruction_::Branch1(Destination {
                                target: return_sink.id(),
                                args: arg_index.iter().map(|i| DestArg::Normal(*i)).collect()
                            })
                        });
106
                        trace!(">> rewrite ret to {}", branch_to_sink);
qinsoon's avatar
qinsoon committed
107
                        new_body.push(branch_to_sink);
108
                        has_ret = true;
qinsoon's avatar
qinsoon committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122
                    }
                    _ => new_body.push(node.clone())
                }
            }

            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 return sink
123 124 125
        if has_ret {
            f_content.blocks.insert(return_sink.id(), return_sink);
        }
qinsoon's avatar
qinsoon committed
126 127 128 129 130

        // put back the function content
        func.content = Some(f_content);
    }
}