aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/attribute/code.lux
blob: 80be8c567668769c3aeeac128450f3ffa007a585 (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
(.module:
  [library
   [lux {"-" [Code]}
    [abstract
     [equivalence {"+" [Equivalence]}]]
    [data
     ["[0]" product]
     ["[0]" binary {"+" [Binary]}]
     [format
      ["[0]F" binary {"+" [Writer]} ("[1]#[0]" monoid)]]
     [collection
      ["[0]" row {"+" [Row]} ("[1]#[0]" functor mix)]]]
    [math
     [number
      ["n" nat]]]]]
  ["[0]" /// "_"
   [bytecode
    [environment
     ["[1][0]" limit {"+" [Limit]}]]]
   [encoding
    ["[1][0]" unsigned {"+" [U2]}]]]
  ["[0]" / "_"
   ["[1][0]" exception {"+" [Exception]}]])

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

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

(def: .public (equivalence attribute_equivalence)
  (All (_ attribute)
    (-> (Equivalence attribute) (Equivalence (Code attribute))))
  ($_ product.equivalence
      ///limit.equivalence
      binary.equivalence
      (row.equivalence /exception.equivalence)
      (row.equivalence attribute_equivalence)
      ))

... https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.3
(def: .public (writer writer code)
  (All (_ Attribute) (-> (Writer Attribute) (Writer (Code Attribute))))
  ($_ binaryF#composite
      ... u2 max_stack;
      ... u2 max_locals;
      (///limit.writer (value@ #limit code))
      ... u4 code_length;
      ... u1 code[code_length];
      (binaryF.binary/32 (value@ #code code))
      ... u2 exception_table_length;
      ... exception_table[exception_table_length];
      ((binaryF.row/16 /exception.writer) (value@ #exception_table code))
      ... u2 attributes_count;
      ... attribute_info attributes[attributes_count];
      ((binaryF.row/16 writer) (value@ #attributes code))
      ))