12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
16e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_COMPILER_OAT_WRITER_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_COMPILER_OAT_WRITER_H_
19e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
20e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include <stdint.h>
21e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include <cstddef>
22700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers#include <memory>
23e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
247940e44f4517de5e2634a7e07d58d0fb26160513Brian Carlstrom#include "driver/compiler_driver.h"
25e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "mem_map.h"
26e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "oat.h"
272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h"
28a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
29e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
30e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstromnamespace art {
31e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
32ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstromclass BitVector;
33cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstromclass OutputStream;
34cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
3581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom// OatHeader         variable length with count of D OatDexFiles
36e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom//
37389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom// OatDexFile[0]     one variable sized OatDexFile with offsets to Dex and OatClasses
38e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom// OatDexFile[1]
39e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom// ...
40e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom// OatDexFile[D]
41e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom//
4289521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom// Dex[0]            one variable sized DexFile for each OatDexFile.
4389521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom// Dex[1]            these are literal copies of the input .dex files.
4489521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom// ...
4589521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom// Dex[D]
4689521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom//
47389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom// OatClass[0]       one variable sized OatClass for each of C DexFile::ClassDefs
48389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom// OatClass[1]       contains OatClass entries with class status, offsets to code, etc.
49e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom// ...
50389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom// OatClass[C]
51e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom//
5296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// GcMap             one variable sized blob with GC map.
5396c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// GcMap             GC maps are deduplicated.
5496c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// ...
5596c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// GcMap
5696c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko//
5796c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// VmapTable         one variable sized VmapTable blob (quick compiler only).
5896c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// VmapTable         VmapTables are deduplicated.
5996c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// ...
6096c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// VmapTable
6196c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko//
6296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// MappingTable      one variable sized blob with MappingTable (quick compiler only).
6396c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// MappingTable      MappingTables are deduplicated.
6496c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// ...
6596c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// MappingTable
6696c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko//
67f03c288c11ce3334fccab7fe31d8ebd14ad02157Brian Carlstrom// padding           if necessary so that the following code will be page aligned
68e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom//
6996c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// OatMethodHeader   fixed size header for a CompiledMethod including the size of the MethodCode.
7096c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// MethodCode        one variable sized blob with the code of a CompiledMethod.
7196c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// OatMethodHeader   (OatMethodHeader, MethodCode) pairs are deduplicated.
7296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// MethodCode
73e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom// ...
7496c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// OatMethodHeader
7596c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko// MethodCode
76e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom//
77e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstromclass OatWriter {
78e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom public:
793320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  OatWriter(const std::vector<const DexFile*>& dex_files,
8028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom            uint32_t image_file_location_oat_checksum,
81ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            uintptr_t image_file_location_oat_begin,
82a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light            int32_t image_patch_delta,
83ca368cb576cf6a436a32c357fca51fbb3082d7a9Ian Rogers            const CompilerDriver* compiler,
8422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe            TimingLogger* timings,
8522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe            SafeMap<std::string, std::string>* key_value_store);
86c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom
87c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom  const OatHeader& GetOatHeader() const {
88c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom    return *oat_header_;
89c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom  }
90c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom
91c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom  size_t GetSize() const {
92c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom    return size_;
93c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom  }
94c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom
953d504075f7c1204d581923460754bf6d3714b13fIan Rogers  bool Write(OutputStream* out);
96c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom
97e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  ~OatWriter();
98e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
99ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  struct DebugInfo {
100ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    DebugInfo(const std::string& method_name, uint32_t low_pc, uint32_t high_pc)
101ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell      : method_name_(method_name), low_pc_(low_pc), high_pc_(high_pc) {
102ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    }
103ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    std::string method_name_;
104ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    uint32_t    low_pc_;
105ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    uint32_t    high_pc_;
106ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  };
107ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell
108ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  const std::vector<DebugInfo>& GetCFIMethodInfo() const {
109ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell    return method_info_;
110ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  }
111ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell
11278382fa44ee505cf16835e4d22515e7252a90864Alex Light  bool DidAddSymbols() const {
11378382fa44ee505cf16835e4d22515e7252a90864Alex Light    return compiler_driver_->DidIncludeDebugSymbols();
11478382fa44ee505cf16835e4d22515e7252a90864Alex Light  }
11578382fa44ee505cf16835e4d22515e7252a90864Alex Light
116c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom private:
11796c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // The DataAccess classes are helper classes that provide access to members related to
11896c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
11996c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // we can share a lot of code for processing the maps with template classes below.
12096c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  struct GcMapDataAccess;
12196c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  struct MappingTableDataAccess;
12296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  struct VmapTableDataAccess;
12396c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko
12496c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // The function VisitDexMethods() below iterates through all the methods in all
12596c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // the compiled dex files in order of their definitions. The method visitor
12696c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // classes provide individual bits of processing for each of the passes we need to
12796c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // first collect the data we want to write to the oat file and then, in later passes,
12896c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // to actually write it.
12996c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class DexMethodVisitor;
13096c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class OatDexMethodVisitor;
13196c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class InitOatClassesMethodVisitor;
13296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class InitCodeMethodVisitor;
13396c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  template <typename DataAccess>
13496c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class InitMapMethodVisitor;
13596c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class InitImageMethodVisitor;
13696c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class WriteCodeMethodVisitor;
13796c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  template <typename DataAccess>
13896c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  class WriteMapMethodVisitor;
13996c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko
14096c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // Visit all the methods in all the compiled dex files in their definition order
14196c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // with a given DexMethodVisitor.
14296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  bool VisitDexMethods(DexMethodVisitor* visitor);
14396c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko
14481f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom  size_t InitOatHeader();
145e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  size_t InitOatDexFiles(size_t offset);
14689521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom  size_t InitDexFiles(size_t offset);
147389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom  size_t InitOatClasses(size_t offset);
14896c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  size_t InitOatMaps(size_t offset);
1490aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  size_t InitOatCode(size_t offset)
1500aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
15100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  size_t InitOatCodeDexFiles(size_t offset)
152b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
153e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
1543d504075f7c1204d581923460754bf6d3714b13fIan Rogers  bool WriteTables(OutputStream* out, const size_t file_offset);
15596c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
15696c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
1573d504075f7c1204d581923460754bf6d3714b13fIan Rogers  size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
158234da578a2d91ed7f2ef47b2ec23fb0033e2746bElliott Hughes
159e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  class OatDexFile {
160e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   public:
161265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    explicit OatDexFile(size_t offset, const DexFile& dex_file);
162e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    size_t SizeOf() const;
1633d504075f7c1204d581923460754bf6d3714b13fIan Rogers    void UpdateChecksum(OatHeader* oat_header) const;
1643d504075f7c1204d581923460754bf6d3714b13fIan Rogers    bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
165e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
166265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    // Offset of start of OatDexFile from beginning of OatHeader. It is
167265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    // used to validate file position when writing.
168265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    size_t offset_;
169265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
170e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    // data to write
171e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    uint32_t dex_file_location_size_;
172e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    const uint8_t* dex_file_location_data_;
1735b332c89fa3fdd7dc184b22c2587d28af304d019Brian Carlstrom    uint32_t dex_file_location_checksum_;
17489521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom    uint32_t dex_file_offset_;
175e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    std::vector<uint32_t> methods_offsets_;
176e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
177e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   private:
1786e3b1d900cc456a2717944f1f562a2f4df000705Brian Carlstrom    DISALLOW_COPY_AND_ASSIGN(OatDexFile);
179e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  };
180e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
181389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom  class OatClass {
182e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   public:
183ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    explicit OatClass(size_t offset,
18496c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko                      const std::vector<CompiledMethod*>& compiled_methods,
185ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom                      uint32_t num_non_null_compiled_methods,
186ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom                      mirror::Class::Status status);
187ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    ~OatClass();
188265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
189265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
190e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    size_t SizeOf() const;
1913d504075f7c1204d581923460754bf6d3714b13fIan Rogers    void UpdateChecksum(OatHeader* oat_header) const;
1923d504075f7c1204d581923460754bf6d3714b13fIan Rogers    bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
193e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
194ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
19596c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko      DCHECK_LT(class_def_method_index, compiled_methods_.size());
19696c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko      return compiled_methods_[class_def_method_index];
197ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    }
198ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
199265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    // Offset of start of OatClass from beginning of OatHeader. It is
200265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    // used to validate file position when writing. For Portable, it
201265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    // is also used to calculate the position of the OatMethodOffsets
202265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    // so that code pointers within the OatMethodOffsets can be
203265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    // patched to point to code in the Portable .o ELF objects.
204265091e581c9f643b37e7966890911f09e223269Brian Carlstrom    size_t offset_;
205265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
206ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // CompiledMethods for each class_def_method_index, or NULL if no method is available.
20796c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko    std::vector<CompiledMethod*> compiled_methods_;
208ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
209ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // Offset from OatClass::offset_ to the OatMethodOffsets for the
210ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // class_def_method_index. If 0, it means the corresponding
211ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // CompiledMethod entry in OatClass::compiled_methods_ should be
212ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // NULL and that the OatClass::type_ should be kOatClassBitmap.
213ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
214ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
215e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    // data to write
216ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
217ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits);
218ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    int16_t status_;
219ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
220ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits);
221ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    uint16_t type_;
222ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
223ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    uint32_t method_bitmap_size_;
224ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
225ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // bit vector indexed by ClassDef method index. When
226ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
227ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // method has an OatMethodOffsets in methods_offsets_, otherwise
228ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // the entry was ommited to save space. If OatClassType::type_ is
229ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // not is kOatClassBitmap, the bitmap will be NULL.
230ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    BitVector* method_bitmap_;
231ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
2328a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko    // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
2338a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko    // present in the OatClass. Note that some may be missing if
234ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // OatClass::compiled_methods_ contains NULL values (and
235ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // oat_method_offsets_offsets_from_oat_class_ should contain 0
236ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom    // values in this case).
2373320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom    std::vector<OatMethodOffsets> method_offsets_;
2387624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Marko    std::vector<OatQuickMethodHeader> method_headers_;
239e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
240e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom   private:
241389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom    DISALLOW_COPY_AND_ASSIGN(OatClass);
242e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  };
243e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
244ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell  std::vector<DebugInfo> method_info_;
245ae9fd93c39a341e2dffe15c61cc7d9e841fa92c4Mark Mendell
2461212a022fa5f8ef9585d765b1809521812af882cIan Rogers  const CompilerDriver* const compiler_driver_;
2473320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
248e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  // note OatFile does not take ownership of the DexFiles
249e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  const std::vector<const DexFile*>* dex_files_;
250e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
251c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom  // Size required for Oat data structures.
252c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom  size_t size_;
253c50d8e11a098cc5c6239aa86b47d4fcf8cbb4899Brian Carlstrom
25428db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom  // dependencies on the image.
25528db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom  uint32_t image_file_location_oat_checksum_;
256ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uintptr_t image_file_location_oat_begin_;
257a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  int32_t image_patch_delta_;
25881f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
259e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  // data to write
26022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  SafeMap<std::string, std::string>* key_value_store_;
261e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  OatHeader* oat_header_;
262e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  std::vector<OatDexFile*> oat_dex_files_;
263389efb00642100fa1c50bd47d1b8267541f9710fBrian Carlstrom  std::vector<OatClass*> oat_classes_;
264700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> interpreter_to_interpreter_bridge_;
265700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> interpreter_to_compiled_code_bridge_;
266700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
267700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> portable_imt_conflict_trampoline_;
268700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> portable_resolution_trampoline_;
269700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> portable_to_interpreter_bridge_;
270700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
271700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
272700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
273700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
2740aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao
2750aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  // output stats
2760aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_dex_file_alignment_;
2770aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_executable_offset_alignment_;
2780aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_header_;
27922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  uint32_t size_oat_header_key_value_store_;
2800aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_dex_file_;
281468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t size_interpreter_to_interpreter_bridge_;
282468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t size_interpreter_to_compiled_code_bridge_;
283468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t size_jni_dlsym_lookup_;
28488474b416eb257078e590bf9bc7957cee604a186Jeff Hao  uint32_t size_portable_imt_conflict_trampoline_;
2850aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_portable_resolution_trampoline_;
286468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t size_portable_to_interpreter_bridge_;
2872da882315a61072664f7ce3c212307342e907207Andreas Gampe  uint32_t size_quick_generic_jni_trampoline_;
28888474b416eb257078e590bf9bc7957cee604a186Jeff Hao  uint32_t size_quick_imt_conflict_trampoline_;
2890aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_quick_resolution_trampoline_;
290468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t size_quick_to_interpreter_bridge_;
291468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t size_trampoline_alignment_;
29296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  uint32_t size_method_header_;
2930aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_code_;
2940aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_code_alignment_;
2950aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_mapping_table_;
2960aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_vmap_table_;
2970aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_gc_map_;
2980aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_dex_file_location_size_;
2990aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_dex_file_location_data_;
3000aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_dex_file_location_checksum_;
3010aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_dex_file_offset_;
3020aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_dex_file_methods_offsets_;
303ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom  uint32_t size_oat_class_type_;
3040aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_class_status_;
305ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom  uint32_t size_oat_class_method_bitmaps_;
3060aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t size_oat_class_method_offsets_;
307e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
3088a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko  struct CodeOffsetsKeyComparator {
3098a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko    bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
3108a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
3118a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko        return lhs->GetQuickCode() < rhs->GetQuickCode();
3128a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      }
3138a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      // If the code is the same, all other fields are likely to be the same as well.
3148a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      if (UNLIKELY(&lhs->GetMappingTable() != &rhs->GetMappingTable())) {
3158a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko        return &lhs->GetMappingTable() < &rhs->GetMappingTable();
3168a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      }
3178a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      if (UNLIKELY(&lhs->GetVmapTable() != &rhs->GetVmapTable())) {
3188a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko        return &lhs->GetVmapTable() < &rhs->GetVmapTable();
3198a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      }
3204cdf4508903d13fd0f9fba5690aeac1b368db81bMathieu Chartier      if (UNLIKELY(&lhs->GetGcMap() != &rhs->GetGcMap())) {
3214cdf4508903d13fd0f9fba5690aeac1b368db81bMathieu Chartier        return &lhs->GetGcMap() < &rhs->GetGcMap();
3224cdf4508903d13fd0f9fba5690aeac1b368db81bMathieu Chartier      }
3238a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko      return false;
3248a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko    }
3258a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko  };
3268a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko
327e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  DISALLOW_COPY_AND_ASSIGN(OatWriter);
328e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom};
329e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
330e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}  // namespace art
331e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
332fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_COMPILER_OAT_WRITER_H_
333