Skip to content

Commit 9d6b24c

Browse files
committed
First batch of update following code review.
1 parent e6f3d76 commit 9d6b24c

File tree

6 files changed

+31
-61
lines changed

6 files changed

+31
-61
lines changed

src/AST.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ module AST (
134134

135135
import Config (magicVersion, wordSize, objectExtension,
136136
sourceExtension, currentModuleAlias,
137-
specialChar, specialName, specialName2, wordSizeBytes,)
137+
specialChar, specialName, specialName2, wordSizeBytes,byteBits)
138138
import Control.Monad
139139
import Control.Monad.Extra
140140
import Control.Monad.Trans (lift,liftIO)
@@ -3542,11 +3542,11 @@ constValueSize (GenericStructMember _) = wordSizeBytes
35423542

35433543
-- | Return the representation of a StructMember
35443544
constValueRepresentation :: ConstValue -> TypeRepresentation
3545-
constValueRepresentation (IntStructMember _ size) = Bits $ size * 8
3546-
constValueRepresentation (FloatStructMember _ size) = Floating $ size * 8
3545+
constValueRepresentation (IntStructMember _ size) = Bits $ size * byteBits
3546+
constValueRepresentation (FloatStructMember _ size) = Floating $ size * byteBits
35473547
constValueRepresentation (PointerStructMember _) = Pointer
35483548
constValueRepresentation (FnPointerStructMember _) = CPointer
3549-
constValueRepresentation (UndefStructMember size) = Bits $ size * 8
3549+
constValueRepresentation (UndefStructMember size) = Bits $ size * byteBits
35503550
constValueRepresentation (GenericStructMember _) = Pointer
35513551

35523552

src/BodyBuilder.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import AST
1717
import Debug.Trace
1818
import Snippets ( boolType, intType, primMove )
1919
import Util
20-
import Config (minimumSwitchCases, wordSize, wordSizeBytes)
20+
import Config (minimumSwitchCases, wordSize, wordSizeBytes, byteBits)
2121
import Options (LogSelection(BodyBuilder))
2222
import Data.Char ( ord )
2323
import Data.Map as Map
@@ -278,7 +278,7 @@ potentialConstStruct var size = do
278278
Map.insert var (newBlock size) $ constStructs st}
279279

280280

281-
-- | Does the specified variable hold a constant structure?
281+
-- | Delete a constant structure record from the BodyBuilder monad.
282282
deleteConstStruct :: PrimVarName -> BodyBuilder ()
283283
deleteConstStruct var = do
284284
logBuild $ "Deleting constant structure " ++ show var
@@ -692,7 +692,7 @@ typeSize :: TypeSpec -> BodyBuilder Int
692692
typeSize ty = do
693693
rep <- trustFromJust ("lookupTypeRepresentation of " ++ show ty)
694694
<$> lift (lookupTypeRepresentation ty)
695-
return $ 1 + ((typeRepSize rep - 1) `div` 8)
695+
return $ 1 + ((typeRepSize rep - 1) `div` byteBits)
696696

697697

698698
-- Do the normal work of instr. First check if we've already computed its

src/Config.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Config (sourceExtension, objectExtension, executableExtension,
1212
bitcodeExtension, assemblyExtension, nativeAssemblyExtension,
1313
archiveExtension, moduleDirectoryBasename, currentModuleAlias,
1414
specialChar, specialName, specialName2, initProcName,
15-
wordSize, wordSizeBytes,
15+
wordSize, wordSizeBytes, byteBits,
1616
availableTagBits, tagMask, smallestAllocatedAddress,
1717
minimumSwitchCases, magicVersion,
1818
linkerDeadStripArgs, removeLPVMSection,
@@ -102,14 +102,19 @@ initProcName = ""
102102

103103
-- |Determining word size of the machine in bits
104104
wordSize :: Int
105-
wordSize = wordSizeBytes * 8
105+
wordSize = wordSizeBytes * byteBits
106106

107107

108108
-- |Word size of the machine in bytes
109109
wordSizeBytes :: Int
110110
wordSizeBytes = sizeOf (3 :: Word)
111111

112112

113+
-- |Size of a byte in bits
114+
byteBits :: Int
115+
byteBits = 8
116+
117+
113118
-- |The number of tag bits available on this architecture. This is the base 2
114119
-- log of the word size in bytes, assuming this is a byte addressed machine.
115120
-- XXX this would need to be fixed for non-byte addressed architectures.

src/LLVM.hs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,10 @@ argConstValue :: PrimArg -> LLVM (Maybe ConstValue)
331331
argConstValue ArgVar{} = return Nothing
332332
argConstValue (ArgInt n ty) = do
333333
sz <- typeRepSize <$> lift (typeRepresentation ty)
334-
return $ Just $ IntStructMember n (sz `div` 8)
334+
return $ Just $ IntStructMember n (sz `div` byteBits)
335335
argConstValue (ArgFloat n ty) = do
336336
sz <- typeRepSize <$> lift (typeRepresentation ty)
337-
return $ Just $ FloatStructMember n (sz `div` 8)
337+
return $ Just $ FloatStructMember n (sz `div` byteBits)
338338
argConstValue ArgClosure{} = return Nothing -- const closures already handled
339339
argConstValue (ArgGlobal info _) = do
340340
-- XXX is ArgGlobal a constant? Does it give the address, or value, of the
@@ -345,7 +345,7 @@ argConstValue (ArgConstRef structID _) = do
345345
argConstValue ArgUnneeded{} = return Nothing
346346
argConstValue (ArgUndef ty) = do
347347
sz <- typeRepSize <$> lift (typeRepresentation ty)
348-
return $ Just $ UndefStructMember (sz `div` 8)
348+
return $ Just $ UndefStructMember (sz `div` byteBits)
349349

350350

351351
-- | If needed, add an extern declaration for a prim to the set.
@@ -472,38 +472,6 @@ writeAssemblyConstants = do
472472
-- declared and recorded. This will happen because sets are sorted
473473
-- alphabetically, and CString comes before WybeString.
474474
writeConstDeclaration :: StructID -> LLVM ()
475-
-- writeConstDeclaration spec@(WybeStringSpec str) n = do
476-
-- -- let stringName = specialName2 "string" $ show n
477-
-- -- modify $ \s -> s { constNames=Map.insert spec stringName
478-
-- -- $ constNames s}
479-
-- cStringID <- lift $ recordConstStruct $ CStringInfo str
480-
-- writeConstDeclaration (StructSpec cStringID) n
481-
-- wStringID <- lift $ recordConstStruct $ StructInfo
482-
-- [ IntStructMember (fromIntegral $ length str) wordSize
483-
-- , PointerStructMember cStringID]
484-
-- writeConstDeclaration (StructSpec wStringID) n
485-
-- -- return ()
486-
-- writeConstDeclaration spec@(CStringSpec str) n = do
487-
-- -- let textName = specialName2 "cstring" $ show n
488-
-- cStringID <- lift $ recordConstStruct $ CStringInfo str
489-
-- writeConstDeclaration (StructSpec cStringID) n
490-
-- -- modify $ \s -> s { constNames=Map.insert spec textName
491-
-- -- $ constNames s}
492-
-- -- declareStringConstant textName str Nothing
493-
-- writeConstDeclaration spec@(ClosureSpec pspec args) n = do
494-
-- -- let closureName = specialName2 "closure" $ show n
495-
-- -- modify $ \s -> s { constNames=Map.insert spec closureName $ constNames s}
496-
-- let pname = show pspec
497-
-- -- argReps <- mapM argTypeRep args
498-
-- -- declareStructConstant closureName
499-
-- -- ((ArgGlobal (GlobalVariable pname) (Representation CPointer), CPointer)
500-
-- -- : zip args argReps)
501-
-- -- Nothing
502-
-- constArgs <- mapM argStructMember args
503-
-- closureID <- lift $ recordConstStruct
504-
-- $ StructInfo (GlobalNameMember pname : constArgs )
505-
-- writeConstDeclaration (StructSpec closureID) n
506-
507475
writeConstDeclaration structID = do
508476
info <- trustFromJust ("writeConstDeclaration of " ++ show structID)
509477
<$> lift (lookupConstInfo structID)
@@ -816,12 +784,9 @@ writeWybeCall wybeProc args pos = do
816784

817785
-- | Generate a Wybe proc call instruction, or defer it if necessary.
818786
writeHOCall :: PrimArg -> [PrimArg] -> OptPos -> LLVM ()
819-
writeHOCall (ArgClosure pspec closed _) args pos = do
820-
-- NB: this case doesn't seem to ever occur -- probably handled earlier
821-
pspec' <- fromMaybe pspec <$> lift (maybeGetClosureOf pspec)
822-
logLLVM $ "Compiling HO call as first order call to " ++ show pspec'
823-
++ " closed over " ++ show closed
824-
writeWybeCall pspec' (closed ++ args) pos
787+
writeHOCall closure@(ArgClosure pspec closed _) args pos = do
788+
-- NB: this case should have been handled earlier
789+
shouldnt $ "Higher order call with constand closure should have been handled earlier: " ++ show closure
825790
writeHOCall closure args pos = do
826791
(ins,outs,oRefs,iRefs) <- partitionArgsWithRefs $ closure:args
827792
unless (List.null oRefs && List.null iRefs)

src/Normalise.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Normalise (normalise, normaliseItem, completeNormalisation) where
1313

1414
import AST
1515
import Config (wordSize, wordSizeBytes, availableTagBits,
16-
tagMask, smallestAllocatedAddress, currentModuleAlias, specialName2, specialName, initProcName)
16+
tagMask, smallestAllocatedAddress, currentModuleAlias, specialName2, specialName, initProcName, byteBits)
1717
import Control.Monad
1818
import Control.Monad.State (gets)
1919
import Control.Monad.Trans (lift,liftIO)
@@ -643,7 +643,7 @@ layoutRecord paramInfos tag tagLimit =
643643
let sizes = (2^) <$> [0..floor $ logBase 2 $ fromIntegral wordSizeBytes]
644644
fields = List.map
645645
(\(CtorParamInfo param anon rep sz) ->
646-
let byteSize = (sz + 7) `div` 8
646+
let byteSize = (sz + 7) `div` byteBits
647647
wordSize = (byteSize + wordSizeBytes - 1)
648648
`div` wordSizeBytes * wordSizeBytes
649649
alignment =

src/c_config.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
* Generate Haskell source code defining Wybe interpretation of standard
99
* C types.
1010
*
11-
* NB: This code assumes a byte-addressed architecture, and that a byte
12-
* is 8 bits.
11+
* NB: This code assumes a byte-addressed architecture.
1312
**/
1413

1514
#include <stdio.h>
@@ -23,17 +22,18 @@ int main() {
2322
printf("-- AUTOMATICALLY GENERATED BY c_config.c\n-- DO NOT EDIT!\n\n");
2423
printf("-- Purpose : Relate C types to Wybe types for foreign interface\n\n");
2524
printf("module CConfig (cTypeRepresentation, cPointerSize) where\n");
26-
printf("\nimport AST (TypeRepresentation(..))\n\n");
25+
printf("\nimport AST (TypeRepresentation(..))\n");
26+
printf("import Config (byteBits)\n\n");
2727
printf("cTypeRepresentation :: String -> Maybe TypeRepresentation\n");
28-
printf("cTypeRepresentation \"int\" = Just $ Signed %lu\n", 8*sizeof(i));
29-
printf("cTypeRepresentation \"float\" = Just $ Floating %lu\n", 8*sizeof(f));
30-
printf("cTypeRepresentation \"double\" = Just $ Floating %lu\n", 8*sizeof(d));
31-
printf("cTypeRepresentation \"char\" = Just $ Bits %lu\n", 8*sizeof(c));
32-
printf("cTypeRepresentation \"pointer\" = Just $ CPointer\n");
28+
printf("cTypeRepresentation \"int\" = Just $ Signed $ byteBits*%lu\n", sizeof(i));
29+
printf("cTypeRepresentation \"float\" = Just $ Floating $ byteBits*%lu\n", sizeof(f));
30+
printf("cTypeRepresentation \"double\" = Just $ Floating $ byteBits*%lu\n", sizeof(d));
31+
printf("cTypeRepresentation \"char\" = Just $ Bits $ byteBits*%lu\n", sizeof(c));
32+
printf("cTypeRepresentation \"pointer\" = Just CPointer\n");
3333
printf("cTypeRepresentation \"void\" = Just $ Bits 0\n");
3434
printf("cTypeRepresentation \"intrinsic_bool\" = Just $ Bits 1\n");
3535
printf("cTypeRepresentation other = Nothing\n\n");
3636
printf("cPointerSize :: Int\n");
37-
printf("cPointerSize = %lu\n", 8*sizeof(p));
37+
printf("cPointerSize = byteBits*%lu\n", sizeof(p));
3838
return 0;
3939
}

0 commit comments

Comments
 (0)