From 954259ee0489724155f5ae17c1d730dc5cdb5f53 Mon Sep 17 00:00:00 2001 From: Yiling Han Date: Mon, 3 Jun 2019 17:15:14 -0400 Subject: [PATCH] Add enumerateFields api to create a TypeLayout object An enumerateFields api is added to create a TypeLayout object based on given OpaqueClassBlock which iterates through each field to construct a TypeLayoutEntry that contains DataType, offset and field name information. Signed-off-by: Yiling Han --- runtime/compiler/env/J9ClassEnv.cpp | 56 +++++++++++++++++++++++++++++ runtime/compiler/env/J9ClassEnv.hpp | 16 +++++++++ 2 files changed, 72 insertions(+) diff --git a/runtime/compiler/env/J9ClassEnv.cpp b/runtime/compiler/env/J9ClassEnv.cpp index db950ec1113..892ab239222 100644 --- a/runtime/compiler/env/J9ClassEnv.cpp +++ b/runtime/compiler/env/J9ClassEnv.cpp @@ -29,11 +29,13 @@ #include "env/ClassEnv.hpp" #include "env/CompilerEnv.hpp" #include "env/jittypes.h" +#include "env/TypeLayout.hpp" #include "env/VMJ9.h" #include "j9.h" #include "j9protos.h" #include "j9cp.h" #include "j9cfg.h" +#include "j9fieldsInfo.h" #include "rommeth.h" @@ -409,6 +411,60 @@ J9::ClassEnv::isAnonymousClass(TR::Compilation *comp, TR_OpaqueClassBlock *clazz return comp->fej9()->isAnonymousClass(clazz); } +const TR::TypeLayout* +J9::ClassEnv::enumerateFields(TR::Region& region, TR_OpaqueClassBlock * opaqueClazz, TR::Compilation *comp) + { + J9Class *clazz = (J9Class*)opaqueClazz; + TR_VMFieldsInfo fieldsInfo(comp, clazz, 1, stackAlloc); + ListIterator iter(fieldsInfo.getFields()); + TR::TypeLayoutBuilder tlb(region); + for (TR_VMField *field = iter.getFirst(); field; field = iter.getNext()) + { + char *signature = field->signature; + char charSignature = *signature; + TR::DataType dataType; + switch(charSignature) + { + case 'Z': + case 'B': + case 'C': + case 'S': + case 'I': + { + dataType = TR::Int32; + break; + } + case 'J': + { + dataType = TR::Int64; + break; + } + case 'F': + { + dataType = TR::Float; + break; + } + case 'D': + { + dataType = TR::Double; + break; + } + case 'L': + case '[': + { + dataType = TR::Address; + break; + } + } + size_t nameSize = strlen(field->name)+1; + char *fieldName = new (region) char[nameSize]; + strncpy(fieldName, field->name, nameSize); + TR_ASSERT_FATAL(fieldName[nameSize-1] == '\0', "fieldName buffer was too small."); + int32_t offset = field->offset + TR::Compiler->om.objectHeaderSizeInBytes(); + tlb.add(TR::TypeLayoutEntry(dataType, offset, fieldName)); + } + return tlb.build(); + } int32_t J9::ClassEnv::vTableSlot(TR::Compilation *comp, TR_OpaqueMethodBlock *method, TR_OpaqueClassBlock *clazz) diff --git a/runtime/compiler/env/J9ClassEnv.hpp b/runtime/compiler/env/J9ClassEnv.hpp index e7cf07aa092..e750f8510bb 100644 --- a/runtime/compiler/env/J9ClassEnv.hpp +++ b/runtime/compiler/env/J9ClassEnv.hpp @@ -41,6 +41,8 @@ namespace J9 { typedef J9::ClassEnv ClassEnvConnector; } #include "j9.h" namespace TR { class SymbolReference; } +namespace TR { class TypeLayout; } +namespace TR { class Region; } namespace J9 { @@ -102,6 +104,20 @@ class OMR_EXTENSIBLE ClassEnv : public OMR::ClassEnvConnector bool jitStaticsAreSame(TR::Compilation *comp, TR_ResolvedMethod * method1, int32_t cpIndex1, TR_ResolvedMethod * method2, int32_t cpIndex2); bool jitFieldsAreSame(TR::Compilation *comp, TR_ResolvedMethod * method1, int32_t cpIndex1, TR_ResolvedMethod * method2, int32_t cpIndex2, int32_t isStatic); + /** \brief + * Populates a TypeLayout object. + * + * \param region + * The region used to allocate TypeLayout. + * + * \param opaqueClazz + * Class of the type whose layout needs to be populated. + * + * \return + * Returns a pointer to the TypeLayout object populated. + */ + const TR::TypeLayout* enumerateFields(TR::Region& region, TR_OpaqueClassBlock * clazz, TR::Compilation *comp); + uintptrj_t getArrayElementWidthInBytes(TR::Compilation *comp, TR_OpaqueClassBlock* arrayClass); uintptrj_t persistentClassPointerFromClassPointer(TR::Compilation *comp, TR_OpaqueClassBlock *clazz);