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