]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/jit.cpp
vm: change "profiler" names to "counting_profiler"
[factor.git] / vm / jit.cpp
index 1bdbbc617197bd7e092b9fc14aa6eb067f6b3110..77b827bef2c290a3ceb8c3b64072f515adc744f2 100644 (file)
@@ -4,7 +4,7 @@ namespace factor
 {
 
 /* Simple code generator used by:
-- profiler (profiler.cpp),
+- counting_profiler (counting_profiler.cpp),
 - quotation compiler (quotations.cpp),
 - megamorphic caches (dispatch.cpp),
 - polymorphic inline caches (inline_cache.cpp) */
@@ -15,6 +15,7 @@ jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
          owner(owner_,vm),
          code(vm),
          relocation(vm),
+         parameters(vm),
          literals(vm),
          computing_offset_p(false),
          position(0),
@@ -22,17 +23,17 @@ jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
          parent(vm)
 {}
 
-void jit::emit_relocation(cell code_template_)
+void jit::emit_relocation(cell relocation_template_)
 {
-       data_root<array> code_template(code_template_,parent);
-       cell capacity = array_capacity(code_template.untagged());
-       for(cell i = 1; i < capacity; i += 3)
+       data_root<byte_array> relocation_template(relocation_template_,parent);
+       cell capacity = array_capacity(relocation_template.untagged())
+               / sizeof(relocation_entry);
+       relocation_entry *relocations = relocation_template->data<relocation_entry>();
+       for(cell i = 0; i < capacity; i++)
        {
-               relocation_class rel_class = (relocation_class)untag_fixnum(array_nth(code_template.untagged(),i));
-               relocation_type rel_type = (relocation_type)untag_fixnum(array_nth(code_template.untagged(),i + 1));
-               cell offset = array_nth(code_template.untagged(),i + 2);
-
-               relocation_entry new_entry(rel_type,rel_class,code.count + untag_fixnum(offset));
+               relocation_entry entry = relocations[i];
+               relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
+                       entry.rel_offset() + code.count);
                relocation.append_bytes(&new_entry,sizeof(relocation_entry));
        }
 }
@@ -42,9 +43,9 @@ void jit::emit(cell code_template_)
 {
        data_root<array> code_template(code_template_,parent);
 
-       emit_relocation(code_template.value());
+       emit_relocation(array_nth(code_template.untagged(),0));
 
-       data_root<byte_array> insns(array_nth(code_template.untagged(),0),parent);
+       data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
 
        if(computing_offset_p)
        {
@@ -67,19 +68,41 @@ void jit::emit(cell code_template_)
        code.append_byte_array(insns.value());
 }
 
-void jit::emit_with(cell code_template_, cell argument_) {
+void jit::emit_with_literal(cell code_template_, cell argument_) {
        data_root<array> code_template(code_template_,parent);
        data_root<object> argument(argument_,parent);
        literal(argument.value());
        emit(code_template.value());
 }
 
-void jit::emit_class_lookup(fixnum index, cell type)
-{
-       emit_with(parent->special_objects[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
-       emit(parent->special_objects[type]);
+void jit::emit_with_parameter(cell code_template_, cell argument_) {
+       data_root<array> code_template(code_template_,parent);
+       data_root<object> argument(argument_,parent);
+       parameter(argument.value());
+       emit(code_template.value());
 }
 
+bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
+{
+       data_root<word> word(word_,parent);
+       data_root<array> code_template(word->subprimitive,parent);
+       parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
+       literals.append(untag<array>(array_nth(code_template.untagged(),1)));
+       emit(array_nth(code_template.untagged(),2));
+       if(array_capacity(code_template.untagged()) == 5)
+       {
+               if(tail_call_p)
+               {
+                       if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
+                       emit(array_nth(code_template.untagged(),4));
+                       return true;
+               }
+               else
+                       emit(array_nth(code_template.untagged(),3));
+       }
+       return false;
+}
+       
 /* Facility to convert compiled code offsets to quotation offsets.
 Call jit_compute_offset() with the compiled code offset, then emit
 code, and at the end jit->position is the quotation position. */
@@ -93,8 +116,14 @@ void jit::compute_position(cell offset_)
 /* Allocates memory */
 code_block *jit::to_code_block()
 {
+       /* Emit dummy GC info */
+       code.grow_bytes(alignment_for(code.count + 4,data_alignment));
+       u32 dummy_gc_info = 0;
+       code.append_bytes(&dummy_gc_info,sizeof(u32));
+
        code.trim();
        relocation.trim();
+       parameters.trim();
        literals.trim();
 
        return parent->add_code_block(
@@ -103,6 +132,7 @@ code_block *jit::to_code_block()
                false_object, /* no labels */
                owner.value(),
                relocation.elements.value(),
+               parameters.elements.value(),
                literals.elements.value());
 }