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

Showing the changes to src/vm/sys/Sysmod.cpp (1 of 1 file). Show all

Unified · Split

src/vm/sys/Sysmod.cpp+70−7
@@ -24,9 +24,11 @@ using disvm::vm_t; · 23 unmodified lines
2424 using disvm::debug::component_trace_t;
2525 using disvm::debug::log_level_t;
2626
27+using disvm::runtime::byte_t;
2728 using disvm::runtime::dereference_nil;
2829 using disvm::runtime::intrinsic_type_desc;
2930 using disvm::runtime::out_of_range_memory;
31+using disvm::runtime::pointer_t;
3032 using disvm::runtime::type_descriptor_t;
3133 using disvm::runtime::vm_alloc_t;
3234 using disvm::runtime::vm_array_t;
@@ -171,9 +173,9 @@ namespace · 138 unmodified lines
171173 word_t printf_to_fd(
172174 disvm::vm_t &vm,
173175 vm_fd_t &fd,
174− const disvm::runtime::vm_string_t &msg_fmt,
175− disvm::runtime::byte_t *msg_args,
176− disvm::runtime::pointer_t base)
176+ const vm_string_t &msg_fmt,
177+ byte_t *msg_args,
178+ pointer_t base)
177179 {
178180 auto static_buffer = std::array<char, 1024>{};
179181 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
265267 Sys_announce(vm_registers_t &r, vm_t &vm)
266268 {
267269 auto &fp = r.stack.peek_frame()->base<F_Sys_announce>();
270+
271+ // [PAL] Consume platform API
268272 throw vm_system_exception{ "Function not implemented" };
269273 }
270274
271275 void
272276 Sys_aprint(vm_registers_t &r, vm_t &vm)
273277 {
278+ auto fp_base = r.stack.peek_frame()->base();
274279 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" };
275283
276− //disvm::runtime::dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret));
277− //*fp.ret = nullptr;
278− throw vm_system_exception{ "Function not implemented" };
284+ auto buffer = std::vector<char>(128);
285+ auto msg_args = &fp.vargs;
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();
279292 }
280293
281294 void
@@ -366,6 +379,8 @@ void · 84 unmodified lines
366379 Sys_chdir(vm_registers_t &r, vm_t &vm)
367380 {
368381 auto &fp = r.stack.peek_frame()->base<F_Sys_chdir>();
382+
383+ // [PAL] Consume platform API
369384 throw vm_system_exception{ "Function not implemented" };
370385 }
371386
@@ -408,6 +423,8 @@ void · 36 unmodified lines
408423 Sys_dial(vm_registers_t &r, vm_t &vm)
409424 {
410425 auto &fp = r.stack.peek_frame()->base<F_Sys_dial>();
426+
427+ // [PAL] Consume platform API
411428 throw vm_system_exception{ "Function not implemented" };
412429 }
413430
@@ -415,6 +432,8 @@ void · 1 unmodified line
415432 Sys_dirread(vm_registers_t &r, vm_t &vm)
416433 {
417434 auto &fp = r.stack.peek_frame()->base<F_Sys_dirread>();
435+
436+ // [PAL] Consume platform API
418437 throw vm_system_exception{ "Function not implemented" };
419438 }
420439
@@ -523,6 +542,8 @@ void · 102 unmodified lines
523542 Sys_fstat(vm_registers_t &r, vm_t &vm)
524543 {
525544 auto &fp = r.stack.peek_frame()->base<F_Sys_fstat>();
545+
546+ // [PAL] Consume platform API
526547 throw vm_system_exception{ "Function not implemented" };
527548 }
528549
@@ -537,6 +558,8 @@ void · 8 unmodified lines
537558 Sys_fwstat(vm_registers_t &r, vm_t &vm)
538559 {
539560 auto &fp = r.stack.peek_frame()->base<F_Sys_fwstat>();
561+
562+ // [PAL] Consume platform API
540563 throw vm_system_exception{ "Function not implemented" };
541564 }
542565
@@ -551,6 +574,8 @@ void · 8 unmodified lines
551574 Sys_listen(vm_registers_t &r, vm_t &vm)
552575 {
553576 auto &fp = r.stack.peek_frame()->base<F_Sys_listen>();
577+
578+ // [PAL] Consume platform API
554579 throw vm_system_exception{ "Function not implemented" };
555580 }
556581
@@ -609,6 +634,8 @@ void · 52 unmodified lines
609634 Sys_pctl(vm_registers_t &r, vm_t &vm)
610635 {
611636 auto &fp = r.stack.peek_frame()->base<F_Sys_pctl>();
637+
638+ // [PAL] Consume platform API
612639 throw vm_system_exception{ "Function not implemented" };
613640 }
614641
@@ -616,6 +643,8 @@ void · 1 unmodified line
616643 Sys_pipe(vm_registers_t &r, vm_t &vm)
617644 {
618645 auto &fp = r.stack.peek_frame()->base<F_Sys_pipe>();
646+
647+ // [PAL] Consume platform API
619648 throw vm_system_exception{ "Function not implemented" };
620649 }
621650
@@ -753,6 +782,7 @@ Sys_sprint(vm_registers_t &r, vm_t &vm) · 131 unmodified lines
753782 new_string = new vm_string_t{ static_cast<std::size_t>(written), reinterpret_cast<uint8_t *>(dynamic_buffer.data()) };
754783 }
755784
785+ disvm::runtime::dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret));
756786 *fp.ret = new_string->get_allocation();
757787 }
758788
@@ -760,6 +790,8 @@ void · 1 unmodified line
760790 Sys_stat(vm_registers_t &r, vm_t &vm)
761791 {
762792 auto &fp = r.stack.peek_frame()->base<F_Sys_stat>();
793+
794+ // [PAL] Consume platform API
763795 throw vm_system_exception{ "Function not implemented" };
764796 }
765797
@@ -767,7 +799,36 @@ void · 1 unmodified line
767799 Sys_stream(vm_registers_t &r, vm_t &vm)
768800 {
769801 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;
771832 }
772833
773834 namespace
@@ -971,5 +1032,7 @@ void · 197 unmodified lines
9711032 Sys_wstat(vm_registers_t &r, vm_t &vm)
9721033 {
9731034 auto &fp = r.stack.peek_frame()->base<F_Sys_wstat>();
1035+
1036+ // [PAL] Consume platform API
9741037 throw vm_system_exception{ "Function not implemented" };
9751038 }