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 FACTOR_ASSERT(old_count >= 0);
31 fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
32 FACTOR_ASSERT(old_count >= 1);
36 void jit::emit_relocation(cell relocation_template_)
38 data_root<byte_array> relocation_template(relocation_template_,parent);
39 cell capacity = array_capacity(relocation_template.untagged())
40 / sizeof(relocation_entry);
41 relocation_entry *relocations = relocation_template->data<relocation_entry>();
42 for(cell i = 0; i < capacity; i++)
44 relocation_entry entry = relocations[i];
45 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
46 entry.rel_offset() + code.count);
47 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
51 /* Allocates memory */
52 void jit::emit(cell code_template_)
54 data_root<array> code_template(code_template_,parent);
56 emit_relocation(array_nth(code_template.untagged(),0));
58 data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
60 if(computing_offset_p)
62 cell size = array_capacity(insns.untagged());
67 computing_offset_p = false;
69 else if(offset < size)
72 computing_offset_p = false;
78 code.append_byte_array(insns.value());
81 void jit::emit_with_literal(cell code_template_, cell argument_) {
82 data_root<array> code_template(code_template_,parent);
83 data_root<object> argument(argument_,parent);
84 literal(argument.value());
85 emit(code_template.value());
88 void jit::emit_with_parameter(cell code_template_, cell argument_) {
89 data_root<array> code_template(code_template_,parent);
90 data_root<object> argument(argument_,parent);
91 parameter(argument.value());
92 emit(code_template.value());
95 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
97 data_root<word> word(word_,parent);
98 data_root<array> code_template(word->subprimitive,parent);
99 parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
100 literals.append(untag<array>(array_nth(code_template.untagged(),1)));
101 emit(array_nth(code_template.untagged(),2));
102 if(array_capacity(code_template.untagged()) == 5)
106 if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
107 emit(array_nth(code_template.untagged(),4));
111 emit(array_nth(code_template.untagged(),3));
116 /* Facility to convert compiled code offsets to quotation offsets.
117 Call jit_compute_offset() with the compiled code offset, then emit
118 code, and at the end jit->position is the quotation position. */
119 void jit::compute_position(cell offset_)
121 computing_offset_p = true;
126 /* Allocates memory */
127 code_block *jit::to_code_block()
129 /* Emit dummy GC info */
130 code.grow_bytes(alignment_for(code.count + 4,data_alignment));
131 u32 dummy_gc_info = 0;
132 code.append_bytes(&dummy_gc_info,sizeof(u32));
139 return parent->add_code_block(
141 code.elements.value(),
142 false_object, /* no labels */
144 relocation.elements.value(),
145 parameters.elements.value(),
146 literals.elements.value(),