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 */
11 jit::jit(code_block_type type_, cell owner_, factor_vm* vm)
18 computing_offset_p(false),
22 fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
23 FACTOR_ASSERT(old_count >= 0);
28 fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
29 FACTOR_ASSERT(old_count >= 1);
33 void jit::emit_relocation(cell relocation_template_) {
34 data_root<byte_array> relocation_template(relocation_template_, parent);
36 array_capacity(relocation_template.untagged()) / sizeof(relocation_entry);
37 relocation_entry* relocations = relocation_template->data<relocation_entry>();
38 for (cell i = 0; i < capacity; i++) {
39 relocation_entry entry = relocations[i];
40 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
41 entry.rel_offset() + code.count);
42 relocation.append_bytes(&new_entry, sizeof(relocation_entry));
46 /* Allocates memory */
47 void jit::emit(cell code_template_) {
48 data_root<array> code_template(code_template_, parent);
50 emit_relocation(array_nth(code_template.untagged(), 0));
52 data_root<byte_array> insns(array_nth(code_template.untagged(), 1), parent);
54 if (computing_offset_p) {
55 cell size = array_capacity(insns.untagged());
59 computing_offset_p = false;
60 } else if (offset < size) {
62 computing_offset_p = false;
67 code.append_byte_array(insns.value());
70 /* Allocates memory */
71 void jit::emit_with_literal(cell code_template_, cell argument_) {
72 data_root<array> code_template(code_template_, parent);
73 data_root<object> argument(argument_, parent);
74 literal(argument.value());
75 emit(code_template.value());
78 /* Allocates memory */
79 void jit::emit_with_parameter(cell code_template_, cell argument_) {
80 data_root<array> code_template(code_template_, parent);
81 data_root<object> argument(argument_, parent);
82 parameter(argument.value());
83 emit(code_template.value());
86 /* Allocates memory */
87 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p) {
88 data_root<word> word(word_, parent);
89 data_root<array> code_template(word->subprimitive, parent);
90 parameters.append(untag<array>(array_nth(code_template.untagged(), 0)));
91 literals.append(untag<array>(array_nth(code_template.untagged(), 1)));
92 emit(array_nth(code_template.untagged(), 2));
94 if (array_capacity(code_template.untagged()) == 5) {
97 emit(parent->special_objects[JIT_EPILOG]);
98 emit(array_nth(code_template.untagged(), 4));
101 emit(array_nth(code_template.untagged(), 3));
106 /* Facility to convert compiled code offsets to quotation offsets.
107 Call jit_compute_offset() with the compiled code offset, then emit
108 code, and at the end jit->position is the quotation position. */
109 void jit::compute_position(cell offset_) {
110 computing_offset_p = true;
115 /* Allocates memory */
116 code_block* jit::to_code_block(cell frame_size) {
117 /* Emit dummy GC info */
118 code.grow_bytes(alignment_for(code.count + 4, data_alignment));
119 u32 dummy_gc_info = 0;
120 code.append_bytes(&dummy_gc_info, sizeof(u32));
127 return parent->add_code_block(
128 type, code.elements.value(), false_object, /* no labels */
129 owner.value(), relocation.elements.value(), parameters.elements.value(),
130 literals.elements.value(), frame_size);