aboutsummaryrefslogtreecommitdiff
path: root/src/lux/compiler/lux.clj
blob: 14d34e6dc6a186f5fe3d776e4286aec35e0262aa (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
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
;;  Copyright (c) Eduardo Julian. All rights reserved.
;;  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
;;  If a copy of the MPL was not distributed with this file,
;;  You can obtain one at http://mozilla.org/MPL/2.0/.

(ns lux.compiler.lux
  (:require (clojure [string :as string]
                     [set :as set]
                     [template :refer [do-template]])
            clojure.core.match
            clojure.core.match.array
            (lux [base :as & :refer [|do return* return fail fail* |let |case]]
                 [type :as &type]
                 [lexer :as &lexer]
                 [parser :as &parser]
                 [analyser :as &analyser]
                 [host :as &host])
            [lux.host.generics :as &host-generics]
            (lux.analyser [base :as &a]
                          [module :as &a-module]
                          [meta :as &a-meta])
            (lux.compiler [base :as &&]
                          [lambda :as &&lambda]
                          [type :as &&type]))
  (:import (org.objectweb.asm Opcodes
                              Label
                              ClassWriter
                              MethodVisitor)))

;; [Exports]
(defn compile-bool [compile ?value]
  (|do [^MethodVisitor *writer* &/get-writer
        :let [_ (.visitFieldInsn *writer* Opcodes/GETSTATIC "java/lang/Boolean" (if ?value "TRUE" "FALSE") "Ljava/lang/Boolean;")]]
    (return &/unit-tag)))

(do-template [<name> <class> <sig> <caster>]
  (defn <name> [compile value]
    (|do [^MethodVisitor *writer* &/get-writer
          :let [_ (doto *writer*
                    (.visitTypeInsn Opcodes/NEW <class>)
                    (.visitInsn Opcodes/DUP)
                    (.visitLdcInsn (<caster> value))
                    (.visitMethodInsn Opcodes/INVOKESPECIAL <class> "<init>" <sig>))]]
      (return &/unit-tag)))

  compile-int  "java/lang/Long"      "(J)V" long
  compile-real "java/lang/Double"    "(D)V" double
  compile-char "java/lang/Character" "(C)V" char
  )

(defn compile-text [compile ?value]
  (|do [^MethodVisitor *writer* &/get-writer
        :let [_ (.visitLdcInsn *writer* ?value)]]
    (return &/unit-tag)))

(defn compile-tuple [compile ?elems]
  (|do [^MethodVisitor *writer* &/get-writer
        :let [num-elems (&/|length ?elems)]]
    (|case num-elems
      0
      (|do [:let [_ (.visitLdcInsn *writer* &/unit-tag)]]
        (return &/unit-tag))

      1
      (compile (&/|head ?elems))
      
      _
      (|do [:let [_ (doto *writer*
                      (.visitLdcInsn (int (inc num-elems)))
                      (.visitTypeInsn Opcodes/ANEWARRAY "java/lang/Object"))]
            _ (&/map2% (fn [idx elem]
                         (|do [:let [_ (doto *writer*
                                         (.visitInsn Opcodes/DUP)
                                         (.visitLdcInsn (int idx)))]
                               ret (compile elem)
                               :let [_ (.visitInsn *writer* Opcodes/AASTORE)]]
                           (return ret)))
                       (&/|range num-elems) ?elems)
            :let [_ (doto *writer*
                      (.visitInsn Opcodes/DUP)
                      (.visitLdcInsn (int num-elems))
                      (.visitLdcInsn &/product-tag)
                      (.visitInsn Opcodes/AASTORE))]]
        (return &/unit-tag)))))

(defn compile-variant [compile ?tag tail? ?value]
  (|do [^MethodVisitor *writer* &/get-writer
        :let [_ (doto *writer*
                  (.visitLdcInsn (int 4))
                  (.visitTypeInsn Opcodes/ANEWARRAY "java/lang/Object")
                  (.visitInsn Opcodes/DUP)
                  (.visitLdcInsn (int 0))
                  (.visitLdcInsn &/sum-tag)
                  (.visitInsn Opcodes/AASTORE)
                  (.visitInsn Opcodes/DUP)
                  (.visitLdcInsn (int 1))
                  (.visitLdcInsn (int ?tag))
                  (&&/wrap-int)
                  (.visitInsn Opcodes/AASTORE)
                  (.visitInsn Opcodes/DUP)
                  (.visitLdcInsn (int 2))
                  (.visitFieldInsn Opcodes/GETSTATIC "java/lang/Boolean" (if tail? "TRUE" "FALSE") "Ljava/lang/Boolean;")
                  (.visitInsn Opcodes/AASTORE)
                  (.visitInsn Opcodes/DUP)
                  (.visitLdcInsn (int 3)))]
        _ (compile ?value)
        :let [_ (.visitInsn *writer* Opcodes/AASTORE)]]
    (return &/unit-tag)))

(defn compile-local [compile ?idx]
  (|do [^MethodVisitor *writer* &/get-writer
        :let [_ (.visitVarInsn *writer* Opcodes/ALOAD (int ?idx))]]
    (return &/unit-tag)))

(defn compile-captured [compile ?scope ?captured-id ?source]
  (|do [^MethodVisitor *writer* &/get-writer
        :let [_ (doto *writer*
                  (.visitVarInsn Opcodes/ALOAD 0)
                  (.visitFieldInsn Opcodes/GETFIELD
                                   (str (&host/->module-class (&/|head ?scope)) "/" (&host/location (&/|tail ?scope)))
                                   (str &&/closure-prefix ?captured-id)
                                   "Ljava/lang/Object;"))]]
    (return &/unit-tag)))

(defn compile-global [compile ?owner-class ?name]
  (|do [^MethodVisitor *writer* &/get-writer
        :let [_ (.visitFieldInsn *writer* Opcodes/GETSTATIC (str (&host/->module-class ?owner-class) "/" (&host/def-name ?name)) &/value-field "Ljava/lang/Object;")]]
    (return &/unit-tag)))

(defn compile-apply [compile ?fn ?args]
  (|do [^MethodVisitor *writer* &/get-writer
        _ (compile ?fn)
        _ (&/map% (fn [?arg]
                    (|do [=arg (compile ?arg)
                          :let [_ (.visitMethodInsn *writer* Opcodes/INVOKEINTERFACE &&/function-class "apply" &&/apply-signature)]]
                      (return =arg)))
                  ?args)]
    (return &/unit-tag)))

(defn ^:private compile-def-type [compile ?body]
  (|do [:let [?def-type (|case ?body
                          [[?def-type ?def-cursor] (&a/$ann ?def-value ?type-expr ?def-value-type)]
                          ?type-expr

                          [[?def-type ?def-cursor] ?def-value]
                          (if (&type/type= &type/Type ?def-type)
                            (&/T [(&/T [?def-type ?def-cursor])
                                  (&/V &a/$tuple (&/|list))])
                            (&&type/type->analysis ?def-type)))]]
    (compile ?def-type)))

(defn ^:private compile-def-meta [compile ?meta]
  (|let [analysis (&&type/defmeta->analysis ?meta)]
    (compile analysis)))

(let [class-flags (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_SUPER)
      field-flags (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC)]
  (defn compile-def [compile ?name ?body ?meta]
    (|do [module-name &/get-module-name
          class-loader &/loader]
      (|case (&a-meta/meta-get &a-meta/alias-tag ?meta)
        (&/$Some (&/$IdentM [r-module r-name]))
        (if (= 1 (&/|length ?meta))
          (|do [:let [current-class (&host-generics/->class-name (str (&host/->module-class r-module) "/" (&host/def-name r-name)))
                      def-class (&&/load-class! class-loader current-class)
                      def-type (-> def-class (.getField &/type-field) (.get nil))
                      def-meta ?meta
                      def-value (-> def-class (.getField &/value-field) (.get nil))]
                _ (&a-module/define module-name ?name def-type def-meta def-value)]
            (return &/unit-tag))
          (fail (str "[Compilation Error] Aliases cannot contain meta-data: " module-name ";" ?name)))

        (&/$Some _)
        (fail "[Compilation Error] Invalid syntax for lux;alias meta-data. Must be an Ident.")
        
        _
        (|do [:let [=value-type (&a/expr-type* ?body)]
              ^ClassWriter *writer* &/get-writer
              [file-name _ _] &/cursor
              :let [datum-sig "Ljava/lang/Object;"
                    def-name (&host/def-name ?name)
                    current-class (str (&host/->module-class module-name) "/" def-name)
                    =class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
                             (.visit &host/bytecode-version class-flags
                                     current-class nil "java/lang/Object" (into-array [&&/function-class]))
                             (-> (.visitField field-flags &/name-field "Ljava/lang/String;" nil ?name)
                                 (doto (.visitEnd)))
                             (-> (.visitField field-flags &/type-field datum-sig nil nil)
                                 (doto (.visitEnd)))
                             (-> (.visitField field-flags &/meta-field datum-sig nil nil)
                                 (doto (.visitEnd)))
                             (-> (.visitField field-flags &/value-field datum-sig nil nil)
                                 (doto (.visitEnd)))
                             (.visitSource file-name nil))]
              _ (&/with-writer (.visitMethod =class Opcodes/ACC_PUBLIC "<clinit>" "()V" nil nil)
                  (|do [^MethodVisitor **writer** &/get-writer
                        :let [_ (.visitCode **writer**)]
                        _ (compile-def-type compile ?body)
                        :let [_ (.visitFieldInsn **writer** Opcodes/PUTSTATIC current-class &/type-field datum-sig)]
                        _ (compile-def-meta compile ?meta)
                        :let [_ (.visitFieldInsn **writer** Opcodes/PUTSTATIC current-class &/meta-field datum-sig)]
                        _ (compile ?body)
                        :let [_ (.visitFieldInsn **writer** Opcodes/PUTSTATIC current-class &/value-field datum-sig)]
                        :let [_ (doto **writer**
                                  (.visitInsn Opcodes/RETURN)
                                  (.visitMaxs 0 0)
                                  (.visitEnd))]]
                    (return &/unit-tag)))
              :let [_ (.visitEnd *writer*)]
              _ (&&/save-class! def-name (.toByteArray =class))
              :let [def-class (&&/load-class! class-loader (&host-generics/->class-name current-class))
                    [def-type is-type?] (|case (&a-meta/meta-get &a-meta/type?-tag ?meta)
                                          (&/$Some (&/$BoolM true))
                                          (&/T [&type/Type
                                                true])

                                          _
                                          (if (&type/type= &type/Type =value-type)
                                            (&/T [&type/Type
                                                  false])
                                            (&/T [(-> def-class (.getField &/type-field) (.get nil))
                                                  false])))
                    def-meta ?meta
                    def-value (-> def-class (.getField &/value-field) (.get nil))]
              _ (&a-module/define module-name ?name def-type def-meta def-value)
              _ (|case (&/T [is-type? (&a-meta/meta-get &a-meta/tags-tag def-meta)])
                  [true (&/$Some (&/$ListM tags*))]
                  (|do [tags (&/map% (fn [tag*]
                                       (|case tag*
                                         (&/$TextM tag)
                                         (return tag)

                                         _
                                         (fail "[Compiler Error] Incorrect format for tags.")))
                                     tags*)
                        _ (&a-module/declare-tags module-name tags def-value)]
                    (return &/unit-tag))

                  [false (&/$Some _)]
                  (fail "[Compiler Error] Can't define tags for non-type.")

                  [true (&/$Some _)]
                  (fail "[Compiler Error] Incorrect format for tags.")

                  [_ (&/$None)]
                  (return &/unit-tag))
              :let [_ (println 'DEF (str module-name ";" ?name))]]
          (return &/unit-tag))))))

(defn compile-ann [compile ?value-ex ?type-ex ?value-type]
  (compile ?value-ex))

(defn compile-coerce [compile ?value-ex ?type-ex ?value-type]
  (compile ?value-ex))