Updated release image date.
[cslatevm.git] / src / mobius / types.slate
blob8fee5eb3f58e01fd134f1e6a31eda3e0dda2d017
1 define: #Type &basedOn: Types Type &slots: {#dispatcher -> Cloneable clone}.
2 "A dispatching object for statically determining which pidgin method to invoke."
4 t@(Type traits) clone
6   resend `>> [dispatcher := t dispatcher clone. ]
7 ].
9 t@(Type traits) dependencies
10 [Set new `>> [addDependency: t withVisited: Set new. remove: t. ]].
12 c addDependency: t@(Type traits) withVisited: visited
16 t@(Type traits) alignment
17 [| bits |
18   (bits := t bitSize) >= LongInt bitSize \/ [bits = 0]
19     ifTrue: [LongInt bitSize]
20     ifFalse: [bits]
23 t@(Type traits) byteSize
25   (t bitSize + Char bitSize - 1) // Char bitSize
28 t@(Type traits) wordSize
30   (t bitSize + LongInt bitSize - 1) // LongInt bitSize
33 define: #NamedType &basedOn: Type &slots: {#cName}.
35 define: #Pointer &basedOn: Type &slots: {#targetType}.
37 p@(Pointer traits) to: targetType
38 "Creates a new Pointer Type for the targetType."
39 "TODO: Map an IdentityDictionary from types to types of pointers to those
40 types, and unify implicitly?"
42   p clone `>> [targetType := targetType. ]
45 p@(Pointer traits) cName ['P' ; p targetType slateName].
46 p@(Pointer traits) slateName ['UndefinedSlatePointer'].
49 _@(Pointer traits) bitSize
51   LongInt bitSize
54 t@(Type traits) pointer
56   Pointer to: t
59 p@(Pointer traits) = q@(Pointer traits)
60 [p targetType = q targetType].
62 c addDependency: p@(Pointer traits) withVisited: visited
64   (visited includes: p)
65     ifFalse:
66       [visited include: p.
67        (p targetType isSameAs: Synonym)
68           ifTrue: [c addDependency: p targetType withVisited: visited]]
71 define: #Integer &basedOn: Type &slots: {#pointerType. #cPrintName}.
73 _@(Integer traits) baseByteSize [overrideThis].
75 n@(Integer traits) byteSize &platform: p
77   n baseByteSizeOn: (p `defaultsTo: Platform Current)
80 n@(Integer traits) bitSize &platform: p
81 [(n byteSize &platform: p) * 8].
83 n@(Integer traits) cName
84 [n cPrintName `defaultsTo: n printName name fromCamelCase].
86 n@(Integer traits) slateName
87 [n printName].
89 n@(Integer traits) pointer
90 [n pointerType `defaultsTo: (Pointer to: n)].
92 define: #Signed &basedOn: Integer.
93 define: #Unsigned &basedOn: Integer.
95 define: #Char &basedOn: Signed.
96 _@(Char traits) baseByteSizeOn: _ [1].
97 define: #UnsignedChar &basedOn: Unsigned.
98 _@(UnsignedChar traits) baseByteSizeOn: _ [1].
99 define: #ShortInt &basedOn: Signed.
100 _@(ShortInt traits) baseByteSizeOn: _ [2].
101 define: #UnsignedShortInt &basedOn: Unsigned.
102 _@(UnsignedShortInt traits) baseByteSizeOn: _ [2].
103 define: #LongInt &basedOn: Signed.
104 _@(LongInt traits) baseByteSizeOn: p [p bytesPerWord].
105 define: #UnsignedLongInt &basedOn: Unsigned.
106 _@(UnsignedLongInt traits) baseByteSizeOn: p [p bytesPerWord].
107 define: #LongLongInt &basedOn: Signed.
108 _@(LongLongInt traits) baseByteSizeOn: p [p bytesPerWord * 2].
109 define: #UnsignedLongLongInt &basedOn: Unsigned.
110 _@(UnsignedLongLongInt traits) baseByteSizeOn: p [p bytesPerWord * 2].
111 define: #Float &basedOn: Type.
112 _@(Float traits) baseByteSizeOn: p [4].
113 define: #Double &basedOn: Float.
114 _@(Double traits) baseByteSizeOn: p [8].
115 define: #Void &basedOn: Type.
116 _@(Void traits) baseByteSizeOn: _ [0].
118 define: #FixedInteger &basedOn: Integer.
120 define: #FixedSigned &basedOn: FixedInteger.
121 define: #FixedUnsigned &basedOn: FixedInteger.
123 define: #Modifier &basedOn: Type &slots: {#baseType -> Type}.
125 t@(Modifier traits) cName [t modifierName ; ' ' ; t baseType cName].
127 m@(Modifier traits) for: b
128 [m clone `>> [baseType := b. ]].
130 define: #Mutability &basedOn: Modifier.
131 define: #Const &basedOn: Mutability.
132 _@Const modifierName [#const].
133 t@(Type traits) const [Const for: t].
134 define: #Volatile &basedOn: Mutability.
135 _@Volatile modifierName [#volatile].
136 t@(Type traits) volatile [Volatile for: t].
138 define: #Storage &basedOn: Modifier.
139 Storage traits None     ::= Storage clone.
140 Storage traits Auto     ::= Storage clone.
141 _@(Storage Auto traits) modifierName [#auto].
142 t@(Type traits) auto [Storage Auto for: t].
143 Storage traits Static   ::= Storage clone.
144 _@(Storage Static traits) modifierName [#static].
145 t@(Type traits) static [Storage Static for: t].
146 Storage traits Extern   ::= Storage clone.
147 _@(Storage Extern traits) modifierName [#extern].
148 t@(Type traits) extern [Storage Extern for: t].
149 Storage traits Register ::= Storage clone.
150 _@(Storage Register traits) modifierName [#register].
151 t@(Type traits) register [Storage Register for: t].
153 define: #Array &basedOn: Type &slots:
154   {#elementType -> UnsignedLongInt.
155    #size -> Nil}.
157 t@(Array traits) of: elementType size: size
158 [t clone `>> [elementType := elementType. size := size. ]].
160 t@(Array traits) of: elementType
161 [t of: elementType size: Nil].
163 c addDependency: t@(Array traits) withVisited: visited
165   (visited includes: t)
166     ifFalse:
167       [
168         visited include: t.
169         c addDependency: t elementType withVisited: visited
170       ]
173 t@(Array traits) bitSize
175   t elementType bitSize * t size
178 define: #Synonym &basedOn: NamedType
179   &slots: {#targetType. #pointer -> Pointer. #isExported -> False}.
181 t@(Synonym traits) newNamed: name type: type
183   t clone `>> [| :newT |
184     cName := name.
185     pointer := t pointer to: newT.
186     targetType := type. ]
189 t@(Synonym traits) export
191   t isExported := True.
192   t
195 c addDependency: t@(Synonym traits) withVisited: visited
197   (visited includes: t)
198     ifFalse:
199       [visited include: t.
200        c include: t.
201        c addDependency: t targetType withVisited: visited]
204 t@(Synonym traits) bitSize
206   t targetType bitSize
209 define: #Structure &basedOn: NamedType
210   &slots: {#basis. #pointer -> Pointer. #isExported -> False.
211            #elementSpecs -> ExtensibleArray new}.
213 t@(Structure traits) newNamed: name basedOn: basis
215   t clone `>>
216    [| :newT | cName := name. basis := basis.
217     pointer := t pointer to: newT.
218     "pointer rules rawAddDelegate: #basisPointer valued: basis pointer rules.
219     pointer dispatcher rawAddDelegate: #basisPointer valued: basis pointer dispatcher."
220     elementSpecs := basis elementSpecs copy.
221     elementSpecs do:
222       [| :se | [| :_ | se type] asMethod: se cName asInferenceRule
223                                 on: {newT}]. ]
226 t@(Structure traits) newNamed: name
228   t clone `>> [| :newT |
229     cName := name.
230     basis := Nil.
231     pointer := t pointer to: newT.
232     elementSpecs := t elementSpecs new. ]
235 t@(Structure traits) export
237   t isExported := True.
238   t
241 c addDependency: t@(Structure traits) withVisited: visited
243   (visited includes: t)
244     ifFalse:
245       [
246         visited include: t.
247         c include: t.
248         t elementSpecs do: [| :se | c addDependency: se type withVisited: visited]
249       ]
252 t@(Structure traits) doElements: block
253 [| previousSE bits |
254   previousSE := t elementSpecs first.
255   block applyTo: {previousSE. 0}.
256   bits := previousSE bitSize.
257   t elementSpecs allButFirstDo:
258     [| :se |
259       previousSE packed /\ [se packed]
260         ifFalse:
261           [| alignment |
262             alignment := se type alignment.
263             previousSE packed
264               ifTrue:
265                 [alignment := alignment max: previousSE type bitSize].
266             bits := (bits / alignment) ceiling * alignment].
267       block applyTo: {se. bits}.
268       bits += se bitSize.
269       previousSE := se]
272 t@(Type traits) walkElements: block &bitOffset: offset
274   t walkElements: block withPath: Stack new atBitOffset: (offset ifNil: [0])
277 _@(Type traits) walkElements: _ withPath: _ atBitOffset: _
281 t@(Structure traits) walkElements: block withPath: path atBitOffset: baseOffset
283   t doElements:
284     [| :se :offset |
285      path push: se.
286      block applyTo: {path. baseOffset + offset}.
287      se type walkElements: block withPath: path atBitOffset: baseOffset + offset.
288      path pop]
291 t@(Structure traits) unpaddedSize
292 [| lastSE lastOffset |
293   t elementSpecs isEmpty
294     ifTrue: [^ 0].
295   t doElements:
296     [| :se :offset |
297       lastSE := se.
298       lastOffset := offset].
299   lastOffset + lastSE bitSize
302 t@(Structure traits) alignment
304   t elementSpecs inject: 0 into:
305     [| :maxAlign :se | maxAlign max: se type alignment]
308 t@(Structure traits) bitSize
309 [| maxAlign |
310   maxAlign := t alignment.
311   (t unpaddedSize / maxAlign) ceiling * maxAlign
315 define: #Union &basedOn: Structure.
317 t@(Union traits) doElements: block
319   t elementSpecs do:
320     [| :se | block applyTo: {se. 0}]
323 t@(Union traits) unpaddedSize
324 [| maxSize |
325   maxSize := 0.
326   t doElements:
327     [| :se :_ |
328       maxSize := maxSize max: se type bitSize].
329   maxSize
332 define: #StructureElement &basedOn: NamedType
333   &slots: {#type -> UnsignedLongInt}.
335 se@(StructureElement traits) newNamed: name type: type
337   se clone `>> [cName := name. type := type. ]
340 t@(Structure traits) addElement: se
342   t elementSpecs
343     do: [| :each | each cName = se cName
344           ifTrue: [warn: 'Attempted Redefinition of type StructureElement, named "' ; each cName ; '".'.
345             ^ each]].
346   [| :_ | se type] asMethod: se cName asInferenceRule on: {t}.
347   [| :_ | se type] asMethod: se cName asInferenceRule on: {t pointer}.
348   t elementSpecs addLast: se
351 t@(Structure traits) addElementNamed: name &type
353   type `defaultsTo: UnsignedLongInt.
354   t addElement: (StructureElement newNamed: name type: type)
357 t@(StructureElement traits) bitSize
359   t type bitSize
362 _@(StructureElement traits) packed
364   False
367 define: #PackedStructureElement &basedOn: StructureElement
368   &slots: {#bitSize -> 0}.
370 se@(PackedStructureElement traits) newNamed: name size: size
372   (se newNamed: name type: UnsignedLongInt) `>> [bitSize := size. ]
375 t@(Structure traits) addPackedElementNamed: name size: size
377   t addElement: (PackedStructureElement newNamed: name size: size)
380 _@(PackedStructureElement traits) packed
381 [True].
383 define: #Function &basedOn: Type
384   &slots: {#argumentTypes -> {}. #resultType -> Void}.
386 fun@(Function traits) from: argumentTypes to: resultType
388   fun clone `>>
389    [| :newFun | argumentTypes := argumentTypes as: newFun argumentTypes.
390     resultType := resultType. ]
393 define: #FunctionPointer &basedOn: Pointer.
395 fun@(Function traits) pointer
397   FunctionPointer to: fun
400 _@(FunctionPointer traits) from: argumentTypes to: resultType
402   (Function from: argumentTypes to: resultType) pointer
405 define: #Module &basedOn: Type.