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_)
19 computing_offset_p(false),
23 if(stack_traces_p()) literal(owner.value());
26 void jit::emit_relocation(cell code_template_)
28 gc_root<array> code_template(code_template_);
29 cell capacity = array_capacity(code_template.untagged());
30 for(cell i = 1; i < capacity; i += 3)
32 cell rel_class = array_nth(code_template.untagged(),i);
33 cell rel_type = array_nth(code_template.untagged(),i + 1);
34 cell offset = array_nth(code_template.untagged(),i + 2);
36 relocation_entry new_entry
37 = (untag_fixnum(rel_type) << 28)
38 | (untag_fixnum(rel_class) << 24)
39 | ((code.count + untag_fixnum(offset)));
40 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
44 /* Allocates memory */
45 void jit::emit(cell code_template_)
47 gc_root<array> code_template(code_template_);
49 emit_relocation(code_template.value());
51 gc_root<byte_array> insns(array_nth(code_template.untagged(),0));
53 if(computing_offset_p)
55 cell size = array_capacity(insns.untagged());
60 computing_offset_p = false;
62 else if(offset < size)
65 computing_offset_p = false;
71 code.append_byte_array(insns.value());
74 void jit::emit_with(cell code_template_, cell argument_) {
75 gc_root<array> code_template(code_template_);
76 gc_root<object> argument(argument_);
77 literal(argument.value());
78 emit(code_template.value());
81 void jit::emit_class_lookup(fixnum index, cell type)
83 emit_with(userenv[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
87 /* Facility to convert compiled code offsets to quotation offsets.
88 Call jit_compute_offset() with the compiled code offset, then emit
89 code, and at the end jit->position is the quotation position. */
90 void jit::compute_position(cell offset_)
92 computing_offset_p = true;
97 /* Allocates memory */
98 code_block *jit::to_code_block()
104 return add_code_block(
106 code.elements.value(),
108 relocation.elements.value(),
109 literals.elements.value());