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

Unified Diff: runtime/vm/code_patcher.cc

Issue 11783066: Optimize function at its entry instead of at exit. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/code_generator.cc ('k') | runtime/vm/code_patcher_ia32.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/code_patcher.cc
===================================================================
--- runtime/vm/code_patcher.cc (revision 0)
+++ runtime/vm/code_patcher.cc (revision 0)
@@ -0,0 +1,85 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#include "vm/code_patcher.h"
+#include "vm/instructions.h"
+#include "vm/object.h"
+
+namespace dart {
+
+static void SwapCode(intptr_t num_bytes, char* a, char* b) {
+ for (intptr_t i = 0; i < num_bytes; i++) {
+ char tmp = *a;
+ *a = *b;
+ *b = tmp;
+ a++;
+ b++;
+ }
+}
+
+
+// The patch code buffer contains the jmp code which will be inserted at
+// entry point.
+void CodePatcher::PatchEntry(const Code& code) {
+ const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId,
+ PcDescriptors::kEntryPatch);
+ JumpPattern jmp_entry(patch_addr);
+ ASSERT(!jmp_entry.IsValid());
+ const uword patch_buffer = code.GetPatchCodePc();
+ ASSERT(patch_buffer != 0);
+ JumpPattern jmp_patch(patch_buffer);
+ ASSERT(jmp_patch.IsValid());
+ const uword jump_target = jmp_patch.TargetAddress();
+ SwapCode(jmp_patch.pattern_length_in_bytes(),
+ reinterpret_cast<char*>(patch_addr),
+ reinterpret_cast<char*>(patch_buffer));
+ jmp_entry.SetTargetAddress(jump_target);
+}
+
+
+// The entry point is a jmp instruction, the patch code buffer contains
+// original code, the entry point contains the jump instruction.
+void CodePatcher::RestoreEntry(const Code& code) {
+ const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId,
+ PcDescriptors::kEntryPatch);
+ JumpPattern jmp_entry(patch_addr);
+ ASSERT(jmp_entry.IsValid());
+ const uword jump_target = jmp_entry.TargetAddress();
+ const uword patch_buffer = code.GetPatchCodePc();
+ ASSERT(patch_buffer != 0);
+ // 'patch_buffer' contains original entry code.
+ JumpPattern jmp_patch(patch_buffer);
+ ASSERT(!jmp_patch.IsValid());
+ SwapCode(jmp_patch.pattern_length_in_bytes(),
+ reinterpret_cast<char*>(patch_addr),
+ reinterpret_cast<char*>(patch_buffer));
+ ASSERT(jmp_patch.IsValid());
+ jmp_patch.SetTargetAddress(jump_target);
+}
+
+
+bool CodePatcher::CodeIsPatchable(const Code& code) {
+ const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId,
+ PcDescriptors::kEntryPatch);
+ // kEntryPatch may not exist which means the function is not patchable.
+ if (patch_addr == 0) {
+ return true;
+ }
+ JumpPattern jmp_entry(patch_addr);
+ if (code.Size() < (jmp_entry.pattern_length_in_bytes() * 2)) {
+ return false;
+ }
+ const uword limit = patch_addr + jmp_entry.pattern_length_in_bytes();
+ // Check no object stored between patch_addr .. limit.
+ for (intptr_t i = 0; i < code.pointer_offsets_length(); i++) {
+ const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint();
+ const uword obj_end = obj_start + kWordSize;
+ if ((obj_start < limit) && (obj_end > patch_addr)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+} // namespace dart
« no previous file with comments | « runtime/vm/code_generator.cc ('k') | runtime/vm/code_patcher_ia32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698