return V(::builder->CreateNot(v));
}
- Value *Nucleus::createLoad(Value *ptr, bool isVolatile, unsigned int align)
+ Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align)
{
+ assert(ptr->getType()->getContainedType(0) == type);
return V(::builder->Insert(new LoadInst(ptr, "", isVolatile, align)));
}
return T(llvm::Type::getVoidTy(*::context));
}
- LValue::LValue(Type *type, int arraySize)
+ LValue::LValue(Type *type, int arraySize) : type(type)
{
address = Nucleus::allocateStackVariable(type, arraySize);
}
Value *LValue::loadValue(unsigned int alignment) const
{
- return Nucleus::createLoad(address, false, alignment);
+ return Nucleus::createLoad(address, type, false, alignment);
}
Value *LValue::storeValue(Value *value, unsigned int alignment) const
Routine *routine = nullptr;
{
- Function<Int(Int, Int)> function;
+ Function<Int(Pointer<Int>, Int)> function;
{
- Int x = function.Arg<0>();
+ Pointer<Int> p = function.Arg<0>();
+ Int x = *p;
Int y = function.Arg<1>();
Int sum = x + y;
if(routine)
{
- int (*add)(int, int) = (int(*)(int,int))routine->getEntry();
- int result = add(1, 2);
+ int (*add)(int*, int) = (int(*)(int*,int))routine->getEntry();
+ int one = 1;
+ int result = add(&one, 2);
assert(result == 3);
}
}
static Value *createNot(Value *V);
// Memory instructions
- static Value *createLoad(Value *ptr, bool isVolatile = false, unsigned int align = 0);
+ static Value *createLoad(Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0);
static Value *createStore(Value *value, Value *ptr, bool isVolatile = false, unsigned int align = 0);
static Value *createStore(Constant *constant, Value *ptr, bool isVolatile = false, unsigned int align = 0);
static Value *createGEP(Value *ptr, Value *index);
{
return true;
}
-
- typedef void ctype;
};
template<class T>
Value *getAddress(Value *index) const;
protected:
+ Type *const type;
Value *address;
};
template<class T>
RValue<T> Reference<T>::operator=(const Reference<T> &ref) const
{
- Value *tmp = Nucleus::createLoad(ref.address, false, ref.alignment);
+ Value *tmp = Nucleus::createLoad(ref.address, T::getType(), false, ref.alignment);
Nucleus::createStore(tmp, address, false, alignment);
return RValue<T>(tmp);
template<class T>
Value *Reference<T>::loadValue() const
{
- return Nucleus::createLoad(address, false, alignment);
+ return Nucleus::createLoad(address, T::getType(), false, alignment);
}
template<class T>
template<class T>
void Return(const Pointer<T> &ret)
{
- Nucleus::createRet(Nucleus::createLoad(ret.address));
+ Nucleus::createRet(Nucleus::createLoad(ret.address, Pointer<T>::getType()));
Nucleus::setInsertBlock(Nucleus::createBasicBlock());
}
Value *Nucleus::allocateStackVariable(Type *t, int arraySize)
{
+ assert(arraySize == 0 && "UNIMPLEMENTED");
+
Ice::Type type = T(t);
- Ice::Variable *value = ::function->makeVariable(type);
- assert(type == Ice::IceType_i32 && arraySize == 0 && "UNIMPLEMENTED");
- auto bytes = Ice::ConstantInteger32::create(::context, type, 4);
- auto alloca = Ice::InstAlloca::create(::function, value, bytes, 4);
- ::function->getEntryNode()->appendInst(alloca);
- return V(value);
+
+ int32_t size = 0;
+ switch(type)
+ {
+ case Ice::IceType_i32: size = 4; break;
+ case Ice::IceType_i64: size = 8; break;
+ default: assert(false && "UNIMPLEMENTED" && type);
+ }
+
+ auto bytes = Ice::ConstantInteger32::create(::context, type, size);
+ auto address = ::function->makeVariable(T(getPointerType(t)));
+ auto alloca = Ice::InstAlloca::create(::function, address, bytes, size);
+ ::function->getEntryNode()->getInsts().push_front(alloca);
+
+ return V(address);
}
BasicBlock *Nucleus::createBasicBlock()
assert(false && "UNIMPLEMENTED"); return nullptr;
}
- Value *Nucleus::createLoad(Value *ptr, bool isVolatile, unsigned int align)
+ Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align)
{
- Ice::Variable *value = ::function->makeVariable(ptr->getType());
+ Ice::Variable *value = ::function->makeVariable(T(type));
auto load = Ice::InstLoad::create(::function, value, ptr, align);
::basicBlock->appendInst(load);
return V(value);
Type *Nucleus::getPointerType(Type *ElementType)
{
- assert(false && "UNIMPLEMENTED"); return nullptr;
+ if(sizeof(void*) == 8)
+ {
+ return T(Ice::IceType_i64);
+ }
+ else
+ {
+ return T(Ice::IceType_i32);
+ }
}
Constant *Nucleus::createNullValue(Type *Ty)
return T(Ice::IceType_void);
}
- LValue::LValue(Type *type, int arraySize)
+ LValue::LValue(Type *type, int arraySize) : type(type)
{
address = Nucleus::allocateStackVariable(type, arraySize);
}
Value *LValue::loadValue(unsigned int alignment) const
{
- return Nucleus::createLoad(address, false, alignment);
+ return Nucleus::createLoad(address, type, false, alignment);
}
Value *LValue::storeValue(Value *value, unsigned int alignment) const