History

Implement Sys module functions aprint and stream. Mark Sys module functions that will require a PAL.

arr committed 9 years ago

Commit 30fdb691 parent a000a971 file · +70 −7Browse files

Unified · Split

src/vm/sys/Sysmod.cpp+70−7
@@ -24,9 +24,11 @@ using disvm::vm_t; · 23 unmodified lines
24using disvm::debug::component_trace_t;24using disvm::debug::component_trace_t;
25using disvm::debug::log_level_t;25using disvm::debug::log_level_t;
26 26
27using disvm::runtime::byte_t;
27using disvm::runtime::dereference_nil;28using disvm::runtime::dereference_nil;
28using disvm::runtime::intrinsic_type_desc;29using disvm::runtime::intrinsic_type_desc;
29using disvm::runtime::out_of_range_memory;30using disvm::runtime::out_of_range_memory;
31using disvm::runtime::pointer_t;
30using disvm::runtime::type_descriptor_t;32using disvm::runtime::type_descriptor_t;
31using disvm::runtime::vm_alloc_t;33using disvm::runtime::vm_alloc_t;
32using disvm::runtime::vm_array_t;34using disvm::runtime::vm_array_t;
@@ -171,9 +173,9 @@ namespace · 138 unmodified lines
171 word_t printf_to_fd(173 word_t printf_to_fd(
172 disvm::vm_t &vm,174 disvm::vm_t &vm,
173 vm_fd_t &fd,175 vm_fd_t &fd,
174 const disvm::runtime::vm_string_t &msg_fmt,176 const vm_string_t &msg_fmt,
175 disvm::runtime::byte_t *msg_args,177 byte_t *msg_args,
176 disvm::runtime::pointer_t base)178 pointer_t base)
177 {179 {
178 auto static_buffer = std::array<char, 1024>{};180 auto static_buffer = std::array<char, 1024>{};
179 auto written = disvm::runtime::sys::printf_to_buffer(vm, msg_fmt, msg_args, base, static_buffer.size(), static_buffer.data());181 auto written = disvm::runtime::sys::printf_to_buffer(vm, msg_fmt, msg_args, base, static_buffer.size(), static_buffer.data());
@@ -265,17 +267,28 @@ void · 85 unmodified lines
265Sys_announce(vm_registers_t &r, vm_t &vm)267Sys_announce(vm_registers_t &r, vm_t &vm)
266{268{
267 auto &fp = r.stack.peek_frame()->base<F_Sys_announce>();269 auto &fp = r.stack.peek_frame()->base<F_Sys_announce>();
270
271 // [PAL] Consume platform API
268 throw vm_system_exception{ "Function not implemented" };272 throw vm_system_exception{ "Function not implemented" };
269}273}
270 274
271void275void
272Sys_aprint(vm_registers_t &r, vm_t &vm)276Sys_aprint(vm_registers_t &r, vm_t &vm)
273{277{
278 auto fp_base = r.stack.peek_frame()->base();
274 auto &fp = r.stack.peek_frame()->base<F_Sys_aprint>();279 auto &fp = r.stack.peek_frame()->base<F_Sys_aprint>();
280 auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s);
281 if (str == nullptr)
282 throw dereference_nil{ "Print string to array" };
275 283
276 //disvm::runtime::dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret));284 auto buffer = std::vector<char>(128);
277 //*fp.ret = nullptr;285 auto msg_args = &fp.vargs;
278 throw vm_system_exception{ "Function not implemented" };286 auto written = disvm::runtime::sys::printf_to_dynamic_buffer(vm, *str, msg_args, fp_base, buffer);
287 auto new_array = new vm_array_t{ intrinsic_type_desc::type<byte_t>(), written };
288 std::memcpy(new_array->at(0), buffer.data(), written);
289
290 disvm::runtime::dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret));
291 *fp.ret = new_array->get_allocation();
279}292}
280 293
281void294void
@@ -366,6 +379,8 @@ void · 84 unmodified lines
366Sys_chdir(vm_registers_t &r, vm_t &vm)379Sys_chdir(vm_registers_t &r, vm_t &vm)
367{380{
368 auto &fp = r.stack.peek_frame()->base<F_Sys_chdir>();381 auto &fp = r.stack.peek_frame()->base<F_Sys_chdir>();
382
383 // [PAL] Consume platform API
369 throw vm_system_exception{ "Function not implemented" };384 throw vm_system_exception{ "Function not implemented" };
370}385}
371 386
@@ -408,6 +423,8 @@ void · 36 unmodified lines
408Sys_dial(vm_registers_t &r, vm_t &vm)423Sys_dial(vm_registers_t &r, vm_t &vm)
409{424{
410 auto &fp = r.stack.peek_frame()->base<F_Sys_dial>();425 auto &fp = r.stack.peek_frame()->base<F_Sys_dial>();
426
427 // [PAL] Consume platform API
411 throw vm_system_exception{ "Function not implemented" };428 throw vm_system_exception{ "Function not implemented" };
412}429}
413 430
@@ -415,6 +432,8 @@ void · 1 unmodified line
415Sys_dirread(vm_registers_t &r, vm_t &vm)432Sys_dirread(vm_registers_t &r, vm_t &vm)
416{433{
417 auto &fp = r.stack.peek_frame()->base<F_Sys_dirread>();434 auto &fp = r.stack.peek_frame()->base<F_Sys_dirread>();
435
436 // [PAL] Consume platform API
418 throw vm_system_exception{ "Function not implemented" };437 throw vm_system_exception{ "Function not implemented" };
419}438}
420 439
@@ -523,6 +542,8 @@ void · 102 unmodified lines
523Sys_fstat(vm_registers_t &r, vm_t &vm)542Sys_fstat(vm_registers_t &r, vm_t &vm)
524{543{
525 auto &fp = r.stack.peek_frame()->base<F_Sys_fstat>();544 auto &fp = r.stack.peek_frame()->base<F_Sys_fstat>();
545
546 // [PAL] Consume platform API
526 throw vm_system_exception{ "Function not implemented" };547 throw vm_system_exception{ "Function not implemented" };
527}548}
528 549
@@ -537,6 +558,8 @@ void · 8 unmodified lines
537Sys_fwstat(vm_registers_t &r, vm_t &vm)558Sys_fwstat(vm_registers_t &r, vm_t &vm)
538{559{
539 auto &fp = r.stack.peek_frame()->base<F_Sys_fwstat>();560 auto &fp = r.stack.peek_frame()->base<F_Sys_fwstat>();
561
562 // [PAL] Consume platform API
540 throw vm_system_exception{ "Function not implemented" };563 throw vm_system_exception{ "Function not implemented" };
541}564}
542 565
@@ -551,6 +574,8 @@ void · 8 unmodified lines
551Sys_listen(vm_registers_t &r, vm_t &vm)574Sys_listen(vm_registers_t &r, vm_t &vm)
552{575{
553 auto &fp = r.stack.peek_frame()->base<F_Sys_listen>();576 auto &fp = r.stack.peek_frame()->base<F_Sys_listen>();
577
578 // [PAL] Consume platform API
554 throw vm_system_exception{ "Function not implemented" };579 throw vm_system_exception{ "Function not implemented" };
555}580}
556 581
@@ -609,6 +634,8 @@ void · 52 unmodified lines
609Sys_pctl(vm_registers_t &r, vm_t &vm)634Sys_pctl(vm_registers_t &r, vm_t &vm)
610{635{
611 auto &fp = r.stack.peek_frame()->base<F_Sys_pctl>();636 auto &fp = r.stack.peek_frame()->base<F_Sys_pctl>();
637
638 // [PAL] Consume platform API
612 throw vm_system_exception{ "Function not implemented" };639 throw vm_system_exception{ "Function not implemented" };
613}640}
614 641
@@ -616,6 +643,8 @@ void · 1 unmodified line
616Sys_pipe(vm_registers_t &r, vm_t &vm)643Sys_pipe(vm_registers_t &r, vm_t &vm)
617{644{
618 auto &fp = r.stack.peek_frame()->base<F_Sys_pipe>();645 auto &fp = r.stack.peek_frame()->base<F_Sys_pipe>();
646
647 // [PAL] Consume platform API
619 throw vm_system_exception{ "Function not implemented" };648 throw vm_system_exception{ "Function not implemented" };
620}649}
621 650
@@ -753,6 +782,7 @@ Sys_sprint(vm_registers_t &r, vm_t &vm) · 131 unmodified lines
753 new_string = new vm_string_t{ static_cast<std::size_t>(written), reinterpret_cast<uint8_t *>(dynamic_buffer.data()) };782 new_string = new vm_string_t{ static_cast<std::size_t>(written), reinterpret_cast<uint8_t *>(dynamic_buffer.data()) };
754 }783 }
755 784
785 disvm::runtime::dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret));
756 *fp.ret = new_string->get_allocation();786 *fp.ret = new_string->get_allocation();
757}787}
758 788
@@ -760,6 +790,8 @@ void · 1 unmodified line
760Sys_stat(vm_registers_t &r, vm_t &vm)790Sys_stat(vm_registers_t &r, vm_t &vm)
761{791{
762 auto &fp = r.stack.peek_frame()->base<F_Sys_stat>();792 auto &fp = r.stack.peek_frame()->base<F_Sys_stat>();
793
794 // [PAL] Consume platform API
763 throw vm_system_exception{ "Function not implemented" };795 throw vm_system_exception{ "Function not implemented" };
764}796}
765 797
@@ -767,7 +799,36 @@ void · 1 unmodified line
767Sys_stream(vm_registers_t &r, vm_t &vm)799Sys_stream(vm_registers_t &r, vm_t &vm)
768{800{
769 auto &fp = r.stack.peek_frame()->base<F_Sys_stream>();801 auto &fp = r.stack.peek_frame()->base<F_Sys_stream>();
770 throw vm_system_exception{ "Function not implemented" };802
803 auto alloc_s = vm_alloc_t::from_allocation(fp.src);
804 if (alloc_s == nullptr)
805 throw dereference_nil{ "Source in stream" };
806
807 auto alloc_d = vm_alloc_t::from_allocation(fp.dst);
808 if (alloc_d == nullptr)
809 throw dereference_nil{ "Destination in stream" };
810
811 assert(alloc_s->alloc_type->is_equal(T_FD.get()) && alloc_d->alloc_type->is_equal(T_FD.get()));
812 auto src = alloc_s->get_allocation<Sys_FD_Impl>()->impl;
813 auto dst = alloc_d->get_allocation<Sys_FD_Impl>()->impl;
814
815 const auto buffer_size = fp.bufsiz;
816 if (buffer_size <= 0)
817 throw out_of_range_memory{};
818
819 std::unique_ptr<void, decltype(&disvm::runtime::free_memory)> buffer{ disvm::runtime::alloc_memory(buffer_size), disvm::runtime::free_memory };
820 auto written = word_t{};
821 for (;;)
822 {
823 auto bytes_read = src->read(vm, buffer_size, buffer.get());
824 if (bytes_read == 0)
825 break;
826
827 dst->write(vm, bytes_read, buffer.get());
828 written += bytes_read;
829 }
830
831 *fp.ret = written;
771}832}
772 833
773namespace834namespace
@@ -971,5 +1032,7 @@ void · 197 unmodified lines
971Sys_wstat(vm_registers_t &r, vm_t &vm)1032Sys_wstat(vm_registers_t &r, vm_t &vm)
972{1033{
973 auto &fp = r.stack.peek_frame()->base<F_Sys_wstat>();1034 auto &fp = r.stack.peek_frame()->base<F_Sys_wstat>();
1035
1036 // [PAL] Consume platform API
974 throw vm_system_exception{ "Function not implemented" };1037 throw vm_system_exception{ "Function not implemented" };
975}1038}