]> gitweb.factorcode.org Git - factor.git/blob - vm/jit.cpp
Merge branch 'master' of git://factorcode.org/git/factor
[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(cell type_, cell owner_)
14         : type(type_),
15           owner(owner_),
16           code(),
17           relocation(),
18           literals(),
19           computing_offset_p(false),
20           position(0),
21           offset(0)
22 {
23         if(stack_traces_p()) literal(owner.value());
24 }
25
26 void jit::emit_relocation(cell code_template_)
27 {
28         gc_root<array> code_template(code_template_);
29         cell capacity = array_capacity(code_template.untagged());
30         for(cell i = 1; i < capacity; i += 3)
31         {
32                 cell rel_class = array_nth(code_template.untagged(),i);
33                 cell rel_type = array_nth(code_template.untagged(),i + 1);
34                 cell offset = array_nth(code_template.untagged(),i + 2);
35
36                 relocation_entry new_entry
37                         = (untag_fixnum(rel_type) << 28)
38                         | (untag_fixnum(rel_class) << 24)
39                         | ((code.count + untag_fixnum(offset)));
40                 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
41         }
42 }
43
44 /* Allocates memory */
45 void jit::emit(cell code_template_)
46 {
47         gc_root<array> code_template(code_template_);
48
49         emit_relocation(code_template.value());
50
51         gc_root<byte_array> insns(array_nth(code_template.untagged(),0));
52
53         if(computing_offset_p)
54         {
55                 cell size = array_capacity(insns.untagged());
56
57                 if(offset == 0)
58                 {
59                         position--;
60                         computing_offset_p = false;
61                 }
62                 else if(offset < size)
63                 {
64                         position++;
65                         computing_offset_p = false;
66                 }
67                 else
68                         offset -= size;
69         }
70
71         code.append_byte_array(insns.value());
72 }
73
74 void jit::emit_with(cell code_template_, cell argument_) {
75         gc_root<array> code_template(code_template_);
76         gc_root<object> argument(argument_);
77         literal(argument.value());
78         emit(code_template.value());
79 }
80
81 void jit::emit_class_lookup(fixnum index, cell type)
82 {
83         emit_with(userenv[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
84         emit(userenv[type]);
85 }
86
87 /* Facility to convert compiled code offsets to quotation offsets.
88 Call jit_compute_offset() with the compiled code offset, then emit
89 code, and at the end jit->position is the quotation position. */
90 void jit::compute_position(cell offset_)
91 {
92         computing_offset_p = true;
93         position = 0;
94         offset = offset_;
95 }
96
97 /* Allocates memory */
98 code_block *jit::to_code_block()
99 {
100         code.trim();
101         relocation.trim();
102         literals.trim();
103
104         return add_code_block(
105                 type,
106                 code.elements.value(),
107                 F, /* no labels */
108                 relocation.elements.value(),
109                 literals.elements.value());
110 }
111
112 }