Commit b98fe5cd by Nicolas Capens

Implement switch constructs.

Bug swiftshader:6 Change-Id: Ifd28cab11e814dd09515ad8721f8d3d86123f19c Reviewed-on: https://swiftshader-review.googlesource.com/7970Reviewed-by: 's avatarNicolas Capens <capn@google.com> Tested-by: 's avatarNicolas Capens <capn@google.com> Reviewed-on: https://swiftshader-review.googlesource.com/8165Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com>
parent a4c30b05
......@@ -81,6 +81,7 @@ namespace sw
class Type : public llvm::Type {};
class Value : public llvm::Value {};
class SwitchCases : public llvm::SwitchInst {};
class BasicBlock : public llvm::BasicBlock {};
inline Type *T(llvm::Type *t)
......@@ -661,14 +662,14 @@ namespace sw
return V(::builder->CreateSelect(C, ifTrue, ifFalse));
}
Value *Nucleus::createSwitch(Value *v, BasicBlock *Dest, unsigned NumCases)
SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
{
return V(::builder->CreateSwitch(v, Dest, NumCases));
return reinterpret_cast<SwitchCases*>(::builder->CreateSwitch(control, defaultBranch, numCases));
}
void Nucleus::addSwitchCase(Value *Switch, int Case, BasicBlock *Branch)
void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
{
reinterpret_cast<SwitchInst*>(Switch)->addCase(llvm::ConstantInt::get(Type::getInt32Ty(*::context), Case, true), Branch);
switchCases->addCase(llvm::ConstantInt::get(Type::getInt32Ty(*::context), label, true), branch);
}
void Nucleus::createUnreachable()
......
......@@ -23,6 +23,7 @@ namespace sw
{
class Type;
class Value;
class SwitchCases;
class BasicBlock;
class Routine;
......@@ -144,8 +145,8 @@ namespace sw
// Other instructions
static Value *createSelect(Value *C, Value *ifTrue, Value *ifFalse);
static Value *createSwitch(Value *V, BasicBlock *Dest, unsigned NumCases);
static void addSwitchCase(Value *Switch, int Case, BasicBlock *Branch);
static SwitchCases *createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases);
static void addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch);
static void createUnreachable();
// Constant values
......
......@@ -72,6 +72,7 @@ namespace sw
};
class Value : public Ice::Variable {};
class SwitchCases : public Ice::InstSwitch {};
class BasicBlock : public Ice::CfgNode {};
Ice::Type T(Type *t)
......@@ -218,15 +219,15 @@ namespace sw
case R_X86_64_NONE:
// No relocation
break;
// case R_X86_64_64:
// *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation->r_addend;
// break;
case R_X86_64_64:
*(int64_t*)patchSite = (int64_t)((intptr_t)symbolValue + *(int64_t*)patchSite) + relocation.r_addend;
break;
case R_X86_64_PC32:
*patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite) + relocation.r_addend;
break;
// case R_X86_64_32S:
// *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend;
// break;
case R_X86_64_32S:
*patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend;
break;
default:
assert(false && "Unsupported relocation type");
return nullptr;
......@@ -446,6 +447,7 @@ namespace sw
objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get());
::context->lowerGlobals("last");
::context->lowerConstants();
::context->lowerJumpTables();
objectWriter->setUndefinedSyms(::context->getConstantExternSyms());
objectWriter->writeNonUserSections();
......@@ -1034,14 +1036,17 @@ namespace sw
return V(result);
}
Value *Nucleus::createSwitch(Value *v, BasicBlock *Dest, unsigned NumCases)
SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
{
assert(false && "UNIMPLEMENTED"); return nullptr;
auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch);
::basicBlock->appendInst(switchInst);
return reinterpret_cast<SwitchCases*>(switchInst);
}
void Nucleus::addSwitchCase(Value *Switch, int Case, BasicBlock *Branch)
void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
{
assert(false && "UNIMPLEMENTED"); return;
switchCases->addBranch(label, label, branch);
}
void Nucleus::createUnreachable()
......
......@@ -1741,11 +1741,11 @@ namespace sw
UInt index = callStack[--stackIndex];
Value *value = index.loadValue();
Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
{
Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
}
}
else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
......
......@@ -1503,11 +1503,11 @@ namespace sw
UInt index = callStack[--stackIndex];
Value *value = index.loadValue();
Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
{
Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
}
}
else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
......
pnacl-subzero @ 83425dec
Subproject commit 21f78bb1b7cd9040ce5baea3be51f7be49a1bb1f
Subproject commit 83425dec5ecae21e092a9a440845ce99a13ded69
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment