]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/callbacks.cpp
webapps.wiki: adding search bar
[factor.git] / vm / callbacks.cpp
index baaa7da1dd7e2da295cd8f4e1c45e9f556571551..492e7a9884c302b27d5b89e6d21aac619a88ccc2 100644 (file)
@@ -2,6 +2,14 @@
 
 namespace factor {
 
+bool return_takes_param_p() {
+#if defined(FACTOR_X86) || defined(FACTOR_AMD64)
+  return true;
+#else
+  return false;
+#endif
+}
+
 callback_heap::callback_heap(cell size, factor_vm* parent) {
   seg = new segment(size, true);
   if (!seg)
@@ -16,27 +24,6 @@ callback_heap::~callback_heap() {
   allocator = NULL;
   delete seg;
   seg = NULL;
-
-}
-
-void factor_vm::init_callbacks(cell size) {
-  callbacks = new callback_heap(size, this);
-}
-
-bool callback_heap::setup_seh_p() {
-#if defined(WINDOWS) && defined(FACTOR_X86)
-  return true;
-#else
-  return false;
-#endif
-}
-
-bool callback_heap::return_takes_param_p() {
-#if defined(FACTOR_X86) || defined(FACTOR_AMD64)
-  return true;
-#else
-  return false;
-#endif
 }
 
 instruction_operand callback_heap::callback_operand(code_block* stub,
@@ -49,23 +36,23 @@ instruction_operand callback_heap::callback_operand(code_block* stub,
   return instruction_operand(entry, stub, 0);
 }
 
-void callback_heap::store_callback_operand(code_block* stub, cell index) {
-  instruction_operand op = callback_operand(stub, index);
-  op.store_value(parent->compute_external_address(op));
-}
-
 void callback_heap::store_callback_operand(code_block* stub, cell index,
                                            cell value) {
-  callback_operand(stub, index).store_value(value);
+  instruction_operand op = callback_operand(stub, index);
+  op.store_value(value);
 }
 
 void callback_heap::update(code_block* stub) {
-  store_callback_operand(stub, setup_seh_p() ? 2 : 1,
-                         callback_entry_point(stub));
+  word* w = untag<word>(stub->owner);
+  store_callback_operand(stub, 1, w->entry_point);
   stub->flush_icache();
 }
 
 code_block* callback_heap::add(cell owner, cell return_rewind) {
+  // code_template is a 2-tuple where the first element contains the
+  // relocations and the second a byte array of compiled assembly
+  // code. The code assumes that there are four relocations on x86 and
+  // three on ppc.
   tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
   tagged<byte_array> insns(array_nth(code_template.untagged(), 1));
   cell size = array_capacity(insns.untagged());
@@ -77,7 +64,6 @@ code_block* callback_heap::add(cell owner, cell return_rewind) {
                           false_object,
                           false_object);
   }
-
   stub->header = bump & ~7;
   stub->owner = owner;
   stub->parameters = false_object;
@@ -85,47 +71,28 @@ code_block* callback_heap::add(cell owner, cell return_rewind) {
 
   memcpy((void*)stub->entry_point(), insns->data<void>(), size);
 
-  /* Store VM pointer */
+  // Store VM pointer in two relocations.
   store_callback_operand(stub, 0, (cell)parent);
+  store_callback_operand(stub, 2, (cell)parent);
 
-  cell index;
-
-  if (setup_seh_p()) {
-    store_callback_operand(stub, 1);
-    index = 1;
-  } else
-    index = 0;
-
-  /* Store VM pointer */
-  store_callback_operand(stub, index + 2, (cell) parent);
-
-  /* On x86, the RET instruction takes an argument which depends on
-     the callback's calling convention */
+  // On x86, the RET instruction takes an argument which depends on
+  // the callback's calling convention
   if (return_takes_param_p())
-    store_callback_operand(stub, index + 3, return_rewind);
+    store_callback_operand(stub, 3, return_rewind);
 
   update(stub);
-
   return stub;
 }
 
-void callback_heap::update() {
-  auto callback_updater = [&](code_block* stub, cell size) {
-    update(stub);
-  };
-  allocator->iterate(callback_updater);
-}
-
-/* Allocates memory (add(), allot_alien())*/
+// Allocates memory (add(), allot_alien())
 void factor_vm::primitive_callback() {
   cell return_rewind = to_cell(ctx->pop());
   tagged<word> w(ctx->pop());
-
-  w.untag_check(this);
+  check_tagged(w);
 
   cell func = callbacks->add(w.value(), return_rewind)->entry_point();
   CODE_TO_FUNCTION_POINTER_CALLBACK(this, func);
-  ctx->push(allot_alien((void*)func));
+  ctx->push(allot_alien(func));
 }
 
 void factor_vm::primitive_free_callback() {
@@ -134,7 +101,7 @@ void factor_vm::primitive_free_callback() {
   callbacks->allocator->free(stub);
 }
 
-/* Allocates memory */
+// Allocates memory
 void factor_vm::primitive_callback_room() {
   allocator_room room = callbacks->allocator->as_allocator_room();
   ctx->push(tag<byte_array>(byte_array_from_value(&room)));