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