History

Add type_descriptor::is_equal() function. - Replace pointer and use function throughout VM Complete implementation of vm_array_t::copy_from() which supports the slicela instruction.

arr committed 9 years ago

Commit 7838bae1 parent 5ef656f5 files · +30 −12Browse files

Unified · Split

5 files
  1. src/include/runtime.h +2 −0
  2. src/vm/array.cpp +10 −10
  3. src/vm/execution_table.cpp +1 −1
  4. src/vm/list.cpp +1 −1
  5. src/vm/vm_memory.cpp +16 −0
src/include/runtime.h+2−0
@@ -69,6 +69,8 @@ namespace disvm · 68 unmodified lines
6969 type_descriptor_t(const type_descriptor_t&) = delete;
7070 type_descriptor_t& operator=(const type_descriptor_t&) = delete;
7171
72+ bool is_equal(const type_descriptor_t *) const;
73+
7274 const word_t size_in_bytes;
7375 const word_t map_in_bytes;
7476 const byte_t * const pointer_map;
src/vm/array.cpp+10−10
@@ -166,7 +166,7 @@ void vm_array_t::copy_from(const vm_array_t &source, word_t this_begin_index) · 165 unmodified lines
166166 if (this_begin_index < 0 || _length < (this_begin_index + source._length))
167167 throw out_of_range_memory{};
168168
169− if (source._element_type != _element_type)
169+ if (!source._element_type->is_equal(_element_type.get()))
170170 throw type_violation{};
171171
172172 auto dest_arr = _arr + (this_begin_index * _element_type->size_in_bytes);
@@ -174,15 +174,15 @@ void vm_array_t::copy_from(const vm_array_t &source, word_t this_begin_index) · 1 unmodified line
174174 // Increment all pointers if we have reference types.
175175 if (_element_type->map_in_bytes != 0)
176176 {
177− assert(false && "Not implemented");
178− //auto &type_desc = *_element_type;
179− //auto source_array_element = source->_arr;
180− //for (word_t i = 0; i < source->_length; ++i)
181− //{
182− // source_array_element += (i * type_desc.size_in_bytes);
183− // inc_ref_count_in_alloc(type_desc, source_array_element);
184− //}
177+ auto &type_desc = *_element_type;
178+ auto source_array_element = source._arr;
179+ for (auto i = word_t{ 0 }; i < source._length; ++i)
180+ {
181+ source_array_element += (i * type_desc.size_in_bytes);
182+ inc_ref_count_in_memory(type_desc, source_array_element);
183+ }
185184 }
186185
187− std::memcpy(dest_arr, source._arr, (source._length * _element_type->size_in_bytes));
186+ // Moving data within the same array is permitted so use memmove().
187+ std::memmove(dest_arr, source._arr, (source._length * _element_type->size_in_bytes));
188188 }
src/vm/execution_table.cpp+1−1
@@ -1403,7 +1403,7 @@ namespace · 1402 unmodified lines
14031403 if (s == nullptr)
14041404 return;
14051405
1406− if (d == nullptr || s->alloc_type != d->alloc_type)
1406+ if (d == nullptr || !s->alloc_type->is_equal(d->alloc_type.get()))
14071407 throw type_violation{};
14081408 }
14091409
src/vm/list.cpp+1−1
@@ -90,7 +90,7 @@ void vm_list_t::set_tail(vm_list_t *new_tail) · 89 unmodified lines
9090 {
9191 if (new_tail != nullptr)
9292 {
93− if (new_tail->get_element_type() != _element_type)
93+ if (!new_tail->get_element_type()->is_equal(_element_type.get()))
9494 throw type_violation{};
9595
9696 new_tail->add_ref();
src/vm/vm_memory.cpp+16−0
@@ -530,3 +530,19 @@ type_descriptor_t::type_descriptor_t( · 529 unmodified lines
530530 if (debug::is_component_tracing_enabled<debug::component_trace_t::memory>())
531531 debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "init: type descriptor");
532532 }
533+
534+bool type_descriptor_t::is_equal(const type_descriptor_t *other) const
535+{
536+ if (other == nullptr)
537+ return false;
538+
539+ if (this == other)
540+ return true;
541+
542+ bool equal = size_in_bytes == other->size_in_bytes;
543+ equal = equal && map_in_bytes == other->map_in_bytes;
544+ equal = equal && 0 == std::memcmp(pointer_map, other->pointer_map, map_in_bytes);
545+ equal = equal && finalizer == other->finalizer;
546+
547+ return equal;
548+}