]> gitweb.factorcode.org Git - factor.git/blob - vm/jit.cpp
vm: replace block comments /**/ with line comments //
[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 (`code` and `relocation` initializers create
11 // growable_byte_array)
12 jit::jit(code_block_type type, cell owner, factor_vm* vm)
13     : type(type),
14       owner(owner, vm),
15       code(vm),
16       relocation(vm),
17       parameters(vm),
18       literals(vm),
19       computing_offset_p(false),
20       position(0),
21       offset(0),
22       parent(vm) {
23   fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
24   FACTOR_ASSERT(old_count >= 0);
25   (void)old_count;
26 }
27
28 jit::~jit() {
29   fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
30   FACTOR_ASSERT(old_count >= 1);
31   (void)old_count;
32 }
33
34 // Allocates memory
35 void jit::emit_relocation(cell relocation_template_) {
36   data_root<byte_array> relocation_template(relocation_template_, parent);
37   cell capacity =
38       array_capacity(relocation_template.untagged()) / sizeof(relocation_entry);
39   relocation_entry* relocations = relocation_template->data<relocation_entry>();
40   for (cell i = 0; i < capacity; i++) {
41     relocation_entry entry = relocations[i];
42     relocation_entry new_entry(entry.type(), entry.klass(),
43                                entry.offset() + code.count);
44     relocation.append_bytes(&new_entry, sizeof(relocation_entry));
45   }
46 }
47
48 // Allocates memory
49 void jit::emit(cell code_template_) {
50   data_root<array> code_template(code_template_, parent);
51
52   emit_relocation(array_nth(code_template.untagged(), 0));
53
54   data_root<byte_array> insns(array_nth(code_template.untagged(), 1), parent);
55
56   if (computing_offset_p) {
57     cell size = array_capacity(insns.untagged());
58
59     if (offset == 0) {
60       position--;
61       computing_offset_p = false;
62     } else if (offset < size) {
63       position++;
64       computing_offset_p = false;
65     } else
66       offset -= size;
67   }
68
69   code.append_byte_array(insns.value());
70 }
71
72 // Allocates memory
73 void jit::emit_with_literal(cell code_template_, cell argument_) {
74   data_root<array> code_template(code_template_, parent);
75   data_root<object> argument(argument_, parent);
76   literal(argument.value());
77   emit(code_template.value());
78 }
79
80 // Allocates memory
81 void jit::emit_with_parameter(cell code_template_, cell argument_) {
82   data_root<array> code_template(code_template_, parent);
83   data_root<object> argument(argument_, parent);
84   parameter(argument.value());
85   emit(code_template.value());
86 }
87
88 // Allocates memory
89 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p) {
90   data_root<word> word(word_, parent);
91   data_root<array> code_template(word->subprimitive, parent);
92   parameters.append(untag<array>(array_nth(code_template.untagged(), 0)));
93   literals.append(untag<array>(array_nth(code_template.untagged(), 1)));
94   emit(array_nth(code_template.untagged(), 2));
95
96   if (array_capacity(code_template.untagged()) == 5) {
97     if (tail_call_p) {
98       if (stack_frame_p)
99         emit(parent->special_objects[JIT_EPILOG]);
100       emit(array_nth(code_template.untagged(), 4));
101       return true;
102     } else
103       emit(array_nth(code_template.untagged(), 3));
104   }
105   return false;
106 }
107
108 // Facility to convert compiled code offsets to quotation offsets.
109 // Call jit_compute_offset() with the compiled code offset, then emit
110 // code, and at the end jit->position is the quotation position.
111 void jit::compute_position(cell offset_) {
112   computing_offset_p = true;
113   position = 0;
114   offset = offset_;
115 }
116
117 // Allocates memory (trim(), add_code_block)
118 code_block* jit::to_code_block(cell frame_size) {
119   // Emit dummy GC info
120   code.grow_bytes(alignment_for(code.count + 4, data_alignment));
121   uint32_t dummy_gc_info = 0;
122   code.append_bytes(&dummy_gc_info, sizeof(uint32_t));
123
124   code.trim();
125   relocation.trim();
126   parameters.trim();
127   literals.trim();
128
129   return parent->add_code_block(
130       type, code.elements.value(), false_object, // no labels
131       owner.value(), relocation.elements.value(), parameters.elements.value(),
132       literals.elements.value(), frame_size);
133 }
134
135 }