Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 151 lines (131 sloc) 3.802 kb
7274aff Porting VM to C++
Slava Pestov authored
1 #include "master.hpp"
2
7f48eb2 Use C++ namespaces
Slava Pestov authored
3 namespace factor
4 {
5
7274aff Porting VM to C++
Slava Pestov authored
6 /* Simple code generator used by:
d44957b Cleaning up VM code
Slava Pestov authored
7 - quotation compiler (quotations.cpp),
8 - megamorphic caches (dispatch.cpp),
9 - polymorphic inline caches (inline_cache.cpp) */
7274aff Porting VM to C++
Slava Pestov authored
10
11 /* Allocates memory */
f9fc348 vm: change code heap layout somewhat, remove unused allocation bitmap…
Slava Pestov authored
12 jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
d44957b Cleaning up VM code
Slava Pestov authored
13 : type(type_),
ebbf215 @phildawes added vm member to gc_root and growable arrays
phildawes authored
14 owner(owner_,vm),
15 code(vm),
16 relocation(vm),
912b126 Split literal table into literal and parameter tables, literal table …
Slava Pestov authored
17 parameters(vm),
ebbf215 @phildawes added vm member to gc_root and growable arrays
phildawes authored
18 literals(vm),
d44957b Cleaning up VM code
Slava Pestov authored
19 computing_offset_p(false),
20 position(0),
9282824 @phildawes added vm member to jit classes
phildawes authored
21 offset(0),
89612b7 vm: rename myvm and parent_vm instance variables to parent, clean up …
Slava Pestov authored
22 parent(vm)
6b2a34f @jckarter vm: count samples during unoptimized compiler
jckarter authored
23 {
9c05513 @jckarter vm: switch atomic::add to atomic::fetch_add
jckarter authored
24 fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
00f0066 @jckarter vm: replace assert with FACTOR_ASSERT
jckarter authored
25 FACTOR_ASSERT(old_count >= 0);
d031cfb @jckarter vm: quell warnings from vars only used by asserts
jckarter authored
26 (void)old_count;
6b2a34f @jckarter vm: count samples during unoptimized compiler
jckarter authored
27 }
28
29 jit::~jit()
30 {
9c05513 @jckarter vm: switch atomic::add to atomic::fetch_add
jckarter authored
31 fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
00f0066 @jckarter vm: replace assert with FACTOR_ASSERT
jckarter authored
32 FACTOR_ASSERT(old_count >= 1);
d031cfb @jckarter vm: quell warnings from vars only used by asserts
jckarter authored
33 (void)old_count;
6b2a34f @jckarter vm: count samples during unoptimized compiler
jckarter authored
34 }
7274aff Porting VM to C++
Slava Pestov authored
35
8336d89 Rework non-optimizing compiler backend to use compiler.codegen.reloca…
Slava Pestov authored
36 void jit::emit_relocation(cell relocation_template_)
7274aff Porting VM to C++
Slava Pestov authored
37 {
8336d89 Rework non-optimizing compiler backend to use compiler.codegen.reloca…
Slava Pestov authored
38 data_root<byte_array> relocation_template(relocation_template_,parent);
39 cell capacity = array_capacity(relocation_template.untagged())
40 / sizeof(relocation_entry);
41 relocation_entry *relocations = relocation_template->data<relocation_entry>();
42 for(cell i = 0; i < capacity; i++)
7274aff Porting VM to C++
Slava Pestov authored
43 {
8336d89 Rework non-optimizing compiler backend to use compiler.codegen.reloca…
Slava Pestov authored
44 relocation_entry entry = relocations[i];
45 relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
46 entry.rel_offset() + code.count);
c1e25f3 JIT now supports multiple relocations per code template. This simplif…
Slava Pestov authored
47 relocation.append_bytes(&new_entry,sizeof(relocation_entry));
7274aff Porting VM to C++
Slava Pestov authored
48 }
49 }
50
51 /* Allocates memory */
7c0a59f The great type renaming
Slava Pestov authored
52 void jit::emit(cell code_template_)
7274aff Porting VM to C++
Slava Pestov authored
53 {
b341527 vm: rename gc_root to data_root, add code_root to fix a problem where…
Slava Pestov authored
54 data_root<array> code_template(code_template_,parent);
7274aff Porting VM to C++
Slava Pestov authored
55
8336d89 Rework non-optimizing compiler backend to use compiler.codegen.reloca…
Slava Pestov authored
56 emit_relocation(array_nth(code_template.untagged(),0));
7274aff Porting VM to C++
Slava Pestov authored
57
8336d89 Rework non-optimizing compiler backend to use compiler.codegen.reloca…
Slava Pestov authored
58 data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
7274aff Porting VM to C++
Slava Pestov authored
59
d44957b Cleaning up VM code
Slava Pestov authored
60 if(computing_offset_p)
7274aff Porting VM to C++
Slava Pestov authored
61 {
7c0a59f The great type renaming
Slava Pestov authored
62 cell size = array_capacity(insns.untagged());
7274aff Porting VM to C++
Slava Pestov authored
63
d44957b Cleaning up VM code
Slava Pestov authored
64 if(offset == 0)
7274aff Porting VM to C++
Slava Pestov authored
65 {
d44957b Cleaning up VM code
Slava Pestov authored
66 position--;
67 computing_offset_p = false;
7274aff Porting VM to C++
Slava Pestov authored
68 }
d44957b Cleaning up VM code
Slava Pestov authored
69 else if(offset < size)
7274aff Porting VM to C++
Slava Pestov authored
70 {
d44957b Cleaning up VM code
Slava Pestov authored
71 position++;
72 computing_offset_p = false;
7274aff Porting VM to C++
Slava Pestov authored
73 }
74 else
d44957b Cleaning up VM code
Slava Pestov authored
75 offset -= size;
7274aff Porting VM to C++
Slava Pestov authored
76 }
77
d44957b Cleaning up VM code
Slava Pestov authored
78 code.append_byte_array(insns.value());
79 }
80
912b126 Split literal table into literal and parameter tables, literal table …
Slava Pestov authored
81 void jit::emit_with_literal(cell code_template_, cell argument_) {
b341527 vm: rename gc_root to data_root, add code_root to fix a problem where…
Slava Pestov authored
82 data_root<array> code_template(code_template_,parent);
83 data_root<object> argument(argument_,parent);
d44957b Cleaning up VM code
Slava Pestov authored
84 literal(argument.value());
85 emit(code_template.value());
86 }
87
912b126 Split literal table into literal and parameter tables, literal table …
Slava Pestov authored
88 void jit::emit_with_parameter(cell code_template_, cell argument_) {
89 data_root<array> code_template(code_template_,parent);
90 data_root<object> argument(argument_,parent);
91 parameter(argument.value());
92 emit(code_template.value());
93 }
94
31a260b vm: big overhaul of non-optimizing compiler
Slava Pestov authored
95 bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
96 {
97 data_root<word> word(word_,parent);
98 data_root<array> code_template(word->subprimitive,parent);
99 parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
100 literals.append(untag<array>(array_nth(code_template.untagged(),1)));
101 emit(array_nth(code_template.untagged(),2));
102 if(array_capacity(code_template.untagged()) == 5)
103 {
104 if(tail_call_p)
105 {
106 if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
107 emit(array_nth(code_template.untagged(),4));
108 return true;
109 }
110 else
111 emit(array_nth(code_template.untagged(),3));
112 }
113 return false;
114 }
115
d44957b Cleaning up VM code
Slava Pestov authored
116 /* Facility to convert compiled code offsets to quotation offsets.
117 Call jit_compute_offset() with the compiled code offset, then emit
118 code, and at the end jit->position is the quotation position. */
7c0a59f The great type renaming
Slava Pestov authored
119 void jit::compute_position(cell offset_)
d44957b Cleaning up VM code
Slava Pestov authored
120 {
121 computing_offset_p = true;
122 position = 0;
123 offset = offset_;
124 }
7274aff Porting VM to C++
Slava Pestov authored
125
d44957b Cleaning up VM code
Slava Pestov authored
126 /* Allocates memory */
8981fcc @jckarter vm: frame size for signal handler code blocks
jckarter authored
127 code_block *jit::to_code_block(cell frame_size)
d44957b Cleaning up VM code
Slava Pestov authored
128 {
a08295d GC maps for more compact inline GC checks
Slava Pestov authored
129 /* Emit dummy GC info */
130 code.grow_bytes(alignment_for(code.count + 4,data_alignment));
131 u32 dummy_gc_info = 0;
132 code.append_bytes(&dummy_gc_info,sizeof(u32));
133
d44957b Cleaning up VM code
Slava Pestov authored
134 code.trim();
135 relocation.trim();
912b126 Split literal table into literal and parameter tables, literal table …
Slava Pestov authored
136 parameters.trim();
d44957b Cleaning up VM code
Slava Pestov authored
137 literals.trim();
138
89612b7 vm: rename myvm and parent_vm instance variables to parent, clean up …
Slava Pestov authored
139 return parent->add_code_block(
d44957b Cleaning up VM code
Slava Pestov authored
140 type,
7c0a59f The great type renaming
Slava Pestov authored
141 code.elements.value(),
f0a1a3c vm: rename F to false_object, and rename T to true_object
Slava Pestov authored
142 false_object, /* no labels */
4d0ec09 vm: put code block owner directly in the header, instead of as the fi…
Slava Pestov authored
143 owner.value(),
7c0a59f The great type renaming
Slava Pestov authored
144 relocation.elements.value(),
912b126 Split literal table into literal and parameter tables, literal table …
Slava Pestov authored
145 parameters.elements.value(),
9186a1b @jckarter vm: store stack frame size in code blocks
jckarter authored
146 literals.elements.value(),
8981fcc @jckarter vm: frame size for signal handler code blocks
jckarter authored
147 frame_size);
7274aff Porting VM to C++
Slava Pestov authored
148 }
149
7f48eb2 Use C++ namespaces
Slava Pestov authored
150 }
Something went wrong with that request. Please try again.