1 --------------------------------------------------------------------------------
2 -- | The LLVM Type System.
5 module Llvm.Types where
7 #include "HsVersions.h"
20 -- -----------------------------------------------------------------------------
21 -- * LLVM Basic Types and Variables
24 -- | A global mutable variable. Maybe defined or external
25 type LMGlobal = (LlvmVar, Maybe LlvmStatic)
27 type LMString = FastString
32 = LMInt Int -- ^ An integer with a given width in bits.
33 | LMFloat -- ^ 32 bit floating point
34 | LMDouble -- ^ 64 bit floating point
35 | LMFloat80 -- ^ 80 bit (x86 only) floating point
36 | LMFloat128 -- ^ 128 bit floating point
37 | LMPointer LlvmType -- ^ A pointer to a 'LlvmType'
38 | LMArray Int LlvmType -- ^ An array of 'LlvmType'
39 | LMLabel -- ^ A 'LlvmVar' can represent a label (address)
40 | LMVoid -- ^ Void type
41 | LMStruct [LlvmType] -- ^ Structure type
42 | LMAlias LMString LlvmType -- ^ A type alias
44 -- | Function type, used to create pointers to functions
45 | LMFunction LlvmFunctionDecl
48 instance Show LlvmType where
49 show (LMInt size ) = "i" ++ show size
50 show (LMFloat ) = "float"
51 show (LMDouble ) = "double"
52 show (LMFloat80 ) = "x86_fp80"
53 show (LMFloat128 ) = "fp128"
54 show (LMPointer x ) = show x ++ "*"
55 show (LMArray nr tp ) = "[" ++ show nr ++ " x " ++ show tp ++ "]"
56 show (LMLabel ) = "label"
57 show (LMVoid ) = "void"
58 show (LMStruct tys ) = "{" ++ (commaCat tys) ++ "}"
60 show (LMFunction (LlvmFunctionDecl _ _ _ r varg p _))
61 = let args = ((drop 1).concat) $ -- use drop since it can handle empty lists
62 map (\(t,a) -> "," ++ show t ++ " " ++ spaceCat a) p
64 VarArgs | not (null args) -> ", ..."
67 in show r ++ " (" ++ args ++ varg' ++ ")"
69 show (LMAlias s _ ) = "%" ++ unpackFS s
71 -- | An LLVM section defenition. If Nothing then let LLVM decide the section
72 type LMSection = Maybe LMString
73 type LMAlign = Maybe Int
74 type LMConst = Bool -- ^ is a variable constant or not
78 -- | Variables with a global scope.
79 = LMGlobalVar LMString LlvmType LlvmLinkageType LMSection LMAlign LMConst
80 -- | Variables local to a function or parameters.
81 | LMLocalVar Unique LlvmType
82 -- | Named local variables. Sometimes we need to be able to explicitly name
83 -- variables (e.g for function arguments).
84 | LMNLocalVar LMString LlvmType
85 -- | A constant variable
89 instance Show LlvmVar where
90 show (LMLitVar x) = show x
91 show (x ) = show (getVarType x) ++ " " ++ getName x
94 -- | Llvm Literal Data.
96 -- These can be used inline in expressions.
98 -- | Refers to an integer constant (i64 42).
99 = LMIntLit Integer LlvmType
100 -- | Floating point literal
101 | LMFloatLit Double LlvmType
102 -- | Literal NULL, only applicable to pointer types
104 -- | Undefined value, random bit pattern. Useful for optimisations.
105 | LMUndefLit LlvmType
108 instance Show LlvmLit where
109 show l = show (getLitType l) ++ " " ++ getLit l
112 -- | Llvm Static Data.
114 -- These represent the possible global level variables and constants.
116 = LMComment LMString -- ^ A comment in a static section
117 | LMStaticLit LlvmLit -- ^ A static variant of a literal value
118 | LMUninitType LlvmType -- ^ For uninitialised data
119 | LMStaticStr LMString LlvmType -- ^ Defines a static 'LMString'
120 | LMStaticArray [LlvmStatic] LlvmType -- ^ A static array
121 | LMStaticStruc [LlvmStatic] LlvmType -- ^ A static structure type
122 | LMStaticPointer LlvmVar -- ^ A pointer to other data
124 -- static expressions, could split out but leave
125 -- for moment for ease of use. Not many of them.
127 | LMBitc LlvmStatic LlvmType -- ^ Pointer to Pointer conversion
128 | LMPtoI LlvmStatic LlvmType -- ^ Pointer to Integer conversion
129 | LMAdd LlvmStatic LlvmStatic -- ^ Constant addition operation
130 | LMSub LlvmStatic LlvmStatic -- ^ Constant subtraction operation
132 instance Show LlvmStatic where
133 show (LMComment s) = "; " ++ unpackFS s
134 show (LMStaticLit l ) = show l
135 show (LMUninitType t) = show t ++ " undef"
136 show (LMStaticStr s t) = show t ++ " c\"" ++ unpackFS s ++ "\\00\""
138 show (LMStaticArray d t)
139 = let struc = case d of
141 ts -> "[" ++ show (head ts) ++
142 concat (map (\x -> "," ++ show x) (tail ts)) ++ "]"
143 in show t ++ " " ++ struc
145 show (LMStaticStruc d t)
146 = let struc = case d of
148 ts -> "{" ++ show (head ts) ++
149 concat (map (\x -> "," ++ show x) (tail ts)) ++ "}"
150 in show t ++ " " ++ struc
152 show (LMStaticPointer v) = show v
155 = show t ++ " bitcast (" ++ show v ++ " to " ++ show t ++ ")"
158 = show t ++ " ptrtoint (" ++ show v ++ " to " ++ show t ++ ")"
161 = let ty1 = getStatType s1
162 op = if isFloat ty1 then " fadd (" else " add ("
163 in if ty1 == getStatType s2
164 then show ty1 ++ op ++ show s1 ++ "," ++ show s2 ++ ")"
165 else error $ "LMAdd with different types! s1: "
166 ++ show s1 ++ ", s2: " ++ show s2
168 = let ty1 = getStatType s1
169 op = if isFloat ty1 then " fsub (" else " sub ("
170 in if ty1 == getStatType s2
171 then show ty1 ++ op ++ show s1 ++ "," ++ show s2 ++ ")"
172 else error $ "LMSub with different types! s1: "
173 ++ show s1 ++ ", s2: " ++ show s2
176 -- | Concatenate an array together, separated by commas
177 commaCat :: Show a => [a] -> String
179 commaCat x = show (head x) ++ (concat $ map (\y -> "," ++ show y) (tail x))
181 -- | Concatenate an array together, separated by commas
182 spaceCat :: Show a => [a] -> String
184 spaceCat x = show (head x) ++ (concat $ map (\y -> " " ++ show y) (tail x))
186 -- -----------------------------------------------------------------------------
187 -- ** Operations on LLVM Basic Types and Variables
190 -- | Return the variable name or value of the 'LlvmVar'
191 -- in Llvm IR textual representation (e.g. @\@x@, @%y@ or @42@).
192 getName :: LlvmVar -> String
193 getName v@(LMGlobalVar _ _ _ _ _ _) = "@" ++ getPlainName v
194 getName v@(LMLocalVar _ _ ) = "%" ++ getPlainName v
195 getName v@(LMNLocalVar _ _ ) = "%" ++ getPlainName v
196 getName v@(LMLitVar _ ) = getPlainName v
198 -- | Return the variable name or value of the 'LlvmVar'
199 -- in a plain textual representation (e.g. @x@, @y@ or @42@).
200 getPlainName :: LlvmVar -> String
201 getPlainName (LMGlobalVar x _ _ _ _ _) = unpackFS x
202 getPlainName (LMLocalVar x _ ) = show x
203 getPlainName (LMNLocalVar x _ ) = unpackFS x
204 getPlainName (LMLitVar x ) = getLit x
206 -- | Print a literal value. No type.
207 getLit :: LlvmLit -> String
208 getLit (LMIntLit i _) = show ((fromInteger i)::Int)
209 getLit (LMFloatLit r LMFloat ) = fToStr $ realToFrac r
210 getLit (LMFloatLit r LMDouble) = dToStr r
211 getLit f@(LMFloatLit _ _) = error $ "Can't print this float literal!" ++ show f
212 getLit (LMNullLit _) = "null"
213 getLit (LMUndefLit _) = "undef"
215 -- | Return the 'LlvmType' of the 'LlvmVar'
216 getVarType :: LlvmVar -> LlvmType
217 getVarType (LMGlobalVar _ y _ _ _ _) = y
218 getVarType (LMLocalVar _ y ) = y
219 getVarType (LMNLocalVar _ y ) = y
220 getVarType (LMLitVar l ) = getLitType l
222 -- | Return the 'LlvmType' of a 'LlvmLit'
223 getLitType :: LlvmLit -> LlvmType
224 getLitType (LMIntLit _ t) = t
225 getLitType (LMFloatLit _ t) = t
226 getLitType (LMNullLit t) = t
227 getLitType (LMUndefLit t) = t
229 -- | Return the 'LlvmType' of the 'LlvmStatic'
230 getStatType :: LlvmStatic -> LlvmType
231 getStatType (LMStaticLit l ) = getLitType l
232 getStatType (LMUninitType t) = t
233 getStatType (LMStaticStr _ t) = t
234 getStatType (LMStaticArray _ t) = t
235 getStatType (LMStaticStruc _ t) = t
236 getStatType (LMStaticPointer v) = getVarType v
237 getStatType (LMBitc _ t) = t
238 getStatType (LMPtoI _ t) = t
239 getStatType (LMAdd t _) = getStatType t
240 getStatType (LMSub t _) = getStatType t
241 getStatType (LMComment _) = error "Can't call getStatType on LMComment!"
243 -- | Return the 'LlvmType' of the 'LMGlobal'
244 getGlobalType :: LMGlobal -> LlvmType
245 getGlobalType (v, _) = getVarType v
247 -- | Return the 'LlvmVar' part of a 'LMGlobal'
248 getGlobalVar :: LMGlobal -> LlvmVar
249 getGlobalVar (v, _) = v
251 -- | Return the 'LlvmLinkageType' for a 'LlvmVar'
252 getLink :: LlvmVar -> LlvmLinkageType
253 getLink (LMGlobalVar _ _ l _ _ _) = l
256 -- | Add a pointer indirection to the supplied type. 'LMLabel' and 'LMVoid'
258 pLift :: LlvmType -> LlvmType
259 pLift (LMLabel) = error "Labels are unliftable"
260 pLift (LMVoid) = error "Voids are unliftable"
261 pLift x = LMPointer x
263 -- | Lower a variable of 'LMPointer' type.
264 pVarLift :: LlvmVar -> LlvmVar
265 pVarLift (LMGlobalVar s t l x a c) = LMGlobalVar s (pLift t) l x a c
266 pVarLift (LMLocalVar s t ) = LMLocalVar s (pLift t)
267 pVarLift (LMNLocalVar s t ) = LMNLocalVar s (pLift t)
268 pVarLift (LMLitVar _ ) = error $ "Can't lower a literal type!"
270 -- | Remove the pointer indirection of the supplied type. Only 'LMPointer'
271 -- constructors can be lowered.
272 pLower :: LlvmType -> LlvmType
273 pLower (LMPointer x) = x
274 pLower x = error $ show x ++ " is a unlowerable type, need a pointer"
276 -- | Lower a variable of 'LMPointer' type.
277 pVarLower :: LlvmVar -> LlvmVar
278 pVarLower (LMGlobalVar s t l x a c) = LMGlobalVar s (pLower t) l x a c
279 pVarLower (LMLocalVar s t ) = LMLocalVar s (pLower t)
280 pVarLower (LMNLocalVar s t ) = LMNLocalVar s (pLower t)
281 pVarLower (LMLitVar _ ) = error $ "Can't lower a literal type!"
283 -- | Test if the given 'LlvmType' is an integer
284 isInt :: LlvmType -> Bool
285 isInt (LMInt _) = True
288 -- | Test if the given 'LlvmType' is a floating point type
289 isFloat :: LlvmType -> Bool
290 isFloat LMFloat = True
291 isFloat LMDouble = True
292 isFloat LMFloat80 = True
293 isFloat LMFloat128 = True
296 -- | Test if the given 'LlvmType' is an 'LMPointer' construct
297 isPointer :: LlvmType -> Bool
298 isPointer (LMPointer _) = True
301 -- | Test if a 'LlvmVar' is global.
302 isGlobal :: LlvmVar -> Bool
303 isGlobal (LMGlobalVar _ _ _ _ _ _) = True
306 -- | Width in bits of an 'LlvmType', returns 0 if not applicable
307 llvmWidthInBits :: LlvmType -> Int
308 llvmWidthInBits (LMInt n) = n
309 llvmWidthInBits (LMFloat) = 32
310 llvmWidthInBits (LMDouble) = 64
311 llvmWidthInBits (LMFloat80) = 80
312 llvmWidthInBits (LMFloat128) = 128
313 -- Could return either a pointer width here or the width of what
314 -- it points to. We will go with the former for now.
315 llvmWidthInBits (LMPointer _) = llvmWidthInBits llvmWord
316 llvmWidthInBits (LMArray _ _) = llvmWidthInBits llvmWord
317 llvmWidthInBits LMLabel = 0
318 llvmWidthInBits LMVoid = 0
319 llvmWidthInBits (LMStruct tys) = sum $ map llvmWidthInBits tys
320 llvmWidthInBits (LMFunction _) = 0
321 llvmWidthInBits (LMAlias _ t) = llvmWidthInBits t
324 -- -----------------------------------------------------------------------------
325 -- ** Shortcut for Common Types
328 i128, i64, i32, i16, i8, i1, i8Ptr :: LlvmType
337 -- | The target architectures word size
338 llvmWord, llvmWordPtr :: LlvmType
339 llvmWord = LMInt (wORD_SIZE * 8)
340 llvmWordPtr = pLift llvmWord
342 -- -----------------------------------------------------------------------------
343 -- * LLVM Function Types
346 -- | An LLVM Function
347 data LlvmFunctionDecl = LlvmFunctionDecl {
348 -- | Unique identifier of the function
350 -- | LinkageType of the function
351 funcLinkage :: LlvmLinkageType,
352 -- | The calling convention of the function
353 funcCc :: LlvmCallConvention,
354 -- | Type of the returned value
355 decReturnType :: LlvmType,
356 -- | Indicates if this function uses varargs
357 decVarargs :: LlvmParameterListType,
358 -- | Parameter types and attributes
359 decParams :: [LlvmParameter],
360 -- | Function align value, must be power of 2
365 instance Show LlvmFunctionDecl where
366 show (LlvmFunctionDecl n l c r varg p a)
367 = let args = ((drop 1).concat) $ -- use drop since it can handle empty lists
368 map (\(t,a) -> "," ++ show t ++ " " ++ spaceCat a) p
370 VarArgs | not (null args) -> ", ..."
374 Just a' -> " align " ++ show a'
376 in show l ++ " " ++ show c ++ " " ++ show r ++ " @" ++ unpackFS n ++
377 "(" ++ args ++ varg' ++ ")" ++ align
379 type LlvmFunctionDecls = [LlvmFunctionDecl]
381 type LlvmParameter = (LlvmType, [LlvmParamAttr])
383 -- | LLVM Parameter Attributes.
385 -- Parameter attributes are used to communicate additional information about
386 -- the result or parameters of a function
388 -- | This indicates to the code generator that the parameter or return value
389 -- should be zero-extended to a 32-bit value by the caller (for a parameter)
390 -- or the callee (for a return value).
392 -- | This indicates to the code generator that the parameter or return value
393 -- should be sign-extended to a 32-bit value by the caller (for a parameter)
394 -- or the callee (for a return value).
396 -- | This indicates that this parameter or return value should be treated in
397 -- a special target-dependent fashion during while emitting code for a
398 -- function call or return (usually, by putting it in a register as opposed
401 -- | This indicates that the pointer parameter should really be passed by
402 -- value to the function.
404 -- | This indicates that the pointer parameter specifies the address of a
405 -- structure that is the return value of the function in the source program.
407 -- | This indicates that the pointer does not alias any global or any other
410 -- | This indicates that the callee does not make any copies of the pointer
411 -- that outlive the callee itself
413 -- | This indicates that the pointer parameter can be excised using the
414 -- trampoline intrinsics.
418 instance Show LlvmParamAttr where
419 show ZeroExt = "zeroext"
420 show SignExt = "signext"
424 show NoAlias = "noalias"
425 show NoCapture = "nocapture"
428 -- | Llvm Function Attributes.
430 -- Function attributes are set to communicate additional information about a
431 -- function. Function attributes are considered to be part of the function,
432 -- not of the function type, so functions with different parameter attributes
433 -- can have the same function type. Functions can have multiple attributes.
435 -- Descriptions taken from <http://llvm.org/docs/LangRef.html#fnattrs>
437 -- | This attribute indicates that the inliner should attempt to inline this
438 -- function into callers whenever possible, ignoring any active inlining
439 -- size threshold for this caller.
441 -- | This attribute indicates that the source code contained a hint that
442 -- inlining this function is desirable (such as the \"inline\" keyword in
443 -- C/C++). It is just a hint; it imposes no requirements on the inliner.
445 -- | This attribute indicates that the inliner should never inline this
446 -- function in any situation. This attribute may not be used together
447 -- with the alwaysinline attribute.
449 -- | This attribute suggests that optimization passes and code generator
450 -- passes make choices that keep the code size of this function low, and
451 -- otherwise do optimizations specifically to reduce code size.
453 -- | This function attribute indicates that the function never returns
454 -- normally. This produces undefined behavior at runtime if the function
455 -- ever does dynamically return.
457 -- | This function attribute indicates that the function never returns with
458 -- an unwind or exceptional control flow. If the function does unwind, its
459 -- runtime behavior is undefined.
461 -- | This attribute indicates that the function computes its result (or
462 -- decides to unwind an exception) based strictly on its arguments, without
463 -- dereferencing any pointer arguments or otherwise accessing any mutable
464 -- state (e.g. memory, control registers, etc) visible to caller functions.
465 -- It does not write through any pointer arguments (including byval
466 -- arguments) and never changes any state visible to callers. This means
467 -- that it cannot unwind exceptions by calling the C++ exception throwing
468 -- methods, but could use the unwind instruction.
470 -- | This attribute indicates that the function does not write through any
471 -- pointer arguments (including byval arguments) or otherwise modify any
472 -- state (e.g. memory, control registers, etc) visible to caller functions.
473 -- It may dereference pointer arguments and read state that may be set in
474 -- the caller. A readonly function always returns the same value (or unwinds
475 -- an exception identically) when called with the same set of arguments and
476 -- global state. It cannot unwind an exception by calling the C++ exception
477 -- throwing methods, but may use the unwind instruction.
479 -- | This attribute indicates that the function should emit a stack smashing
480 -- protector. It is in the form of a \"canary\"—a random value placed on the
481 -- stack before the local variables that's checked upon return from the
482 -- function to see if it has been overwritten. A heuristic is used to
483 -- determine if a function needs stack protectors or not.
485 -- If a function that has an ssp attribute is inlined into a function that
486 -- doesn't have an ssp attribute, then the resulting function will have an
489 -- | This attribute indicates that the function should always emit a stack
490 -- smashing protector. This overrides the ssp function attribute.
492 -- If a function that has an sspreq attribute is inlined into a function
493 -- that doesn't have an sspreq attribute or which has an ssp attribute,
494 -- then the resulting function will have an sspreq attribute.
496 -- | This attribute indicates that the code generator should not use a red
497 -- zone, even if the target-specific ABI normally permits it.
499 -- | This attributes disables implicit floating point instructions.
501 -- | This attribute disables prologue / epilogue emission for the function.
502 -- This can have very system-specific consequences.
506 instance Show LlvmFuncAttr where
507 show AlwaysInline = "alwaysinline"
508 show InlineHint = "inlinehint"
509 show NoInline = "noinline"
510 show OptSize = "optsize"
511 show NoReturn = "noreturn"
512 show NoUnwind = "nounwind"
513 show ReadNone = "readnon"
514 show ReadOnly = "readonly"
516 show SspReq = "ssqreq"
517 show NoRedZone = "noredzone"
518 show NoImplicitFloat = "noimplicitfloat"
522 -- | Different types to call a function.
524 -- | Normal call, allocate a new stack frame.
526 -- | Tail call, perform the call in the current stack frame.
530 -- | Different calling conventions a function can use.
531 data LlvmCallConvention
532 -- | The C calling convention.
533 -- This calling convention (the default if no other calling convention is
534 -- specified) matches the target C calling conventions. This calling
535 -- convention supports varargs function calls and tolerates some mismatch in
536 -- the declared prototype and implemented declaration of the function (as
539 -- | This calling convention attempts to make calls as fast as possible
540 -- (e.g. by passing things in registers). This calling convention allows
541 -- the target to use whatever tricks it wants to produce fast code for the
542 -- target, without having to conform to an externally specified ABI
543 -- (Application Binary Interface). Implementations of this convention should
544 -- allow arbitrary tail call optimization to be supported. This calling
545 -- convention does not support varargs and requires the prototype of al
546 -- callees to exactly match the prototype of the function definition.
548 -- | This calling convention attempts to make code in the caller as efficient
549 -- as possible under the assumption that the call is not commonly executed.
550 -- As such, these calls often preserve all registers so that the call does
551 -- not break any live ranges in the caller side. This calling convention
552 -- does not support varargs and requires the prototype of all callees to
553 -- exactly match the prototype of the function definition.
555 -- | Any calling convention may be specified by number, allowing
556 -- target-specific calling conventions to be used. Target specific calling
557 -- conventions start at 64.
559 -- | X86 Specific 'StdCall' convention. LLVM includes a specific alias for it
560 -- rather than just using CC_Ncc.
564 instance Show LlvmCallConvention where
566 show CC_Fastcc = "fastcc"
567 show CC_Coldcc = "coldcc"
568 show (CC_Ncc i) = "cc " ++ show i
569 show CC_X86_Stdcc = "x86_stdcallcc"
572 -- | Functions can have a fixed amount of parameters, or a variable amount.
573 data LlvmParameterListType
574 -- Fixed amount of arguments.
576 -- Variable amount of arguments.
581 -- | Linkage type of a symbol.
583 -- The description of the constructors is copied from the Llvm Assembly Language
584 -- Reference Manual <http://www.llvm.org/docs/LangRef.html#linkage>, because
585 -- they correspond to the Llvm linkage types.
587 -- | Global values with internal linkage are only directly accessible by
588 -- objects in the current module. In particular, linking code into a module
589 -- with an internal global value may cause the internal to be renamed as
590 -- necessary to avoid collisions. Because the symbol is internal to the
591 -- module, all references can be updated. This corresponds to the notion
592 -- of the @static@ keyword in C.
594 -- | Globals with @linkonce@ linkage are merged with other globals of the
595 -- same name when linkage occurs. This is typically used to implement
596 -- inline functions, templates, or other code which must be generated
597 -- in each translation unit that uses it. Unreferenced linkonce globals are
598 -- allowed to be discarded.
600 -- | @weak@ linkage is exactly the same as linkonce linkage, except that
601 -- unreferenced weak globals may not be discarded. This is used for globals
602 -- that may be emitted in multiple translation units, but that are not
603 -- guaranteed to be emitted into every translation unit that uses them. One
604 -- example of this are common globals in C, such as @int X;@ at global
607 -- | @appending@ linkage may only be applied to global variables of pointer
608 -- to array type. When two global variables with appending linkage are
609 -- linked together, the two global arrays are appended together. This is
610 -- the Llvm, typesafe, equivalent of having the system linker append
611 -- together @sections@ with identical names when .o files are linked.
613 -- | The semantics of this linkage follow the ELF model: the symbol is weak
614 -- until linked, if not linked, the symbol becomes null instead of being an
615 -- undefined reference.
617 -- | The symbol participates in linkage and can be used to resolve external
618 -- symbol references.
620 -- | Alias for 'ExternallyVisible' but with explicit textual form in LLVM
625 instance Show LlvmLinkageType where
626 show Internal = "internal"
627 show LinkOnce = "linkonce"
629 show Appending = "appending"
630 show ExternWeak = "extern_weak"
631 -- ExternallyVisible does not have a textual representation, it is
632 -- the linkage type a function resolves to if no other is specified
634 show ExternallyVisible = ""
635 show External = "external"
638 -- -----------------------------------------------------------------------------
642 -- | Llvm binary operators machine operations.
644 = LM_MO_Add -- ^ add two integer, floating point or vector values.
645 | LM_MO_Sub -- ^ subtract two ...
646 | LM_MO_Mul -- ^ multiply ..
647 | LM_MO_UDiv -- ^ unsigned integer or vector division.
648 | LM_MO_SDiv -- ^ signed integer ..
649 | LM_MO_URem -- ^ unsigned integer or vector remainder (mod)
650 | LM_MO_SRem -- ^ signed ...
652 | LM_MO_FAdd -- ^ add two floating point or vector values.
653 | LM_MO_FSub -- ^ subtract two ...
654 | LM_MO_FMul -- ^ multiply ...
655 | LM_MO_FDiv -- ^ divide ...
656 | LM_MO_FRem -- ^ remainder ...
660 -- | Logical shift right
661 -- Shift right, filling with zero
663 -- | Arithmetic shift right
664 -- The most significant bits of the result will be equal to the sign bit of
668 | LM_MO_And -- ^ AND bitwise logical operation.
669 | LM_MO_Or -- ^ OR bitwise logical operation.
670 | LM_MO_Xor -- ^ XOR bitwise logical operation.
673 instance Show LlvmMachOp where
674 show LM_MO_Add = "add"
675 show LM_MO_Sub = "sub"
676 show LM_MO_Mul = "mul"
677 show LM_MO_UDiv = "udiv"
678 show LM_MO_SDiv = "sdiv"
679 show LM_MO_URem = "urem"
680 show LM_MO_SRem = "srem"
681 show LM_MO_FAdd = "fadd"
682 show LM_MO_FSub = "fsub"
683 show LM_MO_FMul = "fmul"
684 show LM_MO_FDiv = "fdiv"
685 show LM_MO_FRem = "frem"
686 show LM_MO_Shl = "shl"
687 show LM_MO_LShr = "lshr"
688 show LM_MO_AShr = "ashr"
689 show LM_MO_And = "and"
691 show LM_MO_Xor = "xor"
694 -- | Llvm compare operations.
696 = LM_CMP_Eq -- ^ Equal (Signed and Unsigned)
697 | LM_CMP_Ne -- ^ Not equal (Signed and Unsigned)
698 | LM_CMP_Ugt -- ^ Unsigned greater than
699 | LM_CMP_Uge -- ^ Unsigned greater than or equal
700 | LM_CMP_Ult -- ^ Unsigned less than
701 | LM_CMP_Ule -- ^ Unsigned less than or equal
702 | LM_CMP_Sgt -- ^ Signed greater than
703 | LM_CMP_Sge -- ^ Signed greater than or equal
704 | LM_CMP_Slt -- ^ Signed less than
705 | LM_CMP_Sle -- ^ Signed less than or equal
707 -- Float comparisons. GHC uses a mix of ordered and unordered float
709 | LM_CMP_Feq -- ^ Float equal
710 | LM_CMP_Fne -- ^ Float not equal
711 | LM_CMP_Fgt -- ^ Float greater than
712 | LM_CMP_Fge -- ^ Float greater than or equal
713 | LM_CMP_Flt -- ^ Float less than
714 | LM_CMP_Fle -- ^ Float less than or equal
717 instance Show LlvmCmpOp where
718 show LM_CMP_Eq = "eq"
719 show LM_CMP_Ne = "ne"
720 show LM_CMP_Ugt = "ugt"
721 show LM_CMP_Uge = "uge"
722 show LM_CMP_Ult = "ult"
723 show LM_CMP_Ule = "ule"
724 show LM_CMP_Sgt = "sgt"
725 show LM_CMP_Sge = "sge"
726 show LM_CMP_Slt = "slt"
727 show LM_CMP_Sle = "sle"
728 show LM_CMP_Feq = "oeq"
729 show LM_CMP_Fne = "une"
730 show LM_CMP_Fgt = "ogt"
731 show LM_CMP_Fge = "oge"
732 show LM_CMP_Flt = "olt"
733 show LM_CMP_Fle = "ole"
736 -- | Llvm cast operations.
738 = LM_Trunc -- ^ Integer truncate
739 | LM_Zext -- ^ Integer extend (zero fill)
740 | LM_Sext -- ^ Integer extend (sign fill)
741 | LM_Fptrunc -- ^ Float truncate
742 | LM_Fpext -- ^ Float extend
743 | LM_Fptoui -- ^ Float to unsigned Integer
744 | LM_Fptosi -- ^ Float to signed Integer
745 | LM_Uitofp -- ^ Unsigned Integer to Float
746 | LM_Sitofp -- ^ Signed Int to Float
747 | LM_Ptrtoint -- ^ Pointer to Integer
748 | LM_Inttoptr -- ^ Integer to Pointer
749 | LM_Bitcast -- ^ Cast between types where no bit manipulation is needed
752 instance Show LlvmCastOp where
753 show LM_Trunc = "trunc"
754 show LM_Zext = "zext"
755 show LM_Sext = "sext"
756 show LM_Fptrunc = "fptrunc"
757 show LM_Fpext = "fpext"
758 show LM_Fptoui = "fptoui"
759 show LM_Fptosi = "fptosi"
760 show LM_Uitofp = "uitofp"
761 show LM_Sitofp = "sitofp"
762 show LM_Ptrtoint = "ptrtoint"
763 show LM_Inttoptr = "inttoptr"
764 show LM_Bitcast = "bitcast"
767 -- -----------------------------------------------------------------------------
768 -- * Floating point conversion
771 -- | Convert a Haskell Double to an LLVM hex encoded floating point form. In
772 -- Llvm float literals can be printed in a big-endian hexadecimal format,
773 -- regardless of underlying architecture.
774 dToStr :: Double -> String
776 = let bs = doubleToBytes d
777 hex d' = case showHex d' "" of
778 [] -> error "dToStr: too few hex digits for float"
781 _ -> error "dToStr: too many hex digits for float"
783 str = map toUpper $ concat . fixEndian . (map hex) $ bs
786 -- | Convert a Haskell Float to an LLVM hex encoded floating point form.
787 -- LLVM uses the same encoding for both floats and doubles (16 digit hex
788 -- string) but floats must have the last half all zeroes so it can fit into
789 -- a float size type.
790 {-# NOINLINE fToStr #-}
791 fToStr :: Float -> String
792 fToStr = (dToStr . realToFrac)
794 -- | Reverse or leave byte data alone to fix endianness on this target.
795 fixEndian :: [a] -> [a]
796 #ifdef WORDS_BIGENDIAN