Migrating Delphi Code to Mobile from Desktop

From RAD Studio
Jump to: navigation, search

Go Up to Delphi Considerations for Multi-Device Applications


This topic describes how to migrate existing Delphi code to use the Delphi mobile compilers:

Update Array Types

Update all array declarations to be dynamic. Use either of the following:

var x: array of Integer;
x: TArray<Integer>;

There are cases when a structure (record) must be passed through to an external function, and the structure contains an array declared with a specific length. This is especially true for character arrays declared "in-place" within the structure. In this case, and only this case, is the following allowed:

type
   rec = record
    Flags: Integer;
    Chars: array[MAX_PATH] of Char;
  end;

In cases where an external function takes an array directly, use a dynamic array instead. For UTF8 character arrays:

Use a Function Call in a try-except Block to Prevent Uncaught Hardware Exceptions

With compilers for iOS devices, except blocks can catch a hardware exception only if the try block contains a method or function call. This is a difference related to the LLVM backend of the compiler, which cannot return if no method/function is called in the try block.

For example, this is how to structure a try-except block that can catch a hardware exception:

var
  P: ^Integer = nil;

procedure G1;
begin
  P^ := 42;
end;

begin
  try
    G1;
  except
    writeln('Catch:G1 - pass');
  end;
end.

Even if a block of source code looks like it contains a function call, that may not be the case if the function is inlined. By the time LLVM generates machine instructions, the inlining process has already occurred, and there is no longer a function call within the try block.

Use Atomic Instrinsics Instead of Assembly Language

The Delphi mobile compilers do not support a built-in assembler. If you need to atomically exchange, compare-and-exchange, increment, and decrement memory values, you can use the new atomic intrinsic functions.

Atomic operations are used to implement multi-threaded locking primitives and provide the primitives necessary for implementing so-called "lock-free" structures. The kinds of operations needed are implemented as standard functions or "intrinsic" functions.

In a multi-platform application, atomic intrinsics can be used inside {$IFDEF} depending on the target platform.

Atomic Intrinsic Functions

Following are the atomic intrinsic functions supported by the Delphi mobile compilers:

See Also