aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/target/jvm/attribute/code.lux
blob: a350fde0f4f09b5f5eecf44c4d24cbb504e75ba9 (plain)
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
(.require
 [library
  [lux (.except Code)
   [abstract
    [equivalence (.only Equivalence)]]
   [data
    ["[0]" product]
    ["[0]" binary (.only Binary)]
    ["[0]" binary
     ["[1]F" \\format (.only Format) (.use "[1]#[0]" monoid)]]
    [collection
     ["[0]" sequence (.only Sequence) (.use "[1]#[0]" functor mix)]]]
   [math
    [number
     ["n" nat]]]]]
 ["[0]" ///
  [bytecode
   [environment
    ["[1][0]" limit (.only Limit)]]]
  [encoding
   ["[1][0]" unsigned (.only U2)]]]
 ["[0]" /
  ["[1][0]" exception (.only Exception)]])

(type .public (Code Attribute)
  (Record
   [#limit Limit
    #code Binary
    #exception_table (Sequence Exception)
    #attributes (Sequence Attribute)]))

(def .public (length length code)
  (All (_ Attribute) (-> (-> Attribute Nat) (Code Attribute) Nat))
  (all n.+
       ... u2 max_stack;
       ... u2 max_locals;
       ///limit.length
       ... u4 code_length;
       ///unsigned.bytes/4
       ... u1 code[code_length];
       (binary.size (the #code code))
       ... u2 exception_table_length;
       ///unsigned.bytes/2
       ... exception_table[exception_table_length];
       (|> code
           (the #exception_table)
           sequence.size
           (n.* /exception.length))
       ... u2 attributes_count;
       ///unsigned.bytes/2
       ... attribute_info attributes[attributes_count];
       (|> code
           (the #attributes)
           (sequence#each length)
           (sequence#mix n.+ 0))))

(def .public (equivalence attribute_equivalence)
  (All (_ attribute)
    (-> (Equivalence attribute) (Equivalence (Code attribute))))
  (all product.equivalence
       ///limit.equivalence
       binary.equivalence
       (sequence.equivalence /exception.equivalence)
       (sequence.equivalence attribute_equivalence)
       ))

... https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.3
(def .public (format format code)
  (All (_ Attribute) (-> (Format Attribute) (Format (Code Attribute))))
  (all binaryF#composite
       ... u2 max_stack;
       ... u2 max_locals;
       (///limit.format (the #limit code))
       ... u4 code_length;
       ... u1 code[code_length];
       (binaryF.binary_32 (the #code code))
       ... u2 exception_table_length;
       ... exception_table[exception_table_length];
       ((binaryF.sequence_16 /exception.format) (the #exception_table code))
       ... u2 attributes_count;
       ... attribute_info attributes[attributes_count];
       ((binaryF.sequence_16 format) (the #attributes code))
       ))