6 /* Simple code generator used by:
7 - quotation compiler (quotations.cpp),
8 - megamorphic caches (dispatch.cpp),
9 - polymorphic inline caches (inline_cache.cpp) */
11 /* Allocates memory */
12 jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
19 computing_offset_p(false),
24 fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
25 assert(old_count >= 0);
30 fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
31 assert(old_count >= 1);
34 void jit::emit_relocation(cell relocation_template_)
36 data_root<byte_array> relocation_template(relocation_template_,parent);
37 cell capacity = array_capacity(relocation_template.untagged())
38 / sizeof(relocation_entry);
39 relocation_entry *relocations = relocation_template->data<relocation_entry>();
40 for(cell i = 0; i < capacity; i++)
42 relocation_entry entry = relocations[i];
43 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
44 entry.rel_offset() + code.count);
45 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
49 /* Allocates memory */
50 void jit::emit(cell code_template_)
52 data_root<array> code_template(code_template_,parent);
54 emit_relocation(array_nth(code_template.untagged(),0));
56 data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
58 if(computing_offset_p)
60 cell size = array_capacity(insns.untagged());
65 computing_offset_p = false;
67 else if(offset < size)
70 computing_offset_p = false;
76 code.append_byte_array(insns.value());
79 void jit::emit_with_literal(cell code_template_, cell argument_) {
80 data_root<array> code_template(code_template_,parent);
81 data_root<object> argument(argument_,parent);
82 literal(argument.value());
83 emit(code_template.value());
86 void jit::emit_with_parameter(cell code_template_, cell argument_) {
87 data_root<array> code_template(code_template_,parent);
88 data_root<object> argument(argument_,parent);
89 parameter(argument.value());
90 emit(code_template.value());
93 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
95 data_root<word> word(word_,parent);
96 data_root<array> code_template(word->subprimitive,parent);
97 parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
98 literals.append(untag<array>(array_nth(code_template.untagged(),1)));
99 emit(array_nth(code_template.untagged(),2));
100 if(array_capacity(code_template.untagged()) == 5)
104 if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
105 emit(array_nth(code_template.untagged(),4));
109 emit(array_nth(code_template.untagged(),3));
114 /* Facility to convert compiled code offsets to quotation offsets.
115 Call jit_compute_offset() with the compiled code offset, then emit
116 code, and at the end jit->position is the quotation position. */
117 void jit::compute_position(cell offset_)
119 computing_offset_p = true;
124 /* Allocates memory */
125 code_block *jit::to_code_block()
127 /* Emit dummy GC info */
128 code.grow_bytes(alignment_for(code.count + 4,data_alignment));
129 u32 dummy_gc_info = 0;
130 code.append_bytes(&dummy_gc_info,sizeof(u32));
137 return parent->add_code_block(
139 code.elements.value(),
140 false_object, /* no labels */
142 relocation.elements.value(),
143 parameters.elements.value(),
144 literals.elements.value());