Added Reference files.
Added Reference files.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/jitcraft/Reference.cpp Sun Jul 01 00:00:27 2012 -0700
1.3 @@ -0,0 +1,61 @@
1.4 +/*
1.5 + * Copyright (C) 2012 David Anderson
1.6 + *
1.7 + * This file is part of JITCraft.
1.8 + *
1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
1.10 + * the terms of the GNU General Public License as published by the Free
1.11 + * Software Foundation, either version 3 of the License, or (at your option)
1.12 + * any later version.
1.13 + *
1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License along with
1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.20 + */
1.21 +#include "Reference.h"
1.22 +#include "Zone.h"
1.23 +#include "Array.h"
1.24 +#include "Heap-inl.h"
1.25 +
1.26 +using namespace jc;
1.27 +
1.28 +Reference *
1.29 +Reference::NewStack(Zone *zone, void *location, Heap::Tenure tenure)
1.30 +{
1.31 + Object *obj = zone->allocate(MapKind_Reference, sizeof(Reference), tenure);
1.32 + if (!obj)
1.33 + return NULL;
1.34 +
1.35 + Reference *ref = Reference::cast(obj);
1.36 + ref->location_ = location;
1.37 + return ref;
1.38 +}
1.39 +
1.40 +Reference *
1.41 +Reference::NewStack(Zone *zone, const LocalSlot &slot, Heap::Tenure tenure)
1.42 +{
1.43 + Object *obj = zone->allocate(MapKind_Reference, sizeof(Reference), tenure);
1.44 + if (!obj)
1.45 + return NULL;
1.46 +
1.47 + Reference *ref = Reference::cast(obj);
1.48 + ref->location_ = &ref->slot_;
1.49 + ref->slot_ = slot;
1.50 + return ref;
1.51 +}
1.52 +
1.53 +Reference *
1.54 +Reference::NewArray(Zone *zone, Array *base, size_t index, Heap::Tenure tenure)
1.55 +{
1.56 + Object *obj = zone->allocate(MapKind_Reference, sizeof(Reference), tenure);
1.57 + if (!obj)
1.58 + return NULL;
1.59 +
1.60 + Reference *ref = Reference::cast(obj);
1.61 + ref->location_ = base->slotPtr(index);
1.62 + ref->base_ = base;
1.63 + return ref;
1.64 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/jitcraft/Reference.h Sun Jul 01 00:00:27 2012 -0700
2.3 @@ -0,0 +1,63 @@
2.4 +/*
2.5 + * Copyright (C) 2012 David Anderson
2.6 + *
2.7 + * This file is part of JITCraft.
2.8 + *
2.9 + * JITCraft is free software: you can redistribute it and/or modify it under
2.10 + * the terms of the GNU General Public License as published by the Free
2.11 + * Software Foundation, either version 3 of the License, or (at your option)
2.12 + * any later version.
2.13 + *
2.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
2.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
2.17 + *
2.18 + * You should have received a copy of the GNU General Public License along with
2.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
2.20 + */
2.21 +#ifndef _include_jitcraft_reference_h_
2.22 +#define _include_jitcraft_reference_h_
2.23 +
2.24 +#include "Heap.h"
2.25 +#include "Barriers.h"
2.26 +#include "Stack.h"
2.27 +
2.28 +namespace jc {
2.29 +
2.30 +class Array;
2.31 +
2.32 +// A Reference is a typed pointer to another location in memory.
2.33 +class Reference : public Object
2.34 +{
2.35 + void *location_;
2.36 + LocalSlot slot_;
2.37 + Barriered<Array> base_;
2.38 +
2.39 + public:
2.40 + // :TODO: handles
2.41 + static Reference *NewStack(Zone *zone, void *location, Heap::Tenure tenure = Heap::Tenure_Default);
2.42 + static Reference *NewStack(Zone *zone, const LocalSlot &slot, Heap::Tenure tenure = Heap::Tenure_Default);
2.43 + static Reference *NewArray(Zone *zone, Array *base, size_t index, Heap::Tenure tenure = Heap::Tenure_Default);
2.44 +
2.45 + int getInt() {
2.46 + return *reinterpret_cast<int *>(location_);
2.47 + }
2.48 + void setInt(int i) {
2.49 + *reinterpret_cast<int *>(location_) = i;
2.50 + }
2.51 + float getFloat() {
2.52 + return *reinterpret_cast<float *>(location_);
2.53 + }
2.54 + void setFloat(float f) {
2.55 + *reinterpret_cast<float *>(location_) = f;
2.56 + }
2.57 +
2.58 + static inline Reference *cast(Object *obj) {
2.59 + assert(obj->is(MapKind_Reference));
2.60 + return reinterpret_cast<Reference *>(obj);
2.61 + }
2.62 +};
2.63 +
2.64 +}
2.65 +
2.66 +#endif // _include_jitcraft_reference_h_