Skip to content

Commit

Permalink
Make OMR::MethodBuilder client aware
Browse files Browse the repository at this point in the history
Client awareness follows the approach used for IlBuilder.

The function OMRMethodBuilder::injectIL() is removed as superfluous.
All it does is to call the superclass's version and return its
return value, so we should just remove it.

OMR::MethodBuilder::AppendBuilder(TR::BytecodeBuilder *bb) is
removed in favour of OMR::MethodBuilder::AppendBytecodeBuilder().

OMR::MethodBuilder::DefineFunction() is changed to create an internal
copy of the array specifying the function parameter types.

OMR::MethodBuilder::Compile() is added. The new service provides a more
convenient interface for compiling a MethodBuilder instance, ensuring
that resources are cleaned up appropriately when compilation terminates.

Signed-off-by: Leonardo Banderali <leonardo2718@protonmail.com>
  • Loading branch information
mstoodle authored and Leonardo2718 committed Nov 1, 2018
1 parent 224f22a commit 381dfc0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 28 deletions.
62 changes: 38 additions & 24 deletions compiler/ilgen/OMRMethodBuilder.cpp
Expand Up @@ -37,6 +37,7 @@
#include "codegen/CodeGenerator.hpp"
#include "compile/Compilation.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "control/CompileMethod.hpp"
#include "control/Recompilation.hpp"
#include "infra/Assert.hpp"
#include "infra/Cfg.hpp"
Expand Down Expand Up @@ -97,6 +98,7 @@ OMR::MethodBuilder::MemoryManager::~MemoryManager()

OMR::MethodBuilder::MethodBuilder(TR::TypeDictionary *types, TR::VirtualMachineState *vmState)
: TR::IlBuilder(asMethodBuilder(), types),
_clientCallbackRequestFunction(0),
_methodName("NoName"),
_returnType(NoType),
_numParameters(0),
Expand Down Expand Up @@ -229,14 +231,6 @@ OMR::MethodBuilder::setupForBuildIL()
cfg()->addEdge(_entryBlock, _currentBlock);
}

bool
OMR::MethodBuilder::injectIL()
{
bool rc = IlBuilder::injectIL();
return rc;
}


uint32_t
OMR::MethodBuilder::countBlocks()
{
Expand Down Expand Up @@ -482,15 +476,6 @@ OMR::MethodBuilder::isSymbolAnArray(const char *name)
return _symbolIsArray.find(name) != _symbolIsArray.end();
}

void
OMR::MethodBuilder::AppendBuilder(TR::BytecodeBuilder *bb)
{
this->OMR::IlBuilder::AppendBuilder(bb);
if (_vmState)
bb->propagateVMState(_vmState);
addBytecodeBuilderToWorklist(bb);
}

void
OMR::MethodBuilder::DefineLine(const char *line)
{
Expand Down Expand Up @@ -560,13 +545,11 @@ OMR::MethodBuilder::DefineFunction(const char* const name,
int32_t numParms,
...)
{
TR::IlType **parmTypes = (TR::IlType **) malloc(numParms * sizeof(TR::IlType *));
TR::IlType **parmTypes = (TR::IlType **) trMemory()->trPersistentMemory()->allocatePersistentMemory(numParms * sizeof(TR::IlType *));
va_list parms;
va_start(parms, numParms);
for (int32_t p=0;p < numParms;p++)
{
parmTypes[p] = (TR::IlType *) va_arg(parms, TR::IlType *);
}
va_end(parms);

DefineFunction(name, fileName, lineNumber, entryPoint, returnType, numParms, parmTypes);
Expand All @@ -580,14 +563,20 @@ OMR::MethodBuilder::DefineFunction(const char* const name,
TR::IlType * returnType,
int32_t numParms,
TR::IlType ** parmTypes)
{
{
TR_ASSERT_FATAL(_functions.find(name) == _functions.end(), "Function '%s' already defined", name);

// copy parameter types so don't have to force caller to keep the parmTypes array alive
TR::IlType **copiedParmTypes = (TR::IlType **) trMemory()->trPersistentMemory()->allocatePersistentMemory(numParms * sizeof(TR::IlType *));
for (int32_t p=0;p < numParms;p++)
copiedParmTypes[p] = parmTypes[p];

TR::ResolvedMethod *method = new (trMemory()->heapMemoryRegion()) TR::ResolvedMethod(
(char*)fileName,
(char*)lineNumber,
(char*)name,
numParms,
parmTypes,
copiedParmTypes,
returnType,
entryPoint,
0);
Expand Down Expand Up @@ -672,8 +661,10 @@ OMR::MethodBuilder::addToAllBytecodeBuildersList(TR::BytecodeBuilder* bcBuilder)
void
OMR::MethodBuilder::AppendBytecodeBuilder(TR::BytecodeBuilder *builder)
{
IlBuilder::AppendBuilder(builder);

this->OMR::IlBuilder::AppendBuilder(builder);
if (_vmState)
builder->propagateVMState(_vmState);
addBytecodeBuilderToWorklist(builder);
}

void
Expand Down Expand Up @@ -705,3 +696,26 @@ OMR::MethodBuilder::GetNextBytecodeFromWorklist()
_bytecodeWorklist->reset(bci);
return bci;
}

int32_t
OMR::MethodBuilder::Compile(void **entry)
{
TR::ResolvedMethod resolvedMethod(static_cast<TR::MethodBuilder *>(this));
TR::IlGeneratorMethodDetails details(&resolvedMethod);

int32_t rc=0;
*entry = (void *) compileMethodFromDetails(NULL, details, warm, rc);
typeDictionary()->NotifyCompilationDone();
return rc;
}

void *
OMR::MethodBuilder::client()
{
if (_client == NULL && _clientAllocator != NULL)
_client = _clientAllocator(static_cast<TR::MethodBuilder *>(this));
return _client;
}

ClientAllocator OMR::MethodBuilder::_clientAllocator = NULL;
ClientAllocator OMR::MethodBuilder::_getImpl = NULL;
64 changes: 60 additions & 4 deletions compiler/ilgen/OMRMethodBuilder.hpp
Expand Up @@ -41,6 +41,16 @@ namespace TR { class SegmentProvider; }
namespace TR { class Region; }
class TR_Memory;

#ifndef TR_ALLOC
#define TR_ALLOC(x)
#endif


extern "C"
{
typedef bool (*RequestFunctionCallback)(void *client, const char *name);
}

namespace OMR
{

Expand All @@ -55,8 +65,6 @@ class MethodBuilder : public TR::IlBuilder

virtual void setupForBuildIL();

virtual bool injectIL();

/**
* @brief returns the next index to be used for new values
* @returns the next value index
Expand Down Expand Up @@ -105,7 +113,7 @@ class MethodBuilder : public TR::IlBuilder

TR::ResolvedMethod *lookupFunction(const char *name);

void AppendBuilder(TR::BytecodeBuilder *bb);
void AppendBuilder(TR::BytecodeBuilder *bb) { AppendBytecodeBuilder(bb); }
void AppendBuilder(TR::IlBuilder *b) { this->OMR::IlBuilder::AppendBuilder(b); }

void DefineFile(const char *file) { _definingFile = file; }
Expand Down Expand Up @@ -133,13 +141,21 @@ class MethodBuilder : public TR::IlBuilder
int32_t numParms,
TR::IlType ** parmTypes);

int32_t Compile(void **entry);

/**
* @brief will be called if a Call is issued to a function that has not yet been defined, provides a
* mechanism for MethodBuilder subclasses to provide method lookup on demand rather than all up
* front via the constructor.
* @returns true if the function was found and DefineFunction has been called for it, otherwise false
*/
virtual bool RequestFunction(const char *name) { return false; }
virtual bool RequestFunction(const char *name)
{
if (_clientCallbackRequestFunction)
return _clientCallbackRequestFunction(_client, name);

return false;
}

/**
* @brief append the first bytecode builder object to this method
Expand Down Expand Up @@ -233,6 +249,37 @@ class MethodBuilder : public TR::IlBuilder
* @returns the directly inlining MethodBuilder or NULL if no MethodBuilder inlined this one
*/
TR::MethodBuilder *callerMethodBuilder();

/**
* @brief returns the client object associated with this object, allocating it if necessary
*/
void *client();

/**
* @brief Store callback function to be called on client when RequestFunction is called
*/
void setClientCallback_RequestFunction(void *callback)
{
_clientCallbackRequestFunction = (RequestFunctionCallback) callback;
}

/**
* @brief Set the Client Allocator function
*/
static void setClientAllocator(ClientAllocator allocator)
{
_clientAllocator = allocator;
}

/**
* @brief Set the Get Impl function
*
* @param getter function pointer to the impl getter
*/
static void setGetImpl(ImplGetter getter)
{
_getImpl = getter;
}

protected:
virtual uint32_t countBlocks();
Expand Down Expand Up @@ -262,6 +309,11 @@ class MethodBuilder : public TR::IlBuilder

MemoryManager memoryManager;

/**
* @brief client callback function to call when RequestFunction is called
*/
RequestFunctionCallback _clientCallbackRequestFunction;

// These values are typically defined outside of a compilation
const char * _methodName;
TR::IlType * _returnType;
Expand Down Expand Up @@ -323,6 +375,10 @@ class MethodBuilder : public TR::IlBuilder
int32_t _nextInlineSiteIndex;
TR::IlBuilder * _returnBuilder;
const char * _returnSymbolName;

private:
static ClientAllocator _clientAllocator;
static ImplGetter _getImpl;
};

} // namespace OMR
Expand Down

0 comments on commit 381dfc0

Please sign in to comment.