6 /* Simple code generator used by:
7 - profiler (profiler.cpp),
8 - quotation compiler (quotations.cpp),
9 - megamorphic caches (dispatch.cpp),
10 - polymorphic inline caches (inline_cache.cpp) */
12 /* Allocates memory */
13 jit::jit(cell type_, cell owner_, factorvm *vm)
19 computing_offset_p(false),
24 if(myvm->stack_traces_p()) literal(owner.value());
27 void jit::emit_relocation(cell code_template_)
29 gc_root<array> code_template(code_template_,myvm);
30 cell capacity = array_capacity(code_template.untagged());
31 for(cell i = 1; i < capacity; i += 3)
33 cell rel_class = array_nth(code_template.untagged(),i);
34 cell rel_type = array_nth(code_template.untagged(),i + 1);
35 cell offset = array_nth(code_template.untagged(),i + 2);
37 relocation_entry new_entry
38 = (untag_fixnum(rel_type) << 28)
39 | (untag_fixnum(rel_class) << 24)
40 | ((code.count + untag_fixnum(offset)));
41 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
45 /* Allocates memory */
46 void jit::emit(cell code_template_)
48 gc_root<array> code_template(code_template_,myvm);
50 emit_relocation(code_template.value());
52 gc_root<byte_array> insns(array_nth(code_template.untagged(),0),myvm);
54 if(computing_offset_p)
56 cell size = array_capacity(insns.untagged());
61 computing_offset_p = false;
63 else if(offset < size)
66 computing_offset_p = false;
72 code.append_byte_array(insns.value());
75 void jit::emit_with(cell code_template_, cell argument_) {
76 gc_root<array> code_template(code_template_,myvm);
77 gc_root<object> argument(argument_,myvm);
78 literal(argument.value());
79 emit(code_template.value());
82 void jit::emit_class_lookup(fixnum index, cell type)
84 emit_with(myvm->userenv[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
85 emit(myvm->userenv[type]);
88 /* Facility to convert compiled code offsets to quotation offsets.
89 Call jit_compute_offset() with the compiled code offset, then emit
90 code, and at the end jit->position is the quotation position. */
91 void jit::compute_position(cell offset_)
93 computing_offset_p = true;
98 /* Allocates memory */
99 code_block *jit::to_code_block()
105 return myvm->add_code_block(
107 code.elements.value(),
109 relocation.elements.value(),
110 literals.elements.value());