1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#include <stddef.h>
#include <berg/vm.h>
#define MATRIX_SIZE 400
static struct berg_func *compile_init(struct berg_vm *vm)
{
struct berg_gpr A = berg_gpr_tmp(0);
struct berg_gpr B = berg_gpr_tmp(1);
struct berg_gpr I = berg_gpr_tmp(2);
struct berg_gpr J = berg_gpr_tmp(3);
struct berg_gpr T = berg_gpr_tmp(4);
struct berg_operand operands[] = {
/** @todo pointer bounds in type signature? */
berg_operand_gpr(A, BERG_POINTER),
berg_operand_gpr(B, BERG_POINTER),
};
struct berg_func *f = create_berg_func(vm, BERG_VOID, 2, operands);
/* outer loop */
berg_movi(f, I, 0);
struct berg_label outer = berg_label(f);
/* stop if I >= MATRIX_SIZE */
berg_movi(f, T, MATRIX_SIZE);
struct berg_reloc outer_jmp = berg_bger(f, I, T);
/* inner loop */
berg_movi(f, J, 0);
struct berg_label inner = berg_label(f);
/* stop if J >= MATRIX_SIZE */
berg_movi(f, T, MATRIX_SIZE);
struct berg_reloc inner_jmp = berg_bger(f, J, T);
/* &[I][J] = 4 * (J + MATRIX_SIZE * I) */
berg_movi(f, T, MATRIX_SIZE);
berg_mulr(f, T, T, I);
berg_addr(f, T, T, J);
berg_lshi(f, T, T, 2);
/* A[I][J] = I */
berg_stxr_32(f, I, A, T);
/* B[I][J] = J */
berg_stxr_32(f, J, B, T);
berg_addi(f, J, J, 1);
berg_patch(f, berg_jmp(f), inner);
berg_patch(f, inner_jmp, berg_label(f));
berg_addi(f, I, I, 1);
berg_patch(f, berg_jmp(f), outer);
berg_patch(f, outer_jmp, berg_label(f));
berg_ret(f);
compile_berg_func(f);
return f;
}
static struct berg_func *compile_mult(struct berg_vm *vm)
{
/* interesting, not using anything with a register means it gets treated
* as a oneshot reg, which is an issue for arguments. */
struct berg_gpr A = berg_gpr_tmp(0);
struct berg_gpr B = berg_gpr_tmp(1);
struct berg_gpr C = berg_gpr_tmp(2);
struct berg_gpr I = berg_gpr_tmp(3);
struct berg_gpr J = berg_gpr_tmp(4);
struct berg_gpr K = berg_gpr_tmp(5);
struct berg_gpr R = berg_gpr_tmp(6);
struct berg_gpr T = berg_gpr_tmp(7);
struct berg_gpr AT = berg_gpr_tmp(8);
struct berg_gpr BT = berg_gpr_tmp(9);
struct berg_operand operands[] = {
berg_operand_gpr(A, BERG_POINTER),
berg_operand_gpr(B, BERG_POINTER),
berg_operand_gpr(C, BERG_POINTER),
};
struct berg_func *f = create_berg_func(vm, BERG_VOID, 3, operands);
/* outer loop */
berg_movi(f, I, 0);
struct berg_label outer = berg_label(f);
/* stop if I >= MATRIX_SIZE */
berg_movi(f, T, MATRIX_SIZE);
struct berg_reloc outer_jmp = berg_bger(f, I, T);
/* inner loop */
berg_movi(f, J, 0);
struct berg_label inner = berg_label(f);
/* stop if J >= MATRIX_SIZE */
berg_movi(f, T, MATRIX_SIZE);
struct berg_reloc inner_jmp = berg_bger(f, J, T);
berg_movi(f, R, 0);
/* innermost loop */
berg_movi(f, K, 0);
struct berg_label innermost = berg_label(f);
/* stop if K >= MATRIX_SIZE */
berg_movi(f, T, MATRIX_SIZE);
struct berg_reloc innermost_jmp = berg_bger(f, K, T);
/* A[I][K] */
/* &[I][K] = 4 * (K + MATRIX_SIZE * I) */
berg_movi(f, T, MATRIX_SIZE);
berg_mulr(f, T, T, I);
berg_addr(f, T, T, K);
berg_lshi(f, T, T, 2);
berg_ldxr_i32(f, AT, A, T);
/* B[K][J] */
berg_movi(f, T, MATRIX_SIZE);
berg_mulr(f, T, T, K);
berg_addr(f, T, T, J);
berg_lshi(f, T, T, 2);
berg_ldxr_i32(f, BT, B, T);
/* R += A[I][K] * B[K][J] */
berg_mulr(f, T, AT, BT);
berg_addr(f, R, R, T);
berg_addi(f, K, K, 1);
berg_patch(f, berg_jmp(f), innermost);
berg_patch(f, innermost_jmp, berg_label(f));
/* &[I][J] = 4 * (J + MATRIX_SIZE * I) */
berg_movi(f, T, MATRIX_SIZE);
berg_mulr(f, T, T, I);
berg_addr(f, T, T, J);
berg_lshi(f, T, T, 2);
/* C[I][J] = R */
berg_stxr_32(f, R, C, T);
berg_addi(f, J, J, 1);
berg_patch(f, berg_jmp(f), inner);
berg_patch(f, inner_jmp, berg_label(f));
berg_addi(f, I, I, 1);
berg_patch(f, berg_jmp(f), outer);
berg_patch(f, outer_jmp, berg_label(f));
berg_ret(f);
compile_berg_func(f);
return f;
}
static struct berg_func *compile_dump(struct berg_vm *vm)
{
struct berg_gpr A0 = berg_gpr_arg(0);
struct berg_gpr C = berg_gpr_tmp(0);
struct berg_gpr I = berg_gpr_tmp(1);
struct berg_gpr J = berg_gpr_tmp(2);
struct berg_gpr T = berg_gpr_tmp(3);
struct berg_gpr S = berg_gpr_tmp(4);
struct berg_operand operands[] = {
berg_operand_gpr(C, BERG_POINTER),
};
struct berg_func *f = create_berg_func(vm, BERG_VOID, 1, operands);
berg_movi(f, S, 0);
/* outer loop */
berg_movi(f, I, 0);
struct berg_label outer = berg_label(f);
/* stop if I >= MATRIX_SIZE */
berg_movi(f, T, MATRIX_SIZE);
struct berg_reloc outer_jmp = berg_bger(f, I, T);
/* inner loop */
berg_movi(f, J, 0);
struct berg_label inner = berg_label(f);
/* stop if J >= MATRIX_SIZE */
berg_movi(f, T, MATRIX_SIZE);
struct berg_reloc inner_jmp = berg_bger(f, J, T);
/* &[I][J] = 4 * (J + MATRIX_SIZE * I) */
berg_movi(f, T, MATRIX_SIZE);
berg_mulr(f, T, T, I);
berg_addr(f, T, T, J);
berg_lshi(f, T, T, 2);
/* S += C[I][J] = I */
berg_ldxr_i32(f, T, C, T);
berg_addr(f, S, S, T);
berg_addi(f, J, J, 1);
berg_patch(f, berg_jmp(f), inner);
berg_patch(f, inner_jmp, berg_label(f));
berg_addi(f, I, I, 1);
berg_patch(f, berg_jmp(f), outer);
berg_patch(f, outer_jmp, berg_label(f));
/* print out 'hash' of matrix */
berg_movr(f, A0, S);
berg_ecall(f, T, BERG_PUTI32);
berg_ret(f);
compile_berg_func(f);
return f;
}
static struct berg_func *compile_main(struct berg_vm *vm, struct berg_func *init, struct berg_func *mult, struct berg_func *dump)
{
struct berg_gpr T = berg_gpr_tmp(0);
struct berg_gpr A = berg_gpr_tmp(1);
struct berg_gpr B = berg_gpr_tmp(2);
struct berg_gpr C = berg_gpr_tmp(3);
struct berg_func *f = create_berg_func(vm, BERG_VOID, 0, NULL);
struct berg_gpr A0 = berg_gpr_arg(0);
struct berg_gpr A1 = berg_gpr_arg(1);
struct berg_gpr A2 = berg_gpr_arg(2);
/* alloc matrixes */
berg_movi(f, A0, MATRIX_SIZE * MATRIX_SIZE * sizeof(int));
/* ecall takes some fixed registers, 0-1 (at least for now, will have to
* think of a proper ABI at some point) */
berg_ecall(f, A, BERG_MALLOC);
berg_ecall(f, B, BERG_MALLOC);
berg_ecall(f, C, BERG_MALLOC);
/** @todo come up with ABI for calls */
berg_movr(f, A0, A);
berg_movr(f, A1, B);
berg_call(f, init);
berg_movr(f, A2, C);
berg_call(f, mult);
berg_movr(f, A0, C);
berg_call(f, dump);
/* we don't really do anything with the return value so just dump it
* into T (should I have some kind of X0 register like in riscv?) */
berg_movr(f, A0, A);
berg_ecall(f, T, BERG_FREE);
berg_movr(f, A0, B);
berg_ecall(f, T, BERG_FREE);
berg_movr(f, A0, C);
berg_ecall(f, T, BERG_FREE);
berg_ret(f);
compile_berg_func(f);
return f;
}
int main(int argc, char *argv[argc])
{
struct berg_vm *vm = create_berg_vm();
struct berg_func *init = compile_init(vm);
struct berg_func *mult = compile_mult(vm);
struct berg_func *dump = compile_dump(vm);
struct berg_func *mn = compile_main(vm, init, mult, dump);
run_berg_func(mn);
destroy_berg_vm(vm);
return 0;
}
|