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 
CompileMethodWithSeaIr(CompilerDriver & compiler,CompilerBackend * compiler_backend,const DexFile::CodeItem * code_item,uint32_t method_access_flags,InvokeType invoke_type,uint16_t class_def_idx,uint32_t method_idx,jobject class_loader,const DexFile & dex_file,void * llvm_compilation_unit)40 static CompiledMethod* CompileMethodWithSeaIr(CompilerDriver& compiler,
41                                      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                                      void* llvm_compilation_unit) {
47   LOG(INFO) << "Compiling " << PrettyMethod(method_idx, dex_file) << ".";
48   sea_ir::SeaGraph* ir_graph = sea_ir::SeaGraph::GetGraph(dex_file);
49   std::string symbol = "dex_" + MangleForJni(PrettyMethod(method_idx, dex_file));
50   sea_ir::CodeGenData* llvm_data = ir_graph->CompileMethod(symbol,
51           code_item, class_def_idx, method_idx, method_access_flags, dex_file);
52   sea_ir::DotConversion dc;
53   SafeMap<int, const sea_ir::Type*>*  types = ir_graph->ti_->GetTypeMap();
54   dc.DumpSea(ir_graph, "/tmp/temp.dot", types);
55   MethodReference mref(&dex_file, method_idx);
56   std::string llvm_code = llvm_data->GetElf(compiler.GetInstructionSet());
57   CompiledMethod* compiled_method =
58       new CompiledMethod(compiler, compiler.GetInstructionSet(), llvm_code,
59                          *compiler.GetVerifiedMethodsData()->GetDexGcMap(mref), symbol);
60   LOG(INFO) << "Compiled SEA IR method " << PrettyMethod(method_idx, dex_file) << ".";
61   return compiled_method;
62 }
63 
SeaIrCompileOneMethod(CompilerDriver & compiler,CompilerBackend * backend,const DexFile::CodeItem * code_item,uint32_t method_access_flags,InvokeType invoke_type,uint16_t class_def_idx,uint32_t method_idx,jobject class_loader,const DexFile & dex_file,void * llvm_compilation_unit)64 CompiledMethod* SeaIrCompileOneMethod(CompilerDriver& compiler,
65                                  CompilerBackend* backend,
66                                  const DexFile::CodeItem* code_item,
67                                  uint32_t method_access_flags,
68                                  InvokeType invoke_type,
69                                  uint16_t class_def_idx,
70                                  uint32_t method_idx,
71                                  jobject class_loader,
72                                  const DexFile& dex_file,
73                                  void* llvm_compilation_unit) {
74   return CompileMethodWithSeaIr(compiler, backend, code_item, method_access_flags, invoke_type,
75       class_def_idx, method_idx, class_loader, dex_file, llvm_compilation_unit);
76 }
77 
78 extern "C" art::CompiledMethod*
SeaIrCompileMethod(art::CompilerDriver & compiler,const art::DexFile::CodeItem * code_item,uint32_t method_access_flags,art::InvokeType invoke_type,uint16_t class_def_idx,uint32_t method_idx,jobject class_loader,const art::DexFile & dex_file)79     SeaIrCompileMethod(art::CompilerDriver& compiler,
80                           const art::DexFile::CodeItem* code_item,
81                           uint32_t method_access_flags, art::InvokeType invoke_type,
82                           uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
83                           const art::DexFile& dex_file) {
84   // TODO: Check method fingerprint here to determine appropriate backend type.
85   //       Until then, use build default
86   art::CompilerBackend* backend = compiler.GetCompilerBackend();
87   return art::SeaIrCompileOneMethod(compiler, backend, code_item, method_access_flags, invoke_type,
88                                class_def_idx, method_idx, class_loader, dex_file,
89                                NULL /* use thread llvm_info */);
90 }
91 #endif
92 
93 }  // namespace art
94