Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: src/serialize.cc

Issue 373713006: Introduce code serializer/deserializer. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: addressed comments and rebased Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/serialize.h ('k') | src/snapshot-source-sink.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/accessors.h" 7 #include "src/accessors.h"
8 #include "src/api.h" 8 #include "src/api.h"
9 #include "src/base/platform/platform.h" 9 #include "src/base/platform/platform.h"
10 #include "src/bootstrapper.h" 10 #include "src/bootstrapper.h"
11 #include "src/deoptimizer.h" 11 #include "src/deoptimizer.h"
12 #include "src/execution.h" 12 #include "src/execution.h"
13 #include "src/global-handles.h" 13 #include "src/global-handles.h"
14 #include "src/ic-inl.h" 14 #include "src/ic-inl.h"
15 #include "src/natives.h" 15 #include "src/natives.h"
16 #include "src/runtime.h" 16 #include "src/runtime.h"
17 #include "src/serialize.h" 17 #include "src/serialize.h"
18 #include "src/snapshot.h" 18 #include "src/snapshot.h"
19 #include "src/snapshot-source-sink.h" 19 #include "src/snapshot-source-sink.h"
20 #include "src/stub-cache.h" 20 #include "src/stub-cache.h"
21 #include "src/v8threads.h" 21 #include "src/v8threads.h"
22 #include "src/version.h"
22 23
23 namespace v8 { 24 namespace v8 {
24 namespace internal { 25 namespace internal {
25 26
26 27
27 // ----------------------------------------------------------------------------- 28 // -----------------------------------------------------------------------------
28 // Coding of external references. 29 // Coding of external references.
29 30
30 // The encoding of an external reference. The type is in the high word. 31 // The encoding of an external reference. The type is in the high word.
31 // The id is in the low word. 32 // The id is in the low word.
(...skipping 1757 matching lines...) Expand 10 before | Expand all | Expand 10 after
1789 1790
1790 int Serializer::SpaceAreaSize(int space) { 1791 int Serializer::SpaceAreaSize(int space) {
1791 if (space == CODE_SPACE) { 1792 if (space == CODE_SPACE) {
1792 return isolate_->memory_allocator()->CodePageAreaSize(); 1793 return isolate_->memory_allocator()->CodePageAreaSize();
1793 } else { 1794 } else {
1794 return Page::kPageSize - Page::kObjectStartOffset; 1795 return Page::kPageSize - Page::kObjectStartOffset;
1795 } 1796 }
1796 } 1797 }
1797 1798
1798 1799
1800 void Serializer::PadByte() { sink_->Put(kNop, "Padding"); }
1801
1802
1799 void Serializer::Pad() { 1803 void Serializer::Pad() {
1800 // The non-branching GetInt will read up to 3 bytes too far, so we need 1804 // The non-branching GetInt will read up to 3 bytes too far, so we need
1801 // to pad the snapshot to make sure we don't read over the end. 1805 // to pad the snapshot to make sure we don't read over the end.
1802 for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) { 1806 for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) PadByte();
1803 sink_->Put(kNop, "Padding");
1804 }
1805 } 1807 }
1806 1808
1807 1809
1808 void Serializer::InitializeCodeAddressMap() { 1810 void Serializer::InitializeCodeAddressMap() {
1809 isolate_->InitializeLoggingAndCounters(); 1811 isolate_->InitializeLoggingAndCounters();
1810 code_address_map_ = new CodeAddressMap(isolate_); 1812 code_address_map_ = new CodeAddressMap(isolate_);
1811 } 1813 }
1812 1814
1813 1815
1816 ScriptData* CodeSerializer::Serialize(Handle<SharedFunctionInfo> info) {
1817 // Serialize code object.
1818 List<char> payload;
1819 ListSnapshotSink listsink(&payload);
1820 CodeSerializer ser(info->GetIsolate(), &listsink);
1821 DisallowHeapAllocation no_gc;
1822 Object** location = Handle<Object>::cast(info).location();
1823 ser.VisitPointer(location);
1824 ser.Pad();
1825
1826 // Allocate storage. The payload length may not be aligned. Round up.
1827 // TODO(yangguo) replace ScriptData with a more generic super class.
1828 int payload_length = payload.length();
1829 int raw_length = payload_length / sizeof(unsigned) + kHeaderSize;
1830 if (!IsAligned(payload_length, sizeof(unsigned))) raw_length++;
1831 unsigned* raw_data = i::NewArray<unsigned>(raw_length);
1832 char* payload_data = reinterpret_cast<char*>(raw_data + kHeaderSize);
1833
1834 // Write header.
1835 raw_data[kVersionHashOffset] = Version::Hash();
1836 raw_data[kPayloadLengthOffset] = payload_length;
1837 STATIC_ASSERT(NEW_SPACE == 0);
1838 for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
1839 raw_data[kReservationsOffset + i] = ser.CurrentAllocationAddress(i);
1840 }
1841
1842 CopyBytes(payload_data, payload.begin(), static_cast<size_t>(payload_length));
1843
1844 return new ScriptData(Vector<unsigned>(raw_data, raw_length), true);
1845 }
1846
1847
1848 void CodeSerializer::SerializeObject(Object* o, HowToCode how_to_code,
1849 WhereToPoint where_to_point, int skip) {
1850 CHECK(o->IsHeapObject());
1851 HeapObject* heap_object = HeapObject::cast(o);
1852
1853 // The code-caches link to context-specific code objects, which
1854 // the startup and context serializes cannot currently handle.
1855 ASSERT(!heap_object->IsMap() ||
1856 Map::cast(heap_object)->code_cache() ==
1857 heap_object->GetHeap()->empty_fixed_array());
1858
1859 int root_index;
1860 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1861 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1862 return;
1863 }
1864
1865 // TODO(yangguo) wire up builtins.
1866 // TODO(yangguo) wire up stubs from stub cache.
1867 // TODO(yangguo) wire up script source.
1868 // TODO(yangguo) wire up internalized strings
1869 ASSERT(!heap_object->IsInternalizedString());
1870 // TODO(yangguo) We cannot deal with different hash seeds yet.
1871 ASSERT(!heap_object->IsHashTable());
1872
1873 if (address_mapper_.IsMapped(heap_object)) {
1874 int space = SpaceOfObject(heap_object);
1875 int address = address_mapper_.MappedTo(heap_object);
1876 SerializeReferenceToPreviousObject(space, address, how_to_code,
1877 where_to_point, skip);
1878 return;
1879 }
1880
1881 if (skip != 0) {
1882 sink_->Put(kSkip, "SkipFromSerializeObject");
1883 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1884 }
1885 // Object has not yet been serialized. Serialize it here.
1886 ObjectSerializer serializer(this, heap_object, sink_, how_to_code,
1887 where_to_point);
1888 serializer.Serialize();
1889 }
1890
1891
1892 Object* CodeSerializer::Deserialize(Isolate* isolate, ScriptData* data) {
1893 const unsigned* raw_data = reinterpret_cast<const unsigned*>(data->Data());
1894 CHECK_EQ(Version::Hash(), raw_data[kVersionHashOffset]);
1895 int payload_length = raw_data[kPayloadLengthOffset];
1896 const byte* payload_data =
1897 reinterpret_cast<const byte*>(raw_data + kHeaderSize);
1898 ASSERT_LE(payload_length, data->Length() - kHeaderSize);
1899
1900 SnapshotByteSource payload(payload_data, payload_length);
1901 Deserializer deserializer(&payload);
1902 STATIC_ASSERT(NEW_SPACE == 0);
1903 // TODO(yangguo) what happens if remaining new space is too small?
1904 for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
1905 deserializer.set_reservation(
1906 i, raw_data[CodeSerializer::kReservationsOffset + i]);
1907 }
1908 Object* root;
1909 deserializer.DeserializePartial(isolate, &root);
1910 ASSERT(root->IsSharedFunctionInfo());
1911 return root;
1912 }
1814 } } // namespace v8::internal 1913 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/serialize.h ('k') | src/snapshot-source-sink.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698