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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
/***********************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../LICENSE. */
/* */
/***********************************************************************/
/* $Id$ */
#ifdef __ppc64__
#define X(a,b) b
#else
#define X(a,b) a
#endif
#define WORD X(4,8)
#define lg X(lwz,ld)
#define lgu X(lwzu,ldu)
#define stg X(stw,std)
#define stgu X(stwu,stdu)
#define gdata X(.long,.quad)
.macro Addrglobal /* reg, glob */
addis $0, 0, ha16($1)
addi $0, $0, lo16($1)
.endmacro
.macro Loadglobal /* reg,glob,tmp */
addis $2, 0, ha16($1)
lg $0, lo16($1)($2)
.endmacro
.macro Storeglobal /* reg,glob,tmp */
addis $2, 0, ha16($1)
stg $0, lo16($1)($2)
.endmacro
.text
.globl _caml_system__code_begin
_caml_system__code_begin:
/* Invoke the garbage collector. */
.globl _caml_call_gc
_caml_call_gc:
/* Set up stack frame */
#define FRAMESIZE (32*WORD + 32*8 + 32)
stwu r1, -FRAMESIZE(r1)
/* Record return address into OCaml code */
mflr r0
Storeglobal r0, _caml_last_return_address, r11
/* Record lowest stack address */
addi r0, r1, FRAMESIZE
Storeglobal r0, _caml_bottom_of_stack, r11
/* Touch the stack to trigger a recoverable segfault
if insufficient space remains */
addi r1, r1, -4096*WORD
stg r0, 0(r1)
addi r1, r1, 4096*WORD
/* Record pointer to register array */
addi r0, r1, 8*32 + 32
Storeglobal r0, _caml_gc_regs, r11
/* Save current allocation pointer for debugging purposes */
Storeglobal r31, _caml_young_ptr, r11
/* Save exception pointer (if e.g. a sighandler raises) */
Storeglobal r29, _caml_exception_pointer, r11
/* Save all registers used by the code generator */
addi r11, r1, 8*32 + 32 - WORD
stgu r3, WORD(r11)
stgu r4, WORD(r11)
stgu r5, WORD(r11)
stgu r6, WORD(r11)
stgu r7, WORD(r11)
stgu r8, WORD(r11)
stgu r9, WORD(r11)
stgu r10, WORD(r11)
stgu r14, WORD(r11)
stgu r15, WORD(r11)
stgu r16, WORD(r11)
stgu r17, WORD(r11)
stgu r18, WORD(r11)
stgu r19, WORD(r11)
stgu r20, WORD(r11)
stgu r21, WORD(r11)
stgu r22, WORD(r11)
stgu r23, WORD(r11)
stgu r24, WORD(r11)
stgu r25, WORD(r11)
stgu r26, WORD(r11)
stgu r27, WORD(r11)
stgu r28, WORD(r11)
addi r11, r1, 32 - 8
stfdu f1, 8(r11)
stfdu f2, 8(r11)
stfdu f3, 8(r11)
stfdu f4, 8(r11)
stfdu f5, 8(r11)
stfdu f6, 8(r11)
stfdu f7, 8(r11)
stfdu f8, 8(r11)
stfdu f9, 8(r11)
stfdu f10, 8(r11)
stfdu f11, 8(r11)
stfdu f12, 8(r11)
stfdu f13, 8(r11)
stfdu f14, 8(r11)
stfdu f15, 8(r11)
stfdu f16, 8(r11)
stfdu f17, 8(r11)
stfdu f18, 8(r11)
stfdu f19, 8(r11)
stfdu f20, 8(r11)
stfdu f21, 8(r11)
stfdu f22, 8(r11)
stfdu f23, 8(r11)
stfdu f24, 8(r11)
stfdu f25, 8(r11)
stfdu f26, 8(r11)
stfdu f27, 8(r11)
stfdu f28, 8(r11)
stfdu f29, 8(r11)
stfdu f30, 8(r11)
stfdu f31, 8(r11)
/* Call the GC */
bl _caml_garbage_collection
/* Reload new allocation pointer and allocation limit */
Loadglobal r31, _caml_young_ptr, r11
Loadglobal r30, _caml_young_limit, r11
/* Restore all regs used by the code generator */
addi r11, r1, 8*32 + 32 - WORD
lgu r3, WORD(r11)
lgu r4, WORD(r11)
lgu r5, WORD(r11)
lgu r6, WORD(r11)
lgu r7, WORD(r11)
lgu r8, WORD(r11)
lgu r9, WORD(r11)
lgu r10, WORD(r11)
lgu r14, WORD(r11)
lgu r15, WORD(r11)
lgu r16, WORD(r11)
lgu r17, WORD(r11)
lgu r18, WORD(r11)
lgu r19, WORD(r11)
lgu r20, WORD(r11)
lgu r21, WORD(r11)
lgu r22, WORD(r11)
lgu r23, WORD(r11)
lgu r24, WORD(r11)
lgu r25, WORD(r11)
lgu r26, WORD(r11)
lgu r27, WORD(r11)
lgu r28, WORD(r11)
addi r11, r1, 32 - 8
lfdu f1, 8(r11)
lfdu f2, 8(r11)
lfdu f3, 8(r11)
lfdu f4, 8(r11)
lfdu f5, 8(r11)
lfdu f6, 8(r11)
lfdu f7, 8(r11)
lfdu f8, 8(r11)
lfdu f9, 8(r11)
lfdu f10, 8(r11)
lfdu f11, 8(r11)
lfdu f12, 8(r11)
lfdu f13, 8(r11)
lfdu f14, 8(r11)
lfdu f15, 8(r11)
lfdu f16, 8(r11)
lfdu f17, 8(r11)
lfdu f18, 8(r11)
lfdu f19, 8(r11)
lfdu f20, 8(r11)
lfdu f21, 8(r11)
lfdu f22, 8(r11)
lfdu f23, 8(r11)
lfdu f24, 8(r11)
lfdu f25, 8(r11)
lfdu f26, 8(r11)
lfdu f27, 8(r11)
lfdu f28, 8(r11)
lfdu f29, 8(r11)
lfdu f30, 8(r11)
lfdu f31, 8(r11)
/* Return to caller, restarting the allocation */
Loadglobal r0, _caml_last_return_address, r11
addic r0, r0, -16 /* Restart the allocation (4 instructions) */
mtlr r0
/* Say we are back into OCaml code */
li r12, 0
Storeglobal r12, _caml_last_return_address, r11
/* Deallocate stack frame */
addi r1, r1, FRAMESIZE
/* Return */
blr
#undef FRAMESIZE
/* Call a C function from OCaml */
.globl _caml_c_call
_caml_c_call:
/* Save return address */
mflr r25
/* Get ready to call C function (address in 11) */
mtctr r11
/* Record lowest stack address and return address */
Storeglobal r1, _caml_bottom_of_stack, r12
Storeglobal r25, _caml_last_return_address, r12
/* Touch the stack to trigger a recoverable segfault
if insufficient space remains */
addi r1, r1, -4096*WORD
stg r0, 0(r1)
addi r1, r1, 4096*WORD
/* Make the exception handler and alloc ptr available to the C code */
Storeglobal r31, _caml_young_ptr, r11
Storeglobal r29, _caml_exception_pointer, r11
/* Call the function (address in link register) */
bctrl
/* Restore return address (in 25, preserved by the C function) */
mtlr r25
/* Reload allocation pointer and allocation limit*/
Loadglobal r31, _caml_young_ptr, r11
Loadglobal r30, _caml_young_limit, r11
/* Say we are back into OCaml code */
li r12, 0
Storeglobal r12, _caml_last_return_address, r11
/* Return to caller */
blr
/* Raise an exception from OCaml */
.globl _caml_raise_exn
_caml_raise_exn:
addis r11, 0, ha16(_caml_backtrace_active)
lwz r11, lo16(_caml_backtrace_active)(r11)
cmpwi r11, 0
bne L110
L111:
/* Pop trap frame */
lg r0, 0(r29)
mr r1, r29
mtlr r0
lg r29, WORD(r1)
addi r1, r1, 16
/* Branch to handler */
blr
L110:
mr r28, r3 /* preserve exn bucket in callee-save */
/* arg 1: exception bucket (already in r3) */
mflr r4 /* arg 2: PC of raise */
mr r5, r1 /* arg 3: SP of raise */
mr r6, r29 /* arg 4: SP of handler */
addi r1, r1, -(16*WORD) /* reserve stack space for C call */
bl _caml_stash_backtrace
mr r3, r28
b L111
/* Raise an exception from C */
.globl _caml_raise_exception
_caml_raise_exception:
addis r11, 0, ha16(_caml_backtrace_active)
lwz r11, lo16(_caml_backtrace_active)(r11)
cmpwi r11, 0
bne L112
L113:
/* Reload OCaml global registers */
Loadglobal r1, _caml_exception_pointer, r11
Loadglobal r31, _caml_young_ptr, r11
Loadglobal r30, _caml_young_limit, r11
/* Say we are back into OCaml code */
li r0, 0
Storeglobal r0, _caml_last_return_address, r11
/* Pop trap frame */
lg r0, 0(r1)
lg r29, WORD(r1)
mtlr r0
addi r1, r1, 16
/* Branch to handler */
blr
L112:
mr r28, r3 /* preserve exn bucket in callee-save */
/* arg 1: exception bucket (already in r3) */
Loadglobal r4, _caml_last_return_address, r11 /* arg 2: PC of raise */
Loadglobal r5, _caml_bottom_of_stack, r11 /* arg 3: SP of raise */
Loadglobal r6, _caml_exception_pointer, r11 /* arg 4: SP of handler */
addi r1, r1, -(16*WORD) /* reserve stack space for C call */
bl _caml_stash_backtrace
mr r3, r28
b L113
/* Start the OCaml program */
.globl _caml_start_program
_caml_start_program:
Addrglobal r12, _caml_program
/* Code shared between caml_start_program and caml_callback */
L102:
/* Allocate and link stack frame */
#define FRAMESIZE (16 + 20*WORD + 18*8)
stgu r1, -FRAMESIZE(r1)
/* Save return address */
mflr r0
stg r0, WORD(r1)
/* Save all callee-save registers */
/* GPR14 ... GPR31, then FPR14 ... FPR31 starting at sp+16 */
addi r11, r1, 16-WORD
stgu r14, WORD(r11)
stgu r15, WORD(r11)
stgu r16, WORD(r11)
stgu r17, WORD(r11)
stgu r18, WORD(r11)
stgu r19, WORD(r11)
stgu r20, WORD(r11)
stgu r21, WORD(r11)
stgu r22, WORD(r11)
stgu r23, WORD(r11)
stgu r24, WORD(r11)
stgu r25, WORD(r11)
stgu r26, WORD(r11)
stgu r27, WORD(r11)
stgu r28, WORD(r11)
stgu r29, WORD(r11)
stgu r30, WORD(r11)
stgu r31, WORD(r11)
stfdu f14, 8(r11)
stfdu f15, 8(r11)
stfdu f16, 8(r11)
stfdu f17, 8(r11)
stfdu f18, 8(r11)
stfdu f19, 8(r11)
stfdu f20, 8(r11)
stfdu f21, 8(r11)
stfdu f22, 8(r11)
stfdu f23, 8(r11)
stfdu f24, 8(r11)
stfdu f25, 8(r11)
stfdu f26, 8(r11)
stfdu f27, 8(r11)
stfdu f28, 8(r11)
stfdu f29, 8(r11)
stfdu f30, 8(r11)
stfdu f31, 8(r11)
/* Set up a callback link */
addi r1, r1, -32
Loadglobal r9, _caml_bottom_of_stack, r11
Loadglobal r10, _caml_last_return_address, r11
Loadglobal r11, _caml_gc_regs, r11
stg r9, 0(r1)
stg r10, WORD(r1)
stg r11, 2*WORD(r1)
/* Build an exception handler to catch exceptions escaping out of OCaml */
bl L103
b L104
L103:
addi r1, r1, -16
mflr r0
stg r0, 0(r1)
Loadglobal r11, _caml_exception_pointer, r11
stg r11, WORD(r1)
mr r29, r1
/* Reload allocation pointers */
Loadglobal r31, _caml_young_ptr, r11
Loadglobal r30, _caml_young_limit, r11
/* Say we are back into OCaml code */
li r0, 0
Storeglobal r0, _caml_last_return_address, r11
/* Call the OCaml code */
mtctr r12
L105:
bctrl
/* Pop the trap frame, restoring caml_exception_pointer */
lg r9, WORD(r1)
Storeglobal r9, _caml_exception_pointer, r11
addi r1, r1, 16
/* Pop the callback link, restoring the global variables */
L106:
lg r9, 0(r1)
lg r10, WORD(r1)
lg r11, 2*WORD(r1)
Storeglobal r9, _caml_bottom_of_stack, r12
Storeglobal r10, _caml_last_return_address, r12
Storeglobal r11, _caml_gc_regs, r12
addi r1, r1, 32
/* Update allocation pointer */
Storeglobal r31, _caml_young_ptr, r11
/* Restore callee-save registers */
addi r11, r1, 16-WORD
lgu r14, WORD(r11)
lgu r15, WORD(r11)
lgu r16, WORD(r11)
lgu r17, WORD(r11)
lgu r18, WORD(r11)
lgu r19, WORD(r11)
lgu r20, WORD(r11)
lgu r21, WORD(r11)
lgu r22, WORD(r11)
lgu r23, WORD(r11)
lgu r24, WORD(r11)
lgu r25, WORD(r11)
lgu r26, WORD(r11)
lgu r27, WORD(r11)
lgu r28, WORD(r11)
lgu r29, WORD(r11)
lgu r30, WORD(r11)
lgu r31, WORD(r11)
lfdu f14, 8(r11)
lfdu f15, 8(r11)
lfdu f16, 8(r11)
lfdu f17, 8(r11)
lfdu f18, 8(r11)
lfdu f19, 8(r11)
lfdu f20, 8(r11)
lfdu f21, 8(r11)
lfdu f22, 8(r11)
lfdu f23, 8(r11)
lfdu f24, 8(r11)
lfdu f25, 8(r11)
lfdu f26, 8(r11)
lfdu f27, 8(r11)
lfdu f28, 8(r11)
lfdu f29, 8(r11)
lfdu f30, 8(r11)
lfdu f31, 8(r11)
/* Reload return address */
lg r0, WORD(r1)
mtlr r0
/* Return */
addi r1, r1, FRAMESIZE
blr
/* The trap handler: */
L104:
/* Update caml_exception_pointer */
Storeglobal r29, _caml_exception_pointer, r11
/* Encode exception bucket as an exception result and return it */
ori r3, r3, 2
b L106
#undef FRAMESIZE
/* Callback from C to OCaml */
.globl _caml_callback_exn
_caml_callback_exn:
/* Initial shuffling of arguments */
mr r0, r3 /* Closure */
mr r3, r4 /* Argument */
mr r4, r0
lg r12, 0(r4) /* Code pointer */
b L102
.globl _caml_callback2_exn
_caml_callback2_exn:
mr r0, r3 /* Closure */
mr r3, r4 /* First argument */
mr r4, r5 /* Second argument */
mr r5, r0
Addrglobal r12, _caml_apply2
b L102
.globl _caml_callback3_exn
_caml_callback3_exn:
mr r0, r3 /* Closure */
mr r3, r4 /* First argument */
mr r4, r5 /* Second argument */
mr r5, r6 /* Third argument */
mr r6, r0
Addrglobal r12, _caml_apply3
b L102
.globl _caml_system__code_end
_caml_system__code_end:
/* Frame table */
.const
.globl _caml_system__frametable
_caml_system__frametable:
gdata 1 /* one descriptor */
gdata L105 + 4 /* return address into callback */
.short -1 /* negative size count => use callback link */
.short 0 /* no roots here */
.align X(2,3)
|