# HG changeset patch # User David Anderson # Date 1341126027 25200 # Node ID ffd4ddec7b2e5553e19b6ee4f84358ce7046a19d # Parent 8aa89dd41e2527ab0331625956019b89997dad7f Added Reference files. diff -r 8aa89dd41e25 -r ffd4ddec7b2e jitcraft/Reference.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jitcraft/Reference.cpp Sun Jul 01 00:00:27 2012 -0700 @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ +#include "Reference.h" +#include "Zone.h" +#include "Array.h" +#include "Heap-inl.h" + +using namespace jc; + +Reference * +Reference::NewStack(Zone *zone, void *location, Heap::Tenure tenure) +{ + Object *obj = zone->allocate(MapKind_Reference, sizeof(Reference), tenure); + if (!obj) + return NULL; + + Reference *ref = Reference::cast(obj); + ref->location_ = location; + return ref; +} + +Reference * +Reference::NewStack(Zone *zone, const LocalSlot &slot, Heap::Tenure tenure) +{ + Object *obj = zone->allocate(MapKind_Reference, sizeof(Reference), tenure); + if (!obj) + return NULL; + + Reference *ref = Reference::cast(obj); + ref->location_ = &ref->slot_; + ref->slot_ = slot; + return ref; +} + +Reference * +Reference::NewArray(Zone *zone, Array *base, size_t index, Heap::Tenure tenure) +{ + Object *obj = zone->allocate(MapKind_Reference, sizeof(Reference), tenure); + if (!obj) + return NULL; + + Reference *ref = Reference::cast(obj); + ref->location_ = base->slotPtr(index); + ref->base_ = base; + return ref; +} diff -r 8aa89dd41e25 -r ffd4ddec7b2e jitcraft/Reference.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jitcraft/Reference.h Sun Jul 01 00:00:27 2012 -0700 @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ +#ifndef _include_jitcraft_reference_h_ +#define _include_jitcraft_reference_h_ + +#include "Heap.h" +#include "Barriers.h" +#include "Stack.h" + +namespace jc { + +class Array; + +// A Reference is a typed pointer to another location in memory. +class Reference : public Object +{ + void *location_; + LocalSlot slot_; + Barriered base_; + + public: + // :TODO: handles + static Reference *NewStack(Zone *zone, void *location, Heap::Tenure tenure = Heap::Tenure_Default); + static Reference *NewStack(Zone *zone, const LocalSlot &slot, Heap::Tenure tenure = Heap::Tenure_Default); + static Reference *NewArray(Zone *zone, Array *base, size_t index, Heap::Tenure tenure = Heap::Tenure_Default); + + int getInt() { + return *reinterpret_cast(location_); + } + void setInt(int i) { + *reinterpret_cast(location_) = i; + } + float getFloat() { + return *reinterpret_cast(location_); + } + void setFloat(float f) { + *reinterpret_cast(location_) = f; + } + + static inline Reference *cast(Object *obj) { + assert(obj->is(MapKind_Reference)); + return reinterpret_cast(obj); + } +}; + +} + +#endif // _include_jitcraft_reference_h_