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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
|
.text
.globl init_profiler
.globl profile_r
.globl profile_l
.globl profile_l2
.globl profile_n
.globl profile_n2
.globl profile_s
.globl profile_s2
.globl profile_t
.globl write_profile_information
.globl write_profile_stack
.globl stack_trace_depth
.if ! LINUX
.globl allocate_memory
.endif
.globl __STRING__
.globl openF
.globl closeF
.globl writeFC
.globl writeFI
.globl _ab_stack_size
.globl _ew_print_string
.globl _ew_print_char
.globl _ew_print_text
.globl _create_profile_file_name
.globl profile_file_name
/* extrn print_error */
/* extrn profile_stack_pointer */
next = 0
time = 8
n_profiler_calls = 16
n_strict_calls = 24
n_lazy_calls = 32
n_curried_calls = 40
n_words_allocated = 48
name_ = 56
FunctionProfile = 64
profile_t:
push rax
push rdx
rdtsc
push rcx
mov rcx,qword ptr [rip+profile_stack_pointer]
sub edx,dword ptr [rip+global_time_hi]
push rbx
mov ebx,dword ptr [rip+global_time_lo]
mov eax,eax
shl rdx,32
sub rax,rbx
mov rbx,qword ptr [rcx-8]
add rax,rdx
sub rcx,8
mov qword ptr [rip+global_last_tail_call],rbx
mov qword ptr [rip+profile_stack_pointer],rcx
inc qword ptr [rbx+n_profiler_calls]
add qword ptr [rbx+time],rax
mov rax,qword ptr [rip+global_n_words_free]
mov qword ptr [rip+global_n_words_free],r15
sub rax,r15
add qword ptr [rbx+n_words_allocated],rax
pop rbx
pop rcx
rdtsc
mov dword ptr [rip+global_time_hi],edx
pop rdx
mov dword ptr [rip+global_time_lo],eax
pop rax
ret
profile_r:
push rax
push rdx
rdtsc
push rcx
mov rcx,qword ptr [rip+profile_stack_pointer]
sub edx,dword ptr [rip+global_time_hi]
push rbx
mov ebx,dword ptr [rip+global_time_lo]
mov eax,eax
shl rdx,32
sub rax,rbx
mov rbx,qword ptr [rcx-8]
add rax,rdx
sub rcx,8
mov qword ptr [rip+global_last_tail_call],0
mov qword ptr [rip+profile_stack_pointer],rcx
inc qword ptr [rbx+n_profiler_calls]
add qword ptr [rbx+time],rax
mov rax,qword ptr [rip+global_n_words_free]
mov qword ptr [rip+global_n_words_free],r15
sub rax,r15
add qword ptr [rbx+n_words_allocated],rax
pop rbx
pop rcx
rdtsc
mov dword ptr [rip+global_time_hi],edx
pop rdx
mov dword ptr [rip+global_time_lo],eax
pop rax
ret
profile_l:
push rax
push rdx
rdtsc
push rbx
mov rbx,qword ptr [rbp]
test rbx,rbx
je allocate_function_profile_record_l
allocate_function_profile_record_lr:
push rcx
mov rbp,qword ptr [rip+global_last_tail_call]
mov rcx,qword ptr [rip+profile_stack_pointer]
test rbp,rbp
jne use_tail_calling_function_l
mov rbp,qword ptr [rcx-8]
use_tail_calling_function_lr:
mov qword ptr [rcx],rbx
add rcx,8
inc qword ptr [rbx+n_curried_calls]
att_jmp profile_n_
allocate_function_profile_record_l:
att_call allocate_function_profile_record
att_jmp allocate_function_profile_record_lr
use_tail_calling_function_l:
mov qword ptr [rip+global_last_tail_call],0
att_jmp use_tail_calling_function_lr
profile_l2:
push rax
push rdx
rdtsc
push rbx
mov rbx,qword ptr [rbp]
test rbx,rbx
je allocate_function_profile_record_l2
allocate_function_profile_record_l2r:
push rcx
mov rbp,qword ptr [rip+global_last_tail_call]
mov rcx,qword ptr [rip+profile_stack_pointer]
test rbp,rbp
jne use_tail_calling_function_l2
mov rbp,qword ptr [rcx-8]
use_tail_calling_function_l2r:
mov qword ptr [rcx],rbx
mov qword ptr [rcx+8],rbx
add rcx,16
inc qword ptr [rbx+n_curried_calls]
att_jmp profile_n_
allocate_function_profile_record_l2:
att_call allocate_function_profile_record
att_jmp allocate_function_profile_record_l2r
use_tail_calling_function_l2:
mov qword ptr [rip+global_last_tail_call],0
att_jmp use_tail_calling_function_l2r
profile_n:
push rax
push rdx
rdtsc
push rbx
mov rbx,qword ptr [rbp]
test rbx,rbx
je allocate_function_profile_record_n
allocate_function_profile_record_nr:
push rcx
mov rbp,qword ptr [rip+global_last_tail_call]
mov rcx,qword ptr [rip+profile_stack_pointer]
test rbp,rbp
jne use_tail_calling_function_n
mov rbp,qword ptr [rcx-8]
use_tail_calling_function_nr:
mov qword ptr [rcx],rbx
add rcx,8
inc qword ptr [rbx+n_lazy_calls]
att_jmp profile_n_
allocate_function_profile_record_n:
att_call allocate_function_profile_record
att_jmp allocate_function_profile_record_nr
use_tail_calling_function_n:
mov qword ptr [rip+global_last_tail_call],0
att_jmp use_tail_calling_function_nr
profile_n2:
push rax
push rdx
rdtsc
push rbx
mov rbx,qword ptr [rbp]
test rbx,rbx
je allocate_function_profile_record_n2
allocate_function_profile_record_n2r:
push rcx
mov rbp,qword ptr [rip+global_last_tail_call]
mov rcx,qword ptr [rip+profile_stack_pointer]
test rbp,rbp
jne use_tail_calling_function_n2
mov rbp,qword ptr [rcx-8]
use_tail_calling_function_n2r:
mov qword ptr [rcx],rbx
mov qword ptr [rcx+8],rbx
add rcx,16
inc qword ptr [rbx+n_lazy_calls]
att_jmp profile_n_
allocate_function_profile_record_n2:
att_call allocate_function_profile_record
att_jmp allocate_function_profile_record_n2r
use_tail_calling_function_n2:
mov qword ptr [rip+global_last_tail_call],0
att_jmp use_tail_calling_function_n2r
profile_s2:
push rax
push rdx
rdtsc
push rbx
mov rbx,qword ptr [rbp]
test rbx,rbx
je allocate_function_profile_record_s2
allocate_function_profile_record_s2r:
push rcx
mov rbp,qword ptr [rip+global_last_tail_call]
mov rcx,qword ptr [rip+profile_stack_pointer]
test rbp,rbp
jne use_tail_calling_function_s2
mov rbp,qword ptr [rcx-8]
use_tail_calling_function_s2r:
mov qword ptr [rcx],rbx
mov qword ptr [rcx+8],rbx
add rcx,16
att_jmp profile_s_
allocate_function_profile_record_s2:
att_call allocate_function_profile_record
att_jmp allocate_function_profile_record_s2r
use_tail_calling_function_s2:
mov qword ptr [rip+global_last_tail_call],0
att_jmp use_tail_calling_function_s2r
profile_s:
push rax
push rdx
rdtsc
push rbx
mov rbx,qword ptr [rbp]
test rbx,rbx
je allocate_function_profile_record_s
allocate_function_profile_record_sr:
push rcx
mov rbp,qword ptr [rip+global_last_tail_call]
mov rcx,qword ptr [rip+profile_stack_pointer]
test rbp,rbp
jne use_tail_calling_function_s
mov rbp,qword ptr [rcx-8]
use_tail_calling_function_sr:
mov qword ptr [rcx],rbx
add rcx,8
profile_s_:
inc qword ptr [rbx+n_strict_calls]
profile_n_:
mov qword ptr [rip+profile_stack_pointer],rcx
sub edx,dword ptr [rip+global_time_hi]
mov ebx,dword ptr [rip+global_time_lo]
mov eax,eax
shl rdx,32
sub rax,rbx
add rax,rdx
inc qword ptr [rbp+n_profiler_calls]
add qword ptr [rbp+time],rax
mov rax,qword ptr [rip+global_n_words_free]
mov qword ptr [rip+global_n_words_free],r15
sub rax,r15
add qword ptr [rbp+n_words_allocated],rax
pop rcx
pop rbx
rdtsc
mov dword ptr [rip+global_time_hi],edx
pop rdx
mov dword ptr [rip+global_time_lo],eax
pop rax
ret
allocate_function_profile_record_s:
att_call allocate_function_profile_record
att_jmp allocate_function_profile_record_sr
use_tail_calling_function_s:
mov qword ptr [rip+global_last_tail_call],0
att_jmp use_tail_calling_function_sr
/* argument: rbp: function name adress-4 */
/* result: rbx: function profile record adress */
allocate_function_profile_record:
push rax
mov rax,qword ptr [rip+global_n_free_records_in_block]
mov rbx,qword ptr [rip+global_last_allocated_block]
test rax,rax
jne no_alloc
push rcx
push rdx
push rbp
.if LINUX
sub rsp,104
mov qword ptr [rsp],rsi
mov qword ptr [rsp+8],rdi
mov qword ptr [rsp+16],r8
mov qword ptr [rsp+24],r10
mov qword ptr [rsp+32],r11
movsd qword ptr [rsp+40],xmm0
movsd qword ptr [rsp+48],xmm1
movsd qword ptr [rsp+56],xmm2
movsd qword ptr [rsp+64],xmm3
movsd qword ptr [rsp+72],xmm4
movsd qword ptr [rsp+80],xmm5
movsd qword ptr [rsp+88],xmm6
movsd qword ptr [rsp+96],xmm7
.else
sub rsp,72
mov qword ptr [rsp],r8
mov qword ptr [rsp+8],r10
mov qword ptr [rsp+16],r11
movsd qword ptr [rsp+24],xmm0
movsd qword ptr [rsp+32],xmm1
movsd qword ptr [rsp+40],xmm2
movsd qword ptr [rsp+48],xmm3
movsd qword ptr [rsp+56],xmm4
movsd qword ptr [rsp+64],xmm5
.endif
mov rbp,rsp
sub rsp,40
and rsp,-16
.if LINUX
mov rdi,8192
/* 128*FunctionProfile */
att_call _malloc
.else
mov rcx,128*FunctionProfile
att_call allocate_memory
.endif
mov rsp,rbp
.if LINUX
mov rsi,qword ptr [rsp]
mov rdi,qword ptr [rsp+8]
mov r8,qword ptr [rsp+16]
mov r10,qword ptr [rsp+24]
mov r11,qword ptr [rsp+32]
movlpd xmm0,qword ptr [rsp+40]
movlpd xmm1,qword ptr [rsp+48]
movlpd xmm2,qword ptr [rsp+56]
movlpd xmm3,qword ptr [rsp+64]
movlpd xmm4,qword ptr [rsp+72]
movlpd xmm5,qword ptr [rsp+80]
movlpd xmm6,qword ptr [rsp+88]
movlpd xmm7,qword ptr [rsp+96]
add rsp,104
.else
mov r8,qword ptr [rsp]
mov r10,qword ptr [rsp+8]
mov r11,qword ptr [rsp+16]
movlpd xmm0,qword ptr [rsp+24]
movlpd xmm1,qword ptr [rsp+32]
movlpd xmm2,qword ptr [rsp+40]
movlpd xmm3,qword ptr [rsp+48]
movlpd xmm4,qword ptr [rsp+56]
movlpd xmm5,qword ptr [rsp+64]
add rsp,72
.endif
test rax,rax
pop rbp
pop rdx
pop rcx
je no_memory
mov rbx,rax
mov rax,128
mov qword ptr [rip+global_last_allocated_block],rbx
no_alloc:
dec rax
mov qword ptr [rip+global_n_free_records_in_block],rax
lea rax,[rbx+FunctionProfile]
mov qword ptr [rip+global_last_allocated_block],rax
xor rax,rax
mov qword ptr [rbx+time],rax
mov qword ptr [rbx+n_profiler_calls],rax
mov qword ptr [rbx+n_strict_calls],rax
mov qword ptr [rbx+n_lazy_calls],rax
mov qword ptr [rbx+n_curried_calls],rax
mov qword ptr [rbx+n_words_allocated],rax
mov rax,qword ptr [rip+global_profile_records]
mov qword ptr [rbx+name_],rbp
mov qword ptr [rbx+next],rax
mov qword ptr [rip+global_profile_records],rbx
mov qword ptr [rbp],rbx
pop rax
ret
no_memory:
lea rbp,[rip+not_enough_memory_for_profiler]
pop rax
att_jmp print_error
write_profile_information:
mov rbp,rsp
sub rsp,40
and rsp,-16
.if LINUX
mov r13,rsi
mov r14,rdi
lea rdi,[rip+profile_file_name]
.else
lea rcx,[rip+profile_file_name]
.endif
att_call _create_profile_file_name
.if LINUX
mov rsi,r13
mov rdi,r14
.endif
mov rsp,rbp
mov rax,1
lea rcx,[rip+profile_file_name]
att_call openF
test r10,r10
je cannot_open
mov rbp,qword ptr [rip+global_profile_records]
write_profile_lp:
test rbp,rbp
je end_list
mov rdx,qword ptr [rbp+name_]
push rbp
push rdx
movsxd rax,dword ptr [rdx-4]
lea rdx,[rdx+rax-4]
mov eax,dword ptr [rdx]
add rdx,4
write_module_name_lp:
sub rax,1
jc end_module_name
push rax
push rdx
movzx r10,byte ptr [rdx]
att_call writeFC
pop rdx
pop rax
add rdx,1
att_jmp write_module_name_lp
end_module_name:
mov r10,32 # ' '
att_call writeFC
pop rdx
add rdx,7
write_function_name_lp:
movzx r10,byte ptr [rdx+1]
add rdx,1
test r10,r10
je end_function_name
push rdx
att_call writeFC
pop rdx
att_jmp write_function_name_lp
end_function_name:
mov r10,32 # ' '
att_call writeFC
mov rbp,qword ptr [rsp]
mov r10,qword ptr [rbp+n_strict_calls]
att_call writeFI_space
mov rbp,qword ptr [rsp]
mov r10,qword ptr [rbp+n_lazy_calls]
att_call writeFI_space
mov rbp,qword ptr [rsp]
mov r10,qword ptr [rbp+n_curried_calls]
att_call writeFI_space
mov rbp,qword ptr [rsp]
mov r10,qword ptr [rbp+n_profiler_calls]
att_call writeFI_space
mov rbp,qword ptr [rsp]
mov r10,qword ptr [rbp+n_words_allocated]
att_call writeFI_space
mov rbp,qword ptr [rsp]
mov r10,qword ptr [rbp+time]
att_call writeFI
mov r10,10
att_call writeFC
pop rbp
mov rbp,qword ptr [rbp+next]
att_jmp write_profile_lp
writeFI_space:
att_call writeFI
mov r10,32 # ' '
att_jmp writeFC
end_list:
att_call closeF
cannot_open:
ret
write_profile_stack:
mov rax,qword ptr [rip+profile_stack_pointer]
test rax,rax
je stack_not_initialised
push rax
mov rbp,rsp
sub rsp,40
and rsp,-16
.if LINUX
mov r13,rsi
mov r14,rdi
lea rdi,[rip+stack_trace_string]
.else
lea rcx,[rip+stack_trace_string]
.endif
att_call _ew_print_string
.if LINUX
mov rsi,r13
mov rdi,r14
.endif
mov rsp,rbp
pop rax
/* mov rbp,12 */
mov rbp,qword ptr [rip+stack_trace_depth]
write_functions_on_stack:
mov rbx,qword ptr [rax-8]
sub rax,8
test rbx,rbx
je end_profile_stack
push rax
mov rcx,qword ptr [rbx+name_]
push rbp
mov edx,dword ptr [rcx-4]
add rcx,8
mov r12d,dword ptr [rdx]
lea r13,[rdx+4]
mov rbp,rsp
sub rsp,40
and rsp,-16
.if LINUX
mov r11,rsi
mov r14,rdi
mov rdi,rcx
.endif
att_call _ew_print_string
.if LINUX
lea rdi,[rip+module_string]
.else
lea rcx,[rip+module_string]
.endif
att_call _ew_print_string
.if LINUX
mov rsi,r12
mov rdi,r13
.else
mov rdx,r12
mov rcx,r13
.endif
att_call _ew_print_text
.if LINUX
mov rdi,93 # ']'
.else
mov rcx,93 # ']'
.endif
att_call _ew_print_char
.if LINUX
mov rdi,10
.else
mov rcx,10
.endif
att_call _ew_print_char
.if LINUX
mov rsi,r11
mov rdi,r14
.endif
mov rsp,rbp
pop rbp
pop rax
sub rbp,1
att_jne write_functions_on_stack
end_profile_stack:
stack_not_initialised:
ret
init_profiler:
mov rbp,rsp
sub rsp,40
and rsp,-16
.if LINUX
mov r13,rsi
mov r14,rdi
mov rdi,qword ptr [rip+_ab_stack_size]
att_call _malloc
mov rsi,r13
mov rdi,r14
.else
mov rcx,qword ptr [rip+ab_stack_size]
att_call allocate_memory
.endif
mov rsp,rbp
test rax,rax
je init_profiler_error
push rax
lea rbp,[rip+start_string]
att_call allocate_function_profile_record
pop rdx
mov qword ptr [rdx+8],rbx
mov qword ptr [rdx],0
add rdx,16
mov qword ptr [rip+profile_stack_pointer],rdx
mov qword ptr [rip+global_last_tail_call],0
mov qword ptr [rip+global_n_words_free],r15
rdtsc
mov dword ptr [rip+global_time_hi],edx
mov dword ptr [rip+global_time_lo],eax
ret
init_profiler_error:
mov qword ptr [rip+profile_stack_pointer],0
lea rbp,[rip+not_enough_memory_for_profile_stack]
att_jmp print_error
.data
.align 8
global_n_free_records_in_block:
.quad 0
/* 0 n free records in block */
global_last_allocated_block:
.quad 0
/* 8 latest allocated block */
global_profile_records:
.quad 0
/* 16 profile record list */
global_time_hi:
.long 0
/* 24 clock */
global_time_lo:
.long 0
global_last_tail_call:
.quad 0
/* last tail calling function */
global_n_words_free:
.quad 0
profile_file_name:
.quad __STRING__+2
.quad 0
.quad 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.quad 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.quad 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.quad 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.quad 0
stack_trace_depth:
.quad 12
.align 8
/* m_system also defined in istartup.s */
/* m_system: */
/* .quad 6 */
/* .ascii "System" */
/* .byte 0 */
/* .byte 0 */
.long m_system-.
start_string:
.quad 0
.ascii "start"
.byte 0
.align 8
not_enough_memory_for_profile_stack:
.ascii "not enough memory for profile stack"
.byte 10
.byte 0
not_enough_memory_for_profiler:
.ascii "not enough memory for profiler"
.byte 10
.byte 0
stack_trace_string:
.ascii "Stack trace:"
.byte 10
.byte 0
module_string:
.ascii " [module: "
.byte 0
.align 8
/* end */
|