Skip to content

Commit 0021f80

Browse files
committed
#79 Compile Blodwen for JVM
1 parent 6e8683a commit 0021f80

File tree

32 files changed

+1294
-198
lines changed

32 files changed

+1294
-198
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Idris
55
*.ibc
6+
.idrisjvmtypes*
67

78
# IDE
89
.idea

idris-jvm-core/src/main/idris/IdrisJvm/Core/Asm.idr

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,11 @@ Show InferredType where
9494
show IUnknown = "unknown"
9595

9696
Semigroup InferredType where
97-
IUnknown <+> known = known
98-
known <+> IUnknown = known
9997
ty1 <+> ty2 = if ty1 == ty2 then ty1 else inferredObjectType
10098

10199
Monoid InferredType where
102100
neutral = IUnknown
103101

104-
toFrameLocalVarType : InferredType -> String
105-
toFrameLocalVarType IBool = "INTEGER"
106-
toFrameLocalVarType IByte = "INTEGER"
107-
toFrameLocalVarType IChar = "INTEGER"
108-
toFrameLocalVarType IShort = "INTEGER"
109-
toFrameLocalVarType IInt = "INTEGER"
110-
toFrameLocalVarType ILong = "LONG"
111-
toFrameLocalVarType IFloat = "FLOAT"
112-
toFrameLocalVarType IDouble = "DOUBLE"
113-
toFrameLocalVarType (Ref clsName) = clsName
114-
toFrameLocalVarType (IArray elemTy) = "[" ++ toFrameLocalVarType elemTy
115-
toFrameLocalVarType _ = "java/lang/Object"
116-
117-
toFrameLocalVarTypes : InferredTypeStore -> List String
118-
toFrameLocalVarTypes types = toFrameLocalVarType <$> (values types)
119-
120102
isPrimitive : InferredType -> Bool
121103
isPrimitive IBool = True
122104
isPrimitive IByte = True
@@ -507,4 +489,4 @@ isExportedDesc _ = False
507489

508490
isExportedReturnDesc : TypeDescriptor -> Bool
509491
isExportedReturnDesc (FieldDescriptor (FieldTyDescReference (IdrisExportDesc _))) = True
510-
isExportedReturnDesc _ = False
492+
isExportedReturnDesc _ = False

idris-jvm-core/src/main/idris/IdrisJvm/Core/Common.idr

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,37 @@ metafactoryDesc =
116116
unrolledConstructorPropsCount : Nat
117117
unrolledConstructorPropsCount = 10
118118

119-
idrisObjectProperty : Int -> Int -> Asm ()
120-
idrisObjectProperty object propertyIndex = do
121-
Aload object
122-
Iconst propertyIndex
123-
InvokeMethod InvokeStatic idrisObjectType "getProperty" "(Ljava/lang/Object;I)Ljava/lang/Object;" False
119+
inferredTypeToFieldTypeDesc : InferredType -> FieldTypeDescriptor
120+
inferredTypeToFieldTypeDesc IBool = FieldTyDescBoolean
121+
inferredTypeToFieldTypeDesc IByte = FieldTyDescByte
122+
inferredTypeToFieldTypeDesc IChar = FieldTyDescChar
123+
inferredTypeToFieldTypeDesc IShort = FieldTyDescShort
124+
inferredTypeToFieldTypeDesc IInt = FieldTyDescInt
125+
inferredTypeToFieldTypeDesc ILong = FieldTyDescLong
126+
inferredTypeToFieldTypeDesc IFloat = FieldTyDescFloat
127+
inferredTypeToFieldTypeDesc IDouble = FieldTyDescDouble
128+
inferredTypeToFieldTypeDesc (Ref refTy) = FieldTyDescReference (ClassDesc refTy)
129+
inferredTypeToFieldTypeDesc IUnknown = FieldTyDescReference (ClassDesc "java/lang/Object")
130+
inferredTypeToFieldTypeDesc (IArray elemTy) = FieldTyDescReference (ArrayDesc $ inferredTypeToFieldTypeDesc elemTy)
131+
132+
getInferredTyDesc : InferredType -> String
133+
getInferredTyDesc = asmFieldTypeDesc . inferredTypeToFieldTypeDesc
134+
135+
toFrameLocalVarType : InferredType -> String
136+
toFrameLocalVarType IBool = "INTEGER"
137+
toFrameLocalVarType IByte = "INTEGER"
138+
toFrameLocalVarType IChar = "INTEGER"
139+
toFrameLocalVarType IShort = "INTEGER"
140+
toFrameLocalVarType IInt = "INTEGER"
141+
toFrameLocalVarType ILong = "LONG"
142+
toFrameLocalVarType IFloat = "FLOAT"
143+
toFrameLocalVarType IDouble = "DOUBLE"
144+
toFrameLocalVarType (Ref clsName) = clsName
145+
toFrameLocalVarType (IArray elemTy) = "[" ++ getInferredTyDesc elemTy
146+
toFrameLocalVarType _ = "java/lang/Object"
147+
148+
toFrameLocalVarTypes : InferredTypeStore -> List String
149+
toFrameLocalVarTypes types = toFrameLocalVarType <$> (values types)
124150

125151
fullFrame : Asm ()
126152
fullFrame = do
@@ -156,22 +182,6 @@ invokeError x = do
156182
getPrimitiveClass : String -> Asm ()
157183
getPrimitiveClass clazz = Field FGetStatic clazz "TYPE" "Ljava/lang/Class;"
158184

159-
inferredTypeToFieldTypeDesc : InferredType -> FieldTypeDescriptor
160-
inferredTypeToFieldTypeDesc IBool = FieldTyDescBoolean
161-
inferredTypeToFieldTypeDesc IByte = FieldTyDescByte
162-
inferredTypeToFieldTypeDesc IChar = FieldTyDescChar
163-
inferredTypeToFieldTypeDesc IShort = FieldTyDescShort
164-
inferredTypeToFieldTypeDesc IInt = FieldTyDescInt
165-
inferredTypeToFieldTypeDesc ILong = FieldTyDescLong
166-
inferredTypeToFieldTypeDesc IFloat = FieldTyDescFloat
167-
inferredTypeToFieldTypeDesc IDouble = FieldTyDescDouble
168-
inferredTypeToFieldTypeDesc (Ref refTy) = FieldTyDescReference (ClassDesc refTy)
169-
inferredTypeToFieldTypeDesc IUnknown = FieldTyDescReference (ClassDesc "java/lang/Object")
170-
inferredTypeToFieldTypeDesc (IArray elemTy) = FieldTyDescReference (ArrayDesc $ inferredTypeToFieldTypeDesc elemTy)
171-
172-
getInferredTyDesc : InferredType -> String
173-
getInferredTyDesc = asmFieldTypeDesc . inferredTypeToFieldTypeDesc
174-
175185
getInferredFunDesc : List InferredType -> InferredType -> String
176186
getInferredFunDesc [] retTy = "()" ++ getInferredTyDesc retTy
177187
getInferredFunDesc argTypes retTy =
@@ -513,38 +523,54 @@ storeVar IFloat (Ref _) var = boxStore boxFloat var
513523
storeVar IDouble IUnknown var = boxStore boxDouble var
514524
storeVar IDouble (Ref _) var = boxStore boxDouble var
515525

516-
storeVar IUnknown IBool var = storeVarWithWordSize (\index => do unboxBool; Istore index) var
517-
storeVar (Ref _) IBool var = storeVarWithWordSize (\index => do unboxBool; Istore index) var
526+
storeVar IUnknown IBool var = storeVarWithWordSize (\index => do cgCast IUnknown IBool; Istore index) var
527+
storeVar srcTy@(Ref _) IBool var = storeVarWithWordSize (\index => do cgCast srcTy IBool; Istore index) var
518528

519-
storeVar IUnknown IByte var = storeVarWithWordSize (\index => do unboxByte; Istore index) var
520-
storeVar (Ref _) IByte var = storeVarWithWordSize (\index => do unboxByte; Istore index) var
529+
storeVar IUnknown IByte var = storeVarWithWordSize (\index => do cgCast IUnknown IByte; Istore index) var
530+
storeVar srcTy@(Ref _) IByte var = storeVarWithWordSize (\index => do cgCast srcTy IByte; Istore index) var
521531

522-
storeVar IUnknown IChar var = storeVarWithWordSize (\index => do unboxChar; Istore index) var
523-
storeVar (Ref _) IChar var = storeVarWithWordSize (\index => do unboxChar; Istore index) var
532+
storeVar IUnknown IChar var = storeVarWithWordSize (\index => do cgCast IUnknown IChar; Istore index) var
533+
storeVar srcTy@(Ref _) IChar var = storeVarWithWordSize (\index => do cgCast srcTy IChar; Istore index) var
524534

525-
storeVar IUnknown IShort var = storeVarWithWordSize (\index => do unboxShort; Istore index) var
526-
storeVar (Ref _) IShort var = storeVarWithWordSize (\index => do unboxShort; Istore index) var
535+
storeVar IUnknown IShort var = storeVarWithWordSize (\index => do cgCast IUnknown IShort; Istore index) var
536+
storeVar srcTy@(Ref _) IShort var = storeVarWithWordSize (\index => do cgCast srcTy IShort; Istore index) var
527537

528-
storeVar IUnknown IInt var = storeVarWithWordSize (\index => do unboxInt; Istore index) var
529-
storeVar (Ref _) IInt var = storeVarWithWordSize (\index => do unboxInt; Istore index) var
538+
storeVar IUnknown IInt var = storeVarWithWordSize (\index => do cgCast IUnknown IInt; Istore index) var
539+
storeVar srcTy@(Ref _) IInt var = storeVarWithWordSize (\index => do cgCast srcTy IInt; Istore index) var
530540

531-
storeVar IUnknown ILong var = storeVarWithWordSize (\index => do unboxLong; Lstore index) var
532-
storeVar (Ref _) ILong var = storeVarWithWordSize (\index => do unboxLong; Lstore index) var
541+
storeVar IUnknown ILong var = storeVarWithWordSize (\index => do cgCast IUnknown ILong; Lstore index) var
542+
storeVar srcTy@(Ref _) ILong var = storeVarWithWordSize (\index => do cgCast srcTy ILong; Lstore index) var
533543

534-
storeVar IUnknown IFloat var = storeVarWithWordSize (\index => do unboxFloat; Fstore index) var
535-
storeVar (Ref _) IFloat var = storeVarWithWordSize (\index => do unboxFloat; Fstore index) var
544+
storeVar IUnknown IFloat var = storeVarWithWordSize (\index => do cgCast IUnknown IFloat; Fstore index) var
545+
storeVar srcTy@(Ref _) IFloat var = storeVarWithWordSize (\index => do cgCast srcTy IFloat; Fstore index) var
536546

537-
storeVar IUnknown IDouble var = storeVarWithWordSize (\index => do unboxDouble; Dstore index) var
538-
storeVar (Ref _) IDouble var = storeVarWithWordSize (\index => do unboxDouble; Dstore index) var
547+
storeVar IUnknown IDouble var = storeVarWithWordSize (\index => do cgCast IUnknown IDouble; Dstore index) var
548+
storeVar srcTy@(Ref _) IDouble var = storeVarWithWordSize (\index => do cgCast srcTy IDouble; Dstore index) var
539549

540550
storeVar IUnknown arr@(IArray elemTy) var =
541551
storeVarWithWordSize (\index => do Checkcast $ getInferredTyDesc arr; Astore index) var
542552
storeVar (Ref _) arr@(IArray elemTy) var =
543553
storeVarWithWordSize (\index => do Checkcast $ getInferredTyDesc arr; Astore index) var
544554

545-
storeVar (Ref _) (Ref _) var = do types <- GetFunctionLocTypes; opWithWordSize types Astore var
555+
storeVar IUnknown targetTy@(Ref _) var = do
556+
types <- GetFunctionLocTypes
557+
cgCast IUnknown targetTy
558+
opWithWordSize types Astore var
559+
560+
storeVar srcTy@(Ref _) targetTy@(Ref _) var = do
561+
types <- GetFunctionLocTypes
562+
cgCast srcTy targetTy
563+
opWithWordSize types Astore var
564+
546565
storeVar _ _ var = do types <- GetFunctionLocTypes; opWithWordSize types Astore var
547566

567+
idrisObjectProperty : Int -> Int -> Asm ()
568+
idrisObjectProperty object propertyIndex = do
569+
locTypes <- GetFunctionLocTypes
570+
loadVar locTypes inferredObjectType inferredObjectType (Loc object)
571+
Iconst propertyIndex
572+
InvokeMethod InvokeStatic idrisObjectType "getProperty" "(Ljava/lang/Object;I)Ljava/lang/Object;" False
573+
548574
assign : (lhs: List LVar) -> (rhs: List LVar) -> Asm ()
549575
assign lhs rhs = do
550576
locTypes <- GetFunctionLocTypes
@@ -570,4 +596,4 @@ unwrapExportedIO = do
570596
let desc = getInferredFunDesc argTys retTy
571597
cgCast inferredObjectType lastArgTy
572598
InvokeMethod InvokeStatic "main/Main" "call__IO" desc False
573-
InvokeMethod InvokeStatic (rtClass "Runtime") "unwrap" (sig 1) False
599+
InvokeMethod InvokeStatic (rtClass "Runtime") "unwrap" (sig 1) False

idris-jvm-core/src/main/idris/IdrisJvm/Core/ControlFlow.idr

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,20 @@ mutual
150150
cgCase ret cgBody label expr
151151
Goto $ switchEndLabel si
152152

153-
cgAlt : (InferredType -> Asm ()) -> ((InferredType -> Asm ()) -> SExp -> Asm ()) -> Label -> Nat -> Int -> SAlt -> Asm ()
154-
cgAlt ret cgBody label si _ (SConstCase _ expr) = cgAltNonConCase ret cgBody label si expr
155-
156-
cgAlt ret cgBody label si _ (SDefaultCase expr) = cgAltNonConCase ret cgBody label si expr
157-
158-
cgAlt ret cgBody label si sv (SConCase lv _ _ args expr) = do
159-
LabelStart label
160-
addFrame
153+
cgConCase : (InferredType -> Asm ())
154+
-> ((InferredType -> Asm ()) -> SExp -> Asm ())
155+
-> Int
156+
-> Int
157+
-> List String
158+
-> SExp
159+
-> Asm ()
160+
cgConCase ret cgBody idrisObject lv args expr = do
161161
extractConParams lv
162162
cgBody ret expr
163-
164-
Goto $ switchEndLabel si
165-
166163
where
167164
project : Nat -> Nat -> Asm ()
168165
project i v = do
169-
idrisObjectProperty sv (cast i)
166+
idrisObjectProperty idrisObject (cast i)
170167
Astore $ cast v
171168

172169
argsLength : Nat
@@ -182,6 +179,17 @@ mutual
182179
project (cast i) (cast v)
183180
go (i + 1) (v + 1)
184181

182+
cgAlt : (InferredType -> Asm ()) -> ((InferredType -> Asm ()) -> SExp -> Asm ()) -> Label -> Nat -> Int -> SAlt -> Asm ()
183+
cgAlt ret cgBody label si _ (SConstCase _ expr) = cgAltNonConCase ret cgBody label si expr
184+
185+
cgAlt ret cgBody label si _ (SDefaultCase expr) = cgAltNonConCase ret cgBody label si expr
186+
187+
cgAlt ret cgBody label si sv (SConCase lv _ _ args expr) = do
188+
LabelStart label
189+
addFrame
190+
cgConCase ret cgBody sv lv args expr
191+
Goto $ switchEndLabel si
192+
185193
conCase : SAlt -> Bool
186194
conCase (SConCase _ _ _ _ _) = True
187195
conCase _ = False

idris-jvm-core/src/main/idris/IdrisJvm/Core/Foreign.idr

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,6 @@ typeDescriptorToInferredType (ThrowableDescriptor _) = inferredObjectType
116116
typeDescriptorToInferredType VoidDescriptor = IUnknown
117117
typeDescriptorToInferredType (FieldDescriptor fieldTyDesc) = fieldTypeDescriptorToInferredType fieldTyDesc
118118

119-
loadJavaVar : Int -> FieldTypeDescriptor -> Asm ()
120-
loadJavaVar index FieldTyDescBoolean = Iload index
121-
loadJavaVar index FieldTyDescByte = Iload index
122-
loadJavaVar index FieldTyDescShort = Iload index
123-
loadJavaVar index FieldTyDescInt = Iload index
124-
loadJavaVar index FieldTyDescChar = Iload index
125-
loadJavaVar index FieldTyDescLong = Lload index
126-
loadJavaVar index FieldTyDescFloat = Fload index
127-
loadJavaVar index FieldTyDescDouble = Dload index
128-
loadJavaVar index (FieldTyDescReference (IdrisExportDesc cname)) = do
129-
Aload index
130-
InvokeMethod InvokeVirtual cname "getValue" "()Ljava/lang/Object;" False
131-
checkcast idrisObjectType
132-
loadJavaVar index _ = Aload index
133-
134-
storeJavaVar : Int -> FieldTypeDescriptor -> Asm ()
135-
storeJavaVar index FieldTyDescBoolean = Istore index
136-
storeJavaVar index FieldTyDescByte = Istore index
137-
storeJavaVar index FieldTyDescShort = Istore index
138-
storeJavaVar index FieldTyDescInt = Istore index
139-
storeJavaVar index FieldTyDescChar = Istore index
140-
storeJavaVar index FieldTyDescLong = Lstore index
141-
storeJavaVar index FieldTyDescFloat = Fstore index
142-
storeJavaVar index FieldTyDescDouble = Dstore index
143-
storeJavaVar index _ = Astore index
144-
145119
idrisToJava : List (FieldTypeDescriptor, LVar) -> Asm ()
146120
idrisToJava vars = do
147121
locTypes <- GetFunctionLocTypes

idris-jvm-core/src/main/idris/IdrisJvm/Core/Function.idr

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,14 @@ mutual
223223
_))
224224
= cgIfNull ret cgBody e nothingExpr defaultExpr
225225

226+
cgBody ret (SCase _ e [SConCase lv _ _ args expr]) = cgConCase ret cgBody (locIndex e) lv args expr
227+
226228
cgBody ret (SCase _ e [SDefaultCase defaultCaseExpr]) = cgBody ret defaultCaseExpr
227229

228-
cgBody ret (SCase _ e alts) = cgSwitch ret cgBody e alts
230+
cgBody ret (SCase _ e alts) =
231+
if any defaultCase alts
232+
then cgSwitch ret cgBody e alts
233+
else cgSwitch ret cgBody e (alts ++ [SDefaultCase SNothing])
229234

230235
cgBody ret (SChkCase e [SDefaultCase defaultCaseExpr]) = cgBody ret defaultCaseExpr
231236

@@ -235,7 +240,7 @@ mutual
235240

236241
cgBody ret (SOp op args) = cgOp ret op args
237242

238-
cgBody ret SNothing = do Iconst 0; ret IInt
243+
cgBody ret SNothing = do Aconstnull; ret inferredObjectType
239244

240245
cgBody ret (SError x) = do invokeError (show x); ret IUnknown
241246

@@ -260,6 +265,9 @@ mutual
260265
retVoidOrBoxed VoidDescriptor = Aconstnull
261266
retVoidOrBoxed returnDesc = box (inferredRetTy returnDesc)
262267

268+
synthenticMethodArgTys : InferredTypeStore
269+
synthenticMethodArgTys = SortedMap.fromList $ List.zip targetArgIndices $ replicate argsLength inferredObjectType
270+
263271
targetLocTys : InferredTypeStore
264272
targetLocTys =
265273
let targetArgTys = List.zip targetArgIndices $ fst <$> args
@@ -270,8 +278,9 @@ mutual
270278
ThrowableDescriptor returnDesc => do
271279
locTys <- GetFunctionLocTypes
272280
let descriptor = asmMethodDesc $ MkMethodDescriptor (fdescFieldDescriptor . fst <$> args) returnDesc
281+
let arity = length args
273282
let lambdaBody = do
274-
loadVars locTys targetLocTys lambdaArgVars
283+
loadVars synthenticMethodArgTys targetLocTys lambdaArgVars
275284
InvokeMethod InvokeStatic clazz fn descriptor False
276285
retVoidOrBoxed returnDesc
277286
caller <- GetFunctionName
@@ -308,7 +317,7 @@ mutual
308317
let descriptor = asmMethodDesc $ MkMethodDescriptor (fdescFieldDescriptor . fst <$> drop 1 args) returnDesc
309318
locTys <- GetFunctionLocTypes
310319
let lambdaBody = do
311-
loadVars locTys targetLocTys lambdaArgVars
320+
loadVars synthenticMethodArgTys targetLocTys lambdaArgVars
312321
InvokeMethod InvokeVirtual clazz fn descriptor False
313322
retVoidOrBoxed returnDesc
314323
caller <- GetFunctionName
@@ -348,7 +357,7 @@ mutual
348357
locTys <- GetFunctionLocTypes
349358
let descriptor = asmMethodDesc $ MkMethodDescriptor (fdescFieldDescriptor . fst <$> drop 1 args) returnDesc
350359
let lambdaBody = do
351-
loadVars locTys targetLocTys lambdaArgVars
360+
loadVars synthenticMethodArgTys targetLocTys lambdaArgVars
352361
InvokeMethod InvokeInterface clazz fn descriptor True
353362
retVoidOrBoxed returnDesc
354363
caller <- GetFunctionName
@@ -371,7 +380,7 @@ mutual
371380
let lambdaBody = do
372381
New clazz
373382
Dup
374-
loadVars locTys targetLocTys lambdaArgVars
383+
loadVars synthenticMethodArgTys targetLocTys lambdaArgVars
375384
InvokeMethod InvokeSpecial clazz "<init>" descriptor False
376385
caller <- GetFunctionName
377386
let targetMethodName = MkJMethodName clazz "<init>"

0 commit comments

Comments
 (0)