]> gitweb.factorcode.org Git - factor.git/blob - vm/jit.cpp
VM: Refactor jit.cpp/hpp to Factor style
[factor.git] / vm / jit.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 /* Simple code generator used by:
6 - quotation compiler (quotations.cpp),
7 - megamorphic caches (dispatch.cpp),
8 - polymorphic inline caches (inline_cache.cpp) */
9
10 /* Allocates memory */
11 jit::jit(code_block_type type_, cell owner_, factor_vm* vm)
12     : type(type_),
13       owner(owner_, vm),
14       code(vm),
15       relocation(vm),
16       parameters(vm),
17       literals(vm),
18       computing_offset_p(false),
19       position(0),
20       offset(0),
21       parent(vm) {
22   fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
23   FACTOR_ASSERT(old_count >= 0);
24   (void) old_count;
25 }
26
27 jit::~jit() {
28   fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
29   FACTOR_ASSERT(old_count >= 1);
30   (void) old_count;
31 }
32
33 void jit::emit_relocation(cell relocation_template_) {
34   data_root<byte_array> relocation_template(relocation_template_, parent);
35   cell capacity =
36       array_capacity(relocation_template.untagged()) / sizeof(relocation_entry);
37   relocation_entry* relocations = relocation_template->data<relocation_entry>();
38   for (cell i = 0; i < capacity; i++) {
39     relocation_entry entry = relocations[i];
40     relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
41                                entry.rel_offset() + code.count);
42     relocation.append_bytes(&new_entry, sizeof(relocation_entry));
43   }
44 }
45
46 /* Allocates memory */
47 void jit::emit(cell code_template_) {
48   data_root<array> code_template(code_template_, parent);
49
50   emit_relocation(array_nth(code_template.untagged(), 0));
51
52   data_root<byte_array> insns(array_nth(code_template.untagged(), 1), parent);
53
54   if (computing_offset_p) {
55     cell size = array_capacity(insns.untagged());
56
57     if (offset == 0) {
58       position--;
59       computing_offset_p = false;
60     } else if (offset < size) {
61       position++;
62       computing_offset_p = false;
63     } else
64       offset -= size;
65   }
66
67   code.append_byte_array(insns.value());
68 }
69
70 /* Allocates memory */
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 /* Allocates memory */
79 void jit::emit_with_parameter(cell code_template_, cell argument_) {
80   data_root<array> code_template(code_template_, parent);
81   data_root<object> argument(argument_, parent);
82   parameter(argument.value());
83   emit(code_template.value());
84 }
85
86 /* Allocates memory */
87 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p) {
88   data_root<word> word(word_, parent);
89   data_root<array> code_template(word->subprimitive, parent);
90   parameters.append(untag<array>(array_nth(code_template.untagged(), 0)));
91   literals.append(untag<array>(array_nth(code_template.untagged(), 1)));
92   emit(array_nth(code_template.untagged(), 2));
93
94   if (array_capacity(code_template.untagged()) == 5) {
95     if (tail_call_p) {
96       if (stack_frame_p)
97         emit(parent->special_objects[JIT_EPILOG]);
98       emit(array_nth(code_template.untagged(), 4));
99       return true;
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   computing_offset_p = true;
111   position = 0;
112   offset = offset_;
113 }
114
115 /* Allocates memory */
116 code_block* jit::to_code_block(cell frame_size) {
117   /* Emit dummy GC info */
118   code.grow_bytes(alignment_for(code.count + 4, data_alignment));
119   u32 dummy_gc_info = 0;
120   code.append_bytes(&dummy_gc_info, sizeof(u32));
121
122   code.trim();
123   relocation.trim();
124   parameters.trim();
125   literals.trim();
126
127   return parent->add_code_block(
128       type, code.elements.value(), false_object, /* no labels */
129       owner.value(), relocation.elements.value(), parameters.elements.value(),
130       literals.elements.value(), frame_size);
131 }
132
133 }