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