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_, factor_vm *vm)
19 computing_offset_p(false),
25 void jit::emit_relocation(cell code_template_)
27 gc_root<array> code_template(code_template_,parent_vm);
28 cell capacity = array_capacity(code_template.untagged());
29 for(cell i = 1; i < capacity; i += 3)
31 cell rel_class = array_nth(code_template.untagged(),i);
32 cell rel_type = array_nth(code_template.untagged(),i + 1);
33 cell offset = array_nth(code_template.untagged(),i + 2);
35 relocation_entry new_entry
36 = (untag_fixnum(rel_type) << 28)
37 | (untag_fixnum(rel_class) << 24)
38 | ((code.count + untag_fixnum(offset)));
39 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
43 /* Allocates memory */
44 void jit::emit(cell code_template_)
46 gc_root<array> code_template(code_template_,parent_vm);
48 emit_relocation(code_template.value());
50 gc_root<byte_array> insns(array_nth(code_template.untagged(),0),parent_vm);
52 if(computing_offset_p)
54 cell size = array_capacity(insns.untagged());
59 computing_offset_p = false;
61 else if(offset < size)
64 computing_offset_p = false;
70 code.append_byte_array(insns.value());
73 void jit::emit_with(cell code_template_, cell argument_) {
74 gc_root<array> code_template(code_template_,parent_vm);
75 gc_root<object> argument(argument_,parent_vm);
76 literal(argument.value());
77 emit(code_template.value());
80 void jit::emit_class_lookup(fixnum index, cell type)
82 emit_with(parent_vm->userenv[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
83 emit(parent_vm->userenv[type]);
86 /* Facility to convert compiled code offsets to quotation offsets.
87 Call jit_compute_offset() with the compiled code offset, then emit
88 code, and at the end jit->position is the quotation position. */
89 void jit::compute_position(cell offset_)
91 computing_offset_p = true;
96 /* Allocates memory */
97 code_block *jit::to_code_block()
103 return parent_vm->add_code_block(
105 code.elements.value(),
108 relocation.elements.value(),
109 literals.elements.value());