Implement Reactor pointer subscript operator.
Change-Id: I8f4604a0cd81f004d46172d286286722b6198dd5
Reviewed-on: https://swiftshader-review.googlesource.com/5700
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/docs/Reactor.md b/docs/Reactor.md
index cbeec68..263c1bf 100644
--- a/docs/Reactor.md
+++ b/docs/Reactor.md
@@ -216,9 +216,7 @@
For(Int i = 0, i < n, i++)
{
- total += *p;
-
- p = Pointer<Int>(Pointer<Byte>(p) + sizeof(int));
+ total += p[i];
}
Return(total);
@@ -245,22 +243,20 @@
{
if(state.operation == ADD)
{
- total += *p;
+ total += p[i];
}
else if(state.operation == SUBTRACT)
{
- total -= *p;
+ total -= p[i];
}
else if(state.operation == AND)
{
- total &= *p;
+ total &= p[i];
}
else if(...)
{
...
}
-
- p = Pointer<Int>(Pointer<Byte>(p) + sizeof(int));
}
Return(total);
diff --git a/src/Reactor/Nucleus.hpp b/src/Reactor/Nucleus.hpp
index 4a2f22d..3aab2d2 100644
--- a/src/Reactor/Nucleus.hpp
+++ b/src/Reactor/Nucleus.hpp
@@ -2436,6 +2436,8 @@
RValue<Pointer<T>> operator=(const Reference<Pointer<T>> &rhs) const;
Reference<T> operator*();
+ Reference<T> operator[](int index);
+ Reference<T> operator[](RValue<Int> index);
static llvm::Type *getType();
@@ -2465,7 +2467,6 @@
Reference<T> operator[](int index);
Reference<T> operator[](RValue<Int> index);
- Reference<T> operator[](RValue<UInt> index);
};
// RValue<Array<T>> operator++(const Array<T> &val, int); // Post-increment
@@ -2857,6 +2858,22 @@
}
template<class T>
+ Reference<T> Pointer<T>::operator[](int index)
+ {
+ llvm::Value *element = Nucleus::createGEP(LValue::loadValue(), (llvm::Value*)Nucleus::createConstantInt(index));
+
+ return Reference<T>(element, alignment);
+ }
+
+ template<class T>
+ Reference<T> Pointer<T>::operator[](RValue<Int> index)
+ {
+ llvm::Value *element = Nucleus::createGEP(LValue::loadValue(), index.value);
+
+ return Reference<T>(element, alignment);
+ }
+
+ template<class T>
llvm::Type *Pointer<T>::getType()
{
return Nucleus::getPointerType(T::getType());
@@ -2883,14 +2900,6 @@
return Reference<T>(element);
}
- template<class T, int S>
- Reference<T> Array<T, S>::operator[](RValue<UInt> index)
- {
- llvm::Value *element = LValue::getAddress(index.value);
-
- return Reference<T>(element);
- }
-
// template<class T>
// RValue<Array<T>> operator++(const Array<T> &val, int)
// {