6 /* Simple code generator used by:
7 - profiler (profiler.cpp),
8 - quotation compiler (quotations.cpp),
9 - megamorphic caches (dispatch.cpp),
10 - polymorphic inline caches (inline_cache.cpp) */
12 /* Allocates memory */
13 jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
20 computing_offset_p(false),
26 void jit::emit_relocation(cell code_template_)
28 data_root<array> code_template(code_template_,parent);
29 cell capacity = array_capacity(code_template.untagged());
30 for(cell i = 1; i < capacity; i += 3)
32 relocation_class rel_class = (relocation_class)untag_fixnum(array_nth(code_template.untagged(),i));
33 relocation_type rel_type = (relocation_type)untag_fixnum(array_nth(code_template.untagged(),i + 1));
34 cell offset = array_nth(code_template.untagged(),i + 2);
36 relocation_entry new_entry(rel_type,rel_class,code.count + untag_fixnum(offset));
37 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
41 /* Allocates memory */
42 void jit::emit(cell code_template_)
44 data_root<array> code_template(code_template_,parent);
46 emit_relocation(code_template.value());
48 data_root<byte_array> insns(array_nth(code_template.untagged(),0),parent);
50 if(computing_offset_p)
52 cell size = array_capacity(insns.untagged());
57 computing_offset_p = false;
59 else if(offset < size)
62 computing_offset_p = false;
68 code.append_byte_array(insns.value());
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 void jit::emit_with_parameter(cell code_template_, cell argument_) {
79 data_root<array> code_template(code_template_,parent);
80 data_root<object> argument(argument_,parent);
81 parameter(argument.value());
82 emit(code_template.value());
85 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
87 data_root<word> word(word_,parent);
88 data_root<array> code_template(word->subprimitive,parent);
89 parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
90 literals.append(untag<array>(array_nth(code_template.untagged(),1)));
91 emit(array_nth(code_template.untagged(),2));
92 if(array_capacity(code_template.untagged()) == 5)
96 if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
97 emit(array_nth(code_template.untagged(),4));
101 emit(array_nth(code_template.untagged(),3));
106 void jit::emit_class_lookup(fixnum index, cell type)
108 emit_with_literal(parent->special_objects[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
109 emit(parent->special_objects[type]);
112 /* Facility to convert compiled code offsets to quotation offsets.
113 Call jit_compute_offset() with the compiled code offset, then emit
114 code, and at the end jit->position is the quotation position. */
115 void jit::compute_position(cell offset_)
117 computing_offset_p = true;
122 /* Allocates memory */
123 code_block *jit::to_code_block()
130 return parent->add_code_block(
132 code.elements.value(),
133 false_object, /* no labels */
135 relocation.elements.value(),
136 parameters.elements.value(),
137 literals.elements.value());