5 // Simple code generator used by:
6 // - quotation compiler (quotations.cpp),
7 // - megamorphic caches (dispatch.cpp),
8 // - polymorphic inline caches (inline_cache.cpp)
10 // Allocates memory (`code` and `relocation` initializers create
11 // growable_byte_array)
12 jit::jit(code_block_type type, cell owner, factor_vm* vm)
19 computing_offset_p(false),
23 fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
24 FACTOR_ASSERT(old_count >= 0);
29 fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
30 FACTOR_ASSERT(old_count >= 1);
35 void jit::emit_relocation(cell relocation_template_) {
36 data_root<byte_array> relocation_template(relocation_template_, parent);
38 array_capacity(relocation_template.untagged()) / sizeof(relocation_entry);
39 relocation_entry* relocations = relocation_template->data<relocation_entry>();
40 for (cell i = 0; i < capacity; i++) {
41 relocation_entry entry = relocations[i];
42 relocation_entry new_entry(entry.type(), entry.klass(),
43 entry.offset() + code.count);
44 relocation.append_bytes(&new_entry, sizeof(relocation_entry));
49 void jit::emit(cell code_template_) {
50 data_root<array> code_template(code_template_, parent);
52 emit_relocation(array_nth(code_template.untagged(), 0));
54 data_root<byte_array> insns(array_nth(code_template.untagged(), 1), parent);
56 if (computing_offset_p) {
57 cell size = array_capacity(insns.untagged());
61 computing_offset_p = false;
62 } else if (offset < size) {
64 computing_offset_p = false;
69 code.append_byte_array(insns.value());
73 void jit::emit_with_literal(cell code_template_, cell argument_) {
74 data_root<array> code_template(code_template_, parent);
75 data_root<object> argument(argument_, parent);
76 literal(argument.value());
77 emit(code_template.value());
81 void jit::emit_with_parameter(cell code_template_, cell argument_) {
82 data_root<array> code_template(code_template_, parent);
83 data_root<object> argument(argument_, parent);
84 parameter(argument.value());
85 emit(code_template.value());
89 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p) {
90 data_root<word> word(word_, parent);
91 data_root<array> code_template(word->subprimitive, parent);
92 parameters.append(untag<array>(array_nth(code_template.untagged(), 0)));
93 literals.append(untag<array>(array_nth(code_template.untagged(), 1)));
94 emit(array_nth(code_template.untagged(), 2));
96 if (array_capacity(code_template.untagged()) == 5) {
99 emit(parent->special_objects[JIT_EPILOG]);
100 emit(array_nth(code_template.untagged(), 4));
103 emit(array_nth(code_template.untagged(), 3));
108 // Facility to convert compiled code offsets to quotation offsets.
109 // Call jit_compute_offset() with the compiled code offset, then emit
110 // code, and at the end jit->position is the quotation position.
111 void jit::compute_position(cell offset_) {
112 computing_offset_p = true;
117 // Allocates memory (trim(), add_code_block)
118 code_block* jit::to_code_block(cell frame_size) {
119 // Emit dummy GC info
120 code.grow_bytes(alignment_for(code.count + 4, data_alignment));
121 uint32_t dummy_gc_info = 0;
122 code.append_bytes(&dummy_gc_info, sizeof(uint32_t));
129 return parent->add_code_block(
130 type, code.elements.value(), false_object, // no labels
131 owner.value(), relocation.elements.value(), parameters.elements.value(),
132 literals.elements.value(), frame_size);