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)
20 computing_offset_p(false),
26 void jit::emit_relocation(cell relocation_template_)
28 data_root<byte_array> relocation_template(relocation_template_,parent);
29 cell capacity = array_capacity(relocation_template.untagged())
30 / sizeof(relocation_entry);
31 relocation_entry *relocations = relocation_template->data<relocation_entry>();
32 for(cell i = 0; i < capacity; i++)
34 relocation_entry entry = relocations[i];
35 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
36 entry.rel_offset() + code.count);
37 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
41 /* Allocates memory */
42 void jit::emit(cell code_template_)
44 data_root<array> code_template(code_template_,parent);
46 emit_relocation(array_nth(code_template.untagged(),0));
48 data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
50 if(computing_offset_p)
52 cell size = array_capacity(insns.untagged());
57 computing_offset_p = false;
59 else if(offset < size)
62 computing_offset_p = false;
68 code.append_byte_array(insns.value());
71 void jit::emit_with_literal(cell code_template_, cell argument_) {
72 data_root<array> code_template(code_template_,parent);
73 data_root<object> argument(argument_,parent);
74 literal(argument.value());
75 emit(code_template.value());
78 void jit::emit_with_parameter(cell code_template_, cell argument_) {
79 data_root<array> code_template(code_template_,parent);
80 data_root<object> argument(argument_,parent);
81 parameter(argument.value());
82 emit(code_template.value());
85 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
87 data_root<word> word(word_,parent);
88 data_root<array> code_template(word->subprimitive,parent);
89 parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
90 literals.append(untag<array>(array_nth(code_template.untagged(),1)));
91 emit(array_nth(code_template.untagged(),2));
92 if(array_capacity(code_template.untagged()) == 5)
96 if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
97 emit(array_nth(code_template.untagged(),4));
101 emit(array_nth(code_template.untagged(),3));
106 /* Facility to convert compiled code offsets to quotation offsets.
107 Call jit_compute_offset() with the compiled code offset, then emit
108 code, and at the end jit->position is the quotation position. */
109 void jit::compute_position(cell offset_)
111 computing_offset_p = true;
116 /* Allocates memory */
117 code_block *jit::to_code_block()
119 /* Emit dummy GC info */
120 code.grow_bytes(alignment_for(code.count + 4,data_alignment));
121 u32 dummy_gc_info = 0;
122 code.append_bytes(&dummy_gc_info,sizeof(u32));
129 return parent->add_code_block(
131 code.elements.value(),
132 false_object, /* no labels */
134 relocation.elements.value(),
135 parameters.elements.value(),
136 literals.elements.value());