Home | History | Annotate | Download | only in sea_ir
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifdef ART_SEA_IR_MODE
     18 #include <llvm/Support/Threading.h>
     19 #include <llvm/Support/raw_ostream.h>
     20 #include <llvm/Bitcode/ReaderWriter.h>
     21 
     22 #include "base/logging.h"
     23 #include "llvm/llvm_compilation_unit.h"
     24 #include "dex/portable/mir_to_gbc.h"
     25 #include "driver/compiler_driver.h"
     26 #include "verifier/method_verifier.h"
     27 #include "mirror/object.h"
     28 #include "utils.h"
     29 
     30 #include "runtime.h"
     31 #include "safe_map.h"
     32 
     33 #include "sea_ir/ir/sea.h"
     34 #include "sea_ir/debug/dot_gen.h"
     35 #include "sea_ir/types/types.h"
     36 #include "sea_ir/code_gen/code_gen.h"
     37 
     38 namespace art {
     39 
     40 static CompiledMethod* CompileMethodWithSeaIr(CompilerDriver& compiler,
     41                                      const CompilerBackend compiler_backend,
     42                                      const DexFile::CodeItem* code_item,
     43                                      uint32_t method_access_flags, InvokeType invoke_type,
     44                                      uint16_t class_def_idx, uint32_t method_idx,
     45                                      jobject class_loader, const DexFile& dex_file
     46 #if defined(ART_USE_PORTABLE_COMPILER)
     47                                      , llvm::LlvmCompilationUnit* llvm_compilation_unit
     48 #endif
     49 ) {
     50   LOG(INFO) << "Compiling " << PrettyMethod(method_idx, dex_file) << ".";
     51   sea_ir::SeaGraph* ir_graph = sea_ir::SeaGraph::GetGraph(dex_file);
     52   std::string symbol = "dex_" + MangleForJni(PrettyMethod(method_idx, dex_file));
     53   sea_ir::CodeGenData* llvm_data = ir_graph->CompileMethod(symbol,
     54           code_item, class_def_idx, method_idx, method_access_flags, dex_file);
     55   sea_ir::DotConversion dc;
     56   SafeMap<int, const sea_ir::Type*>*  types = ir_graph->ti_->GetTypeMap();
     57   dc.DumpSea(ir_graph, "/tmp/temp.dot", types);
     58   MethodReference mref(&dex_file, method_idx);
     59   std::string llvm_code = llvm_data->GetElf(compiler.GetInstructionSet());
     60   CompiledMethod* compiled_method =
     61       new CompiledMethod(compiler, compiler.GetInstructionSet(), llvm_code,
     62                          *verifier::MethodVerifier::GetDexGcMap(mref), symbol);
     63   LOG(INFO) << "Compiled SEA IR method " << PrettyMethod(method_idx, dex_file) << ".";
     64   return compiled_method;
     65 }
     66 
     67 CompiledMethod* SeaIrCompileOneMethod(CompilerDriver& compiler,
     68                                  const CompilerBackend backend,
     69                                  const DexFile::CodeItem* code_item,
     70                                  uint32_t method_access_flags,
     71                                  InvokeType invoke_type,
     72                                  uint16_t class_def_idx,
     73                                  uint32_t method_idx,
     74                                  jobject class_loader,
     75                                  const DexFile& dex_file,
     76                                  llvm::LlvmCompilationUnit* llvm_compilation_unit) {
     77   return CompileMethodWithSeaIr(compiler, backend, code_item, method_access_flags, invoke_type,
     78       class_def_idx, method_idx, class_loader, dex_file
     79 #if defined(ART_USE_PORTABLE_COMPILER)
     80                        , llvm_compilation_unit
     81 #endif
     82                        );  // NOLINT
     83 }
     84 
     85 extern "C" art::CompiledMethod*
     86     SeaIrCompileMethod(art::CompilerDriver& compiler,
     87                           const art::DexFile::CodeItem* code_item,
     88                           uint32_t method_access_flags, art::InvokeType invoke_type,
     89                           uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
     90                           const art::DexFile& dex_file) {
     91   // TODO: Check method fingerprint here to determine appropriate backend type.
     92   //       Until then, use build default
     93   art::CompilerBackend backend = compiler.GetCompilerBackend();
     94   return art::SeaIrCompileOneMethod(compiler, backend, code_item, method_access_flags, invoke_type,
     95                                class_def_idx, method_idx, class_loader, dex_file,
     96                                NULL /* use thread llvm_info */);
     97 }
     98 #endif
     99 
    100 }  // namespace art
    101