-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabc.k
1474 lines (1280 loc) · 47.3 KB
/
abc.k
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ABC file writer and reader
;;;
;;; This read ABC file and construct two kinds of form.
;;;
;;; ABC form - It keeps original structure of ABC
;;; ASM form - For human, easy to read
;;;
;;; Typical usage:
;;; (call-with-input-file "hello.abc" (lambda (port) (pretty-print (to-asm (read-abc port)))))
;;;
;; Copyright (c) 2009 Takashi Yamamiya
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
;; Caution: It is expected that map and build-list executes inner loop
;; in order of the list. Neither documents of map and build-list don't
;; assure the behavior.
;;;;;;;;;; ABC Form Reader ;;;;;;;;;;
;;;; Primitive Data Types Reader
;; Read one byte unsigned integer from the port.
(define read-u8 read-byte)
;; Read two bytes unsigned integer value from the port.
(define read-u16
(lambda (port)
(let ((l (read-byte port))
(h (read-byte port)))
(+ (* h 256) l))))
;; Read a variale number integer from the port.
(define read-u32
(lambda (port)
(let ((a (read-byte port)))
(if (< a 128) a
(let ((*a (bitwise-and a #x7f))
(b (read-byte port)))
(if (< b 128) (+ (arithmetic-shift b 7) *a)
(let ((*b (bitwise-and b #x7f))
(c (read-byte port)))
(if (< c 128) (+ (+ (arithmetic-shift c 14) (arithmetic-shift *b 7)) *a)
(let ((*c (bitwise-and c #x7f))
(d (read-byte port)))
(if (< d 128) (+ (+ (+ (arithmetic-shift d 21) (arithmetic-shift *c 14)) (arithmetic-shift *b 7)) *a)
(let ((*d (bitwise-and d #x7f))
(e (bitwise-and (read-byte port) #x0f)))
(+ (+ (+ (+ (arithmetic-shift e 28) (arithmetic-shift *d 21)) (arithmetic-shift *c 14)) (arithmetic-shift *b 7)) *a))))))))))))
(define read-u30 read-u32)
(define read-s24
(lambda (port)
(let ((b0 (read-byte port))
(b1 (read-byte port))
(b2 (read-byte port)))
(if (positive? (bitwise-and b2 #x80))
(+ (+ (+ (* b2 #x10000) (* b1 #x100)) b0) (- 0 #x1000000))
(+ (+ (* b2 #x10000) (* b1 #x100)) b0)))))
(define read-s32
(lambda (port)
(let ((u32 (read-u32 port)))
(if (< #x7fffffff u32)
(- u32 #x100000000)
u32))))
;; 8-byte IEEE-754 floating point value.
(define read-d64
(lambda (port) (floating-point-bytes->real (read-bytes 8 port) #f)))
;; Read u30 from the port and construct a datum typed the type.
(define read-id
(lambda (type port) `(,type ,(read-u30 port))))
;;;; List Reader
;; Read entries by func. The count is the number of entries plus one.
(define read-list1
(lambda (func port)
(let ((count (read-u30 port)))
(if (= count 0)
'()
(build-list (- count 1) (lambda (_) (func port)))))))
;; Read entries by func. The count is the number of entries.
(define read-list0
(lambda (func port)
(let ((count (read-u30 port)))
(build-list count (lambda (_) (func port))))))
;;;; Constant Pool Reader
;;; string_info reader
(define read-string_info
(lambda (port)
(let ((size (read-u30 port)))
(bytes->string (read-bytes size port)))))
;;; namespace_info reader
(define CONSTANT_Namespace
'((#x08 . ns)
(#x16 . package)
(#x17 . internal)
(#x18 . protected)
(#x19 . explicit)
(#x1a . static)
(#x05 . private)))
(define read-namespace_info (lambda (port)
(let ((kind (read-u8 port))
(name (read-id 'string port)))
`(,(cdr (assq kind CONSTANT_Namespace)) ,name))))
;;; ns_set_info reader
(define read-ns_set_info (lambda (port)
(cons 'ns_set
(read-list0 (lambda (p) (read-id 'namespace p)) port))))
;;; multiname_info reader
(define CONSTANT_QName #x07)
(define CONSTANT_QNameA #x0D)
(define CONSTANT_RTQName #x0F)
(define CONSTANT_RTQNameA #x10)
(define CONSTANT_RTQNameL #x11)
(define CONSTANT_RTQNameLA #x12)
(define CONSTANT_Multiname #x09)
(define CONSTANT_MultinameA #x0E)
(define CONSTANT_MultinameL #x1B)
(define CONSTANT_MultinameLA #x1C)
(define multiname-attribute
(lambda (x) `(attribute ,x)))
(define read-multiname_kind_QName
(lambda (port)
(let ((namespace (read-id 'namespace port))
(string (read-id 'string port)))
`(,namespace ,string))))
(define read-multiname_kind_RTQName
(lambda (port) `(rtqname ,(read-id 'string port))))
(define read-multiname_kind_RTQNameL
(lambda (port) 'rtqnamel))
(define read-multiname_kind_Multiname
(lambda (port)
(let ((name (read-id 'string port))
(ns_set (read-id 'ns_set port)))
`(,ns_set ,name)))) ;; Note: the order is swapped.
(define read-multiname_kind_MultinameL
(lambda (port) `(multinamel ,(read-id 'ns_set port))))
(define transpose
(lambda (dict) (map (lambda (pair) (cons (cdr pair) (car pair))) dict)))
(define CONSTANT_Namespace_write (transpose CONSTANT_Namespace))
(define read-multiname_info (lambda (port)
(let ((kind (read-u8 port)))
(cond
((= kind CONSTANT_QName) (read-multiname_kind_QName port))
((= kind CONSTANT_QNameA) (multiname-attribute (read-multiname_kind_QName port)))
((= kind CONSTANT_RTQName) (read-multiname_kind_RTQName port))
((= kind CONSTANT_RTQNameA) (multiname-attribute (read-multiname_kind_RTQName port)))
((= kind CONSTANT_RTQNameL) (read-multiname_kind_RTQNameL port))
((= kind CONSTANT_RTQNameLA) (multiname-attribute (read-multiname_kind_RTQNameL port)))
((= kind CONSTANT_Multiname) (read-multiname_kind_Multiname port))
((= kind CONSTANT_MultinameA) (multiname-attribute (read-multiname_kind_Multiname port)))
((= kind CONSTANT_MultinameL) (read-multiname_kind_MultinameL port))
((= kind CONSTANT_MultinameLA) (multiname-attribute (read-multiname_kind_MultinameL port)))
))))
;;; cpool reader
(define read-cpool_info (lambda (port)
(let* ((integers (read-list1 read-s32 port))
(uintegers (read-list1 read-u32 port))
(doubles (read-list1 read-d64 port))
(strings (read-list1 read-string_info port))
(namespaces (read-list1 read-namespace_info port))
(ns_sets (read-list1 read-ns_set_info port))
(multinames (read-list1 read-multiname_info port)))
`((integer ,integers)
(uinteger ,uintegers)
(double ,doubles)
(string ,strings)
(namespace ,namespaces)
(ns_set ,ns_sets)
(multiname ,multinames)))))
;;; method_info reader
(define method-info-NEED_ARGUMENTS (lambda (flags) (positive? (bitwise-and flags #x01))))
(define method-info-NEED_ACTIVATION (lambda (flags) (positive? (bitwise-and flags #x02))))
(define method-info-NEED_REST (lambda (flags) (positive? (bitwise-and flags #x04))))
(define method-info-HAS_OPTIONAL (lambda (flags) (positive? (bitwise-and flags #x08))))
(define method-info-SET_DXNS (lambda (flags) (positive? (bitwise-and flags #x40))))
(define method-info-HAS_PARAM_NAMES (lambda (flags) (positive? (bitwise-and flags #x80))))
(define read-option_detail ; todo
(lambda (port)
`((val ,(read-u30 port))
(kind ,(read-u8 port)))))
(define read-option_info
(lambda (port) (read-list0 read-option_detail port)))
(define read-param_info
(lambda (param_count port) (build-list param_count (lambda (_) (read-u30 port)))))
(define read-method_info
(lambda (port)
(let* ((param_count (read-u30 port))
(return_type (read-id 'multiname port))
(param_type (build-list param_count (lambda (_) (read-id 'multiname port))))
(name (read-id 'string port))
(flags (read-u8 port))
(options (if (method-info-HAS_OPTIONAL flags) (read-option_info port) '()))
(param_names (if (method-info-HAS_PARAM_NAMES flags) (read-param_info param_count port) '())))
`((return_type ,return_type)
(param_type ,param_type)
(name ,name)
(flags ,flags)
(options ,options)
(param_names ,param_names)))))
;;; metadata_info reader
(define read-item_info
(lambda (port)
(let ((key (read-id 'string port))
(value (read-id 'string port)))
`((key ,key) (value ,value)))))
(define read-metadata_info
(lambda (port)
(let ((name (read-id 'string port))
(items (read-list0 read-item_info port)))
`((name ,name) (items ,items)))))
;;; traits_info reader
(define Trait_Slot 0)
(define Trait_Method 1)
(define Trait_Getter 2)
(define Trait_Setter 3)
(define Trait_Class 4)
(define Trait_Function 5)
(define Trait_Const 6)
(define read-trait_slot
(lambda (port)
(let* ((slot_id (read-u30 port))
(type_name (read-id 'multiname port))
(vindex (read-u30 port))
(vkind (if (= vindex 0) 0 (read-u8 port))))
`((slot_id ,slot_id)
(type_name ,type_name)
(vindex ,vindex)
(vkind ,vkind)))))
(define read-trait_class
(lambda (port)
(let ((slot_id (read-u30 port))
(classi (read-id 'class port)))
`((slot_id ,slot_id) (classi ,classi)))))
(define read-trait_function
(lambda (port)
(let ((slot_id (read-u30 port))
(function (read-u30 port)))
`((slot_id ,slot_id) (function ,function)))))
(define read-trait_method
(lambda (port)
(let ((disp_id (read-u30 port))
(method (read-id 'method port)))
`((disp_id ,disp_id) ,method))))
(define read-traits_info_data (lambda (name kind port)
(cond
((= kind Trait_Slot) (cons 'slot (read-trait_slot port)))
((= kind Trait_Method) (cons 'method (read-trait_method port)))
((= kind Trait_Getter) (cons 'getter (read-trait_method port)))
((= kind Trait_Setter) (cons 'setter (read-trait_method port)))
((= kind Trait_Class) (cons 'class (read-trait_class port)))
((= kind Trait_Function) (cons 'function (read-trait_function port)))
((= kind Trait_Const) (cons 'const (read-trait_slot port)))
(else (error (format "Unknown trait type ~a" kind)))
)))
(define read-traits_info (lambda (port)
(let* ((name (read-id 'multiname port))
(kind (read-u8 port))
(trait-type (bitwise-and kind #x0f))
(is-ATTR_Final (positive? (bitwise-and kind #x10)))
(is-ATTR_Override (positive? (bitwise-and kind #x20)))
(is-ATTR_Metadata (positive? (bitwise-and kind #x40)))
(data (read-traits_info_data name trait-type port))
(typename (car data))
(others (cdr data))
(metadata (if is-ATTR_Metadata
(read-list0 read-metadata_info port)
'())))
; (if (zero? name) (error "traits_info.name cannot be zero") '())
`((kind ,typename)
(name ,name)
,@others
(metadata ,metadata)))))
(define read-traits_info*
(lambda (port) (read-list0 read-traits_info port)))
;;; instance_info reader
(define CONSTANT_ClassSealed #x01)
(define CONSTANT_ClassFinal #x02)
(define CONSTANT_ClassInterface #x04)
(define CONSTANT_ClassProtectedNs #x08)
(define read-instance_info
(lambda (port)
(let* ((name (read-id 'multiname port))
(super_name (read-id 'multiname port))
(flags (read-u8 port))
(protectedNs (if (positive? (bitwise-and flags CONSTANT_ClassProtectedNs))
(read-u30 port)
'()))
(interface (read-list0 read-u30 port))
(iinit (read-id 'method port))
(trait (read-traits_info* port)))
`((name ,name)
(super_name ,super_name)
(flags ,flags)
(protectedNs (namespace ,protectedNs))
(interface ,(map (lambda (i) `(multiname ,i)) interface))
(iinit ,iinit)
(trait ,trait)
))))
;;; class_info reader
(define read-class_info
(lambda (port)
`((cinit ,(read-id 'method port))
(trait ,(read-traits_info* port)))))
;;; script_info reader
(define read-script_info
(lambda (port)
`((init ,(read-id 'method port))
(trait ,(read-traits_info* port)))))
;;; code reader
;; make a label from label symbol list and offset.
(define label-from-list
(lambda (destination labels)
(let ((found (memq destination labels)))
(if found
(string->symbol (string-append "L" (number->string (length found))))
#f))))
(define destination-from-args
(lambda (xs)
(if (and (not (null? xs))
(pair? (car xs))
(eq? (car (car xs)) 'offset))
(cadr (car xs))
#f)))
;; Extract and insert jump labels
(define insert-label
(lambda (code labels)
(if (null? code)
'()
(let* ((line (car code))
(pos (car line))
(inst (cadr line))
(args (cddr line))
(destination (destination-from-args args))
(*line (if destination
`(,pos ,inst ,(label-from-list destination labels))
line))
(dst-label (label-from-list pos labels))
(more (cons *line (insert-label (cdr code) labels))))
(if dst-label
(cons dst-label more)
more))
)))
;; label
(define find-label-new
(lambda (pos labels)
(if (memq pos labels)
labels
(cons pos labels))))
(define *find-label
(lambda (line labels)
(let* ((args (cddr line))
(offset (destination-from-args args)))
(if offset
(find-label-new offset labels)
labels))))
(define find-label-exception
(lambda (exception labels)
(let ((from (cadr (cadr (car exception))))
(to (cadr (cadr (cadr exception))))
(target (cadr (cadr (caddr exception)))))
(let* ((labels (find-label-new from labels))
(labels (find-label-new to labels))
(labels (find-label-new target labels)))
labels))))
(define insert-label-exception
(lambda (exception labels)
(let ((from (cadr (cadr (car exception))))
(to (cadr (cadr (cadr exception))))
(target (cadr (cadr (caddr exception))))
(rest (cdddr exception)))
`((from ,(label-from-list from labels))
(to ,(label-from-list to labels))
(target ,(label-from-list target labels))
. ,rest))))
(define make-label-exceptions
(lambda (exceptions)
(let ((labels (fold find-label-exception '() exceptions)))
(let ((labeled (map
(lambda (exception) (insert-label-exception exception labels))
exceptions)))
(cons labeled labels)))))
;; Return all jump addreses in reverse order
;; The result numbers are used for label index. For example,
;; 3 in (3 4 5) is L2 because 3 is the third in reverse order.
(define find-label
(lambda (code labels) (fold *find-label labels code)))
;; instruction reader
(define read-arguments
(lambda (spec pos port)
(let ((opname (car spec))
(types (cdr spec)))
(let ((args (map (lambda (type)
(cond
((eq? type 'u8) (read-u8 port))
((eq? type 'offset)
`(offset ,(+ 4 pos (read-s24 port))))
((eq? type 's24) (read-s24 port))
((eq? type 'u30) (read-u30 port))
((eq? type 'register) (read-u30 port))
((eq? type 'integer) (read-id 'integer port))
((eq? type 'uinteger) (read-id 'uinteger port))
((eq? type 'double) (read-id 'double port))
((eq? type 'string) (read-id 'string port))
((eq? type 'multiname) (read-id 'multiname port))
((eq? type 'method) (read-id 'method port))
(else (error (format "unknown param type ~a" type)))))
types)))
`(,pos ,opname ,@args)))))
(define read-instruction
(lambda (decoded port)
(let ((pos (file-position port))
(inst (read-byte port)))
(if (eof-object? inst)
decoded
(let ((spec (hash-ref read-instruction-map inst)))
(read-instruction
(cons (read-arguments spec pos port) decoded) port))))))
(define read-instructions
(lambda (port labels)
(let* ((code (reverse (read-instruction '() port)))
(labels (find-label code labels)))
(insert-label code labels))))
(define decode-instructions
(lambda (code labels)
(let ((code-port (open-input-bytes code)))
(read-instructions code-port labels))))
(define read-exception_info
(lambda (port)
(let ((from (read-id 'offset port))
(to (read-id 'offset port))
(target (read-id 'offset port))
(exc_type (read-id 'multiname port))
(var_name (read-id 'multiname port)))
`((from, from) (to ,to) (target ,target) (exc_type ,exc_type) (var_name ,var_name)))))
;;; method_body_info reader
(define read-method_body_info
(lambda (port)
(let* ((method (read-u30 port))
(max_stack (read-u30 port))
(local_count (read-u30 port))
(init_scope_depth (read-u30 port))
(max_scope_depth (read-u30 port))
(code_length (read-u30 port))
(code (read-bytes code_length port))
(exception (read-list0 read-exception_info port))
(trait (read-traits_info* port))
(labeled-pair (make-label-exceptions exception))
(labeled-exception (car labeled-pair))
(labels (cdr labeled-pair)))
`((method ,method)
(max_stack ,max_stack)
(local_count ,local_count)
(init_scope_depth ,init_scope_depth)
(max_scope_depth ,max_scope_depth)
(code ,(decode-instructions code labels))
(exception ,labeled-exception)
(trait ,trait)))))
;;; abcFile reader
(define read-abc
(lambda (port)
(let* ((minor_version (read-u16 port))
(major_version (read-u16 port))
(constant_pool (read-cpool_info port))
(method (read-list0 read-method_info port))
(metadata (read-list0 read-metadata_info port))
(class_count (read-u30 port))
(instance (build-list class_count (lambda (_) (read-instance_info port))))
(class (build-list class_count (lambda (_) (read-class_info port))))
(script (read-list0 read-script_info port))
(method_body (read-list0 read-method_body_info port))
)
`(abc
(minor_version ,minor_version)
(major_version ,major_version)
(constant_pool ,constant_pool)
(method ,method)
(metadata ,metadata)
(instance ,instance)
(class ,class)
(script ,script)
(method_body ,method_body)))))
;;;;;;;;;; ASM Form Decoder ;;;;;;;;;;
;;; ASM form
(define make-method-body-reference
(lambda (methods method-bodies index)
(if (null? methods)
'()
(let* ((method-body
(find (lambda (m)
(let ((id (car m))) ; The first element should be '(method id)
(= (cadr id) index)))
method-bodies))
(method-body1 (cdr method-body)) ; remove '(method id) in the method-body
(max_stack (assq 'max_stack method-body))
(local_count (assq 'local_count method-body))
(init_scope_depth (assq 'init_scope_depth method-body))
(max_scope_depth (assq 'max_scope_depth method-body))
(code (assq 'code method-body))
(exception (assq 'exception method-body))
(trait (assq 'trait method-body))
(method `((signature ,(car methods))
(hint (,max_stack ,local_count ,init_scope_depth ,max_scope_depth))
,code
,trait
,exception)))
(cons method
(make-method-body-reference (cdr methods) method-bodies (+ index 1)))))))
;; For some reason, a method_body refers a method instead of the other way.
;; So I have to make reverse reference by my hand.
(define set-method-body
(lambda (abc)
(let* ((method (assq 'method abc))
(method-bodies (cadr (assq 'method_body abc)))
(methods (make-method-body-reference (cadr method) method-bodies 0)))
(map (lambda (tag)
(if (eq? (car tag) 'method) `(method ,methods)
tag))
abc))))
;; find one-base index from the constant pool
(define find-constant
(lambda (type id default abc)
(let* ((pool (cadr (assq 'constant_pool abc)))
(table (cadr (assq type pool))))
(if (zero? id)
default
(decode-id (list-ref table (- id 1)) abc)))))
(define constant-id?
(lambda (e)
(and (pair? e)
(symbol? (car e))
(pair? (cdr e))
(number? (cadr e)))))
;;; Convert index expression to the literal
;;; (string 1) => "hello"
(define decode-id
(lambda (e abc)
(if (pair? e)
(if (constant-id? e)
(let ((key (car e))
(id (cadr e)))
(cond
((eq? key 'integer) (find-constant 'integer id 0 abc))
((eq? key 'uinteger) (find-constant 'uinteger id 0 abc))
((eq? key 'double) (find-constant 'double id 0.0 abc))
((eq? key 'string) (find-constant 'string id '* abc))
((eq? key 'namespace) (find-constant 'namespace id '* abc)) ; ???
((eq? key 'multiname) (find-constant 'multiname id '* abc)) ; ???
;; ((eq? key 'method) (find-element 'method id abc))
(else (cons (decode-id (car e) abc) (decode-id (cdr e) abc)))))
(cons (decode-id (car e) abc) (decode-id (cdr e) abc)))
e)))
; find zero-base index from a body
(define find-element
(lambda (type id abc)
(let* ((pool abc)
(table (cadr (assq type pool))))
(decode-id (list-ref table id) abc))))
(define to-asm
(lambda (abc)
(let* ((*abc (set-method-body (cdr abc)))
(**abc (decode-id *abc *abc))
(cpool (assq 'constant_pool **abc))
(ns_set (assq 'ns_set (cadr cpool)))
(method (assq 'method **abc))
(instance (assq 'instance **abc))
(class (assq 'class **abc))
(script (assq 'script **abc)))
(list 'asm ns_set method instance class script))))
;;;;;;;;;; ABC Form Writer ;;;;;;;;;;
;;; Primitive Data Writer
(define write-u8 write-byte)
(define write-s24 (lambda (i port)
(let* ((i0 (if (positive? i)
i
(+ i #x1000000)))
(b0 (bitwise-and i0 #xff))
(b1 (bitwise-and (arithmetic-shift i0 -8) #xff))
(b2 (bitwise-and (arithmetic-shift i0 -16) #xff)))
(write-byte b0 port)
(write-byte b1 port)
(write-byte b2 port))))
(define write-d64 (lambda (e port)
(write-bytes (real->floating-point-bytes e 8 #f) port)))
;; This can handle both 32 bits signed and unsigned integer
(define write-u32
(lambda (i port)
(let* ((sign (not (= 0 (bitwise-and i #x80000000))))
(rest (bitwise-and i #x7fffffff))
(e (bitwise-and (arithmetic-shift rest -28) #x7))
(d (bitwise-and (arithmetic-shift i -21) #x7f))
(c (bitwise-and (arithmetic-shift i -14) #x7f))
(b (bitwise-and (arithmetic-shift i -7) #x7f))
(a (bitwise-and i #x7f)))
(if (or (positive? b) (positive? c) (positive? d) (positive? e) sign)
(begin (write-byte (bitwise-ior a #x80) port)
(if (or (positive? c) (positive? d) (positive? e) sign)
(begin (write-byte (bitwise-ior b #x80) port)
(if (or (positive? d) (positive? e) sign)
(begin (write-byte (bitwise-ior c #x80) port)
(if (or (positive? e) sign)
(begin (write-byte (bitwise-ior d #x80) port)
(if sign
(write-byte (bitwise-ior e #x8) port)
(write-byte e port)))
(write-byte d port)))
(write-byte c port)))
(write-byte b port)))
(write-byte a port)))))
(define write-u16 (lambda (i port)
(let ((b0 (bitwise-and #xff i))
(b1 (arithmetic-shift i -8)))
(write-byte b0 port)
(write-byte b1 port))))
(define write-s32 write-u32)
(define write-u30 write-u32)
;; Write a u30 index with making sure if the type is correct and write the index.
(define write-id
(lambda (type x port)
(cond
((not (pair? x)) (error (format "Type error, expect: (~a i) given: ~a" type x)))
((eq? type (car x)) (write-u30 (cadr x) port))
(else (error (format "Type error, expect: (~a i) given: ~a" type x))))))
;;; List Writer
;; Write the number of entries, and entries itself by func.
;; There are two versions.
;; write-list1's count is the number of entries plus one that is used in cpool info.
;; write-list1's count is the number of entries.
(define write-list1
(lambda (func xs port)
(let ((n (add1 (length xs))))
(write-u30 n port)
(for-each (lambda (x) (func x port)) xs))))
(define write-list0
(lambda (func xs port)
(let ((n (length xs)))
(write-u30 n port)
(for-each (lambda (x) (func x port)) xs))))
;;; cpool_info writer
(define write-string_info (lambda (x port)
(let ((bytes (string->bytes x)))
(write-u30 (bytes-length bytes) port)
(write-bytes bytes port))))
(define write-namespace_info (lambda (x port)
(let ((kind (car x))
(name (cadr (cadr x))))
(write-u8 (cdr (assq kind CONSTANT_Namespace_write)) port)
(write-u30 name port))))
(define write-ns_set_info
(lambda (x port)
(let ((xs (cdr x)))
(write-list0 (lambda (x p) (write-u30 (cadr x) p)) xs port))))
(define write-multiname_kind_QName
(lambda (x port)
(let ((ns (cadr (car x)))
(name (cadr (cadr x))))
(write-u8 CONSTANT_QName port)
(write-u30 ns port)
(write-u30 name port))))
(define write-multiname_kind_Multiname
(lambda (x port)
(let ((ns_set (cadr (car x)))
(name (cadr (cadr x))))
(write-u8 CONSTANT_Multiname port)
(write-u30 name port)
(write-u30 ns_set port))))
(define write-multiname_kind_RTQName
(lambda (x port)
(let ((name (cadr x)))
(write-u8 CONSTANT_RTQName port)
(write-id 'string name port))))
(define write-multiname_kind_MultinameL
(lambda (x port)
(let ((ns_set (cadr x)))
(write-u8 CONSTANT_MultinameL port)
(write-id 'ns_set ns_set port))))
(define write-multiname_info (lambda (x port)
(cond
((eq? (car x) 'attribute) (error "attribute is not supported yet"))
((eq? (car x) 'rtqnamel) (error "RTQNameL is not supported yet"))
((eq? (car x) 'rtqname) (write-multiname_kind_RTQName x port))
((eq? (car x) 'ns_set) (error "MultinameL is not supported yet"))
((eq? (car x) 'multinamel) (write-multiname_kind_MultinameL x port))
((eq? (car (car x)) 'namespace) (write-multiname_kind_QName x port))
((eq? (car (car x)) 'ns_set) (write-multiname_kind_Multiname x port))
)))
(define write-cpool_info
(lambda (x port)
(write-list1 write-s32 (cadr (assq 'integer x)) port)
(write-list1 write-u32 (cadr (assq 'uinteger x)) port)
(write-list1 write-d64 (cadr (assq 'double x)) port)
(write-list1 write-string_info (cadr (assq 'string x)) port)
(write-list1 write-namespace_info (cadr (assq 'namespace x)) port)
(write-list1 write-ns_set_info (cadr (assq 'ns_set x)) port)
(write-list1 write-multiname_info (cadr (assq 'multiname x)) port)))
;;; method_info writer
;; Find a key in the dictionary and return only the value.
;; dictionary is like ((key0 value0) (key1 value1) ...)
;; The optional third argument is returned if the key is not found.
(define ref*
(lambda (key dictionary default)
(let ((found (assq key dictionary)))
(if found
(cadr found)
default))))
(define ref
(lambda (key dictionary)
(let ((found (ref* key dictionary '**not-found**)))
(if (eq? found '**not-found**)
(error (format "'~a' is not found in ~a." key dictionary))
found))))
(define write-method_info (lambda (x port)
(let* ((param_type (ref 'param_type x))
(param_count (length param_type)))
(write-u30 param_count port)
(write-id 'multiname (ref 'return_type x) port)
(for-each (lambda (type) (write-id 'multiname type port)) param_type) ;todo
(write-id 'string (ref 'name x) port)
(write-u8 (ref 'flags x) port)
(if (method-info-HAS_OPTIONAL (ref 'flags x)) (error "HAS_OPTIONAL is not supported yet") '()) ;todo
(if (method-info-HAS_PARAM_NAMES (ref 'flags x)) (error "HAS_OPTIONAL is not supported yet") '()) ;todo
)))
(define write-exception_info
(lambda (x port)
(write-id 'offset (ref 'from x) port)
(write-id 'offset (ref 'to x) port)
(write-id 'offset (ref 'target x) port)
(write-id 'multiname (ref 'exc_type x) port)
(write-id 'multiname (ref 'var_name x) port)))
;;; metadata_info writer
(define write-item_info
(lambda (x port)
(write-id 'string (ref 'key x) port)
(write-id 'string (ref 'value x) port)))
(define write-metadata_info
(lambda (x port)
(write-id 'string (ref 'name x) port)
(write-list0 write-item_info (ref 'items x) port)))
;;; traits_info writer
(define *write-traits_info
(lambda (func kind x port)
(let* ((name (ref 'name x))
(has-metadata (> (length (ref 'metadata x)) 0))
(kind* (if has-metadata
(bitwise-ior kind #x40) ; ATTR_Metadata
kind)))
(write-id 'multiname name port)
(write-u8 kind* port)
(func x port)
(if has-metadata
(write-list0 write-metadata_info (ref 'metadata x) port)
'()))))
(define write-trait_slot
(lambda (x port)
(let ((slot_id (ref 'slot_id x))
(type_name (ref 'type_name x))
(vindex (ref 'vindex x))
(vkind (if (= (ref 'vindex x) 0)
'()
(ref 'vkind x))))
(write-u30 slot_id port)
(write-id 'multiname type_name port)
(write-u30 vindex port)
(if (null? vkind)
'()
(begin (write-u8 vkind port)
(display "WARNING: Constant slots (vkind) is not supported.\n"))))))
(define write-trait_class
(lambda (x port)
(let ((slot_id (ref 'slot_id x))
(classi (ref 'classi x)))
(write-u30 slot_id port)
(write-id 'class classi port))))
(define write-trait_method
(lambda (x port)
(let ((disp_id (ref 'disp_id x))
(method (ref 'method x)))
(write-u30 disp_id port)
(write-u30 method port))))
(define write-traits_info
(lambda (x port)
(let ((kind (cadr (assq 'kind x))))
(cond
((eq? kind 'slot) (*write-traits_info write-trait_slot Trait_Slot x port))
((eq? kind 'method) (*write-traits_info write-trait_method Trait_Method x port))
((eq? kind 'getter) (*write-traits_info write-trait_slot Trait_Getter x port))
((eq? kind 'setter) (*write-traits_info write-trait_slot Trait_Setter x port))
((eq? kind 'class) (*write-traits_info write-trait_class Trait_Class x port))
((eq? kind 'function) (error "function trait is not supported yet"))
((eq? kind 'const) (error "const trait is not supported yet"))
(else (error (format "Unknown trait type ~a" kind)))))))
(define write-instance_info
(lambda (x port)
(let ((name (ref 'name x))
(super_name (ref 'super_name x))
(flags (ref 'flags x))
(interface (ref 'interface x))
(iinit (ref 'iinit x)))
(write-id 'multiname name port)
(write-id 'multiname super_name port)
(write-u8 flags port)
(if (positive? (bitwise-and flags CONSTANT_ClassProtectedNs))
(write-id 'namespace (ref 'protectedNs x) port)
'())
(write-list0 (lambda (x p) (write-id 'multiname x p)) interface port)
(write-id 'method iinit port)
(write-list0 write-traits_info (ref 'trait x) port)
)))
(define write-class_info
(lambda (x port)
(write-id 'method (ref 'cinit x) port)
(write-list0 write-traits_info (ref 'trait x) port)))
;;; script_info writer
(define write-script_info
(lambda (x port)
(write-id 'method (ref 'init x) port)
(write-list0 write-traits_info (ref 'trait x) port)))
;;; code writer
;; Return (inst args ...) without line number.
(define get-statement
(lambda (line)
(if (or (number? (car line))
(eq? (car line) '_))
(cdr line)
line)))
;; Caluculate instruction length
(define instruction-u30-length
(lambda (value)
(cond
((= (arithmetic-shift value -7) 0) 1)
((= (arithmetic-shift value -14) 0) 2)
((= (arithmetic-shift value -21) 0) 3)
((= (arithmetic-shift value -28) 0) 4)
(else 5))))
(define instruction-arg-length
(lambda (type value sum)
(+ sum
(cond
((eq? type 'u8) 1)
((eq? type 'offset) 3)
((memq type '(u30 register)) (instruction-u30-length value))
((memq type '(double integer method multiname string uinteger))
(instruction-u30-length (cadr value)))
(else (error (format "Unknown type (instruction-arg-length): ~a ~a" type value)))))))
(define instruction-length
(lambda (line)
(let* ((statement (get-statement line))
(inst (car statement))
(spec (hash-ref write-instruction-map inst))
(arg-types (cddr spec))
(arg-values (cdr statement))
(arg-length (fold2 instruction-arg-length 0 arg-types arg-values)))