Skip to content

Commit d70756e

Browse files
committed
Fix constant case expression scope, char conversion
1 parent c3678b9 commit d70756e

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Runtime.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ public static int unwrapIntThunk(Object possibleThunk) {
136136
if (possibleThunk instanceof Thunk) {
137137
return ((Thunk) possibleThunk).getInt();
138138
} else {
139-
return (int) possibleThunk;
139+
return Conversion.toInt1(possibleThunk);
140+
}
141+
}
142+
143+
public static char unwrapIntThunkToChar(Object possibleThunk) {
144+
if (possibleThunk instanceof Thunk) {
145+
return (char) ((Thunk) possibleThunk).getInt();
146+
} else {
147+
return (char) possibleThunk;
140148
}
141149
}
142150

idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Strings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static String pack(IdrisList idrisCharacterList) {
3131
Object[] objectArray = idrisCharacterList.toArray();
3232
char[] chars = new char[objectArray.length];
3333
for (int index = 0; index < objectArray.length; index++) {
34-
chars[index] = (char) objectArray[index];
34+
chars[index] = Conversion.toChar(objectArray[index]);
3535
}
3636
return String.valueOf(chars);
3737
}

src/Compiler/Jvm/Asm.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ Show Scope where
719719
("parentIndex", show $ parentIndex scope),
720720
("nextVariableIndex", show $ nextVariableIndex scope),
721721
("lineNumbers", show $ lineNumbers scope),
722-
("labels", show $ labels scope),
722+
("variableIndices", toString $ variableIndices scope),
723723
("returnType", show $ returnType scope),
724724
("nextVariableIndex", show $ nextVariableIndex scope),
725725
("childIndices", show $ childIndices scope)

src/Compiler/Jvm/Codegen.idr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,12 +1220,6 @@ mutual
12201220
assembleCaseWithScope labelStart labelEnd expr
12211221

12221222
assembleConstantSwitch returnType constantType fc sc alts def = do
1223-
constantExprVariableSuffixIndex <- newDynamicVariableIndex
1224-
let constantExprVariableName = "constantCaseExpr" ++ show constantExprVariableSuffixIndex
1225-
constantExprVariableIndex <- getVariableIndex constantExprVariableName
1226-
hashCodePositionVariableSuffixIndex <- newDynamicVariableIndex
1227-
let hashCodePositionVariableName = "hashCodePosition" ++ show hashCodePositionVariableSuffixIndex
1228-
hashCodePositionVariableIndex <- getVariableIndex hashCodePositionVariableName
12291223
hashPositionAndAlts <- traverse (constantAltHashCodeExpr fc) $
12301224
List.zip [0 .. the Int $ cast $ length $ drop 1 alts] alts
12311225
let positionAndAltsByHash = multiValueMap fst snd hashPositionAndAlts
@@ -1236,6 +1230,12 @@ mutual
12361230
CreateLabel switchEndLabel
12371231
traverse_ CreateLabel labels
12381232
assembleExpr False constantType sc
1233+
constantExprVariableSuffixIndex <- newDynamicVariableIndex
1234+
let constantExprVariableName = "constantCaseExpr" ++ show constantExprVariableSuffixIndex
1235+
constantExprVariableIndex <- getVariableIndex constantExprVariableName
1236+
hashCodePositionVariableSuffixIndex <- newDynamicVariableIndex
1237+
let hashCodePositionVariableName = "hashCodePosition" ++ show hashCodePositionVariableSuffixIndex
1238+
hashCodePositionVariableIndex <- getVariableIndex hashCodePositionVariableName
12391239
storeVar constantType constantType constantExprVariableIndex
12401240
constantClass <- getHashCodeSwitchClass fc constantType
12411241
Iconst (-1)

src/Compiler/Jvm/Optimizer.idr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ getLineNumbers (lineStart, _) (lineEnd, colEnd) =
8282

8383
getFileName : OriginDesc -> String
8484
getFileName (PhysicalIdrSrc moduleIdent) = case unsafeUnfoldModuleIdent moduleIdent of
85-
(moduleName :: _) => moduleName
85+
(moduleName :: _) => moduleName ++ ".idr"
8686
_ => "(unknown-source)"
8787
getFileName (PhysicalPkgSrc fname) = fname
8888
getFileName (Virtual Interactive) = "(Interactive)"
@@ -470,13 +470,21 @@ getConstantType : List NamedConstAlt -> Asm InferredType
470470
getConstantType [] = Throw emptyFC "Unknown constant switch type"
471471
getConstantType ((MkNConstAlt constant _) :: _) = case constant of
472472
I _ => Pure IInt
473+
B8 _ => Pure IInt
474+
B16 _ => Pure IInt
475+
B32 _ => Pure IInt
473476
Ch _ => Pure IInt
474477
Str _ => Pure inferredStringType
475478
BI _ => Pure inferredBigIntegerType
479+
B64 _ => Pure inferredBigIntegerType
476480
unsupportedConstant => Throw emptyFC $ "Unsupported constant switch " ++ show unsupportedConstant
477481

478482
export
479483
isTypeConst : TT.Constant -> Bool
484+
isTypeConst Bits8Type = True
485+
isTypeConst Bits16Type = True
486+
isTypeConst Bits32Type = True
487+
isTypeConst Bits64Type = True
480488
isTypeConst IntType = True
481489
isTypeConst IntegerType = True
482490
isTypeConst StringType = True
@@ -488,6 +496,9 @@ isTypeConst _ = False
488496
export
489497
getIntConstantValue : FC -> TT.Constant -> Asm Int
490498
getIntConstantValue _ (I i) = Pure i
499+
getIntConstantValue _ (B8 i) = Pure i
500+
getIntConstantValue _ (B16 i) = Pure i
501+
getIntConstantValue _ (B32 i) = Pure i
491502
getIntConstantValue _ (Ch c) = Pure $ ord c
492503
getIntConstantValue _ WorldVal = Pure 0
493504
getIntConstantValue fc x =

src/Compiler/Jvm/ShowUtil.idr

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ showType typeName properties = showObj (("__name", quoted typeName) :: propertie
2222
indent : Nat -> String -> String
2323
indent n s = concat (List.replicate (n * 4) " ") ++ s
2424

25+
showConstant : Constant -> String
26+
showConstant (I value) = "prim$I$" ++ show value
27+
showConstant (BI value) = "prim$BI$" ++ show value
28+
showConstant (Ch value) = "prim$Ch$" ++ show value
29+
showConstant (Str value) = "prim$Str$" ++ show value
30+
showConstant (I8 value) = "prim$I8$" ++ show value
31+
showConstant (I16 value) = "prim$I16$" ++ show value
32+
showConstant (I32 value) = "prim$I32$" ++ show value
33+
showConstant (I64 value) = "prim$I64$" ++ show value
34+
showConstant (B8 value) = "prim$B8$" ++ show value
35+
showConstant (B16 value) = "prim$B16$" ++ show value
36+
showConstant (B32 value) = "prim$B32$" ++ show value
37+
showConstant (B64 value) = "prim$B64$" ++ show value
38+
showConstant (Db value) = "prim$Db$" ++ show value
39+
showConstant value = "prim$" ++ show value
40+
2541
mutual
2642
export
2743
showNamedCExp : Nat -> NamedCExp -> String
@@ -43,17 +59,16 @@ mutual
4359
showNamedCExp n (NmForce _ _ x) = "force" ++ "(" ++ showNamedCExp n x ++ ")"
4460
showNamedCExp n (NmDelay _ _ x) = "delay" ++ "(" ++ showNamedCExp n x ++ ")"
4561
showNamedCExp n (NmConCase fc sc xs def) = "\n" ++
46-
indent n ("constructorswitch" ++ "(" ++ showNamedCExp n sc ++ ") \n") ++
62+
indent n ("constructorswitch" ++ "(" ++ showNamedCExp n sc ++ ")\n") ++
4763
showSep "\n" (showNamedConAlt (n + 1) <$> xs) ++
4864
maybe "" (\defExp => "\n" ++ indent (n + 1) "default:\n" ++
4965
indent (n + 1) (showNamedCExp (n + 1) defExp)) def
5066
showNamedCExp n (NmConstCase fc sc xs def) = "\n" ++
51-
indent n ("constantswitch" ++"(" ++ show sc ++ ")\n") ++
67+
indent n ("constantswitch" ++"(" ++ showNamedCExp n sc ++ ")\n") ++
5268
showSep "\n" (showNamedConstAlt (n + 1) <$> xs) ++
5369
maybe "" (\defExp => "\n" ++ indent (n + 1) "default:\n" ++
5470
indent (n + 1) (showNamedCExp (n + 2) defExp)) def
55-
showNamedCExp n (NmPrimVal fc (BI value)) = "prim$BI$" ++ show value
56-
showNamedCExp n (NmPrimVal fc x) = "prim$" ++ show x
71+
showNamedCExp n (NmPrimVal fc x) = showConstant x
5772
showNamedCExp n (NmErased fc) = "erased"
5873
showNamedCExp n (NmCrash fc x) = "crash " ++ show x
5974

@@ -65,7 +80,7 @@ mutual
6580

6681
export
6782
showNamedConstAlt : Nat -> NamedConstAlt -> String
68-
showNamedConstAlt n (MkNConstAlt x exp) = indent n ("case " ++ show x ++ ":\n") ++
83+
showNamedConstAlt n (MkNConstAlt x exp) = indent n ("case " ++ showConstant x ++ ":\n") ++
6984
indent (n + 1) (showNamedCExp (n + 1) exp)
7085

7186

src/Compiler/Jvm/Variable.idr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ unboxToDoubleThunk =
9292
unwrapIntThunk : Asm ()
9393
unwrapIntThunk = InvokeMethod InvokeStatic runtimeClass "unwrapIntThunk" "(Ljava/lang/Object;)I" False
9494

95+
unwrapIntThunkToChar : Asm ()
96+
unwrapIntThunkToChar = InvokeMethod InvokeStatic runtimeClass "unwrapIntThunkToChar" "(Ljava/lang/Object;)C" False
97+
9598
unwrapLongThunk : Asm ()
9699
unwrapLongThunk = InvokeMethod InvokeStatic runtimeClass "unwrapLongThunk" "(Ljava/lang/Object;)J" False
97100

@@ -295,7 +298,7 @@ loadAndUnboxByte ty sourceLocTys var =
295298

296299
loadAndUnboxChar : InferredType -> Map Int InferredType -> Int -> Asm ()
297300
loadAndUnboxChar ty sourceLocTys var =
298-
let loadInstr = \index => do Aload index; if ty == intThunkType then unwrapIntThunk else objToChar
301+
let loadInstr = \index => do Aload index; if ty == intThunkType then unwrapIntThunkToChar else objToChar
299302
in opWithWordSize sourceLocTys loadInstr var
300303

301304
loadAndUnboxShort : InferredType -> Map Int InferredType -> Int -> Asm ()

0 commit comments

Comments
 (0)