local compiler directives

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) français (fr)

Local compiler directives may be used more than once in a Pascal source code file.

Syntax

Data layout

Code generation

$R versus $Q

There is the completely artificial distinction between the meaning of "overflow" and "range check" errors as defined by Borland. They are basically exactly the same error, except that one is detected by checking the overflow flag of the CPU and the other by explicitly comparing ranges.

On 64 bit CPUs, integer arithmetic is performed using 64 bit integers because of Pascal's convention to evaluate expressions using the native signed integer type. As a result, overflows cannot occur there when performing 32 bit arithmetic and you will get a range error instead when assigning the result to a 32 bit variable. Ideally, there would be only a single switch that governs both range/overflow checking, but because of historical reasons (as explained above) there are two.

Platform-specific

For x86 processors only

  • {$asmMode} determines the syntax the assembler reader expects. Previously, this has been the {$i386…} directives.
  • {$MMX} enables optimizations for MMX processors.
  • {$safeFPUExceptions}, whether fwait instructions are inserted
  • {$saturation} (in conjunction with {$MMX}) enables saturation operations.
  • {$maxFPUregisters} determines the maximum number of floating-points registers to use.

Other

  • {$linkFramework} inserts a framework. This directive is only available on Darwin-based operating systems.

Data inclusion

Compile-time context

  • {$define} defines a symbol. In {$mode MacPas} the directive {$defineC} is considered, too.
  • {$include} or {$I} reads a file as source or includes certain compile-time/compiler information.
  • {$push} and {$pop} store and restore the compiler settings.
  • {$setC} sets a compile-time variable, if the current mode allows it.
  • {$undef} dismisses the definition of a previously defined symbol. In {$mode MacPas} the directive {$undefC} is recognized, too.

Conditional compilation

Conditional compilation can be achieved via the directives

  • {$if}
  • {$else}
  • {$elseIf}
  • {$endIf}
  • {$ifDef}
  • {$ifNDef}
  • {$ifOpt}

Additionally, in {$mode MacPas} the directives

  • {$ifC},
  • {$elseC},
  • {$elIfC}, and
  • {$endC}

are allowed, too.

Compile-time behavior

With {$wait}, the compiler waits till the user hits ↵ Enter, and then resumes compilation.

Self-defined messages can be triggered with the directives:

  • {$message}, and the shortcuts
    • {$stop}, which also aborts compilation
    • {$fatal}, which also aborts compilation
    • {$error} (in {$mode MacPas} the directive {$errorC} is accepted, too)
    • {$warning}
    • {$hint}
    • {$note}
  • {$info}

Emission of messages can be controlled via the directives:

  • {$warn} for specific warnings, or
  • all messages of one kind in one go:
    • {$warnings}
    • {$hints}
    • {$notes}

Ignored

Since FPC intends to be sort of compatible to some other compilers, some very common compiler directives stemming from the non-FPC-lands are recognized – not generating an illegal directive error – and ignored. Those are:

  • {$F} (far or near functions)
  • {$extendedSym}
  • {$externalSym}
  • {$hppEmit}
  • {$libExport}
  • {$noDefine}
  • {$region} and {$endRegion}
  • {$stringChecks} which in Delphi, this would control the generation of code that checks the sanity of string variables and arguments.

Historical

Following directives were recognized in earlier versions of FPC and are now illegal:

  • {$output_format} determined the output format of an object file.

See also

Directives, definitions and conditionals definitions
global compiler directives • local compiler directives

Conditional Compiler Options • Conditional compilation • Macros and Conditionals • Platform defines
$IF