FFI: FFI-Kernel-mt.91.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

FFI: FFI-Kernel-mt.91.mcz

commits-2
Marcel Taeumel uploaded a new version of FFI-Kernel to project FFI:
http://source.squeak.org/FFI/FFI-Kernel-mt.91.mcz

==================== Summary ====================

Name: FFI-Kernel-mt.91
Author: mt
Time: 4 June 2020, 5:49:29.568669 pm
UUID: 863883be-18f8-4945-a8e5-d138c32b50c4
Ancestors: FFI-Kernel-mt.90

Slightly refactor the process of updating external types and external structs when a platform change is detected.

1. Null (and maybe resize) all external addresses.
2. For all struct types, update pointer sizes and null struct sizes.
3. For all struct classes, look for definition changes and re-configure struct types in any case.
4. Update external pools.

Note that I pushed the field recompilatino code from ExternalObject down to ExternalStruct. Apologies if I overlooked update code for other external objects. There are none at the moment, or are there?

I tried to retain the identity of #compiledSpec in atomic types and struct types. Not sure whether this is necessary. Well, it is nice to have that identity check for #isTypeAlias and #originalType. :-)

=============== Diff against FFI-Kernel-mt.90 ===============

Item was removed:
- ----- Method: ExternalObject class>>install (in category 'system startup') -----
- install
- "Notify all instances of the receiver that we're coming up on a new platform.
- Note: The default implementation does nothing since the general external
- objects are cleaned up by ExternalAddress>>platformChangedFrom:to: but subclasses may
- implement this method so that the appropriate action for existing instances can
- be taken."!

Item was removed:
- ----- Method: ExternalObject class>>installSubclasses (in category 'system startup') -----
- installSubclasses
- "Notify all the subclasses of ExternalObject that we are starting up on a new platform."
- self withAllSubclassesDo:[:cls| cls install].!

Item was removed:
- ----- Method: ExternalObject class>>platformChangedFrom:to: (in category 'system startup') -----
- platformChangedFrom: lastPlatform to: currentPlatform
- "The system is coming up on a new platform. Clear out the existing handles."
- self installSubclasses.!

Item was removed:
- ----- Method: ExternalStructure class>>install (in category 'system startup') -----
- install
- "Resume the system on a new platform. Recompile all structures to account for different word size etc."
-
- self recompileStructures.!

Item was added:
+ ----- Method: ExternalStructure class>>platformChangedFrom:to: (in category 'system startup') -----
+ platformChangedFrom: lastPlatform to: currentPlatform
+ "The system is coming up on a new platform. Clear out the existing handles."
+ self recompileStructures.!

Item was changed:
  ----- Method: ExternalStructure class>>recompileStructures (in category 'system startup') -----
  recompileStructures
  "Check and update the layout of all subclasses for host machine dependency.
  Arrange to check the inner nested structures first."
 
  "ExternalStructure recompileStructures"
  | sorted unsorted priorAuthorInitials |
  unsorted := self withAllSubclasses.
  sorted := OrderedCollection new: unsorted size.
  self sortStructs: unsorted into: sorted.
  priorAuthorInitials := Utilities authorInitialsPerSe.
  Utilities setAuthorInitials: 'FFI'.
+ [sorted do: [:struct | struct checkFieldLayoutChange ifFalse: [
+ "Even if no layout change, communicate that result to the corresponding types."
+ struct externalType
+ compiledSpec: struct compiledSpec;
+ byteAlignment: struct byteAlignment]]]
- [sorted do: [:e | e checkFieldLayoutChange]]
  ensure: [Utilities setAuthorInitials: priorAuthorInitials]!

Item was changed:
  ----- Method: ExternalType class>>initializeAtomicTypes (in category 'class initialization') -----
  initializeAtomicTypes
  "ExternalType initialize"
 
  | atomicType byteSize type typeName byteAlignment |
  self flag: #ffiLongVsInt. "For a discussion about long vs. int see http://forum.world.st/Re-squeak-dev-64-bit-FFI-was-porting-Croquet-to-Squeak6-0-alpha-tp5113318.html."
 
  #(
  "name atomic id byte size byte alignment"
  ('void' 0 0 0)
  ('bool' 1 1 1)
  ('byte' 2 1 1)
  ('sbyte' 3 1 1)
  ('ushort' 4 2 2)
  ('short' 5 2 2)
  "!!!!!!" ('ulong' 6 4 "!!!!!!" 4)
  "!!!!!!" ('long' 7 4 "!!!!!!" 4)
  ('ulonglong' 8 8 8) "v.i."
  ('longlong' 9 8 8) "v.i."
  ('char' 10 1 1)
  ('schar' 11 1 1)
  ('float' 12 4 4)
  ('double' 13 8 8) "v.i."
  "TODO: ('longdouble' 14 10 16? 4?)"
  ) do:[:typeSpec| | compiled |
  typeName := typeSpec first.
  atomicType := typeSpec second.
  byteSize := typeSpec third.
  byteAlignment := typeSpec fourth.
 
  "0) On 32-bits Windows and MacOS, double and long long have an alignment of 8. But on 32-bit Linux, their alignment is 4. But not on a 32-bit Raspberry Pi OS."
  (FFIPlatformDescription current wordSize = 4
  and: [FFIPlatformDescription current isUnix
  and: [FFIPlatformDescription current isARM not]]) ifTrue: [
  (#('double' 'longlong' 'ulonglong') includes: typeName) ifTrue: [
  byteAlignment := 4]].
 
  "1) Regular type"
+ type := (AtomicTypes at: typeName).
  compiled := WordArray with: ((byteSize bitOr: FFIFlagAtomic) bitOr:
  (atomicType bitShift: FFIAtomicTypeShift)).
+ compiled ~= type compiledSpec
+ "Preserve the identity of #compiledSpec."
+ ifTrue: [type compiledSpec: compiled].
+ type byteAlignment: byteAlignment.
- type := (AtomicTypes at: typeName).
- type
- compiledSpec: compiled;
- byteAlignment: byteAlignment.
 
  "2) Pointer type"
+ type := type asPointerType.
  compiled := WordArray with: ((self pointerSpec bitOr: FFIFlagAtomic) bitOr:
  (atomicType bitShift: FFIAtomicTypeShift)).
+ compiled ~= type compiledSpec
+ "Preserve the identity of #compiledSpec."
+ ifTrue: [type compiledSpec: compiled].
+ type byteAlignment: self pointerAlignment.
- type asPointerType
- byteAlignment: self pointerAlignment;
- compiledSpec: compiled.
  ].!

Item was changed:
  ----- Method: ExternalType class>>initializeStructureTypes (in category 'class initialization') -----
  initializeStructureTypes
+ "Reset all non-pointer struct types to zero and their pointer companions to the appropriate pointer size."
- "ExternalType initialize"
 
+ StructTypes valuesDo:[:structType |
+ structType "asNonPointerType"
+ compiledSpec: (WordArray with: self structureSpec);
+ byteAlignment: nil.
+ structType asPointerType
+ compiledSpec: (WordArray with: self pointerSpec);
+ byteAlignment: nil].!
- | referentClassOrNil |
- self cleanupUnusedTypes.
-
- StructTypes keysAndValuesDo:[:referentName :type |
- referentClassOrNil := (self environment classNamed: referentName)
- ifNotNil: [:cls | (cls  includesBehavior: ExternalStructure) ifTrue: [cls]].
-
- self flag: #remove. "mt: Recompilation happens already via ExternalObject."
- referentClassOrNil ifNotNil: [referentClassOrNil compileFieldsSilently].
-
- type asNonPointerType
- newReferentClass: referentClassOrNil.
- type asPointerType
- newReferentClass: referentClassOrNil.
-
- ].!

Item was changed:
  ----- Method: ExternalType class>>platformChangedFrom:to: (in category 'system startup') -----
  platformChangedFrom: lastPlatform to: currentPlatform
  "Byte size or byte alignment for atomic types might be different on the new platform."
 
+ self cleanupUnusedTypes.
+
  self initializeAtomicTypes.
  self initializeStructureTypes.!

Item was changed:
  ----- Method: FFIPlatformDescription class>>startUp: (in category 'system startup') -----
  startUp: resuming
  "Notify all FFI classes about platform changes."
 
  resuming ifTrue: [
  LastPlatform in: [:lastPlatform | self newCurrent in: [:currentPlatform |
  lastPlatform = currentPlatform
  ifTrue: [
  self flag: #discuss. "mt: Maybe add #platformResuming?"
  ExternalAddress allBeNull]
  ifFalse: [
  LastPlatform := currentPlatform. "Update now. See #current."
+ { ExternalAddress. ExternalType. ExternalStructure. ExternalPool }
- { ExternalType. ExternalAddress. ExternalObject. ExternalPool }
  do: [:cls | cls
  platformChangedFrom: lastPlatform
  to: currentPlatform] ]]] ].!