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
209209 vm_fd_t *fd_stdout = nullptr;
210210 vm_fd_t *fd_stderr = nullptr;
211211
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+
212235 std::ios::openmode convert_to_openmode(const word_t mode)
213236 {
214237 auto om = std::ios::openmode{};
@@ -422,6 +445,7 @@ Sys_create(vm_registers_t &r, vm_t &vm) · 207 unmodified lines
422445 }
423446
424447 *fp.ret = fd_alloc->get_allocation();
448+ fd_alloc->add_ref();
425449 }
426450
427451 void
@@ -492,8 +516,16 @@ Sys_file2chan(vm_registers_t &r, vm_t &vm) · 64 unmodified lines
492516 void
493517 Sys_fprint(vm_registers_t &r, vm_t &vm)
494518 {
519+ auto fp_base = r.stack.peek_frame()->base();
495520 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);
497529 }
498530
499531 void
@@ -582,6 +614,7 @@ Sys_open(vm_registers_t &r, vm_t &vm) · 82 unmodified lines
582614 }
583615
584616 *fp.ret = fd_alloc->get_allocation();
617+ fd_alloc->add_ref();
585618 }
586619
587620 void
@@ -611,25 +644,10 @@ Sys_print(vm_registers_t &r, vm_t &vm) · 23 unmodified lines
611644 auto fp_base = r.stack.peek_frame()->base();
612645 auto &fp = r.stack.peek_frame()->base<F_Sys_print>();
613646 auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s);
614−
615647 if (str == nullptr)
616648 throw dereference_nil{ "Print string" };
617649
618− auto static_buffer = std::array<char, 1024>{};
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;
650+ *fp.ret = printf_to_fd(vm, *fd_stdout, *str, &fp.vargs, fp_base);
633651 }
634652
635653 void
@@ -680,7 +698,13 @@ void · 44 unmodified lines
680698 Sys_remove(vm_registers_t &r, vm_t &vm)
681699 {
682700 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());
684708 }
685709
686710 void
@@ -715,8 +739,29 @@ Sys_sleep(vm_registers_t &r, vm_t &vm) · 28 unmodified lines
715739 void
716740 Sys_sprint(vm_registers_t &r, vm_t &vm)
717741 {
742+ auto fp_base = r.stack.peek_frame()->base();
718743 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();
720765 }
721766
722767 void

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