on master help sign in
Fix LLVM codegen integer interconversion and integer <-> character primitives
commit1c1ad77cfb78f81be8385b2eaad0ab0f8593aa30 authorBenjamin Saunders <besaunde@sfu.ca> dateSat 20 Jul 2013, 12:03 (13 years ago) parent0b25ad1
2 files changed+16−4
src/IRTS/CodegenLLVM.hs+15−3 src/Idris/Primitives.hs+1−1
src/IRTS/CodegenLLVM.hs+15−3
@@ -816,6 +816,13 @@ itWidth :: IntTy -> Word32
816816itWidth ITNative = 32
817817itWidth ITChar = 32
818818itWidth (ITFixed x) = fromIntegral $ nativeTyWidth x
819+itWidth x = ierror $ "itWidth: " ++ show x
820+
821+itConst :: IntTy -> Integer -> C.Constant
822+itConst (ITFixed n) x = C.Int (fromIntegral $ nativeTyWidth n) x
823+itConst ITNative x = itConst (ITFixed IT32) x
824+itConst ITChar x = itConst (ITFixed IT32) x
825+itConst (ITVec elts size) x = C.Vector (replicate size (itConst (ITFixed elts) x))
819826
820827cgOp :: PrimFn -> [Operand] -> Codegen Operand
821828cgOp (LTrunc ITBig ity) [x] = do
@@ -842,6 +849,10 @@ cgOp (LSExt from ITBig) [x] = do
842849 inst' $ simpleCall "mpz_init_set_sll" [mpz, nx']
843850 box (FArith (ATInt ITBig)) mpz
844851
852+-- ITChar, ITNative, and IT32 all share representation
853+cgOp (LChInt ITNative) [x] = return x
854+cgOp (LIntCh ITNative) [x] = return x
855+
845856cgOp (LLt (ATInt ITBig)) [x,y] = mpzCmp IPred.SLT x y
846857cgOp (LLe (ATInt ITBig)) [x,y] = mpzCmp IPred.SLE x y
847858cgOp (LEq (ATInt ITBig)) [x,y] = mpzCmp IPred.EQ x y
@@ -893,7 +904,7 @@ cgOp (LURem ity) [x,y] = ibin ity x y URem
893904cgOp (LAnd ity) [x,y] = ibin ity x y And
894905cgOp (LOr ity) [x,y] = ibin ity x y Or
895906cgOp (LXOr ity) [x,y] = ibin ity x y Xor
896−cgOp (LCompl ity) [x] = iun ity x (Xor (ConstantOperand (C.Int (itWidth ity) (-1))))
907+cgOp (LCompl ity) [x] = iun ity x (Xor . ConstantOperand $ itConst ity (-1))
897908cgOp (LSHL ity) [x,y] = ibin ity x y (Shl False False)
898909cgOp (LLSHR ity) [x,y] = ibin ity x y (LShr False)
899910cgOp (LASHR ity) [x,y] = ibin ity x y (AShr False)
@@ -1029,13 +1040,14 @@ cgOp LStdErr [] = do
10291040 ptr <- inst $ loadInv stdErr
10301041 box FPtr ptr
10311042
1032−cgOp prim args = ierror $ "Unimplemented primitive: [" ++ show prim ++ "]("
1043+cgOp prim args = ierror $ "Unimplemented primitive: <" ++ show prim ++ ">("
10331044 ++ intersperse ',' (take (length args) ['a'..]) ++ ")"
10341045
10351046iCoerce :: (Operand -> Type -> InstructionMetadata -> Instruction) -> NativeTy -> NativeTy -> Operand -> Codegen Operand
1047+iCoerce _ from to x | from == to = return x
10361048iCoerce operator from to x = do
10371049 x' <- unbox (FArith (ATInt (ITFixed from))) x
1038− x'' <- inst $ operator x' (ftyToTy (FArith (ATInt (ITFixed from)))) []
1050+ x'' <- inst $ operator x' (ftyToTy (FArith (ATInt (ITFixed to)))) []
10391051 box (FArith (ATInt (ITFixed to))) x''
10401052
10411053cgStrCat :: Operand -> Operand -> Codegen Operand
src/Idris/Primitives.hs+1−1
@@ -121,7 +121,7 @@ primitives =
121121 (1, LStrLen) total,
122122 -- Conversions
123123 Prim (UN "prim__charToInt") (ty [(AType (ATInt ITChar))] (AType (ATInt ITNative))) 1 (c_charToInt)
124− (1, LChInt ITChar) total,
124+ (1, LChInt ITNative) total,
125125 Prim (UN "prim__intToChar") (ty [(AType (ATInt ITNative))] (AType (ATInt ITChar))) 1 (c_intToChar)
126126 (1, LIntCh ITNative) total,
127127 Prim (UN "prim__strToFloat") (ty [StrType] (AType ATFloat)) 1 (c_strToFloat)