aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/luxc/lang/translation/jvm/primitive.lux
blob: d8ab2cbee129428810089122e8500517c18d4d33 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(.module:
  [lux (#- i64)
   ["." host (#+ import:)]
   [data
    [number
     ["i" int]]]
   [target
    [jvm
     ["." type]]]
   [tool
    [compiler
     [phase ("operation@." monad)]]]]
  [luxc
   [lang
    [host
     ["." jvm (#+ Inst Operation)
      ["_" inst]]]]])

(def: #export bit
  (-> Bit (Operation Inst))
  (let [Boolean (type.class "java.lang.Boolean" (list))]
    (function (_ value)
      (operation@wrap (_.GETSTATIC Boolean (if value "TRUE" "FALSE") Boolean)))))

(import: java/lang/Byte
  (#static MAX_VALUE byte)
  (#static MIN_VALUE byte))

(import: java/lang/Short
  (#static MAX_VALUE short)
  (#static MIN_VALUE short))

(def: #export (i64 value)
  (-> (I64 Any) (Operation Inst))
  (case (.int value)
    (^template [<int> <instruction>]
      <int>
      (operation@wrap (|>> <instruction> (_.wrap type.long))))
    ([+0 _.LCONST_0]
     [+1 _.LCONST_1])

    (^template [<int> <instruction>]
      <int>
      (operation@wrap (|>> <instruction> _.I2L (_.wrap type.long))))
    ([-1 _.ICONST_M1]
     ## [+0 _.ICONST_0]
     ## [+1 _.ICONST_1]
     [+2 _.ICONST_2]
     [+3 _.ICONST_3]
     [+4 _.ICONST_4]
     [+5 _.ICONST_5])

    value
    (let [constantI (cond (and (i.>= (java/lang/Byte::MIN_VALUE) value)
                               (i.<= (java/lang/Byte::MAX_VALUE) value))
                          (|>> (_.BIPUSH value) _.I2L)
                          
                          (and (i.>= (java/lang/Short::MIN_VALUE) value)
                               (i.<= (java/lang/Short::MAX_VALUE) value))
                          (|>> (_.SIPUSH value) _.I2L)

                          ## else
                          (|> value .int _.long))]
      (operation@wrap (|>> constantI (_.wrap type.long))))))

(import: java/lang/Double
  (#static doubleToRawLongBits #manual [double] int))

(def: d0-bits
  Int
  (java/lang/Double::doubleToRawLongBits +0.0))

(def: #export (f64 value)
  (-> Frac (Operation Inst))
  (case value
    (^template [<int> <instruction>]
      <int>
      (operation@wrap (|>> <instruction> (_.wrap type.double))))
    ([+1.0 _.DCONST_1])

    (^template [<int> <instruction>]
      <int>
      (operation@wrap (|>> <instruction> _.F2D (_.wrap type.double))))
    ([+2.0 _.FCONST_2])

    (^template [<int> <instruction>]
      <int>
      (operation@wrap (|>> <instruction> _.I2D (_.wrap type.double))))
    ([-1.0 _.ICONST_M1]
     ## [+0.0 _.ICONST_0]
     ## [+1.0 _.ICONST_1]
     [+2.0 _.ICONST_2]
     [+3.0 _.ICONST_3]
     [+4.0 _.ICONST_4]
     [+5.0 _.ICONST_5])
    
    _
    (let [constantI (if (i.= ..d0-bits
                             (java/lang/Double::doubleToRawLongBits (:coerce java/lang/Double value)))
                      _.DCONST_0
                      (_.double value))]
      (operation@wrap (|>> constantI (_.wrap type.double))))))

(def: #export (text value)
  (-> Text (Operation Inst))
  (operation@wrap (_.string value)))