Difference between revisions of "local compiler directives"

From Free Pascal wiki
(Code generation: mention $checkPointer only works in conjunction with heaptrc)
(resolve some redlinks, remove excessive vertical space)
Line 2: Line 2:
  
 
Local [[Compiler directive|compiler directives]] may be used more than once in a Pascal [[Source code|source code]] file.
 
Local [[Compiler directive|compiler directives]] may be used more than once in a Pascal [[Source code|source code]] file.
 
  
 
== Syntax ==
 
== Syntax ==
 
 
* <syntaxhighlight lang="pascal" inline>{$cOperators}</syntaxhighlight> enables usage of [[Operator|operators]] similar to conventions of in the [[Pascal for C users|C]] language.
 
* <syntaxhighlight lang="pascal" inline>{$cOperators}</syntaxhighlight> enables usage of [[Operator|operators]] similar to conventions of in the [[Pascal for C users|C]] language.
 
* [[sGoto|<syntaxhighlight lang="pascal" inline>{$goto}</syntaxhighlight>]] enables [[Goto|<syntaxhighlight lang="pascal" inline>goto</syntaxhighlight>]] and [[Label|<syntaxhighlight lang="pascal" inline>label</syntaxhighlight>]].
 
* [[sGoto|<syntaxhighlight lang="pascal" inline>{$goto}</syntaxhighlight>]] enables [[Goto|<syntaxhighlight lang="pascal" inline>goto</syntaxhighlight>]] and [[Label|<syntaxhighlight lang="pascal" inline>label</syntaxhighlight>]].
* <syntaxhighlight lang="pascal" inline>{$inline}</syntaxhighlight> allows the [[inline|<syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>]] [[Modifiers|modifier]].
+
* <syntaxhighlight lang="pascal" inline>{$inline}</syntaxhighlight> allows the [[Inline|<syntaxhighlight lang="pascal" inline>inline</syntaxhighlight>]] [[modifier]].
 
* [[$H|<syntaxhighlight lang="pascal" inline>{$LongStrings}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$H}</syntaxhighlight>]] determines the data type referenced by the reserved word [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]].
 
* [[$H|<syntaxhighlight lang="pascal" inline>{$LongStrings}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$H}</syntaxhighlight>]] determines the data type referenced by the reserved word [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]].
 
* <syntaxhighlight lang="pascal" inline>{$macro}</syntaxhighlight> enables usage of [[Macros|macros]].
 
* <syntaxhighlight lang="pascal" inline>{$macro}</syntaxhighlight> enables usage of [[Macros|macros]].
Line 16: Line 14:
 
* [[$varStringChecks|<syntaxhighlight lang="pascal" inline>{$varStringChecks}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>{$V}</syntaxhighlight>]] enables strict checking of assignment compatibility of string variables.
 
* [[$varStringChecks|<syntaxhighlight lang="pascal" inline>{$varStringChecks}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>{$V}</syntaxhighlight>]] enables strict checking of assignment compatibility of string variables.
 
* [[$writableConst|<syntaxhighlight lang="pascal" inline>{$writableConst}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$J}</syntaxhighlight>]] enables assignment of values to typed [[Constant|constants]] during [[runtime|run-time]].
 
* [[$writableConst|<syntaxhighlight lang="pascal" inline>{$writableConst}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$J}</syntaxhighlight>]] enables assignment of values to typed [[Constant|constants]] during [[runtime|run-time]].
 
  
 
== Data layout ==
 
== Data layout ==
 
 
* [[$A|<syntaxhighlight lang="pascal" inline>{$align}</syntaxhighlight>]] and [[$A|<syntaxhighlight lang="pascal" inline>{$A}</syntaxhighlight>]] determine the alignment of data in records
 
* [[$A|<syntaxhighlight lang="pascal" inline>{$align}</syntaxhighlight>]] and [[$A|<syntaxhighlight lang="pascal" inline>{$A}</syntaxhighlight>]] determine the alignment of data in records
 
* [[$Bitpacking|<syntaxhighlight lang="pascal" inline>{$bitpacking}</syntaxhighlight>]] determines, whether <syntaxhighlight lang="pascal" inline>packed</syntaxhighlight> is interpreted as <syntaxhighlight lang="pascal" inline>bitpacked</syntaxhighlight>.
 
* [[$Bitpacking|<syntaxhighlight lang="pascal" inline>{$bitpacking}</syntaxhighlight>]] determines, whether <syntaxhighlight lang="pascal" inline>packed</syntaxhighlight> is interpreted as <syntaxhighlight lang="pascal" inline>bitpacked</syntaxhighlight>.
Line 28: Line 24:
 
* [[$packRecords|<syntaxhighlight lang="pascal" inline>{$packRecords}</syntaxhighlight>]] determines alignment of [[Record|records]] in memory.
 
* [[$packRecords|<syntaxhighlight lang="pascal" inline>{$packRecords}</syntaxhighlight>]] determines alignment of [[Record|records]] in memory.
 
* [[$packSet|<syntaxhighlight lang="pascal" inline>{$packSet}</syntaxhighlight>]] determines packing of [[Set|sets]].
 
* [[$packSet|<syntaxhighlight lang="pascal" inline>{$packSet}</syntaxhighlight>]] determines packing of [[Set|sets]].
 
  
 
== Code generation ==
 
== Code generation ==
 
 
* [[$boolEval|<syntaxhighlight lang="pascal" inline>{$boolEval}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>{$B}</syntaxhighlight>]] controls short-cut evaluation of Boolean expressions
 
* [[$boolEval|<syntaxhighlight lang="pascal" inline>{$boolEval}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>{$B}</syntaxhighlight>]] controls short-cut evaluation of Boolean expressions
 
* [[$Assertions|<syntaxhighlight lang="pascal" inline>{$assertions}</syntaxhighlight>]] or <syntaxhighlight lang="pascal" inline>{$C}</syntaxhighlight> control, whether [[assert|<syntaxhighlight lang="pascal" inline>assert</syntaxhighlight> statements]] are compiled into the [[Executable program|executable program]]
 
* [[$Assertions|<syntaxhighlight lang="pascal" inline>{$assertions}</syntaxhighlight>]] or <syntaxhighlight lang="pascal" inline>{$C}</syntaxhighlight> control, whether [[assert|<syntaxhighlight lang="pascal" inline>assert</syntaxhighlight> statements]] are compiled into the [[Executable program|executable program]]
Line 49: Line 43:
  
 
== Platform-specific ==
 
== Platform-specific ==
 
 
 
=== For x86 processors only ===
 
=== For x86 processors only ===
 
 
* [[$asmMode|<syntaxhighlight lang="pascal" inline>{$asmMode}</syntaxhighlight>]] determines the syntax the assembler reader expects. Previously, this has been the <syntaxhighlight lang="pascal" inline>{$i386…}</syntaxhighlight> directives.
 
* [[$asmMode|<syntaxhighlight lang="pascal" inline>{$asmMode}</syntaxhighlight>]] determines the syntax the assembler reader expects. Previously, this has been the <syntaxhighlight lang="pascal" inline>{$i386…}</syntaxhighlight> directives.
 
* [[$MMX|<syntaxhighlight lang="pascal" inline>{$MMX}</syntaxhighlight>]] enables optimizations for MMX processors.
 
* [[$MMX|<syntaxhighlight lang="pascal" inline>{$MMX}</syntaxhighlight>]] enables optimizations for MMX processors.
Line 58: Line 49:
 
* [[$saturation|<syntaxhighlight lang="pascal" inline>{$saturation}</syntaxhighlight>]] (in conjunction with <syntaxhighlight lang="pascal" inline>{$MMX}</syntaxhighlight>) enables saturation operations.
 
* [[$saturation|<syntaxhighlight lang="pascal" inline>{$saturation}</syntaxhighlight>]] (in conjunction with <syntaxhighlight lang="pascal" inline>{$MMX}</syntaxhighlight>) enables saturation operations.
 
* [[$maxFPUregisters|<syntaxhighlight lang="pascal" inline>{$maxFPUregisters}</syntaxhighlight>]] determines the maximum number of floating-points registers to use.
 
* [[$maxFPUregisters|<syntaxhighlight lang="pascal" inline>{$maxFPUregisters}</syntaxhighlight>]] determines the maximum number of floating-points registers to use.
 
  
 
=== Other ===
 
=== Other ===
 
 
* [[$linkFramework|<syntaxhighlight lang="pascal" inline>{$linkFramework}</syntaxhighlight>]] inserts a framework. This directive is only available on Darwin-based operating systems.
 
* [[$linkFramework|<syntaxhighlight lang="pascal" inline>{$linkFramework}</syntaxhighlight>]] inserts a framework. This directive is only available on Darwin-based operating systems.
 
  
 
== Data inclusion ==
 
== Data inclusion ==
 
 
* [[$link|<syntaxhighlight lang="pascal" inline>{$link}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$L}</syntaxhighlight>]] inserts an object file during linking.
 
* [[$link|<syntaxhighlight lang="pascal" inline>{$link}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$L}</syntaxhighlight>]] inserts an object file during linking.
 
* [[$linkLib|<syntaxhighlight lang="pascal" inline>{$linkLib}</syntaxhighlight>]] inserts a library during linking.
 
* [[$linkLib|<syntaxhighlight lang="pascal" inline>{$linkLib}</syntaxhighlight>]] inserts a library during linking.
 
* [[$typeInfo|<syntaxhighlight lang="pascal" inline>{$typeInfo}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$M}</syntaxhighlight>]] creates [[Runtime Type Information (RTTI)|run-time type information]].
 
* [[$typeInfo|<syntaxhighlight lang="pascal" inline>{$typeInfo}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$M}</syntaxhighlight>]] creates [[Runtime Type Information (RTTI)|run-time type information]].
 
* [[$resource|<syntaxhighlight lang="pascal" inline>{$resource}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$R}</syntaxhighlight>]] inserts a resource file.
 
* [[$resource|<syntaxhighlight lang="pascal" inline>{$resource}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$R}</syntaxhighlight>]] inserts a resource file.
 
  
 
== Compile-time context ==
 
== Compile-time context ==
 
 
* [[$define|<syntaxhighlight lang="pascal" inline>{$define}</syntaxhighlight>]] defines a symbol. In <syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight> the directive <syntaxhighlight lang="pascal" inline>{$defineC}</syntaxhighlight> is considered, too.
 
* [[$define|<syntaxhighlight lang="pascal" inline>{$define}</syntaxhighlight>]] defines a symbol. In <syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight> the directive <syntaxhighlight lang="pascal" inline>{$defineC}</syntaxhighlight> is considered, too.
 
* [[$include|<syntaxhighlight lang="pascal" inline>{$include}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$I}</syntaxhighlight>]] reads a file as source or includes certain compile-time/compiler information.
 
* [[$include|<syntaxhighlight lang="pascal" inline>{$include}</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>{$I}</syntaxhighlight>]] reads a file as source or includes certain compile-time/compiler information.
Line 80: Line 65:
 
* <syntaxhighlight lang="pascal" inline>{$setC}</syntaxhighlight> sets a compile-time variable, if the current mode allows it.
 
* <syntaxhighlight lang="pascal" inline>{$setC}</syntaxhighlight> sets a compile-time variable, if the current mode allows it.
 
* <syntaxhighlight lang="pascal" inline>{$undef}</syntaxhighlight> dismisses the definition of a previously defined symbol. In <syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight> the directive <syntaxhighlight lang="pascal" inline>{$undefC}</syntaxhighlight> is recognized, too.
 
* <syntaxhighlight lang="pascal" inline>{$undef}</syntaxhighlight> dismisses the definition of a previously defined symbol. In <syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight> the directive <syntaxhighlight lang="pascal" inline>{$undefC}</syntaxhighlight> is recognized, too.
 
  
 
== Conditional compilation ==
 
== Conditional compilation ==
 
 
[[Conditional compilation]] can be achieved via the directives
 
[[Conditional compilation]] can be achieved via the directives
 
 
* <syntaxhighlight lang="pascal" inline>{$if}</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>{$if}</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>{$else}</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>{$else}</syntaxhighlight>
Line 95: Line 77:
  
 
Additionally, in <syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight> the directives
 
Additionally, in <syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight> the directives
 
 
* <syntaxhighlight lang="pascal" inline>{$ifC}</syntaxhighlight>,
 
* <syntaxhighlight lang="pascal" inline>{$ifC}</syntaxhighlight>,
 
* <syntaxhighlight lang="pascal" inline>{$elseC}</syntaxhighlight>,
 
* <syntaxhighlight lang="pascal" inline>{$elseC}</syntaxhighlight>,
 
* <syntaxhighlight lang="pascal" inline>{$elIfC}</syntaxhighlight>, and
 
* <syntaxhighlight lang="pascal" inline>{$elIfC}</syntaxhighlight>, and
 
* <syntaxhighlight lang="pascal" inline>{$endC}</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>{$endC}</syntaxhighlight>
 
 
are allowed, too.
 
are allowed, too.
 
  
 
== Compile-time behavior ==
 
== Compile-time behavior ==
 
 
With <syntaxhighlight lang="pascal" inline>{$wait}</syntaxhighlight>, the compiler waits till the user hits ↵ Enter, and then resumes compilation.
 
With <syntaxhighlight lang="pascal" inline>{$wait}</syntaxhighlight>, the compiler waits till the user hits ↵ Enter, and then resumes compilation.
  
 
Self-defined messages can be triggered with the directives:
 
Self-defined messages can be triggered with the directives:
 
 
* [[$message|<syntaxhighlight lang="pascal" inline>{$message}</syntaxhighlight>]], and the shortcuts
 
* [[$message|<syntaxhighlight lang="pascal" inline>{$message}</syntaxhighlight>]], and the shortcuts
 
** <syntaxhighlight lang="pascal" inline>{$stop}</syntaxhighlight>, which also aborts compilation
 
** <syntaxhighlight lang="pascal" inline>{$stop}</syntaxhighlight>, which also aborts compilation
Line 120: Line 97:
  
 
Emission of messages can be controlled via the directives:
 
Emission of messages can be controlled via the directives:
 
 
* [[$warn|<syntaxhighlight lang="pascal" inline>{$warn}</syntaxhighlight>]] for specific warnings, or
 
* [[$warn|<syntaxhighlight lang="pascal" inline>{$warn}</syntaxhighlight>]] for specific warnings, or
 
* all messages of one kind in one go:
 
* all messages of one kind in one go:
Line 126: Line 102:
 
** <syntaxhighlight lang="pascal" inline>{$hints}</syntaxhighlight>
 
** <syntaxhighlight lang="pascal" inline>{$hints}</syntaxhighlight>
 
** <syntaxhighlight lang="pascal" inline>{$notes}</syntaxhighlight>
 
** <syntaxhighlight lang="pascal" inline>{$notes}</syntaxhighlight>
 
  
 
== Ignored ==
 
== 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.
 
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:
 
Those are:
 
 
* <syntaxhighlight lang="pascal" inline>{$F}</syntaxhighlight> (far or near functions)
 
* <syntaxhighlight lang="pascal" inline>{$F}</syntaxhighlight> (far or near functions)
 
* <syntaxhighlight lang="pascal" inline>{$extendedSym}</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>{$extendedSym}</syntaxhighlight>
Line 141: Line 114:
 
* <syntaxhighlight lang="pascal" inline>{$region}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>{$endRegion}</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>{$region}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>{$endRegion}</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>{$stringChecks}</syntaxhighlight> which in Delphi, this would control the generation of code that checks the sanity of string variables and arguments.
 
* <syntaxhighlight lang="pascal" inline>{$stringChecks}</syntaxhighlight> which in Delphi, this would control the generation of code that checks the sanity of string variables and arguments.
 
  
 
== Historical ==
 
== Historical ==
 
 
Following directives were recognized in earlier versions of FPC and are now illegal:
 
Following directives were recognized in earlier versions of FPC and are now illegal:
 
 
* <syntaxhighlight lang="pascal" inline>{$output_format}</syntaxhighlight> determined the output format of an object file.
 
* <syntaxhighlight lang="pascal" inline>{$output_format}</syntaxhighlight> determined the output format of an object file.
 
  
 
== See also ==
 
== See also ==
 
 
* [[Pascal basics]]
 
* [[Pascal basics]]
 
* [https://www.freepascal.org/docs-html/prog/progse2.html § “Local directives” in the ''Free Pascal programmer’s guide'']
 
* [https://www.freepascal.org/docs-html/prog/progse2.html § “Local directives” in the ''Free Pascal programmer’s guide'']
  
 
{{Directives, Defines and Conditionals}}
 
{{Directives, Defines and Conditionals}}

Revision as of 11:29, 18 August 2021

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

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