History

Implement more sys functions for limbo compiler - fprint - sprint - remove Introduce a new comment ID for PAL issues.

arr committed 9 years ago

Commit 3f81a2c1 parent 058335e3 files · +66 −20Browse files

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

Unified · Split

src/vm/sys/Sysmod.cpp+64−19
@@ -209,6 +209,29 @@ namespace · 208 unmodified lines
209 vm_fd_t *fd_stdout = nullptr;209 vm_fd_t *fd_stdout = nullptr;
210 vm_fd_t *fd_stderr = nullptr;210 vm_fd_t *fd_stderr = nullptr;
211 211
212 word_t printf_to_fd(
213 disvm::vm_t &vm,
214 vm_fd_t &fd,
215 const disvm::runtime::vm_string_t &msg_fmt,
216 disvm::runtime::byte_t *msg_args,
217 disvm::runtime::pointer_t base)
218 {
219 auto static_buffer = std::array<char, 1024>{};
220 auto written = sys::printf_to_buffer(vm, msg_fmt, msg_args, base, static_buffer.size(), static_buffer.data());
221 if (written >= 0)
222 {
223 fd.write(vm, written, static_buffer.data());
224 }
225 else
226 {
227 auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2);
228 written = sys::printf_to_dynamic_buffer(vm, msg_fmt, msg_args, base, dynamic_buffer);
229 fd.write(vm, written, dynamic_buffer.data());
230 }
231
232 return written;
233 }
234
212 std::ios::openmode convert_to_openmode(const word_t mode)235 std::ios::openmode convert_to_openmode(const word_t mode)
213 {236 {
214 auto om = std::ios::openmode{};237 auto om = std::ios::openmode{};
@@ -422,6 +445,7 @@ Sys_create(vm_registers_t &r, vm_t &vm) · 207 unmodified lines
422 }445 }
423 446
424 *fp.ret = fd_alloc->get_allocation();447 *fp.ret = fd_alloc->get_allocation();
448 fd_alloc->add_ref();
425}449}
426 450
427void451void
@@ -492,8 +516,16 @@ Sys_file2chan(vm_registers_t &r, vm_t &vm) · 64 unmodified lines
492void516void
493Sys_fprint(vm_registers_t &r, vm_t &vm)517Sys_fprint(vm_registers_t &r, vm_t &vm)
494{518{
519 auto fp_base = r.stack.peek_frame()->base();
495 auto &fp = r.stack.peek_frame()->base<F_Sys_fprint>();520 auto &fp = r.stack.peek_frame()->base<F_Sys_fprint>();
496 throw vm_system_exception{ "Instruction not implemented" };521 auto fd_alloc = vm_alloc_t::from_allocation(fp.fd);
522 assert(fd_alloc->alloc_type == T_FD);
523 auto fd = fd_alloc->get_allocation<vm_fd_t>();
524 auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s);
525 if (str == nullptr)
526 throw dereference_nil{ "Print string to FD" };
527
528 *fp.ret = printf_to_fd(vm, *fd, *str, &fp.vargs, fp_base);
497}529}
498 530
499void531void
@@ -582,6 +614,7 @@ Sys_open(vm_registers_t &r, vm_t &vm) · 82 unmodified lines
582 }614 }
583 615
584 *fp.ret = fd_alloc->get_allocation();616 *fp.ret = fd_alloc->get_allocation();
617 fd_alloc->add_ref();
585}618}
586 619
587void620void
@@ -611,25 +644,10 @@ Sys_print(vm_registers_t &r, vm_t &vm) · 23 unmodified lines
611 auto fp_base = r.stack.peek_frame()->base();644 auto fp_base = r.stack.peek_frame()->base();
612 auto &fp = r.stack.peek_frame()->base<F_Sys_print>();645 auto &fp = r.stack.peek_frame()->base<F_Sys_print>();
613 auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s);646 auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s);
614
615 if (str == nullptr)647 if (str == nullptr)
616 throw dereference_nil{ "Print string" };648 throw dereference_nil{ "Print string" };
617 649
618 auto static_buffer = std::array<char, 1024>{};650 *fp.ret = printf_to_fd(vm, *fd_stdout, *str, &fp.vargs, fp_base);
619 auto msg_args = &fp.vargs;
620 auto written = sys::printf_to_buffer(vm, *str, msg_args, fp_base, static_buffer.size(), static_buffer.data());
621 if (written >= 0)
622 {
623 fd_stdout->write(vm, written, static_buffer.data());
624 }
625 else
626 {
627 auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2);
628 written = sys::printf_to_dynamic_buffer(vm, *str, msg_args, fp_base, dynamic_buffer);
629 fd_stdout->write(vm, written, dynamic_buffer.data());
630 }
631
632 *fp.ret = written;
633}651}
634 652
635void653void
@@ -680,7 +698,13 @@ void · 44 unmodified lines
680Sys_remove(vm_registers_t &r, vm_t &vm)698Sys_remove(vm_registers_t &r, vm_t &vm)
681{699{
682 auto &fp = r.stack.peek_frame()->base<F_Sys_remove>();700 auto &fp = r.stack.peek_frame()->base<F_Sys_remove>();
683 throw vm_system_exception{ "Instruction not implemented" };701 auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s);
702 if (str == nullptr)
703 throw dereference_nil{ "Remove path" };
704
705 // [PAL] There are a lot of implementation details with this function.
706 // Consider adding a PAL function to optimize per platform.
707 *fp.ret = std::remove(str->str());
684}708}
685 709
686void710void
@@ -715,8 +739,29 @@ Sys_sleep(vm_registers_t &r, vm_t &vm) · 28 unmodified lines
715void739void
716Sys_sprint(vm_registers_t &r, vm_t &vm)740Sys_sprint(vm_registers_t &r, vm_t &vm)
717{741{
742 auto fp_base = r.stack.peek_frame()->base();
718 auto &fp = r.stack.peek_frame()->base<F_Sys_sprint>();743 auto &fp = r.stack.peek_frame()->base<F_Sys_sprint>();
719 throw vm_system_exception{ "Instruction not implemented" };744 auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s);
745 if (str == nullptr)
746 throw dereference_nil{ "Print string to string" };
747
748 vm_string_t *new_string;
749 auto static_buffer = std::array<char, 128>{};
750 auto msg_args = &fp.vargs;
751 auto written = sys::printf_to_buffer(vm, *str, msg_args, fp_base, static_buffer.size(), static_buffer.data());
752 if (written >= 0)
753 {
754 new_string = new vm_string_t{ static_cast<std::size_t>(written), reinterpret_cast<uint8_t *>(static_buffer.data()) };
755 }
756 else
757 {
758 auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2);
759 written = sys::printf_to_dynamic_buffer(vm, *str, msg_args, fp_base, dynamic_buffer);
760 new_string = new vm_string_t{ static_cast<std::size_t>(written), reinterpret_cast<uint8_t *>(dynamic_buffer.data()) };
761 }
762
763 assert(new_string != nullptr);
764 *fp.ret = new_string->get_allocation();
720}765}
721 766
722void767void

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t