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

(type: .public (Code Attribute)
  {#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\map length)
          (row\fold 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\compose
      ... 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))
      ))