Remove the hard coded "constants" related to shadow frame.

Change-Id: I716f8ed8ea01605ccbd0aa47bf12acd04ed85c2b
diff --git a/src/compiler_llvm/art_module.ll b/src/compiler_llvm/art_module.ll
index 39fbec7..78d388b 100644
--- a/src/compiler_llvm/art_module.ll
+++ b/src/compiler_llvm/art_module.ll
@@ -21,14 +21,14 @@
 
 %JavaObject = type opaque
 
-%ArtFrame = type { %ArtFrame*           ; Previous frame (sirt)
-                 , %JavaObject*         ; Method object pointer
-                 , i32                  ; Line number for stack backtrace
-                 , i32                  ; Size of SIRT
-                 ; [0 x %JavaObject*]   ; Stack indirect reference table
-                 }
+%ShadowFrame = type { %ShadowFrame*        ; Previous frame
+                    , %JavaObject*         ; Method object pointer
+                    , i32                  ; Line number for stack backtrace
+                    , i32                  ; Number of references
+                    ; [0 x %JavaObject*]   ; References
+                    }
 
-declare void @__art_type_list(%JavaObject*, %ArtFrame*)
+declare void @__art_type_list(%JavaObject*, %ShadowFrame*)
 
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -43,7 +43,7 @@
 
 declare void @art_test_suspend_from_code()
 
-declare void @art_push_shadow_frame_from_code(%ArtFrame*)
+declare void @art_push_shadow_frame_from_code(%ShadowFrame*)
 declare void @art_pop_shadow_frame_from_code()
 
 
diff --git a/src/compiler_llvm/generated/art_module.cc b/src/compiler_llvm/generated/art_module.cc
index dedc392..fe708ae 100644
--- a/src/compiler_llvm/generated/art_module.cc
+++ b/src/compiler_llvm/generated/art_module.cc
@@ -39,19 +39,19 @@
 PointerType* PointerTy_1 = PointerType::get(StructTy_JavaObject, 0);
 
 FuncTy_0_args.push_back(PointerTy_1);
-StructType *StructTy_ArtFrame = mod->getTypeByName("ArtFrame");
-if (!StructTy_ArtFrame) {
-StructTy_ArtFrame = StructType::create(mod->getContext(), "ArtFrame");
+StructType *StructTy_ShadowFrame = mod->getTypeByName("ShadowFrame");
+if (!StructTy_ShadowFrame) {
+StructTy_ShadowFrame = StructType::create(mod->getContext(), "ShadowFrame");
 }
-std::vector<Type*>StructTy_ArtFrame_fields;
-PointerType* PointerTy_2 = PointerType::get(StructTy_ArtFrame, 0);
+std::vector<Type*>StructTy_ShadowFrame_fields;
+PointerType* PointerTy_2 = PointerType::get(StructTy_ShadowFrame, 0);
 
-StructTy_ArtFrame_fields.push_back(PointerTy_2);
-StructTy_ArtFrame_fields.push_back(PointerTy_1);
-StructTy_ArtFrame_fields.push_back(IntegerType::get(mod->getContext(), 32));
-StructTy_ArtFrame_fields.push_back(IntegerType::get(mod->getContext(), 32));
-if (StructTy_ArtFrame->isOpaque()) {
-StructTy_ArtFrame->setBody(StructTy_ArtFrame_fields, /*isPacked=*/false);
+StructTy_ShadowFrame_fields.push_back(PointerTy_2);
+StructTy_ShadowFrame_fields.push_back(PointerTy_1);
+StructTy_ShadowFrame_fields.push_back(IntegerType::get(mod->getContext(), 32));
+StructTy_ShadowFrame_fields.push_back(IntegerType::get(mod->getContext(), 32));
+if (StructTy_ShadowFrame->isOpaque()) {
+StructTy_ShadowFrame->setBody(StructTy_ShadowFrame_fields, /*isPacked=*/false);
 }
 
 
@@ -142,26 +142,26 @@
  /*isVarArg=*/false);
 
 std::vector<Type*>FuncTy_14_args;
-FuncTy_14_args.push_back(PointerTy_1);
 FuncTy_14_args.push_back(IntegerType::get(mod->getContext(), 32));
+FuncTy_14_args.push_back(PointerTy_1);
+FuncTy_14_args.push_back(PointerTy_1);
 FunctionType* FuncTy_14 = FunctionType::get(
  /*Result=*/PointerTy_1,
  /*Params=*/FuncTy_14_args,
  /*isVarArg=*/false);
 
 std::vector<Type*>FuncTy_15_args;
-FuncTy_15_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_15_args.push_back(PointerTy_1);
 FuncTy_15_args.push_back(IntegerType::get(mod->getContext(), 32));
 FunctionType* FuncTy_15 = FunctionType::get(
- /*Result=*/IntegerType::get(mod->getContext(), 32),
+ /*Result=*/PointerTy_1,
  /*Params=*/FuncTy_15_args,
  /*isVarArg=*/false);
 
 std::vector<Type*>FuncTy_16_args;
 FuncTy_16_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_16_args.push_back(PointerTy_1);
-FuncTy_16_args.push_back(IntegerType::get(mod->getContext(), 64));
+FuncTy_16_args.push_back(IntegerType::get(mod->getContext(), 32));
 FunctionType* FuncTy_16 = FunctionType::get(
  /*Result=*/IntegerType::get(mod->getContext(), 32),
  /*Params=*/FuncTy_16_args,
@@ -170,7 +170,7 @@
 std::vector<Type*>FuncTy_17_args;
 FuncTy_17_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_17_args.push_back(PointerTy_1);
-FuncTy_17_args.push_back(PointerTy_1);
+FuncTy_17_args.push_back(IntegerType::get(mod->getContext(), 64));
 FunctionType* FuncTy_17 = FunctionType::get(
  /*Result=*/IntegerType::get(mod->getContext(), 32),
  /*Params=*/FuncTy_17_args,
@@ -179,6 +179,7 @@
 std::vector<Type*>FuncTy_18_args;
 FuncTy_18_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_18_args.push_back(PointerTy_1);
+FuncTy_18_args.push_back(PointerTy_1);
 FunctionType* FuncTy_18 = FunctionType::get(
  /*Result=*/IntegerType::get(mod->getContext(), 32),
  /*Params=*/FuncTy_18_args,
@@ -188,17 +189,15 @@
 FuncTy_19_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_19_args.push_back(PointerTy_1);
 FunctionType* FuncTy_19 = FunctionType::get(
- /*Result=*/IntegerType::get(mod->getContext(), 64),
+ /*Result=*/IntegerType::get(mod->getContext(), 32),
  /*Params=*/FuncTy_19_args,
  /*isVarArg=*/false);
 
 std::vector<Type*>FuncTy_20_args;
 FuncTy_20_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_20_args.push_back(PointerTy_1);
-FuncTy_20_args.push_back(PointerTy_1);
-FuncTy_20_args.push_back(IntegerType::get(mod->getContext(), 32));
 FunctionType* FuncTy_20 = FunctionType::get(
- /*Result=*/IntegerType::get(mod->getContext(), 32),
+ /*Result=*/IntegerType::get(mod->getContext(), 64),
  /*Params=*/FuncTy_20_args,
  /*isVarArg=*/false);
 
@@ -206,7 +205,7 @@
 FuncTy_21_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_21_args.push_back(PointerTy_1);
 FuncTy_21_args.push_back(PointerTy_1);
-FuncTy_21_args.push_back(IntegerType::get(mod->getContext(), 64));
+FuncTy_21_args.push_back(IntegerType::get(mod->getContext(), 32));
 FunctionType* FuncTy_21 = FunctionType::get(
  /*Result=*/IntegerType::get(mod->getContext(), 32),
  /*Params=*/FuncTy_21_args,
@@ -216,7 +215,7 @@
 FuncTy_22_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_22_args.push_back(PointerTy_1);
 FuncTy_22_args.push_back(PointerTy_1);
-FuncTy_22_args.push_back(PointerTy_1);
+FuncTy_22_args.push_back(IntegerType::get(mod->getContext(), 64));
 FunctionType* FuncTy_22 = FunctionType::get(
  /*Result=*/IntegerType::get(mod->getContext(), 32),
  /*Params=*/FuncTy_22_args,
@@ -226,8 +225,9 @@
 FuncTy_23_args.push_back(IntegerType::get(mod->getContext(), 32));
 FuncTy_23_args.push_back(PointerTy_1);
 FuncTy_23_args.push_back(PointerTy_1);
+FuncTy_23_args.push_back(PointerTy_1);
 FunctionType* FuncTy_23 = FunctionType::get(
- /*Result=*/IntegerType::get(mod->getContext(), 64),
+ /*Result=*/IntegerType::get(mod->getContext(), 32),
  /*Params=*/FuncTy_23_args,
  /*isVarArg=*/false);
 
@@ -236,7 +236,7 @@
 FuncTy_24_args.push_back(PointerTy_1);
 FuncTy_24_args.push_back(PointerTy_1);
 FunctionType* FuncTy_24 = FunctionType::get(
- /*Result=*/PointerTy_1,
+ /*Result=*/IntegerType::get(mod->getContext(), 64),
  /*Params=*/FuncTy_24_args,
  /*isVarArg=*/false);
 
@@ -526,7 +526,7 @@
 Function* func_art_find_interface_method_from_code = mod->getFunction("art_find_interface_method_from_code");
 if (!func_art_find_interface_method_from_code) {
 func_art_find_interface_method_from_code = Function::Create(
- /*Type=*/FuncTy_24,
+ /*Type=*/FuncTy_14,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_find_interface_method_from_code", mod); // (external, no body)
 func_art_find_interface_method_from_code->setCallingConv(CallingConv::C);
@@ -537,7 +537,7 @@
 Function* func_art_find_virtual_method_from_code = mod->getFunction("art_find_virtual_method_from_code");
 if (!func_art_find_virtual_method_from_code) {
 func_art_find_virtual_method_from_code = Function::Create(
- /*Type=*/FuncTy_24,
+ /*Type=*/FuncTy_14,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_find_virtual_method_from_code", mod); // (external, no body)
 func_art_find_virtual_method_from_code->setCallingConv(CallingConv::C);
@@ -548,7 +548,7 @@
 Function* func_art_find_super_method_from_code = mod->getFunction("art_find_super_method_from_code");
 if (!func_art_find_super_method_from_code) {
 func_art_find_super_method_from_code = Function::Create(
- /*Type=*/FuncTy_24,
+ /*Type=*/FuncTy_14,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_find_super_method_from_code", mod); // (external, no body)
 func_art_find_super_method_from_code->setCallingConv(CallingConv::C);
@@ -592,7 +592,7 @@
 Function* func_art_resolve_string_from_code = mod->getFunction("art_resolve_string_from_code");
 if (!func_art_resolve_string_from_code) {
 func_art_resolve_string_from_code = Function::Create(
- /*Type=*/FuncTy_14,
+ /*Type=*/FuncTy_15,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_resolve_string_from_code", mod); // (external, no body)
 func_art_resolve_string_from_code->setCallingConv(CallingConv::C);
@@ -603,7 +603,7 @@
 Function* func_art_set32_static_from_code = mod->getFunction("art_set32_static_from_code");
 if (!func_art_set32_static_from_code) {
 func_art_set32_static_from_code = Function::Create(
- /*Type=*/FuncTy_15,
+ /*Type=*/FuncTy_16,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_set32_static_from_code", mod); // (external, no body)
 func_art_set32_static_from_code->setCallingConv(CallingConv::C);
@@ -614,7 +614,7 @@
 Function* func_art_set64_static_from_code = mod->getFunction("art_set64_static_from_code");
 if (!func_art_set64_static_from_code) {
 func_art_set64_static_from_code = Function::Create(
- /*Type=*/FuncTy_16,
+ /*Type=*/FuncTy_17,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_set64_static_from_code", mod); // (external, no body)
 func_art_set64_static_from_code->setCallingConv(CallingConv::C);
@@ -625,7 +625,7 @@
 Function* func_art_set_obj_static_from_code = mod->getFunction("art_set_obj_static_from_code");
 if (!func_art_set_obj_static_from_code) {
 func_art_set_obj_static_from_code = Function::Create(
- /*Type=*/FuncTy_17,
+ /*Type=*/FuncTy_18,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_set_obj_static_from_code", mod); // (external, no body)
 func_art_set_obj_static_from_code->setCallingConv(CallingConv::C);
@@ -636,7 +636,7 @@
 Function* func_art_get32_static_from_code = mod->getFunction("art_get32_static_from_code");
 if (!func_art_get32_static_from_code) {
 func_art_get32_static_from_code = Function::Create(
- /*Type=*/FuncTy_18,
+ /*Type=*/FuncTy_19,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_get32_static_from_code", mod); // (external, no body)
 func_art_get32_static_from_code->setCallingConv(CallingConv::C);
@@ -647,7 +647,7 @@
 Function* func_art_get64_static_from_code = mod->getFunction("art_get64_static_from_code");
 if (!func_art_get64_static_from_code) {
 func_art_get64_static_from_code = Function::Create(
- /*Type=*/FuncTy_19,
+ /*Type=*/FuncTy_20,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_get64_static_from_code", mod); // (external, no body)
 func_art_get64_static_from_code->setCallingConv(CallingConv::C);
@@ -669,7 +669,7 @@
 Function* func_art_set32_instance_from_code = mod->getFunction("art_set32_instance_from_code");
 if (!func_art_set32_instance_from_code) {
 func_art_set32_instance_from_code = Function::Create(
- /*Type=*/FuncTy_20,
+ /*Type=*/FuncTy_21,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_set32_instance_from_code", mod); // (external, no body)
 func_art_set32_instance_from_code->setCallingConv(CallingConv::C);
@@ -680,7 +680,7 @@
 Function* func_art_set64_instance_from_code = mod->getFunction("art_set64_instance_from_code");
 if (!func_art_set64_instance_from_code) {
 func_art_set64_instance_from_code = Function::Create(
- /*Type=*/FuncTy_21,
+ /*Type=*/FuncTy_22,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_set64_instance_from_code", mod); // (external, no body)
 func_art_set64_instance_from_code->setCallingConv(CallingConv::C);
@@ -691,7 +691,7 @@
 Function* func_art_set_obj_instance_from_code = mod->getFunction("art_set_obj_instance_from_code");
 if (!func_art_set_obj_instance_from_code) {
 func_art_set_obj_instance_from_code = Function::Create(
- /*Type=*/FuncTy_22,
+ /*Type=*/FuncTy_23,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_set_obj_instance_from_code", mod); // (external, no body)
 func_art_set_obj_instance_from_code->setCallingConv(CallingConv::C);
@@ -702,7 +702,7 @@
 Function* func_art_get32_instance_from_code = mod->getFunction("art_get32_instance_from_code");
 if (!func_art_get32_instance_from_code) {
 func_art_get32_instance_from_code = Function::Create(
- /*Type=*/FuncTy_17,
+ /*Type=*/FuncTy_18,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_get32_instance_from_code", mod); // (external, no body)
 func_art_get32_instance_from_code->setCallingConv(CallingConv::C);
@@ -713,7 +713,7 @@
 Function* func_art_get64_instance_from_code = mod->getFunction("art_get64_instance_from_code");
 if (!func_art_get64_instance_from_code) {
 func_art_get64_instance_from_code = Function::Create(
- /*Type=*/FuncTy_23,
+ /*Type=*/FuncTy_24,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_get64_instance_from_code", mod); // (external, no body)
 func_art_get64_instance_from_code->setCallingConv(CallingConv::C);
@@ -724,7 +724,7 @@
 Function* func_art_get_obj_instance_from_code = mod->getFunction("art_get_obj_instance_from_code");
 if (!func_art_get_obj_instance_from_code) {
 func_art_get_obj_instance_from_code = Function::Create(
- /*Type=*/FuncTy_24,
+ /*Type=*/FuncTy_14,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_get_obj_instance_from_code", mod); // (external, no body)
 func_art_get_obj_instance_from_code->setCallingConv(CallingConv::C);
diff --git a/src/compiler_llvm/ir_builder.cc b/src/compiler_llvm/ir_builder.cc
index 60f31f2..d38960a 100644
--- a/src/compiler_llvm/ir_builder.cc
+++ b/src/compiler_llvm/ir_builder.cc
@@ -33,7 +33,7 @@
 
   // Get java object type from module
   llvm::Type* jobject_struct_type = module.getTypeByName("JavaObject");
-  CHECK_NE(jobject_struct_type, static_cast<llvm::Type*>(NULL));
+  CHECK(jobject_struct_type != NULL);
   jobject_type_ = jobject_struct_type->getPointerTo();
 
   // Create JEnv* type
@@ -41,8 +41,8 @@
   jenv_type_ = jenv_struct_type->getPointerTo();
 
   // Get Art shadow frame struct type from module
-  art_frame_type_ = module.getTypeByName("ArtFrame");
-  CHECK_NE(art_frame_type_, static_cast<llvm::StructType*>(NULL));
+  art_frame_type_ = module.getTypeByName("ShadowFrame");
+  CHECK(art_frame_type_ != NULL);
 
   // Load the runtime support function declaration from module
   InitRuntimeSupportFuncDecl();
@@ -185,7 +185,7 @@
 
 
 llvm::StructType* IRBuilder::getShadowFrameTy(uint32_t sirt_size) {
-  std::string name(StringPrintf("ArtFrame%u", sirt_size));
+  std::string name(StringPrintf("ShadowFrame%u", sirt_size));
 
   // Try to find the existing struct type definition
   if (llvm::Type* type = module_->getTypeByName(name)) {
diff --git a/src/compiler_llvm/method_compiler.cc b/src/compiler_llvm/method_compiler.cc
index 18e157e..03f4458 100644
--- a/src/compiler_llvm/method_compiler.cc
+++ b/src/compiler_llvm/method_compiler.cc
@@ -26,6 +26,7 @@
 #include "object.h"
 #include "object_utils.h"
 #include "runtime_support_func.h"
+#include "shadow_frame.h"
 #include "stl_util.h"
 #include "stringprintf.h"
 #include "utils_llvm.h"
@@ -199,26 +200,25 @@
 
   irb_.CreateStore(zero_initializer, shadow_frame_);
 
-  // Variables for GetElementPtr
-  llvm::Constant* zero = irb_.getInt32(0);
-
-  llvm::Value* gep_index[] = {
-    zero, // No displacement for shadow frame pointer
-    zero, // Get the %ArtFrame data structure
-    NULL,
-  };
-
   // Store the method pointer
-  gep_index[2] = irb_.getInt32(1);
-  llvm::Value* method_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
+  llvm::Value* method_field_addr =
+    irb_.CreatePtrDisp(shadow_frame_,
+                       irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
+                       irb_.getJObjectTy()->getPointerTo());
+
   llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
   irb_.CreateStore(method_object_addr, method_field_addr);
 
   // Store the number of the pointer slots
-  gep_index[2] = irb_.getInt32(3);
-  llvm::Value* size_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
-  llvm::ConstantInt* sirt_size_value = irb_.getInt32(sirt_size);
-  irb_.CreateStore(sirt_size_value, size_field_addr);
+  llvm::ConstantInt* num_of_refs_offset =
+    irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset());
+
+  llvm::Value* num_of_refs_field_addr =
+    irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset,
+                       irb_.getJIntTy()->getPointerTo());
+
+  llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size);
+  irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr);
 
   // Push the shadow frame
   llvm::Value* shadow_frame_upcast =
@@ -3881,15 +3881,11 @@
 
 
 void MethodCompiler::EmitUpdateLineNum(int32_t line_num) {
-  llvm::Constant* zero = irb_.getInt32(0);
+  llvm::Value* line_num_field_addr =
+    irb_.CreatePtrDisp(shadow_frame_,
+                       irb_.getPtrEquivInt(ShadowFrame::LineNumOffset()),
+                       irb_.getJIntTy()->getPointerTo());
 
-  llvm::Value* gep_index[] = {
-    zero, // No displacement for shadow frame pointer
-    zero, // Get the %ArtFrame data structure
-    irb_.getInt32(2),
-  };
-
-  llvm::Value* line_num_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
   llvm::ConstantInt* line_num_value = irb_.getInt32(line_num);
   irb_.CreateStore(line_num_value, line_num_field_addr);
 }
diff --git a/src/shadow_frame.h b/src/shadow_frame.h
index 533d924..3bdee8e 100644
--- a/src/shadow_frame.h
+++ b/src/shadow_frame.h
@@ -51,6 +51,31 @@
     references_[i] = object;
   }
 
+  // Offset of link within shadow frame
+  static size_t LinkOffset() {
+    return OFFSETOF_MEMBER(ShadowFrame, link_);
+  }
+
+  // Offset of method within shadow frame
+  static size_t MethodOffset() {
+    return OFFSETOF_MEMBER(ShadowFrame, method_);
+  }
+
+  // Offset of line number within shadow frame
+  static size_t LineNumOffset() {
+    return OFFSETOF_MEMBER(ShadowFrame, line_num_);
+  }
+
+  // Offset of length within shadow frame
+  static size_t NumberOfReferencesOffset() {
+    return OFFSETOF_MEMBER(ShadowFrame, number_of_references_);
+  }
+
+  // Offset of references within shadow frame
+  static size_t ReferencesOffset() {
+    return OFFSETOF_MEMBER(ShadowFrame, references_);
+  }
+
  private:
   // ShadowFrame should be allocated by the generated code directly.
   // We should not create new shadow stack in the runtime support function.