6 /* Simple code generator used by:
7 - counting_profiler (counting_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),
25 fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
26 assert(old_count >= 0);
31 fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
32 assert(old_count >= 1);
35 void jit::emit_relocation(cell relocation_template_)
37 data_root<byte_array> relocation_template(relocation_template_,parent);
38 cell capacity = array_capacity(relocation_template.untagged())
39 / sizeof(relocation_entry);
40 relocation_entry *relocations = relocation_template->data<relocation_entry>();
41 for(cell i = 0; i < capacity; i++)
43 relocation_entry entry = relocations[i];
44 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
45 entry.rel_offset() + code.count);
46 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
50 /* Allocates memory */
51 void jit::emit(cell code_template_)
53 data_root<array> code_template(code_template_,parent);
55 emit_relocation(array_nth(code_template.untagged(),0));
57 data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
59 if(computing_offset_p)
61 cell size = array_capacity(insns.untagged());
66 computing_offset_p = false;
68 else if(offset < size)
71 computing_offset_p = false;
77 code.append_byte_array(insns.value());
80 void jit::emit_with_literal(cell code_template_, cell argument_) {
81 data_root<array> code_template(code_template_,parent);
82 data_root<object> argument(argument_,parent);
83 literal(argument.value());
84 emit(code_template.value());
87 void jit::emit_with_parameter(cell code_template_, cell argument_) {
88 data_root<array> code_template(code_template_,parent);
89 data_root<object> argument(argument_,parent);
90 parameter(argument.value());
91 emit(code_template.value());
94 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
96 data_root<word> word(word_,parent);
97 data_root<array> code_template(word->subprimitive,parent);
98 parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
99 literals.append(untag<array>(array_nth(code_template.untagged(),1)));
100 emit(array_nth(code_template.untagged(),2));
101 if(array_capacity(code_template.untagged()) == 5)
105 if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
106 emit(array_nth(code_template.untagged(),4));
110 emit(array_nth(code_template.untagged(),3));
115 /* Facility to convert compiled code offsets to quotation offsets.
116 Call jit_compute_offset() with the compiled code offset, then emit
117 code, and at the end jit->position is the quotation position. */
118 void jit::compute_position(cell offset_)
120 computing_offset_p = true;
125 /* Allocates memory */
126 code_block *jit::to_code_block()
128 /* Emit dummy GC info */
129 code.grow_bytes(alignment_for(code.count + 4,data_alignment));
130 u32 dummy_gc_info = 0;
131 code.append_bytes(&dummy_gc_info,sizeof(u32));
138 return parent->add_code_block(
140 code.elements.value(),
141 false_object, /* no labels */
143 relocation.elements.value(),
144 parameters.elements.value(),
145 literals.elements.value());