Commit cec951b0 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>
parent 80b0bc6a
...@@ -81,6 +81,7 @@ namespace sw ...@@ -81,6 +81,7 @@ namespace sw
class Type : public llvm::Type {}; class Type : public llvm::Type {};
class Value : public llvm::Value {}; class Value : public llvm::Value {};
class SwitchCases : public llvm::SwitchInst {};
class BasicBlock : public llvm::BasicBlock {}; class BasicBlock : public llvm::BasicBlock {};
inline Type *T(llvm::Type *t) inline Type *T(llvm::Type *t)
...@@ -661,14 +662,14 @@ namespace sw ...@@ -661,14 +662,14 @@ namespace sw
return V(::builder->CreateSelect(C, ifTrue, ifFalse)); 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() void Nucleus::createUnreachable()
......
...@@ -23,6 +23,7 @@ namespace sw ...@@ -23,6 +23,7 @@ namespace sw
{ {
class Type; class Type;
class Value; class Value;
class SwitchCases;
class BasicBlock; class BasicBlock;
class Routine; class Routine;
...@@ -144,8 +145,8 @@ namespace sw ...@@ -144,8 +145,8 @@ namespace sw
// Other instructions // Other instructions
static Value *createSelect(Value *C, Value *ifTrue, Value *ifFalse); static Value *createSelect(Value *C, Value *ifTrue, Value *ifFalse);
static Value *createSwitch(Value *V, BasicBlock *Dest, unsigned NumCases); static SwitchCases *createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases);
static void addSwitchCase(Value *Switch, int Case, BasicBlock *Branch); static void addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch);
static void createUnreachable(); static void createUnreachable();
// Constant values // Constant values
......
...@@ -72,6 +72,7 @@ namespace sw ...@@ -72,6 +72,7 @@ namespace sw
}; };
class Value : public Ice::Variable {}; class Value : public Ice::Variable {};
class SwitchCases : public Ice::InstSwitch {};
class BasicBlock : public Ice::CfgNode {}; class BasicBlock : public Ice::CfgNode {};
Ice::Type T(Type *t) Ice::Type T(Type *t)
...@@ -218,15 +219,15 @@ namespace sw ...@@ -218,15 +219,15 @@ namespace sw
case R_X86_64_NONE: case R_X86_64_NONE:
// No relocation // No relocation
break; break;
// case R_X86_64_64: case R_X86_64_64:
// *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation->r_addend; *(int64_t*)patchSite = (int64_t)((intptr_t)symbolValue + *(int64_t*)patchSite) + relocation.r_addend;
// break; break;
case R_X86_64_PC32: case R_X86_64_PC32:
*patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite) + relocation.r_addend; *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite) + relocation.r_addend;
break; break;
// case R_X86_64_32S: case R_X86_64_32S:
// *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend; *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend;
// break; break;
default: default:
assert(false && "Unsupported relocation type"); assert(false && "Unsupported relocation type");
return nullptr; return nullptr;
...@@ -446,6 +447,7 @@ namespace sw ...@@ -446,6 +447,7 @@ namespace sw
objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get()); objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get());
::context->lowerGlobals("last"); ::context->lowerGlobals("last");
::context->lowerConstants(); ::context->lowerConstants();
::context->lowerJumpTables();
objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); objectWriter->setUndefinedSyms(::context->getConstantExternSyms());
objectWriter->writeNonUserSections(); objectWriter->writeNonUserSections();
...@@ -1034,14 +1036,17 @@ namespace sw ...@@ -1034,14 +1036,17 @@ namespace sw
return V(result); 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() void Nucleus::createUnreachable()
......
...@@ -1741,11 +1741,11 @@ namespace sw ...@@ -1741,11 +1741,11 @@ namespace sw
UInt index = callStack[--stackIndex]; UInt index = callStack[--stackIndex];
Value *value = index.loadValue(); 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++) 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 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
......
...@@ -1503,11 +1503,11 @@ namespace sw ...@@ -1503,11 +1503,11 @@ namespace sw
UInt index = callStack[--stackIndex]; UInt index = callStack[--stackIndex];
Value *value = index.loadValue(); 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++) 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 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