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(code_block_type type_, cell owner_, factor_vm *vm)
19 computing_offset_p(false),
25 void jit::emit_relocation(cell code_template_)
27 data_root<array> code_template(code_template_,parent);
28 cell capacity = array_capacity(code_template.untagged());
29 for(cell i = 1; i < capacity; i += 3)
31 relocation_class rel_class = (relocation_class)untag_fixnum(array_nth(code_template.untagged(),i));
32 relocation_type rel_type = (relocation_type)untag_fixnum(array_nth(code_template.untagged(),i + 1));
33 cell offset = array_nth(code_template.untagged(),i + 2);
35 relocation_entry new_entry(rel_type,rel_class,code.count + untag_fixnum(offset));
36 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
40 /* Allocates memory */
41 void jit::emit(cell code_template_)
43 data_root<array> code_template(code_template_,parent);
45 emit_relocation(code_template.value());
47 data_root<byte_array> insns(array_nth(code_template.untagged(),0),parent);
49 if(computing_offset_p)
51 cell size = array_capacity(insns.untagged());
56 computing_offset_p = false;
58 else if(offset < size)
61 computing_offset_p = false;
67 code.append_byte_array(insns.value());
70 void jit::emit_with(cell code_template_, cell argument_) {
71 data_root<array> code_template(code_template_,parent);
72 data_root<object> argument(argument_,parent);
73 literal(argument.value());
74 emit(code_template.value());
77 void jit::emit_class_lookup(fixnum index, cell type)
79 emit_with(parent->special_objects[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
80 emit(parent->special_objects[type]);
83 /* Facility to convert compiled code offsets to quotation offsets.
84 Call jit_compute_offset() with the compiled code offset, then emit
85 code, and at the end jit->position is the quotation position. */
86 void jit::compute_position(cell offset_)
88 computing_offset_p = true;
93 /* Allocates memory */
94 code_block *jit::to_code_block()
100 return parent->add_code_block(
102 code.elements.value(),
103 false_object, /* no labels */
105 relocation.elements.value(),
106 literals.elements.value());