-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject.c
More file actions
211 lines (184 loc) · 4.95 KB
/
object.c
File metadata and controls
211 lines (184 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Copyright (C) 2020-2021
* "Mu Lei" known as "NalaGinrut" <NalaGinrut@gmail.com>
* Animula is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* Animula 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 Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "object.h"
/* Special Const
* is used for representing these values:
* 1. false: #f in Scheme, False in Python, false in Lua
* 2. true: #t in Scheme, True in Python, true in Lua
* 3. empty list: () in Scheme, [] in Python
* 4. none: None in Python, nil in Lua
*
* NOTE:
* We need to check them with pointer by the unified interface,
* so don't use enum.
*/
GLOBAL_DEF (const Object, true_const)
= {.attr = {.type = boolean, .gc = 0}, .value = (void *)1};
GLOBAL_DEF (const Object, false_const)
= {.attr = {.type = boolean, .gc = 0}, .value = NULL};
GLOBAL_DEF (const Object, null_const)
= {.attr = {.type = null_obj, .gc = 0}, .value = NULL};
GLOBAL_DEF (const Object, none_const)
= {.attr = {.type = none, .gc = 0}, .value = NULL};
// ----------- Closure
closure_t make_closure (u8_t arity, u8_t frame_size, reg_t entry)
{
VM_DEBUG ("create new closure!\n");
closure_t closure
= (closure_t)GC_MALLOC (sizeof (Closure) + sizeof (Object) * frame_size);
if (!closure)
{
return NULL;
}
closure->frame_size = frame_size;
closure->entry = entry;
closure->arity = arity;
closure->attr.gc = GEN_1_OBJ;
closure->attr.type = closure_on_heap;
return closure;
}
list_node_t animula_new_list_node (void)
{
return (list_node_t)GC_MALLOC (sizeof (ListNode));
}
u32_t list_cnt = 0;
pair_t animula_new_pair (void)
{
CREATE_NEW_OBJ (pair_t, pair, Pair);
}
vector_t animula_new_vector (void)
{
CREATE_NEW_OBJ (vector_t, vector, Vector);
}
list_t animula_new_list (void)
{
CREATE_NEW_OBJ (list_t, list, List);
}
closure_t animula_new_closure (void)
{
CREATE_NEW_OBJ (closure_t, closure_on_heap, Closure);
}
bytevector_t animula_new_bytevector (void)
{
CREATE_NEW_OBJ (bytevector_t, bytevector, ByteVector);
}
mut_bytevector_t animula_new_mut_bytevector (void)
{
CREATE_NEW_OBJ (mut_bytevector_t, mut_bytevector, MutByteVector);
}
object_t animula_new_object (otype_t type)
{
bool has_inner_obj = true;
bool new_alloc = false;
bool new_inner = false;
object_t object = NULL;
void *value = NULL;
#ifdef OBG_GC
object = (object_t)gc_pool_malloc (0);
#else
object = NULL;
#endif
if (!object)
{
object = (object_t)GC_MALLOC (sizeof (Object));
new_alloc = true;
// Alloc failed, return NULL to trigger GC
if (!object)
return NULL;
}
switch (type)
{
case pair:
{
value = (void *)animula_new_pair ();
((pair_t)value)->attr.type = type;
((pair_t)value)->attr.gc = GEN_1_OBJ;
break;
}
case vector:
{
value = (void *)animula_new_vector ();
((vector_t)value)->attr.type = type;
((vector_t)value)->attr.gc = GEN_1_OBJ;
break;
}
case list:
{
value = (void *)animula_new_list ();
((list_t)value)->attr.type = type;
((list_t)value)->attr.gc = GEN_1_OBJ;
break;
}
case closure_on_heap:
{
value = (void *)animula_new_closure ();
((closure_t)value)->attr.type = type;
((closure_t)value)->attr.gc = GEN_1_OBJ;
break;
}
case bytevector:
{
value = (void *)animula_new_bytevector ();
((bytevector_t)value)->attr.type = type;
((bytevector_t)value)->attr.gc = GEN_1_OBJ;
break;
}
case mut_bytevector:
{
value = (void *)animula_new_mut_bytevector ();
((mut_bytevector_t)value)->attr.type = type;
((mut_bytevector_t)value)->attr.gc = GEN_1_OBJ;
break;
}
default:
{
has_inner_obj = false;
break;
}
}
if (has_inner_obj && (NULL == value))
{
// The inner object wasn't successfully allocated, return NULL for GC
if (new_alloc)
{
#ifdef USE_OBG_GC
os_free (object);
#endif
}
else
{
object->attr.gc = FREE_OBJ;
}
return NULL;
}
object->attr.type = type;
object->attr.gc = GEN_1_OBJ;
#ifdef OBG_GC
if (new_alloc)
gc_obj_book (object);
#endif
if (has_inner_obj) // value is checked before
{
object->value = value;
#ifdef OBG_GC
gc_inner_obj_book (type, (void *)value);
#endif
}
else
{
object->value = (void *)0;
}
return object;
}