Skip to content

Commit

Permalink
Renames CallContext to InferContext, analyzeCall to inferMessage
Browse files Browse the repository at this point in the history
Issue: #17
  • Loading branch information
0x7CFE committed May 25, 2016
1 parent 2f7a417 commit 4b2e1a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
13 changes: 7 additions & 6 deletions include/inference.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ class Type {
typedef std::size_t TNodeIndex;
typedef std::map<TNodeIndex, Type> TTypeList;

class CallContext {
class InferContext {
public:
CallContext(std::size_t index, const Type& arguments)
InferContext(std::size_t index, const Type& arguments)
: m_index(index), m_arguments(arguments) {}

std::size_t getIndex() const { return m_index; }
Expand Down Expand Up @@ -258,14 +258,15 @@ class TypeSystem {
typedef TSymbol* TSelector;
CallContext* findCallContext(TSelector selector, const Type& arguments);

CallContext* analyzeCall(TSelector selector, const Type& arguments);
InferContext* inferMessage(TSelector selector, const Type& arguments);

ControlGraph* getControlGraph(TMethod* method);

private:
typedef std::pair<ParsedBytecode*, ControlGraph*> TGraphEntry;
typedef std::map<TMethod*, TGraphEntry> TGraphCache;

typedef std::map<Type, CallContext*> TContextMap;
typedef std::map<Type, InferContext*> TContextMap;
typedef std::map<TSelector, TContextMap> TContextCache;

private:
Expand All @@ -278,7 +279,7 @@ class TypeSystem {

class TypeAnalyzer {
public:
TypeAnalyzer(TypeSystem& system, ControlGraph& graph, CallContext& context)
TypeAnalyzer(TypeSystem& system, ControlGraph& graph, InferContext& context)
: m_system(system), m_graph(graph), m_context(context), m_walker(*this) {}

void run();
Expand Down Expand Up @@ -333,7 +334,7 @@ class TypeAnalyzer {
private:
TypeSystem& m_system;
ControlGraph& m_graph;
CallContext& m_context;
InferContext& m_context;
Walker m_walker;

bool m_baseRun;
Expand Down
36 changes: 18 additions & 18 deletions src/TypeAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,18 @@ void TypeAnalyzer::doSendUnary(const InstructionNode& instruction) {
}

void TypeAnalyzer::doSendBinary(const InstructionNode& instruction) {
const Type& type1 = m_context[*instruction.getArgument(0)];
const Type& type2 = m_context[*instruction.getArgument(1)];
const Type& lhsType = m_context[*instruction.getArgument(0)];
const Type& rhsType = m_context[*instruction.getArgument(1)];
const binaryBuiltIns::Operator opcode = static_cast<binaryBuiltIns::Operator>(instruction.getInstruction().getArgument());

Type& result = m_context[instruction];

if (isSmallInteger(type1.getValue()) && isSmallInteger(type2.getValue())) {
if (isSmallInteger(lhsType.getValue()) && isSmallInteger(rhsType.getValue())) {
if (!m_baseRun)
return;

const int32_t leftOperand = TInteger(type1.getValue());
const int32_t rightOperand = TInteger(type2.getValue());
const int32_t leftOperand = TInteger(lhsType.getValue());
const int32_t rightOperand = TInteger(rhsType.getValue());

switch (opcode) {
case binaryBuiltIns::operatorLess:
Expand All @@ -268,8 +268,8 @@ void TypeAnalyzer::doSendBinary(const InstructionNode& instruction) {
}

// Literal int or (SmallInt) monotype
const bool isInt1 = isSmallInteger(type1.getValue()) || type1.getValue() == globals.smallIntClass;
const bool isInt2 = isSmallInteger(type2.getValue()) || type2.getValue() == globals.smallIntClass;
const bool isInt1 = isSmallInteger(lhsType.getValue()) || lhsType.getValue() == globals.smallIntClass;
const bool isInt2 = isSmallInteger(rhsType.getValue()) || rhsType.getValue() == globals.smallIntClass;

if (isInt1 && isInt2) {
switch (opcode) {
Expand All @@ -296,10 +296,10 @@ void TypeAnalyzer::doSendBinary(const InstructionNode& instruction) {
TSymbol* const selector = globals.binaryMessages[opcode]->cast<TSymbol>();

Type arguments(Type::tkArray);
arguments.addSubType(type1); // lhs
arguments.addSubType(type2); // rhs
arguments.addSubType(lhsType);
arguments.addSubType(rhsType);

if (CallContext* const context = m_system.analyzeCall(selector, arguments))
if (InferContext* const context = m_system.inferMessage(selector, arguments))
result = context->getReturnType();
else
result = Type(Type::tkPolytype);
Expand Down Expand Up @@ -351,7 +351,7 @@ void TypeAnalyzer::doSendMessage(const InstructionNode& instruction) {
const Type& arguments = m_context[*instruction.getArgument()];

Type& result = m_context[instruction];
if (CallContext* const context = m_system.analyzeCall(selector, arguments))
if (InferContext* const context = m_system.inferMessage(selector, arguments))
result = context->getReturnType();
else
result = Type(Type::tkPolytype);
Expand Down Expand Up @@ -598,7 +598,7 @@ ControlGraph* TypeSystem::getControlGraph(TMethod* method) {
return controlGraph;
}

CallContext* TypeSystem::analyzeCall(TSelector selector, const Type& arguments) {
InferContext* TypeSystem::inferMessage(TSelector selector, const Type& arguments) {
if (!selector || arguments.getKind() != Type::tkArray || arguments.getSubTypes().empty())
return 0;

Expand Down Expand Up @@ -638,10 +638,10 @@ CallContext* TypeSystem::analyzeCall(TSelector selector, const Type& arguments)
if (! method) // TODO Redirect to #doesNotUnderstand: statically
return 0;

CallContext* const callContext = new CallContext(m_lastContextIndex++, arguments);
contextMap[arguments] = callContext;
InferContext* const inferContext = new InferContext(m_lastContextIndex++, arguments);
contextMap[arguments] = inferContext;

ControlGraph* const controlGraph = getControlGraph(method);
ControlGraph* const methodGraph = getControlGraph(method);
assert(controlGraph);

std::printf("Analyzing %s::%s>>%s...\n",
Expand All @@ -650,16 +650,16 @@ CallContext* TypeSystem::analyzeCall(TSelector selector, const Type& arguments)
selector->toString().c_str());

// TODO Handle recursive and tail calls
type::TypeAnalyzer analyzer(*this, *controlGraph, *callContext);
type::TypeAnalyzer analyzer(*this, *methodGraph, *inferContext);
analyzer.run();

Type& returnType = callContext->getReturnType();
Type& returnType = inferContext->getReturnType();

std::printf("%s::%s>>%s -> %s\n",
arguments.toString().c_str(),
method->klass->name->toString().c_str(),
selector->toString().c_str(),
returnType.toString().c_str());

return callContext;
return inferContext;
}

0 comments on commit 4b2e1a8

Please sign in to comment.