Implemented LLVMType>>sizeInBits/sizeInBytes for all data types
authorJan Vrany <jan.vrany@fit.cvut.cz>
Wed, 06 Jul 2016 22:40:59 +0100
changeset 71 ab03b0a6d037
parent 70 ced2a5c16e70
child 72 2c876bd46960
Implemented LLVMType>>sizeInBits/sizeInBytes for all data types ...i.e., also for structures, vectors and arrays.
LLVMExamples.st
LLVMTargetData.st
LLVMType.st
LLVMTypeDouble.st
LLVMTypeFP128.st
LLVMTypeFloat.st
LLVMTypeHalt.st
LLVMTypeInteger.st
LLVMTypePPC_FP128.st
LLVMTypePointer.st
LLVMTypeVoid.st
LLVMTypeX86_FP80.st
tests/LLVMTypeTests.st
--- a/LLVMExamples.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMExamples.st	Wed Jul 06 22:40:59 2016 +0100
@@ -709,12 +709,21 @@
     point := asm alloca: pointTy as: 'point'.
     asm memset: point  _: (LLVMConstant uint8: 0) _: (LLVMConstant uint64: (LLVMType int32 sizeInBytes * 2))  _: 1 _: false.
     asm ret.
+
+    module verify.
     self halt.
     "
     LLVMExamples new example9_memset
     "
 
     "Created: / 06-07-2016 / 00:01:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 06-07-2016 / 09:46:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 08-07-2016 / 15:44:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!LLVMExamples class methodsFor:'documentation'!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+! !
+
--- a/LLVMTargetData.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTargetData.st	Wed Jul 06 22:40:59 2016 +0100
@@ -17,7 +17,7 @@
 
 LLVMDisposableObject subclass:#LLVMTargetData
 	instanceVariableNames:''
-	classVariableNames:''
+	classVariableNames:'Default'
 	poolDictionaries:''
 	category:'LLVM-S-Core'
 !
@@ -55,6 +55,20 @@
     "Created: / 11-07-2015 / 07:03:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!LLVMTargetData class methodsFor:'accessing'!
+
+default
+    "Return a target data for platform we're currently
+     running on."
+
+    Default isNil ifTrue:[ 
+        Default := self new.
+    ].
+    ^ Default
+
+    "Created: / 06-07-2016 / 21:40:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !LLVMTargetData methodsFor:'initialization & release'!
 
 dispose
--- a/LLVMType.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMType.st	Wed Jul 06 22:40:59 2016 +0100
@@ -242,11 +242,11 @@
 !
 
 intptr
-    IntPtr isNil ifTrue:[ IntPtr := LLVM IntPtrType: LLVMTargetData new ].
+    IntPtr isNil ifTrue:[ IntPtr := LLVM IntPtrType: LLVMTargetData default ].
     ^ IntPtr
 
     "Created: / 11-07-2015 / 07:05:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified (format): / 13-08-2015 / 17:22:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 06-07-2016 / 22:29:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !LLVMType class methodsFor:'types - structures'!
@@ -293,15 +293,16 @@
 !LLVMType methodsFor:'accessing'!
 
 alignmentInBits
-    "Return an alignment of the type in bits"
+    "Return an alignment of the type in bits in default data model"
 
     ^ self alignmentInBytes * 8
 
     "Created: / 14-08-2015 / 09:16:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 06-07-2016 / 22:27:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 alignmentInBytes
-    "Return an alignment of the type in bytes"
+    "Return an alignment of the type in bytes in default data model"
 
     | alignmentAsValue |
 
@@ -312,6 +313,7 @@
 
     "Created: / 14-08-2015 / 09:16:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 17-09-2015 / 19:25:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 06-07-2016 / 22:27:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 kind
@@ -321,27 +323,52 @@
 !
 
 sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
+    "Return the size of the type in bits in default data model.
+
+     LLVMType int32 sizeInBits -> 32
+     LLVMType int1 sizeInBits  -> 1
+     (LLVMType struct: { LLVMType int16 . LLVMType int16 }) sizeInBits.
+    "
+    ^self sizeInBitsIn: LLVMTargetData default.
+
+    "Created: / 13-08-2015 / 16:25:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 06-07-2016 / 22:26:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+sizeInBitsIn: anLLVMTargetData
+    "Return the size of the type in bits in given data model.
 
      LLVMType int32 sizeInBits -> 32
      LLVMType int1 sizeInBits  -> 1
     "
-    LLVMTypeError new signal: 'type size not known'
+    self assert: (anLLVMTargetData isKindOf: LLVMTargetData).
+    ^ LLVM SizeOfTypeInBits: anLLVMTargetData _: self
 
-    "Created: / 13-08-2015 / 16:25:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 06-07-2016 / 22:13:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 sizeInBytes
-    "For integer, pointer, FP  types, return the size in bytes (rounded up). For all 
-     other types, throw an LLVMTypeError.
+    "Return the size of the type in bytes in default data model,
+     prettu much like C's sizeof() operator."
+
+    ^ self sizeInBytesIn: LLVMTargetData default
+
+    "Created: / 14-08-2015 / 07:27:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 06-07-2016 / 22:27:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+sizeInBytesIn: anLLVMTargetData
+    "Return size of the type in bytes for given data model.
 
      LLVMType int32 sizeInBytes -> 4
      LLVMType int1 sizeInBytes  -> 1
     "
-    ^ (self sizeInBits // 8) + (self sizeInBits \\ 8)
+    | sizeInBits |
 
-    "Created: / 14-08-2015 / 07:27:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    sizeInBits := self sizeInBits.
+    ^ (sizeInBits // 8) + (sizeInBits \\ 8)
+
+    "Created: / 06-07-2016 / 22:25:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !LLVMType methodsFor:'comparing'!
--- a/LLVMTypeDouble.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypeDouble.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypeDouble methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-    ^ 64
-
-    "Created: / 13-08-2015 / 17:02:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypeDouble methodsFor:'testing'!
 
 isDoubleType
--- a/LLVMTypeFP128.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypeFP128.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypeFP128 methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-    ^ 128
-
-    "Created: / 13-08-2015 / 17:02:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypeFP128 methodsFor:'testing'!
 
 isFP128Type
--- a/LLVMTypeFloat.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypeFloat.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypeFloat methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-    ^ 32
-
-    "Created: / 13-08-2015 / 17:02:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypeFloat methodsFor:'testing'!
 
 isFloatType
--- a/LLVMTypeHalt.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypeHalt.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypeHalt methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-   ^ 16
-
-    "Created: / 13-08-2015 / 17:02:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypeHalt methodsFor:'testing'!
 
 isHalfType
--- a/LLVMTypeInteger.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypeInteger.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypeInteger methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-    ^ LLVM GetIntTypeWidth: self
-
-    "Created: / 13-08-2015 / 17:03:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypeInteger methodsFor:'converting'!
 
 vector: numberOfElements
--- a/LLVMTypePPC_FP128.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypePPC_FP128.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypePPC_FP128 methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-    ^ 128
-
-    "Created: / 13-08-2015 / 17:03:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypePPC_FP128 methodsFor:'testing'!
 
 isPPC_FP128Type
--- a/LLVMTypePointer.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypePointer.st	Wed Jul 06 22:40:59 2016 +0100
@@ -46,18 +46,6 @@
     ^ LLVM GetElementType: self
 
     "Created: / 12-10-2015 / 16:23:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-    ^ LLVM GetIntTypeWidth: self
-
-    "Created: / 13-08-2015 / 17:03:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !LLVMTypePointer methodsFor:'converting'!
--- a/LLVMTypeVoid.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypeVoid.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypeVoid methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-    ^ 0
-
-    "Created: / 12-10-2015 / 18:27:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypeVoid methodsFor:'converting'!
 
 array: numberOfElements
--- a/LLVMTypeX86_FP80.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/LLVMTypeX86_FP80.st	Wed Jul 06 22:40:59 2016 +0100
@@ -40,20 +40,6 @@
 "
 ! !
 
-!LLVMTypeX86_FP80 methodsFor:'accessing'!
-
-sizeInBits
-    "For integer, pointer, FP  types, return the size in bits. For all 
-     other types, throw an LLVMTypeError.
-
-     LLVMType int32 sizeInBits -> 32
-     LLVMType int1 sizeInBits  -> 1
-    "
-   ^ 80
-
-    "Created: / 13-08-2015 / 17:03:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !LLVMTypeX86_FP80 methodsFor:'testing'!
 
 isX86_FP80Type
--- a/tests/LLVMTypeTests.st	Wed Jul 06 09:53:56 2016 +0100
+++ b/tests/LLVMTypeTests.st	Wed Jul 06 22:40:59 2016 +0100
@@ -93,6 +93,24 @@
     self assert: LLVMType fp128 sizeInBits == 128.
 
     "Created: / 13-08-2015 / 18:46:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_sizeInBits_02
+
+    self assert: LLVMType int1 pointer sizeInBits == (ExternalAddress pointerSize * 8)
+
+    "Created: / 06-07-2016 / 22:21:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_sizeInBits_03
+    | pointTy pointArrTy |
+
+    pointTy := LLVMType struct:{ LLVMType int64 . LLVMType int64 }.
+    self assert: pointTy sizeInBits == 128.
+    pointArrTy := LLVMType arrayOf: pointTy size: 10.
+    self assert: pointArrTy sizeInBits == 1280.
+
+    "Created: / 06-07-2016 / 22:22:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !LLVMTypeTests methodsFor:'tests - sequential types'!