]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/jit.cpp
vm: change "profiler" names to "counting_profiler"
[factor.git] / vm / jit.cpp
index d474d23a18a22fbd2d39e727043eec24678ffdfa..77b827bef2c290a3ceb8c3b64072f515adc744f2 100644 (file)
@@ -4,40 +4,36 @@ 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) */
 
 /* Allocates memory */
-jit::jit(cell type_, cell owner_, factorvm *vm)
+jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
        : type(type_),
          owner(owner_,vm),
          code(vm),
          relocation(vm),
+         parameters(vm),
          literals(vm),
          computing_offset_p(false),
          position(0),
          offset(0),
-         myvm(vm)
-{
-       if(myvm->stack_traces_p()) literal(owner.value());
-}
+         parent(vm)
+{}
 
-void jit::emit_relocation(cell code_template_)
+void jit::emit_relocation(cell relocation_template_)
 {
-       gc_root<array> code_template(code_template_,myvm);
-       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++)
        {
-               cell rel_class = array_nth(code_template.untagged(),i);
-               cell rel_type = array_nth(code_template.untagged(),i + 1);
-               cell offset = array_nth(code_template.untagged(),i + 2);
-
-               relocation_entry new_entry
-                       = (untag_fixnum(rel_type) << 28)
-                       | (untag_fixnum(rel_class) << 24)
-                       | ((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));
        }
 }
@@ -45,11 +41,11 @@ void jit::emit_relocation(cell code_template_)
 /* Allocates memory */
 void jit::emit(cell code_template_)
 {
-       gc_root<array> code_template(code_template_,myvm);
+       data_root<array> code_template(code_template_,parent);
 
-       emit_relocation(code_template.value());
+       emit_relocation(array_nth(code_template.untagged(),0));
 
-       gc_root<byte_array> insns(array_nth(code_template.untagged(),0),myvm);
+       data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
 
        if(computing_offset_p)
        {
@@ -72,19 +68,41 @@ void jit::emit(cell code_template_)
        code.append_byte_array(insns.value());
 }
 
-void jit::emit_with(cell code_template_, cell argument_) {
-       gc_root<array> code_template(code_template_,myvm);
-       gc_root<object> argument(argument_,myvm);
+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(userenv[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
-       emit(userenv[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. */
@@ -98,15 +116,23 @@ 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 myvm->add_code_block(
+       return parent->add_code_block(
                type,
                code.elements.value(),
-               F, /* no labels */
+               false_object, /* no labels */
+               owner.value(),
                relocation.elements.value(),
+               parameters.elements.value(),
                literals.elements.value());
 }