]> gitweb.factorcode.org Git - factor.git/blob - vm/jit.cpp
615601ecf64ed566d6abcb6412ed2cc1a1d47cde
[factor.git] / vm / jit.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
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) */
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
26 void jit::emit_relocation(cell relocation_template_)
27 {
28         data_root<byte_array> relocation_template(relocation_template_,parent);
29         cell capacity = array_capacity(relocation_template.untagged())
30                 / sizeof(relocation_entry);
31         relocation_entry *relocations = relocation_template->data<relocation_entry>();
32         for(cell i = 0; i < capacity; i++)
33         {
34                 relocation_entry entry = relocations[i];
35                 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
36                         entry.rel_offset() + code.count);
37                 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
38         }
39 }
40
41 /* Allocates memory */
42 void jit::emit(cell code_template_)
43 {
44         data_root<array> code_template(code_template_,parent);
45
46         emit_relocation(array_nth(code_template.untagged(),0));
47
48         data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
49
50         if(computing_offset_p)
51         {
52                 cell size = array_capacity(insns.untagged());
53
54                 if(offset == 0)
55                 {
56                         position--;
57                         computing_offset_p = false;
58                 }
59                 else if(offset < size)
60                 {
61                         position++;
62                         computing_offset_p = false;
63                 }
64                 else
65                         offset -= size;
66         }
67
68         code.append_byte_array(insns.value());
69 }
70
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());
76 }
77
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());
83 }
84
85 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
86 {
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)
93         {
94                 if(tail_call_p)
95                 {
96                         if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
97                         emit(array_nth(code_template.untagged(),4));
98                         return true;
99                 }
100                 else
101                         emit(array_nth(code_template.untagged(),3));
102         }
103         return false;
104 }
105         
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 {
111         computing_offset_p = true;
112         position = 0;
113         offset = offset_;
114 }
115
116 /* Allocates memory */
117 code_block *jit::to_code_block()
118 {
119         /* Emit dummy GC info */
120         code.grow_bytes(alignment_for(code.count + 4,data_alignment));
121         u32 dummy_gc_info = 0;
122         code.append_bytes(&dummy_gc_info,sizeof(u32));
123
124         code.trim();
125         relocation.trim();
126         parameters.trim();
127         literals.trim();
128
129         return parent->add_code_block(
130                 type,
131                 code.elements.value(),
132                 false_object, /* no labels */
133                 owner.value(),
134                 relocation.elements.value(),
135                 parameters.elements.value(),
136                 literals.elements.value());
137 }
138
139 }