Merge "Fix x86 long math." into ics-mr1-plus-art
diff --git a/build/Android.common.mk b/build/Android.common.mk
index 41edefa..28ac65b 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -104,7 +104,6 @@
 LIBART_COMMON_SRC_FILES := \
 	src/atomic.cc.arm \
 	src/card_table.cc \
-	src/constants.cc \
 	src/check_jni.cc \
 	src/class_linker.cc \
 	src/class_loader.cc \
@@ -268,6 +267,12 @@
 $(error unsupported HOST_ARCH=$(HOST_ARCH))
 endif # HOST_ARCH != x86
 
+LIBART_ENUM_OPERATOR_OUT_HEADER_FILES := \
+	src/indirect_reference_table.h \
+	src/instruction_set.h \
+	src/invoke_type.h \
+	src/mutex.h
+
 LIBARTTEST_COMMON_SRC_FILES := \
 	test/StackWalk/stack_walk_jni.cc \
 	test/ReferenceMap/stack_walk_refmap_jni.cc
diff --git a/build/Android.libart.mk b/build/Android.libart.mk
index 5406c9e..f14f934 100644
--- a/build/Android.libart.mk
+++ b/build/Android.libart.mk
@@ -54,8 +54,20 @@
     LOCAL_SRC_FILES := $(LIBART_TARGET_SRC_FILES)
   else # host
     LOCAL_SRC_FILES := $(LIBART_HOST_SRC_FILES)
+    LOCAL_IS_HOST_MODULE := true
   endif
 
+  GENERATED_SRC_DIR := $$(call intermediates-dir-for,$$(LOCAL_MODULE_CLASS),$$(LOCAL_MODULE),$$(LOCAL_IS_HOST_MODULE),)
+  ENUM_OPERATOR_OUT_CC_FILES := $$(patsubst %.h,%_operator_out.cc,$$(LIBART_ENUM_OPERATOR_OUT_HEADER_FILES))
+  ENUM_OPERATOR_OUT_GEN := $$(addprefix $$(GENERATED_SRC_DIR)/,$$(ENUM_OPERATOR_OUT_CC_FILES))
+
+$$(ENUM_OPERATOR_OUT_GEN): art/tools/generate-operator-out.py
+$$(ENUM_OPERATOR_OUT_GEN): PRIVATE_CUSTOM_TOOL = art/tools/generate-operator-out.py $$< > $$@
+$$(ENUM_OPERATOR_OUT_GEN): $$(GENERATED_SRC_DIR)/%_operator_out.cc : $(LOCAL_PATH)/%.h
+	$$(transform-generated-source)
+
+  LOCAL_GENERATED_SOURCES += $$(ENUM_OPERATOR_OUT_GEN)
+
   LOCAL_CFLAGS := $(LIBART_CFLAGS)
   ifeq ($$(art_target_or_host),target)
     LOCAL_CFLAGS += $(ART_TARGET_CFLAGS)
@@ -102,7 +114,6 @@
     endif
     include $(BUILD_SHARED_LIBRARY)
   else # host
-    LOCAL_IS_HOST_MODULE := true
     ifeq ($(ART_USE_LLVM_COMPILER),true)
       include $(LLVM_GEN_INTRINSICS_MK)
       include $(LLVM_HOST_BUILD_MK)
diff --git a/src/compiler_llvm/art_module.ll b/src/compiler_llvm/art_module.ll
index 8eb1783..b85ba7c 100644
--- a/src/compiler_llvm/art_module.ll
+++ b/src/compiler_llvm/art_module.ll
@@ -58,7 +58,7 @@
 declare void @art_throw_array_bounds_from_code(i32, i32)
 declare void @art_throw_no_such_method_from_code(i32)
 declare void @art_throw_null_pointer_exception_from_code()
-declare void @art_throw_stack_overflow_from_code(%JavaObject*)
+declare void @art_throw_stack_overflow_from_code()
 declare void @art_throw_exception_from_code(%JavaObject*)
 
 declare i32 @art_find_catch_block_from_code(%JavaObject*, i32)
diff --git a/src/compiler_llvm/generated/art_module.cc b/src/compiler_llvm/generated/art_module.cc
index 18432d5..973ef06 100644
--- a/src/compiler_llvm/generated/art_module.cc
+++ b/src/compiler_llvm/generated/art_module.cc
@@ -413,7 +413,7 @@
 Function* func_art_throw_stack_overflow_from_code = mod->getFunction("art_throw_stack_overflow_from_code");
 if (!func_art_throw_stack_overflow_from_code) {
 func_art_throw_stack_overflow_from_code = Function::Create(
- /*Type=*/FuncTy_4,
+ /*Type=*/FuncTy_5,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_throw_stack_overflow_from_code", mod); // (external, no body)
 func_art_throw_stack_overflow_from_code->setCallingConv(CallingConv::C);
diff --git a/src/compiler_llvm/method_compiler.cc b/src/compiler_llvm/method_compiler.cc
index 0ab8871..bcd40de 100644
--- a/src/compiler_llvm/method_compiler.cc
+++ b/src/compiler_llvm/method_compiler.cc
@@ -139,6 +139,9 @@
 
 void MethodCompiler::EmitPrologue() {
   // Create basic blocks for prologue
+  basic_block_stack_overflow_ =
+    llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
+
   basic_block_reg_alloca_ =
     llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
 
@@ -151,6 +154,9 @@
   basic_block_reg_arg_init_ =
     llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
 
+  // Before alloca, check stack overflow.
+  EmitStackOverflowCheck();
+
   // Create register array
   for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
     regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
@@ -170,7 +176,61 @@
 }
 
 
+void MethodCompiler::EmitStackOverflowCheck() {
+  irb_.SetInsertPoint(basic_block_stack_overflow_);
+
+  // Call llvm intrinsic function to get frame address.
+  llvm::Function* frameaddress =
+      llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
+
+  // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
+  llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
+
+  // Cast i8* to int
+  frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
+
+  // Get thread.stack_end_
+  llvm::Value* thread_object_addr =
+    irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
+
+  llvm::Value* stack_end_addr =
+    irb_.CreatePtrDisp(thread_object_addr,
+                       irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()),
+                       irb_.getPtrEquivIntTy()->getPointerTo());
+
+  llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr);
+
+  // Check the frame address < thread.stack_end_ ?
+  llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
+
+  llvm::BasicBlock* block_exception =
+      llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
+
+  llvm::BasicBlock* block_continue =
+      llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
+
+  irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
+
+  // If stack overflow, throw exception.
+  irb_.SetInsertPoint(block_exception);
+  irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
+
+  // Unwind.
+  char ret_shorty = method_helper_.GetShorty()[0];
+  if (ret_shorty == 'V') {
+    irb_.CreateRetVoid();
+  } else {
+    irb_.CreateRet(irb_.getJZero(ret_shorty));
+  }
+
+  basic_block_stack_overflow_ = block_continue;
+}
+
+
 void MethodCompiler::EmitPrologueLastBranch() {
+  irb_.SetInsertPoint(basic_block_stack_overflow_);
+  irb_.CreateBr(basic_block_reg_alloca_);
+
   irb_.SetInsertPoint(basic_block_reg_alloca_);
   irb_.CreateBr(basic_block_shadow_frame_alloca_);
 
diff --git a/src/compiler_llvm/method_compiler.h b/src/compiler_llvm/method_compiler.h
index 6f24799..006f763 100644
--- a/src/compiler_llvm/method_compiler.h
+++ b/src/compiler_llvm/method_compiler.h
@@ -88,6 +88,7 @@
  private:
   void CreateFunction();
   void EmitPrologue();
+  void EmitStackOverflowCheck();
   void EmitPrologueLastBranch();
   void EmitPrologueAllocShadowFrame();
   void EmitPrologueAssignArgRegister();
@@ -447,6 +448,7 @@
   std::vector<DalvikReg*> regs_;
   UniquePtr<DalvikReg> retval_reg_;
 
+  llvm::BasicBlock* basic_block_stack_overflow_;
   llvm::BasicBlock* basic_block_reg_alloca_;
   llvm::BasicBlock* basic_block_shadow_frame_alloca_;
   llvm::BasicBlock* basic_block_reg_zero_init_;
diff --git a/src/compiler_llvm/runtime_support_func_list.h b/src/compiler_llvm/runtime_support_func_list.h
index 38d7a89..1c21d48 100644
--- a/src/compiler_llvm/runtime_support_func_list.h
+++ b/src/compiler_llvm/runtime_support_func_list.h
@@ -23,6 +23,7 @@
   V(PopShadowFrame, art_pop_shadow_frame_from_code) \
   V(TestSuspend, art_test_suspend_from_code) \
   V(ThrowException, art_throw_exception_from_code) \
+  V(ThrowStackOverflowException, art_throw_stack_overflow_from_code) \
   V(ThrowNullPointerException, art_throw_null_pointer_exception_from_code) \
   V(ThrowDivZeroException, art_throw_div_zero_from_code) \
   V(ThrowIndexOutOfBounds, art_throw_array_bounds_from_code) \
diff --git a/src/compiler_llvm/runtime_support_llvm.cc b/src/compiler_llvm/runtime_support_llvm.cc
index 59b8efb..6ad80f8 100644
--- a/src/compiler_llvm/runtime_support_llvm.cc
+++ b/src/compiler_llvm/runtime_support_llvm.cc
@@ -113,17 +113,26 @@
   thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
 }
 
-void art_throw_stack_overflow_from_code(void*) {
+void art_throw_stack_overflow_from_code() {
   Thread* thread = Thread::Current();
+  if (Runtime::Current()->IsMethodTracingActive()) {
+    TraceMethodUnwindFromCode(thread);
+  }
+  thread->SetStackEndForStackOverflow();  // Allow space on the stack for constructor to execute
   thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
       "stack size %zdkb; default stack size: %zdkb",
       thread->GetStackSize() / KB,
       Runtime::Current()->GetDefaultStackSize() / KB);
+  thread->ResetDefaultStackEnd();  // Return to default stack size
 }
 
 void art_throw_exception_from_code(Object* exception) {
   Thread* thread = Thread::Current();
-  thread->SetException(static_cast<Throwable*>(exception));
+  if (exception == NULL) {
+    thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
+  } else {
+    thread->SetException(static_cast<Throwable*>(exception));
+  }
 }
 
 int32_t art_find_catch_block_from_code(Method* current_method, int32_t dex_pc) {
@@ -446,7 +455,7 @@
 class CStringComparator {
  public:
   bool operator()(const char* lhs, const char* rhs) const {
-    return (strcmp(lhs, rhs) <= 0);
+    return (strcmp(lhs, rhs) < 0);
   }
 };
 
@@ -477,13 +486,16 @@
 
   static const size_t num_entries = sizeof(names) / sizeof(const char* const);
 
-  const char* const* matched_name_ptr =
-      std::lower_bound(names, names + num_entries, name, CStringComparator());
+  const char* const* const names_begin = names;
+  const char* const* const names_end = names + num_entries;
 
-  if (matched_name_ptr == names + num_entries) {
-    return NULL;
+  const char* const* name_lbound_ptr =
+      std::lower_bound(names_begin, names_end, name, CStringComparator());
+
+  if (name_lbound_ptr < names_end && strcmp(*name_lbound_ptr, name) == 0) {
+    return funcs[name_lbound_ptr - names_begin];
   } else {
-    return funcs[matched_name_ptr - names];
+    return NULL;
   }
 }
 
diff --git a/src/compiler_llvm/runtime_support_llvm.h b/src/compiler_llvm/runtime_support_llvm.h
index f0e0a58..51d238b 100644
--- a/src/compiler_llvm/runtime_support_llvm.h
+++ b/src/compiler_llvm/runtime_support_llvm.h
@@ -42,7 +42,7 @@
 
 void art_throw_null_pointer_exception_from_code();
 
-void art_throw_stack_overflow_from_code(void*);
+void art_throw_stack_overflow_from_code();
 
 void art_throw_exception_from_code(Object* exception);
 
diff --git a/src/constants.cc b/src/constants.cc
deleted file mode 100644
index 12632eb..0000000
--- a/src/constants.cc
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// TODO: automatically generate operator<<s for enum types.
-
-#include <iostream>
-
-#include "instruction_set.h"
-#include "invoke_type.h"
-
-namespace art {
-
-std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs) {
-  switch (rhs) {
-    case kNone: os << "none"; break;
-    case kArm: os << "ARM"; break;
-    case kThumb2: os << "Thumb2"; break;
-    case kX86: os << "x86"; break;
-    case kMips: os << "MIPS"; break;
-    default: os << "InstructionSet[" << static_cast<int>(rhs) << "]"; break;
-  }
-  return os;
-}
-
-std::ostream& operator<<(std::ostream& os, const InvokeType& rhs) {
-  switch (rhs) {
-    case kStatic: os << "static"; break;
-    case kDirect: os << "direct"; break;
-    case kVirtual: os << "virtual"; break;
-    case kSuper: os << "super"; break;
-    case kInterface: os << "interface"; break;
-    default: os << "InvokeType[" << static_cast<int>(rhs) << "]"; break;
-  }
-  return os;
-}
-
-}  // namespace art
diff --git a/src/indirect_reference_table.cc b/src/indirect_reference_table.cc
index ac5e402..14d804b 100644
--- a/src/indirect_reference_table.cc
+++ b/src/indirect_reference_table.cc
@@ -329,27 +329,6 @@
   }
 }
 
-std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) {
-  switch (rhs) {
-  case kSirtOrInvalid:
-    os << "stack indirect reference table or invalid reference";
-    break;
-  case kLocal:
-    os << "local reference";
-    break;
-  case kGlobal:
-    os << "global reference";
-    break;
-  case kWeakGlobal:
-    os << "weak global reference";
-    break;
-  default:
-    os << "IndirectRefKind[" << static_cast<int>(rhs) << "]";
-    break;
-  }
-  return os;
-}
-
 void IndirectReferenceTable::Dump() const {
   LOG(WARNING) << kind_ << " table dump:";
   std::vector<const Object*> entries(table_, table_ + Capacity());
diff --git a/src/indirect_reference_table.h b/src/indirect_reference_table.h
index b1f6d8c..b0a0d64 100644
--- a/src/indirect_reference_table.h
+++ b/src/indirect_reference_table.h
@@ -106,12 +106,12 @@
  * For convenience these match up with enum jobjectRefType from jni.h.
  */
 enum IndirectRefKind {
-    kSirtOrInvalid = 0,
-    kLocal         = 1,
-    kGlobal        = 2,
-    kWeakGlobal    = 3
+  kSirtOrInvalid = 0, // <<stack indirect reference table or invalid reference>>
+  kLocal         = 1, // <<local reference>>
+  kGlobal        = 2, // <<global reference>>
+  kWeakGlobal    = 3  // <<weak global reference>>
 };
-std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs);
+std::ostream& operator<<(std::ostream& os, const IndirectRefKind& rhs);
 
 /*
  * Determine what kind of indirect reference this is.
diff --git a/src/invoke_type.h b/src/invoke_type.h
index f37f1b4..d724fdb 100644
--- a/src/invoke_type.h
+++ b/src/invoke_type.h
@@ -22,7 +22,11 @@
 namespace art {
 
 enum InvokeType {
-  kStatic, kDirect, kVirtual, kSuper, kInterface,
+  kStatic,    // <<static>>
+  kDirect,    // <<direct>>
+  kVirtual,   // <<virtual>>
+  kSuper,     // <<super>>
+  kInterface, // <<interface>>
   kMaxInvokeType = kInterface
 };
 
diff --git a/src/jdwp/jdwp.h b/src/jdwp/jdwp.h
index d297f6f..7d8bf22 100644
--- a/src/jdwp/jdwp.h
+++ b/src/jdwp/jdwp.h
@@ -87,8 +87,8 @@
  */
 enum JdwpTransportType {
   kJdwpTransportUnknown = 0,
-  kJdwpTransportSocket,       /* transport=dt_socket */
-  kJdwpTransportAndroidAdb,   /* transport=dt_android_adb */
+  kJdwpTransportSocket,       // transport=dt_socket
+  kJdwpTransportAndroidAdb,   // transport=dt_android_adb
 };
 std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
 
diff --git a/src/mark_sweep.cc b/src/mark_sweep.cc
index 381c7f0..1553dd8 100644
--- a/src/mark_sweep.cc
+++ b/src/mark_sweep.cc
@@ -139,6 +139,7 @@
   void* arg = reinterpret_cast<void*>(this);
   const std::vector<Space*>& spaces = heap_->GetSpaces();
   for (size_t i = 0; i < spaces.size(); ++i) {
+#if !defined(ART_USE_LLVM_COMPILER)
 #ifndef NDEBUG
     uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
     uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
@@ -154,6 +155,12 @@
       mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
     }
 #endif
+#else
+    // TODO: Implement card marking.
+    uintptr_t begin = reinterpret_cast<uintptr_t>(spaces[i]->Begin());
+    uintptr_t end = reinterpret_cast<uintptr_t>(spaces[i]->End());
+    mark_bitmap_->ScanWalk(begin, end, &MarkSweep::ScanBitmapCallback, arg);
+#endif
   }
   finger_ = reinterpret_cast<Object*>(~0);
   // TODO: tune the frequency of emptying the mark stack
diff --git a/src/mutex.cc b/src/mutex.cc
index 1e30543..b0b82f3 100644
--- a/src/mutex.cc
+++ b/src/mutex.cc
@@ -194,14 +194,4 @@
   }
 }
 
-std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
-  switch (rhs) {
-    case kHeapLock: os << "HeapLock"; break;
-    case kThreadListLock: os << "ThreadListLock"; break;
-    case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
-    default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
-  }
-  return os;
-}
-
 }  // namespace
diff --git a/src/thread.cc b/src/thread.cc
index 10b099d..846aa39 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -482,6 +482,7 @@
     }
     const int kMaxRepetition = 3;
     Method* m = frame.GetMethod();
+#if !defined(ART_USE_LLVM_COMPILER)
     Class* c = m->GetDeclaringClass();
     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
     const DexCache* dex_cache = c->GetDexCache();
@@ -490,6 +491,10 @@
       const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
       line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc));
     }
+#else
+    // Compiler_LLVM stores line_number in the ShadowFrame, and passes it to visitor.
+    int line_number = static_cast<int>(pc);
+#endif
     if (line_number == last_line_number && last_method == m) {
       repetition_count++;
     } else {
@@ -1594,8 +1599,8 @@
 #else
 Method* Thread::GetCurrentMethod(uintptr_t*, Method***) const {
   ShadowFrame* frame = top_shadow_frame_;
-  while (frame->GetMethod()->IsNative()) {
-    frame = frame->GetLink();
+  if (frame == NULL) {
+    return NULL;
   }
   return frame->GetMethod();
 }
@@ -1705,12 +1710,14 @@
   SirtVisitRoots(visitor, arg);
   ShadowFrameVisitRoots(visitor, arg);
 
+#if !defined(ART_USE_LLVM_COMPILER)
   // Cheat and steal the long jump context. Assume that we are not doing a GC during exception
   // delivery.
   Context* context = GetLongJumpContext();
   // Visit roots on this thread's stack
   ReferenceMapVisitor mapper(context, visitor, arg);
   WalkStack(&mapper);
+#endif
 }
 
 #if VERIFY_OBJECT_ENABLED
@@ -1719,9 +1726,11 @@
 }
 
 void Thread::VerifyStack() {
+#if !defined(ART_USE_LLVM_COMPILER)
   UniquePtr<Context> context(Context::Create());
   ReferenceMapVisitor mapper(context.get(), VerifyObject, NULL);
   WalkStack(&mapper);
+#endif
 }
 #endif
 
diff --git a/src/thread.h b/src/thread.h
index 40409b6..a6459c6 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -87,7 +87,11 @@
   };
 
   // Space to throw a StackOverflowError in.
+#if !defined(ART_USE_LLVM_COMPILER)
   static const size_t kStackOverflowReservedBytes = 4 * KB;
+#else  // LLVM_x86 requires more memory to throw stack overflow exception.
+  static const size_t kStackOverflowReservedBytes = 8 * KB;
+#endif
 
   static const size_t kDefaultStackSize = 96 * KB;
 
diff --git a/tools/generate-operator-out.py b/tools/generate-operator-out.py
new file mode 100755
index 0000000..b1d102e
--- /dev/null
+++ b/tools/generate-operator-out.py
@@ -0,0 +1,150 @@
+#!/usr/bin/python2.4
+#
+# Copyright 2012 Google Inc. All Rights Reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#    * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#    * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""Generates default implementations of operator<< for enum types."""
+
+import codecs
+import os
+import re
+import string
+import sys
+
+
+_ENUM_START_RE = re.compile(r'\benum\b\s+(\S+)\s+\{')
+_ENUM_VALUE_RE = re.compile(r'([A-Za-z0-9_]+)(.*)')
+_ENUM_END_RE = re.compile(r'^\s*\};$')
+_ENUMS = {}
+
+def Confused(filename, line_number, line):
+  sys.stderr.write('%s:%d: confused by:\n%s\n' % (filename, line_number, line))
+  sys.exit(1)
+
+
+def ProcessFile(filename):
+  lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
+  in_enum = False
+  line_number = 0
+  for raw_line in lines:
+    line_number += 1
+
+    # TODO: take namespaces and enclosing classes/structs into account.
+
+    # Is this the start of a new enum?
+    if not in_enum:
+      m = _ENUM_START_RE.search(raw_line)
+      if m:
+        # Yes, so add an empty entry to _ENUMS for this enum.
+        enum_name = m.group(1)
+        _ENUMS[enum_name] = []
+        in_enum = True
+      continue
+
+    # Is this the end of the current enum?
+    m = _ENUM_END_RE.search(raw_line)
+    if m:
+      if not in_enum:
+        Confused(filename, line_number, raw_line)
+      in_enum = False
+      continue
+
+    # Is this another enum value?
+    m = _ENUM_VALUE_RE.search(raw_line.strip())
+    if not m:
+      Confused(filename, line_number, raw_line)
+
+    enum_value = m.group(1)
+
+    # By default, we turn "kSomeValue" into "SomeValue".
+    enum_text = enum_value
+    if enum_text.startswith('k'):
+      enum_text = enum_text[1:]
+
+    # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah".
+    rest = m.group(2).strip()
+    m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+)').search(rest)
+    if m_literal:
+      rest = rest[(len(m_literal.group(0))):]
+
+    # With "kSomeValue = kOtherValue," we take the original and skip later synonyms.
+    # TODO: check that the rhs is actually an existing value.
+    if rest.startswith('= k'):
+      continue
+
+    # Remove any trailing comma and whitespace
+    if rest.startswith(','):
+      rest = rest[1:]
+    rest = rest.strip()
+
+    # Anything left should be a comment.
+    if len(rest) and not rest.startswith('// '):
+      print rest
+      Confused(filename, line_number, raw_line)
+
+    m_comment = re.compile(r'<<(.*?)>>').search(rest)
+    if m_comment:
+      enum_text = m_comment.group(1)
+
+    _ENUMS[enum_name].append((enum_value, enum_text))
+
+def main():
+  header_files = []
+  for header_file in sys.argv[1:]:
+    header_files.append(header_file)
+    ProcessFile(header_file)
+
+  print '#include <iostream>'
+  print
+
+  for header_file in header_files:
+    print '#include "%s"' % header_file
+
+  print
+  print 'namespace art {'
+  print
+
+  for enum_name in _ENUMS:
+    print '// This was automatically generated by %s --- do not edit!' % sys.argv[0]
+    print 'std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name
+    print '  switch (rhs) {'
+    for (enum_value, enum_text) in _ENUMS[enum_name]:
+      print '    case %s: os << "%s"; break;' % (enum_value, enum_text)
+    print '    default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name
+    print '  }'
+    print '  return os;'
+    print '}'
+    print
+
+  print '} // namespace art'
+
+  sys.exit(0)
+
+
+if __name__ == '__main__':
+  main()