File 1143-jit-Provide-the-BeamModuleAssembler-with-an-array-of.patch of Package erlang (Revision c2e36122ef81b9f452e25d60632772de)
Currently displaying revision c2e36122ef81b9f452e25d60632772de , Show latest
214
1
From 905f92b519a7075f5df751f5c13751d5111b75c7 Mon Sep 17 00:00:00 2001
2
From: Frej Drejhammar <frej.drejhammar@gmail.com>
3
Date: Thu, 17 Jun 2021 15:35:22 +0200
4
Subject: [PATCH 3/7] jit: Provide the BeamModuleAssembler with an array of
5
named labels
6
7
As the BEAM-file contains information about the name and arity of each
8
function, the information can be used to have asmjit pretty print the
9
labels starting the body of a function as name/arity.
10
11
This patch extends beam_load_prepare_emit() to retrieve all named
12
labels in the BEAM-file, sort them, and pass them on to the
13
assembler. Platform specific support will follow in subsequent
14
patches.
15
---
16
erts/emulator/beam/jit/arm/beam_asm.hpp | 6 ++-
17
.../emulator/beam/jit/arm/beam_asm_module.cpp | 8 ++--
18
erts/emulator/beam/jit/asm_load.c | 39 ++++++++++++++++++-
19
erts/emulator/beam/jit/beam_asm.h | 5 ++-
20
erts/emulator/beam/jit/beam_jit_main.cpp | 11 +++++-
21
erts/emulator/beam/jit/x86/beam_asm.hpp | 6 ++-
22
.../emulator/beam/jit/x86/beam_asm_module.cpp | 8 ++--
23
7 files changed, 69 insertions(+), 14 deletions(-)
24
25
diff --git a/erts/emulator/beam/jit/arm/beam_asm.hpp b/erts/emulator/beam/jit/arm/beam_asm.hpp
26
index c5f9c06358..a9b11bb94d 100644
27
--- a/erts/emulator/beam/jit/arm/beam_asm.hpp
28
+++ b/erts/emulator/beam/jit/arm/beam_asm.hpp
29
30
public:
31
BeamModuleAssembler(BeamGlobalAssembler *ga,
32
Eterm mod,
33
- unsigned num_labels);
34
+ unsigned num_labels,
35
+ BeamFile_ExportTable *named_labels = NULL);
36
BeamModuleAssembler(BeamGlobalAssembler *ga,
37
Eterm mod,
38
unsigned num_labels,
39
- unsigned num_functions);
40
+ unsigned num_functions,
41
+ BeamFile_ExportTable *named_labels = NULL);
42
43
bool emit(unsigned op, const Span<ArgVal> &args);
44
45
diff --git a/erts/emulator/beam/jit/arm/beam_asm_module.cpp b/erts/emulator/beam/jit/arm/beam_asm_module.cpp
46
index 6a57e4e7db..c0728c8f34 100644
47
--- a/erts/emulator/beam/jit/arm/beam_asm_module.cpp
48
+++ b/erts/emulator/beam/jit/arm/beam_asm_module.cpp
49
50
51
BeamModuleAssembler::BeamModuleAssembler(BeamGlobalAssembler *ga,
52
Eterm mod,
53
- unsigned num_labels)
54
+ unsigned num_labels,
55
+ BeamFile_ExportTable *named_labels)
56
: BeamAssembler(getAtom(mod)) {
57
this->ga = ga;
58
this->mod = mod;
59
60
BeamModuleAssembler::BeamModuleAssembler(BeamGlobalAssembler *ga,
61
Eterm mod,
62
unsigned num_labels,
63
- unsigned num_functions)
64
- : BeamModuleAssembler(ga, mod, num_labels) {
65
+ unsigned num_functions,
66
+ BeamFile_ExportTable *named_labels)
67
+ : BeamModuleAssembler(ga, mod, num_labels, named_labels) {
68
codeHeader = a.newLabel();
69
a.align(kAlignCode, 8);
70
a.bind(codeHeader);
71
diff --git a/erts/emulator/beam/jit/asm_load.c b/erts/emulator/beam/jit/asm_load.c
72
index 46ea3ba9b7..744fb9bcfe 100644
73
--- a/erts/emulator/beam/jit/asm_load.c
74
+++ b/erts/emulator/beam/jit/asm_load.c
75
76
77
static void init_label(Label *lp);
78
79
+static int named_labels_compare(BeamFile_ExportEntry *a,
80
+ BeamFile_ExportEntry *b) {
81
+ if (a->label < b->label)
82
+ return -1;
83
+ else if (a->label == b->label)
84
+ return 0;
85
+ else
86
+ return 1;
87
+}
88
+
89
int beam_load_prepare_emit(LoaderState *stp) {
90
BeamCodeHeader *hdr;
91
+ BeamFile_ExportTable *named_labels_ptr = NULL, named_labels;
92
int i;
93
94
+ if (erts_jit_asm_dump) {
95
+ /* Dig out all named labels from the BEAM-file and sort them on the
96
+ label id. */
97
+ named_labels.count = stp->beam.exports.count + stp->beam.locals.count;
98
+ named_labels.entries = erts_alloc(
99
+ ERTS_ALC_T_PREPARED_CODE,
100
+ named_labels.count * sizeof(named_labels.entries[0]));
101
+
102
+ for (unsigned i = 0; i < stp->beam.exports.count; i++)
103
+ memcpy(&named_labels.entries[i],
104
+ &stp->beam.exports.entries[i],
105
+ sizeof(stp->beam.exports.entries[i]));
106
+ for (unsigned i = 0; i < stp->beam.locals.count; i++)
107
+ memcpy(&named_labels.entries[i + stp->beam.exports.count],
108
+ &stp->beam.locals.entries[i],
109
+ sizeof(stp->beam.locals.entries[i]));
110
+
111
+ qsort(named_labels.entries,
112
+ named_labels.count,
113
+ sizeof(named_labels.entries[0]),
114
+ (int (*)(const void *, const void *))named_labels_compare);
115
+ named_labels_ptr = &named_labels;
116
+ }
117
stp->ba = beamasm_new_assembler(stp->module,
118
stp->beam.code.label_count,
119
- stp->beam.code.function_count);
120
+ stp->beam.code.function_count,
121
+ named_labels_ptr);
122
+ if (named_labels_ptr != NULL)
123
+ erts_free(ERTS_ALC_T_PREPARED_CODE, named_labels_ptr->entries);
124
125
/* Initialize code header */
126
stp->codev_size = stp->beam.code.function_count + 1;
127
diff --git a/erts/emulator/beam/jit/beam_asm.h b/erts/emulator/beam/jit/beam_asm.h
128
index 0b7260b063..98290657ff 100644
129
--- a/erts/emulator/beam/jit/beam_asm.h
130
+++ b/erts/emulator/beam/jit/beam_asm.h
131
132
133
void beamasm_init(void);
134
void beamasm_init_perf(void);
135
-void *beamasm_new_assembler(Eterm mod, int num_labels, int num_functions);
136
+void *beamasm_new_assembler(Eterm mod,
137
+ int num_labels,
138
+ int num_functions,
139
+ BeamFile_ExportTable *named_labels);
140
void beamasm_codegen(void *ba,
141
const void **native_module_exec,
142
void **native_module_rw,
143
diff --git a/erts/emulator/beam/jit/beam_jit_main.cpp b/erts/emulator/beam/jit/beam_jit_main.cpp
144
index 9c3a85f96b..b5a3d80052 100644
145
--- a/erts/emulator/beam/jit/beam_jit_main.cpp
146
+++ b/erts/emulator/beam/jit/beam_jit_main.cpp
147
148
#endif
149
}
150
151
- void *beamasm_new_assembler(Eterm mod, int num_labels, int num_functions) {
152
- return new BeamModuleAssembler(bga, mod, num_labels, num_functions);
153
+ void *beamasm_new_assembler(Eterm mod,
154
+ int num_labels,
155
+ int num_functions,
156
+ BeamFile_ExportTable *named_labels) {
157
+ return new BeamModuleAssembler(bga,
158
+ mod,
159
+ num_labels,
160
+ num_functions,
161
+ named_labels);
162
}
163
164
int beamasm_emit(void *instance, unsigned specific_op, BeamOp *op) {
165
diff --git a/erts/emulator/beam/jit/x86/beam_asm.hpp b/erts/emulator/beam/jit/x86/beam_asm.hpp
166
index e8f5db8106..5d4c817a09 100644
167
--- a/erts/emulator/beam/jit/x86/beam_asm.hpp
168
+++ b/erts/emulator/beam/jit/x86/beam_asm.hpp
169
170
public:
171
BeamModuleAssembler(BeamGlobalAssembler *ga,
172
Eterm mod,
173
- unsigned num_labels);
174
+ unsigned num_labels,
175
+ BeamFile_ExportTable *named_labels = NULL);
176
BeamModuleAssembler(BeamGlobalAssembler *ga,
177
Eterm mod,
178
unsigned num_labels,
179
- unsigned num_functions);
180
+ unsigned num_functions,
181
+ BeamFile_ExportTable *named_labels = NULL);
182
183
bool emit(unsigned op, const Span<ArgVal> &args);
184
185
diff --git a/erts/emulator/beam/jit/x86/beam_asm_module.cpp b/erts/emulator/beam/jit/x86/beam_asm_module.cpp
186
index a2548276be..6f062be001 100644
187
--- a/erts/emulator/beam/jit/x86/beam_asm_module.cpp
188
+++ b/erts/emulator/beam/jit/x86/beam_asm_module.cpp
189
190
191
BeamModuleAssembler::BeamModuleAssembler(BeamGlobalAssembler *ga,
192
Eterm mod,
193
- unsigned num_labels)
194
+ unsigned num_labels,
195
+ BeamFile_ExportTable *named_labels)
196
: BeamAssembler(getAtom(mod)) {
197
this->ga = ga;
198
this->mod = mod;
199
200
BeamModuleAssembler::BeamModuleAssembler(BeamGlobalAssembler *ga,
201
Eterm mod,
202
unsigned num_labels,
203
- unsigned num_functions)
204
- : BeamModuleAssembler(ga, mod, num_labels) {
205
+ unsigned num_functions,
206
+ BeamFile_ExportTable *named_labels)
207
+ : BeamModuleAssembler(ga, mod, num_labels, named_labels) {
208
codeHeader = a.newLabel();
209
a.align(kAlignCode, 8);
210
a.bind(codeHeader);
211
--
212
2.26.2
213
214