64 Bit Delphi
What does it all mean?
David Intersimone “David I”
VP of Developer Relations and Chief Evangelist
davidi@embarcadero.com
http://blogs.embarcadero.com/davidi
Twitter: davidi99
Skype: davidi99
1Agenda• RAD Studio Product Family Overview• 64bit computing – What’s the same? Different?
– Data types• The CPU - Instructions, Addresses• What this means for Delphi• What can I do today?
• Q&A

2

Product Editions
Delphi XE• Starter• Professional• Enterprise• Architect

C++Builder XE• Starter• Professional• Enterprise• Architect

Delphi Prism XE

RadPHP XE• Professional• Enterprise

RAD Studio XE• Professional• Enterprise• Architect

3ER Studio Developer Edition
Included on Architect edition

Included all IDE’s

InterBase Developer Edition FREEfor development

Delphi XE and C++Builder XE Starter Edition• Low cost solution for hobbyists, students, andindependent developers to be able to get started building
and distributing apps
• VCL Form Designer to visually build native Windowsapplications• Full-featured debugger with color syntax highlighting• Hundreds of included components––––

4Touch and Gesture support
Ribbon Controls
IBX for database development
Allow third party components

Bit Computing

5

What’s the same?
••
••
••

6

Integer, Longint, Cardinal – still 32bits
Int64, UInt64 – still 64bits
UnicodeString, AnsiString, WideString
Exceptions
Runtime Library (RTL)
SysUtils, Classes, etc…

Delphi 32 and 64-bit Type Sizes

7

Signed types
ShortInt
SmallIntLongInt
Integer
Int64

Delphi/32
1 byte2 bytes
4 bytes
4 bytes
8 bytes

Delphi/64Unsigned types
Byte
WordLongWord
Cardinal
UInt64

Delphi/32
1 byte2 bytes
4 bytes
4 bytes
8 bytes

Delphi/64What’s different?

•••
•

8

NativeInt, NativeUint – 64bits
Pointer (all pointers) – 64bits
Dynamic Arrays – 64bit indexing
Floating point math - DoubleDelphi 32 and 64-bit Type Sizes

9Signed types
NativeInt

Delphi/32
4 bytes

Delphi/64
8 bytes

Unsigned types
NativeUInt

Delphi/32
4 bytes

Delphi/64
8 bytes

Delphi 32 and 64-bit Pointer Types
Pointer types
PointerStringClass instanceClass reference
Interface
AnsiString
WideString
UnicodeString
Procedure pointerDynamic array
PAnsiChar
PWideChar
PChar
10Delphi/32

Delphi/64

4 bytes

8 bytes

The CPU – Programming model

11

Instructions and Addresses
••
•
•
•
•
•
•

64 bit address space
Limited by physical hardware
Same core Intel instruction set
New REX prefix for 64bit instructions
RIP-relative addressing
Jumps – relative +/- 2GB
16 byte stack alignments

Online resources• http://msdn.microsoft.com/en-us/magazine/cc300794.aspx• http://msdn.microsoft.com/en-us/library/7kcdt6fy.aspx

12

What does this mean for Delphi?

• Will a black hole open up in my programs?
• Will my 32-bit applications stop working?
• Will I have to change all of my code?
• Will I tear my hair out looking for obscuremigration issues?
• Will the world end?

13

Don’t panic!

• Quoting from
“The Hitchhiker’s Guide to the Galaxy”• Don’t Panic• Delphi/64’s got your code & back covered!

14Delphi 64-bit on Windows• Same Windows API– CreateWindowEx, PeekMessage, etc..

• Same Delphi RTL– SysUtils, Classes, Generics.Collections, etc…

• Same VCL– Forms, Graphics, Controls, Menus, etc..

15Pre-Defined Conditionals• Category Identifier• Compiler DCC
VER230• Platform MSWINDOWS
WIN32WIN64• CPUCPU386
CPUX86
CPUX64• Availability ASSEMBLERUNICODE16

dcc32
defineddefineddefineddefinednot defined
defineddefinednot defined
defineddefineddcc64
defineddefineddefinednot defined
definednot defined
not defined
defineddefineddefinedDelphi 64-bit on Windows – some gotcha’s• SizeOf(Pointer) <> SizeOf(Integer)– Integer<->Pointer casts will break in 64bit– SizeOf(THandle) = SizeOf(Pointer)– All Handles = SizeOf(Pointer) (HWND, HDC, etc..).

• All code in process must be 64bit– Must have 64bit versions of external non-Delphi libraries(DLLs)• One, and only one, calling convention– register,pascal,cdecl,stdcall ignored.

• safecall is still “special”• Old “pointer math” code may break– Works in 32 and 64bit: MyPtr := PByte(P) + 10;

17

Delphi 64-bit on Windows – some gotcha’s• Inline Assembly– Cannot mix asm blocks with Pascal code–––––

Only procedural level asm blocks supported
Stack must be 16-byte aligned at each call instruction
Define locals for temp storage
Do not modify the RSP stack pointerNew unified calling convention. First 4 parameters in registers,RCX, RDX, R8, R9 (or XMM0-XMM3)• Exception unwinding– No change for pure Delphi code. Exceptions function
identically.
– Inline Assembly can cause exception unwinding to fail if notproperly written.

18

Windows API gotcha’s• SetWindowLong / GetWindowLog should
be replaced by SetWindowLongPtr /GetWindowLongPtr for
GWLP_HINSTANCE, GWLP_WNDPROC,
etc… as they return pointers and handles.
– Pointers passed to SetWindowLongPtr should be typecasted to LONG_PTR and not to Integer / Longint.

• SetWindowLong mapped toSetWindowLongPtr in Windows.pas.
– Calls to our declaration of SetWindowLong are safe, as longas they are cast correctly.

19Windows API gotcha’s (con’t)• Use explicit casts to WPARAM andLPARAM where appropriate.
– Example: Passing pointers through SendMessage
SendMessage(hWnd, WM_SETTEXT, 0,
LPARAM(@MyCharArray));• Use LRESULT to cast message
results– Example: Message.Result := LRESULT(Self);• Message cracker records(TWMxxx) changed– Alignment changes and field-size changes

20What can I do today?
• Find all Integer<->Pointer casts, including
Integer<->instance casts.
– Check for Pointer size assumptions• Ensure external dependencies are also
64bit
– Image/bitmap libraries– Hardware interfaces libraries– ActiveX controls• Consider rewriting Assembler in purePascal– Better future portability (think ARM CPUs…)– Rely more on algorithmic performance rather than raw
assembly performance.

21Delphi 64-bit Demonstration

22

RAD Studio Client Vision
KeyboardLocation

Biometrics
Mouse

Touch

VoiceRESTCOMCloudSOAP.NET
Social23

Rich Client Application

InterBaseREST

DataSnap
COM

CloudSOAP.NET
Social24Server Methods

dbExpress

RAD Studio Server VisionInterBase

Q&A

25

Thank You 

David Intersimone “David I”
VP of Developer Relations and Chief Evangelist
davidi@embarcadero.com
http://blogs.embarcadero.com/davidi
Twitter: davidi99
Skype: davidi99
26