]> 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(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 code_template_)
27 {
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)
31         {
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);
35
36                 relocation_entry new_entry(rel_type,rel_class,code.count + untag_fixnum(offset));
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(code_template.value());
47
48         data_root<byte_array> insns(array_nth(code_template.untagged(),0),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 void jit::emit_class_lookup(fixnum index, cell type)
86 {
87         emit_with_literal(parent->special_objects[PIC_LOAD],tag_fixnum(-index * sizeof(cell)));
88         emit(parent->special_objects[type]);
89 }
90
91 /* Facility to convert compiled code offsets to quotation offsets.
92 Call jit_compute_offset() with the compiled code offset, then emit
93 code, and at the end jit->position is the quotation position. */
94 void jit::compute_position(cell offset_)
95 {
96         computing_offset_p = true;
97         position = 0;
98         offset = offset_;
99 }
100
101 /* Allocates memory */
102 code_block *jit::to_code_block()
103 {
104         code.trim();
105         relocation.trim();
106         parameters.trim();
107         literals.trim();
108
109         return parent->add_code_block(
110                 type,
111                 code.elements.value(),
112                 false_object, /* no labels */
113                 owner.value(),
114                 relocation.elements.value(),
115                 parameters.elements.value(),
116                 literals.elements.value());
117 }
118
119 }