]> gitweb.factorcode.org Git - factor.git/blob - vm/jit.cpp
d31123a63ce94b76b6cc5faf3be61a9e70e8c7ff
[factor.git] / vm / jit.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 /* Simple code generator used by:
7 - counting_profiler (counting_profiler.cpp),
8 - quotation compiler (quotations.cpp),
9 - megamorphic caches (dispatch.cpp),
10 - polymorphic inline caches (inline_cache.cpp) */
11
12 /* Allocates memory */
13 jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
14         : type(type_),
15           owner(owner_,vm),
16           code(vm),
17           relocation(vm),
18           parameters(vm),
19           literals(vm),
20           computing_offset_p(false),
21           position(0),
22           offset(0),
23           parent(vm)
24 {
25         fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
26         assert(old_count >= 0);
27 }
28
29 jit::~jit()
30 {
31         fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
32         assert(old_count >= 1);
33 }
34
35 void jit::emit_relocation(cell relocation_template_)
36 {
37         data_root<byte_array> relocation_template(relocation_template_,parent);
38         cell capacity = array_capacity(relocation_template.untagged())
39                 / sizeof(relocation_entry);
40         relocation_entry *relocations = relocation_template->data<relocation_entry>();
41         for(cell i = 0; i < capacity; i++)
42         {
43                 relocation_entry entry = relocations[i];
44                 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
45                         entry.rel_offset() + code.count);
46                 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
47         }
48 }
49
50 /* Allocates memory */
51 void jit::emit(cell code_template_)
52 {
53         data_root<array> code_template(code_template_,parent);
54
55         emit_relocation(array_nth(code_template.untagged(),0));
56
57         data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
58
59         if(computing_offset_p)
60         {
61                 cell size = array_capacity(insns.untagged());
62
63                 if(offset == 0)
64                 {
65                         position--;
66                         computing_offset_p = false;
67                 }
68                 else if(offset < size)
69                 {
70                         position++;
71                         computing_offset_p = false;
72                 }
73                 else
74                         offset -= size;
75         }
76
77         code.append_byte_array(insns.value());
78 }
79
80 void jit::emit_with_literal(cell code_template_, cell argument_) {
81         data_root<array> code_template(code_template_,parent);
82         data_root<object> argument(argument_,parent);
83         literal(argument.value());
84         emit(code_template.value());
85 }
86
87 void jit::emit_with_parameter(cell code_template_, cell argument_) {
88         data_root<array> code_template(code_template_,parent);
89         data_root<object> argument(argument_,parent);
90         parameter(argument.value());
91         emit(code_template.value());
92 }
93
94 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
95 {
96         data_root<word> word(word_,parent);
97         data_root<array> code_template(word->subprimitive,parent);
98         parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
99         literals.append(untag<array>(array_nth(code_template.untagged(),1)));
100         emit(array_nth(code_template.untagged(),2));
101         if(array_capacity(code_template.untagged()) == 5)
102         {
103                 if(tail_call_p)
104                 {
105                         if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
106                         emit(array_nth(code_template.untagged(),4));
107                         return true;
108                 }
109                 else
110                         emit(array_nth(code_template.untagged(),3));
111         }
112         return false;
113 }
114         
115 /* Facility to convert compiled code offsets to quotation offsets.
116 Call jit_compute_offset() with the compiled code offset, then emit
117 code, and at the end jit->position is the quotation position. */
118 void jit::compute_position(cell offset_)
119 {
120         computing_offset_p = true;
121         position = 0;
122         offset = offset_;
123 }
124
125 /* Allocates memory */
126 code_block *jit::to_code_block()
127 {
128         /* Emit dummy GC info */
129         code.grow_bytes(alignment_for(code.count + 4,data_alignment));
130         u32 dummy_gc_info = 0;
131         code.append_bytes(&dummy_gc_info,sizeof(u32));
132
133         code.trim();
134         relocation.trim();
135         parameters.trim();
136         literals.trim();
137
138         return parent->add_code_block(
139                 type,
140                 code.elements.value(),
141                 false_object, /* no labels */
142                 owner.value(),
143                 relocation.elements.value(),
144                 parameters.elements.value(),
145                 literals.elements.value());
146 }
147
148 }