-
Notifications
You must be signed in to change notification settings - Fork 2
/
Primitives.hs
78 lines (53 loc) · 1.97 KB
/
Primitives.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE OverloadedStrings #-}
module Codegen.Primitives where
import Data.Word (Word32)
import LLVM.AST hiding (function, alignment, Call)
import LLVM.AST.AddrSpace
import LLVM.AST.Type as AST
import qualified LLVM.AST as A
import qualified LLVM.AST.Float as F
import qualified LLVM.AST.Constant as C
import LLVM.IRBuilder.Monad
import LLVM.IRBuilder.Instruction hiding (load, store)
import LLVM.IRBuilder.Constant hiding (double)
import qualified LLVM.IRBuilder.Instruction as I
import StringUtils
addrSpace :: AddrSpace
addrSpace = AddrSpace 0
iSize :: Word32
iSize = 32
alignment :: Word32
alignment = 4
integerConstant i = ConstantOperand (C.Int {C.integerBits = iSize, C.integerValue = i})
integerPointer :: AST.Type
integerPointer = AST.PointerType i32 addrSpace
allocate :: MonadIRBuilder m => AST.Type -> m Operand
allocate type_ = alloca type_ Nothing alignment
allocateInt :: MonadIRBuilder m => m Operand
allocateInt = allocate i32
load :: MonadIRBuilder m => Operand -> m Operand
load pointer = I.load pointer alignment
store :: MonadIRBuilder m => Operand -> Operand -> m ()
store pointer value = I.store pointer alignment value
saveInt :: MonadIRBuilder m => Integer -> m Operand
saveInt value = do
pointer <- allocateInt
store pointer (int32 value)
return pointer
refName :: String -> A.Name
refName name = Name (toShort' $ name ++ "_0")
globalName :: String -> A.Name
globalName name = Name (toShort' name)
reference :: AST.Type -> String -> Operand
reference type_ name = LocalReference type_ (refName name)
referenceInt :: String -> Operand
referenceInt name = reference i32 name
referenceIntPointer :: String -> Operand
referenceIntPointer name = reference integerPointer name
makeFuncRef :: String -> Operand
makeFuncRef funcName = ConstantOperand (C.GlobalReference funcType $ globalName funcName)
where funcType = FunctionType i32 [] False
bodyLabel = toShort' "Body"
argName = ("arg_" ++)
intToFloat op = sitofp op double
floatToInt op = fptosi op i32