on master help sign in
Check for nulls in ChkCase
4072b67Benjamin Saunders
src/IRTS/CodegenLLVM.hs+13−9
@@ -331,26 +331,30 @@ cgExpr (SChkCase inspect alts) = do
331 case mval of
331 case mval of
332 Nothing -> return Nothing
332 Nothing -> return Nothing
333 Just val ->
333 Just val ->
334+ do endBBN <- getName "endChkCase"
335+ notNullBBN <- getName "notNull"
336+ originBlock <- gets currentBlockName
337+ isNull <- inst $ ICmp IPred.EQ val (ConstantOperand nullValue) []
338+ terminate $ CondBr isNull endBBN notNullBBN []
339+ newBlock notNullBBN
334− do ptr <- inst $ BitCast val (PointerType (IntegerType 8) (AddrSpace 0)) []
340+ ptr <- inst $ BitCast val (PointerType (IntegerType 8) (AddrSpace 0)) []
335 flag <- inst $ Load False ptr Nothing 0 []
341 flag <- inst $ Load False ptr Nothing 0 []
336 isVal <- inst $ ICmp IPred.EQ flag (ConstantOperand (C.Int 8 (-1))) []
342 isVal <- inst $ ICmp IPred.EQ flag (ConstantOperand (C.Int 8 (-1))) []
337 conBBN <- getName "constructor"
343 conBBN <- getName "constructor"
338− valBBN <- getName "value"
339− originBlock <- gets currentBlockName
340− terminate $ CondBr isVal valBBN conBBN []
344+ terminate $ CondBr isVal endBBN conBBN []
341 newBlock conBBN
345 newBlock conBBN
342 result <- cgCase val alts
346 result <- cgCase val alts
343 caseExitBlock <- gets currentBlockName
347 caseExitBlock <- gets currentBlockName
344 case result of
348 case result of
345 Nothing -> do
349 Nothing -> do
346 terminate $ Unreachable []
350 terminate $ Unreachable []
347− newBlock valBBN
351+ newBlock endBBN
348 return $ Just val
352 return $ Just val
349− Just r -> do
353+ Just caseVal -> do
350− terminate $ Br valBBN []
354+ terminate $ Br endBBN []
351− newBlock valBBN
355+ newBlock endBBN
352 Just <$> (inst $ Phi (PointerType valueType (AddrSpace 0))
356 Just <$> (inst $ Phi (PointerType valueType (AddrSpace 0))
353− [(val, originBlock), (r, caseExitBlock)] [])
357+ [(val, originBlock), (val, notNullBBN), (caseVal, caseExitBlock)] [])
354cgExpr (SProj conVar idx) = do
358cgExpr (SProj conVar idx) = do
355 val <- var conVar
359 val <- var conVar
356 case val of
360 case val of