aboutsummaryrefslogtreecommitdiff
path: root/src/lux/compiler/lux.clj
blob: a761f431a22556f34022d385934f5ac2fb46a13a (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
(ns lux.compiler.lux
  (:require (clojure [string :as string]
                     [set :as set]
                     [template :refer [do-template]])
            [clojure.core.match :as M :refer [matchv]]
            clojure.core.match.array
            (lux [base :as & :refer [exec return* return fail fail* |let]]
                 [type :as &type]
                 [lexer :as &lexer]
                 [parser :as &parser]
                 [analyser :as &analyser]
                 [host :as &host])
            [lux.analyser.base :as &a]
            (lux.compiler [base :as &&]
                          [lambda :as &&lambda])
            ;; :reload
            )
  (:import (org.objectweb.asm Opcodes
                              Label
                              ClassWriter
                              MethodVisitor)))

;; [Exports]
(let [+class+ (&host/->class "java.lang.Boolean")
      +sig+ (&host/->type-signature "java.lang.Boolean")]
  (defn compile-bool [compile *type* ?value]
    (exec [*writer* &/get-writer
           :let [_ (.visitFieldInsn *writer* Opcodes/GETSTATIC (&host/->class "java.lang.Boolean") (if ?value "TRUE" "FALSE") (&host/->type-signature "java.lang.Boolean"))]]
      (return nil))))

(do-template [<name> <class> <sig> <caster>]
  (let [+class+ (&host/->class <class>)]
    (defn <name> [compile *type* value]
      (exec [*writer* &/get-writer
             :let [_ (doto *writer*
                       (.visitTypeInsn Opcodes/NEW +class+)
                       (.visitInsn Opcodes/DUP)
                       (.visitLdcInsn (<caster> value))
                       (.visitMethodInsn Opcodes/INVOKESPECIAL +class+ "<init>" <sig>))]]
        (return nil))))

  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 *type* ?value]
  (exec [*writer* &/get-writer
         :let [_ (.visitLdcInsn *writer* ?value)]]
    (return nil)))

(defn compile-tuple [compile *type* ?elems]
  (exec [*writer* &/get-writer
         :let [num-elems (&/|length ?elems)
               _ (doto *writer*
                   (.visitLdcInsn (int num-elems))
                   (.visitTypeInsn Opcodes/ANEWARRAY (&host/->class "java.lang.Object")))]
         _ (&/map% (fn [idx+elem]
                     (|let [[idx elem] idx+elem]
                       (exec [:let [_ (doto *writer*
                                        (.visitInsn Opcodes/DUP)
                                        (.visitLdcInsn (int idx)))]
                              ret (compile elem)
                              :let [_ (.visitInsn *writer* Opcodes/AASTORE)]]
                         (return ret))))
                   (&/zip2 (&/|range num-elems) ?elems))]
    (return nil)))

(defn compile-record [compile *type* ?elems]
  (exec [*writer* &/get-writer
         :let [num-elems (&/|length ?elems)
               _ (doto *writer*
                   (.visitLdcInsn (int (* 2 num-elems)))
                   (.visitTypeInsn Opcodes/ANEWARRAY (&host/->class "java.lang.Object")))]
         _ (&/map% (fn [idx+kv]
                     (|let [[idx [k v]] idx+kv]
                       (exec [:let [idx* (* 2 idx)
                                    _ (doto *writer*
                                        (.visitInsn Opcodes/DUP)
                                        (.visitLdcInsn (int idx*))
                                        (.visitLdcInsn k)
                                        (.visitInsn Opcodes/AASTORE))]
                              :let [_ (doto *writer*
                                        (.visitInsn Opcodes/DUP)
                                        (.visitLdcInsn (int (inc idx*))))]
                              ret (compile v)
                              :let [_ (.visitInsn *writer* Opcodes/AASTORE)]]
                         (return ret))))
                   (&/zip2 (&/|range num-elems) ?elems))]
    (return nil)))

(defn compile-variant [compile *type* ?tag ?value]
  (exec [*writer* &/get-writer
         :let [_ (doto *writer*
                   (.visitLdcInsn (int 2))
                   (.visitTypeInsn Opcodes/ANEWARRAY (&host/->class "java.lang.Object"))
                   (.visitInsn Opcodes/DUP)
                   (.visitLdcInsn (int 0))
                   (.visitLdcInsn ?tag)
                   (.visitInsn Opcodes/AASTORE)
                   (.visitInsn Opcodes/DUP)
                   (.visitLdcInsn (int 1)))]
         _ (compile ?value)
         :let [_ (.visitInsn *writer* Opcodes/AASTORE)]]
    (return nil)))

(defn compile-local [compile *type* ?idx]
  (exec [*writer* &/get-writer
         :let [_ (.visitVarInsn *writer* Opcodes/ALOAD (int ?idx))]]
    (return nil)))

(defn compile-captured [compile *type* ?scope ?captured-id ?source]
  ;; (prn 'compile-captured ?scope ?captured-id)
  (exec [*writer* &/get-writer
         :let [_ (doto *writer*
                   (.visitVarInsn Opcodes/ALOAD 0)
                   (.visitFieldInsn Opcodes/GETFIELD
                                    (&host/location ?scope)
                                    (str &&/closure-prefix ?captured-id)
                                    "Ljava/lang/Object;"))]]
    (return nil)))

(defn compile-global [compile *type* ?owner-class ?name]
  (exec [*writer* &/get-writer
         :let [_ (.visitFieldInsn *writer* Opcodes/GETSTATIC (&host/->class (&host/location (&/|list ?owner-class ?name))) "_datum" "Ljava/lang/Object;")]]
    (return nil)))

(defn compile-apply [compile *type* ?fn ?arg]
  (exec [*writer* &/get-writer
         _ (compile ?fn)
         _ (compile ?arg)
         :let [_ (.visitMethodInsn *writer* Opcodes/INVOKEINTERFACE (&host/->class &host/function-class) "apply" &&/apply-signature)]]
    (return nil)))

(defn compile-def [compile ?name ?body]
  (exec [*writer* &/get-writer
         module-name &/get-module-name
         :let [outer-class (&host/->class module-name)
               datum-sig (&host/->type-signature "java.lang.Object")
               current-class (&host/location (&/|list outer-class ?name))
               _ (.visitInnerClass *writer* current-class outer-class nil (+ Opcodes/ACC_STATIC Opcodes/ACC_SYNTHETIC))
               =class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
                        (.visit Opcodes/V1_5 (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_SUPER)
                                current-class nil "java/lang/Object" (into-array [(&host/->class &host/function-class)]))
                        (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) "_datum" datum-sig nil nil)
                            (doto (.visitEnd))))]
         ;; :let [_ (prn 'compile-def/pre-body)]
         _ (&/with-writer (.visitMethod =class Opcodes/ACC_PUBLIC "<clinit>" "()V" nil nil)
             (exec [*writer* &/get-writer
                    :let [_ (.visitCode *writer*)]
                    ;; :let [_ (prn 'compile-def/pre-body2)]
                    _ (compile ?body)
                    ;; :let [_ (prn 'compile-def/post-body2)]
                    :let [_ (doto *writer*
                              (.visitFieldInsn Opcodes/PUTSTATIC current-class "_datum" datum-sig)
                              (.visitInsn Opcodes/RETURN)
                              (.visitMaxs 0 0)
                              (.visitEnd))]]
               (return nil)))
         ;; :let [_ (prn 'compile-def/post-body)]
         :let [_ (.visitEnd *writer*)]
         ;; :let [_ (prn 'compile-def/_1 ?name current-class)]
         _ (&&/save-class! current-class (.toByteArray =class))
         ;; :let [_ (prn 'compile-def/_2 ?name)]
         ]
    (return nil)))