Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 86 lines (68 sloc) 1.661 kb
7f48eb2 Use C++ namespaces
Slava Pestov authored
1 namespace factor
2 {
3
3887292 Big VM cleanup
Slava Pestov authored
4 template<typename Type> cell tag(Type *value)
88c43ee Clean up VM's array code
Slava Pestov authored
5 {
d65296b vm: 4 bit tags, new representation of alien objects makes unbox-any-c-pt...
Slava Pestov authored
6 return RETAG(value,Type::type_number);
1857f90 VM: use better abstractions for tagged pointers, eliminate get()/set() s...
Slava Pestov authored
7 }
8
7c0a59f The great type renaming
Slava Pestov authored
9 inline static cell tag_dynamic(object *value)
1857f90 VM: use better abstractions for tagged pointers, eliminate get()/set() s...
Slava Pestov authored
10 {
4168ffb New identity-hashcode primitive
Slava Pestov authored
11 return RETAG(value,value->type());
88c43ee Clean up VM's array code
Slava Pestov authored
12 }
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
13
3887292 Big VM cleanup
Slava Pestov authored
14 template<typename Type>
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
15 struct tagged
16 {
17 cell value_;
18
b341527 vm: rename gc_root to data_root, add code_root to fix a problem where co...
Slava Pestov authored
19 cell type() const
20 {
d65296b vm: 4 bit tags, new representation of alien objects makes unbox-any-c-pt...
Slava Pestov authored
21 return TAG(value_);
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
22 }
23
f9fc348 vm: change code heap layout somewhat, remove unused allocation bitmap fr...
Slava Pestov authored
24 bool type_p(cell type_) const
25 {
26 return type() == type_;
27 }
28
29 bool type_p() const
30 {
31 if(Type::type_number == TYPE_COUNT)
32 return true;
33 else
34 return type_p(Type::type_number);
35 }
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
36
b341527 vm: rename gc_root to data_root, add code_root to fix a problem where co...
Slava Pestov authored
37 cell value() const
38 {
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
39 #ifdef FACTOR_DEBUG
00f0066 @jckarter vm: replace assert with FACTOR_ASSERT
jckarter authored
40 FACTOR_ASSERT(type_p());
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
41 #endif
9cfe50f vm: tagged typechecks work better with DEBUG=1
Slava Pestov authored
42 return value_;
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
43 }
b341527 vm: rename gc_root to data_root, add code_root to fix a problem where co...
Slava Pestov authored
44
45 Type *untagged() const
46 {
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
47 #ifdef FACTOR_DEBUG
00f0066 @jckarter vm: replace assert with FACTOR_ASSERT
jckarter authored
48 FACTOR_ASSERT(type_p());
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
49 #endif
9cfe50f vm: tagged typechecks work better with DEBUG=1
Slava Pestov authored
50 return (Type *)(UNTAG(value_));
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
51 }
52
b341527 vm: rename gc_root to data_root, add code_root to fix a problem where co...
Slava Pestov authored
53 Type *untag_check(factor_vm *parent) const
54 {
9cfe50f vm: tagged typechecks work better with DEBUG=1
Slava Pestov authored
55 if(!type_p())
56 parent->type_error(Type::type_number,value_);
57 return untagged();
58 }
59
60 explicit tagged(cell tagged) : value_(tagged) {}
61 explicit tagged(Type *untagged) : value_(factor::tag(untagged)) {}
62
3887292 Big VM cleanup
Slava Pestov authored
63 Type *operator->() const { return untagged(); }
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
64 cell *operator&() const { return &value_; }
65
3887292 Big VM cleanup
Slava Pestov authored
66 const tagged<Type> &operator=(const Type *x) { value_ = tag(x); return *this; }
67 const tagged<Type> &operator=(const cell &x) { value_ = x; return *this; }
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
68
3887292 Big VM cleanup
Slava Pestov authored
69 bool operator==(const tagged<Type> &x) { return value_ == x.value_; }
70 bool operator!=(const tagged<Type> &x) { return value_ != x.value_; }
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
71
3887292 Big VM cleanup
Slava Pestov authored
72 template<typename NewType> tagged<NewType> as() { return tagged<NewType>(value_); }
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
73 };
74
3887292 Big VM cleanup
Slava Pestov authored
75 template<typename Type> Type *factor_vm::untag_check(cell value)
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
76 {
3887292 Big VM cleanup
Slava Pestov authored
77 return tagged<Type>(value).untag_check(this);
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
78 }
79
6ee56ca vm: use iostreams instead of printf for debug messages, clean up a few t...
Slava Pestov authored
80 template<typename Type> Type *untag(cell value)
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
81 {
3887292 Big VM cleanup
Slava Pestov authored
82 return tagged<Type>(value).untagged();
967bfa6 @phildawes moved tagged template code back into tagged.hpp header
phildawes authored
83 }
84
7f48eb2 Use C++ namespaces
Slava Pestov authored
85 }
Something went wrong with that request. Please try again.