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 /* Allocates memory */
82 void jit::emit_with_literal(cell code_template_, cell argument_) {
83 data_root<array> code_template(code_template_,parent);
84 data_root<object> argument(argument_,parent);
85 literal(argument.value());
86 emit(code_template.value());
89 /* Allocates memory */
90 void jit::emit_with_parameter(cell code_template_, cell argument_) {
91 data_root<array> code_template(code_template_,parent);
92 data_root<object> argument(argument_,parent);
93 parameter(argument.value());
94 emit(code_template.value());
97 /* Allocates memory */
98 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
100 data_root<word> word(word_,parent);
101 data_root<array> code_template(word->subprimitive,parent);
102 parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
103 literals.append(untag<array>(array_nth(code_template.untagged(),1)));
104 emit(array_nth(code_template.untagged(),2));
106 if(array_capacity(code_template.untagged()) == 5)
110 if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
111 emit(array_nth(code_template.untagged(),4));
115 emit(array_nth(code_template.untagged(),3));
120 /* Facility to convert compiled code offsets to quotation offsets.
121 Call jit_compute_offset() with the compiled code offset, then emit
122 code, and at the end jit->position is the quotation position. */
123 void jit::compute_position(cell offset_)
125 computing_offset_p = true;
130 /* Allocates memory */
131 code_block *jit::to_code_block(cell frame_size)
133 /* Emit dummy GC info */
134 code.grow_bytes(alignment_for(code.count + 4,data_alignment));
135 u32 dummy_gc_info = 0;
136 code.append_bytes(&dummy_gc_info,sizeof(u32));
143 return parent->add_code_block(
145 code.elements.value(),
146 false_object, /* no labels */
148 relocation.elements.value(),
149 parameters.elements.value(),
150 literals.elements.value(),