Difference between revisions of "Inc and Dec"

From Lazarus wiki
Jump to navigationJump to search
(unify Inc and Dec)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Translate}}
+
{{LanguageBar}}
The [[Procedure|procedures]] <syntaxhighlight lang="pascal" enclose="none">inc</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">dec</syntaxhighlight> increment or decrement a given variable by default by one.
+
 
 +
The [[Procedure|procedures]] <syntaxhighlight lang="pascal" inline>inc</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>dec</syntaxhighlight> increment or decrement a given variable by default by one.
  
 
== usage ==
 
== usage ==
The first parameter specifies an ordinal value variable (e.g. an [[Integer|<syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>]] or enumeration type) and the second optional parameter may specify a different addend/subtrahend.
+
The first parameter specifies an ordinal value variable (e.&#8239;g. an [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>]] or enumeration type) and the second optional parameter may specify a different addend/subtrahend.
 
 
 
<syntaxhighlight lang="pascal" line highlight="13,18,22">
 
<syntaxhighlight lang="pascal" line highlight="13,18,22">
 
program incDecDemo(input, output, stderr);
 
program incDecDemo(input, output, stderr);
Line 33: Line 33:
  
 
== background ==
 
== background ==
In [[FPC]]'s [[System unit|system unit]] the {{Doc|package=RTL|unit=system|identifier=inc|text=<syntaxhighlight lang="pascal" enclose="none">inc</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=dec|text=<syntaxhighlight lang="pascal" enclose="none">dec</syntaxhighlight>}} procedures are compiler procedures.
+
In [[FPC]]’s [[System unit|system unit]] the {{Doc|package=RTL|unit=system|identifier=inc|text=<syntaxhighlight lang="pascal" inline>inc</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=dec|text=<syntaxhighlight lang="pascal" inline>dec</syntaxhighlight>}} procedures are compiler procedures.
They exist in order to optimize for certain architectures where faster <syntaxhighlight lang="asm" enclose="none">inc</syntaxhighlight> and <syntaxhighlight lang="asm" enclose="none">dec</syntaxhighlight> [[Assembly language|assembler]] instructions are available.
+
They exist in order to optimize for certain architectures where dedicated <syntaxhighlight lang="asm" inline>inc</syntaxhighlight> and <syntaxhighlight lang="asm" inline>dec</syntaxhighlight> [[Assembly language|assembler]] instructions are available.
  
 
== special behaviors ==
 
== special behaviors ==
If <syntaxhighlight lang="pascal" enclose="none">{$rangeChecks}</syntaxhighlight> are turned on, those procedures may generate a run-time error (RTE 201).
+
If <syntaxhighlight lang="pascal" inline>{$rangeChecks}</syntaxhighlight> are turned on, those procedures may generate a run-time error (RTE 201).
Also <syntaxhighlight lang="pascal" enclose="none">{$overflowChecks}</syntaxhighlight> may be generated (with a small difference in TP mode).
+
Also <syntaxhighlight lang="pascal" inline>{$overflowChecks}</syntaxhighlight> may be generated (with a small difference in [[Mode TP|TP mode]]).
 +
 
 +
If [[Pointer|<syntaxhighlight lang="pascal" inline>pointer</syntaxhighlight>]] arithmetics are allowed by the <syntaxhighlight lang="pascal" inline>{$pointerMath}</syntaxhighlight> compiler switch, <syntaxhighlight lang="pascal" inline>inc</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>dec</syntaxhighlight> work on pointers, too.
  
If [[Pointer|<syntaxhighlight lang="pascal" enclose="none">pointer</syntaxhighlight>]] arithmetics are allowed by the <syntaxhighlight lang="pascal" enclose="none">{$pointerMath}</syntaxhighlight> compiler switch, <syntaxhighlight lang="pascal" enclose="none">inc</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">dec</syntaxhighlight> work on pointers, too.
+
In the case of typed pointers, e.&#8239;g. a pointer to a [[Record|<syntaxhighlight lang="pascal" inline>record</syntaxhighlight>]], the target’s type size is considered automatically.
In the case of typed pointers, e.g. a pointer to a [[Record|<syntaxhighlight lang="pascal" enclose="none">record</syntaxhighlight>]], the target's type size is considered automatically.
+
For instance:
For instance
 
 
<syntaxhighlight lang="pascal" line highlight="9,10">
 
<syntaxhighlight lang="pascal" line highlight="9,10">
 
program pointerIncDemo(input, output, stderr);
 
program pointerIncDemo(input, output, stderr);
Line 60: Line 61:
 
; [pointerIncDemo.pas]
 
; [pointerIncDemo.pas]
 
; [8] begin
 
; [8] begin
leaq -8(%rsp),%rsp
+
leaq   -8(%rsp),%rsp
 
.Lc3:
 
.Lc3:
 
; Var p located in register rax
 
; Var p located in register rax
call FPC_INITIALIZEUNITS
+
call   FPC_INITIALIZEUNITS
movq $0,%rax
+
movq   $0,%rax
 
; [9] inc(p);
 
; [9] inc(p);
addq $8,%rax
+
addq   $8,%rax
 
; [10] inc(p, 3);
 
; [10] inc(p, 3);
addq $24,%rax
+
addq   $24,%rax
 
; [11] end.
 
; [11] end.
call FPC_DO_EXIT
+
call   FPC_DO_EXIT
leaq 8(%rsp),%rsp
+
leaq   8(%rsp),%rsp
 
ret
 
ret
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== see also ==
 
== see also ==
* {{Doc|package=RTL|unit=system|identifier=succ|text=<syntaxhighlight lang="pascal" enclose="none">succ</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=succ|text=<syntaxhighlight lang="pascal" enclose="none">pred</syntaxhighlight>}}
+
* {{Doc|package=RTL|unit=system|identifier=succ|text=<syntaxhighlight lang="pascal" inline>succ</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=succ|text=<syntaxhighlight lang="pascal" inline>pred</syntaxhighlight>}}
* [[For|<syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-loops]]
+
* [[For|<syntaxhighlight lang="pascal" inline>for</syntaxhighlight>-loops]]
 +
 
 +
[[Category:Pascal]]

Latest revision as of 14:37, 10 August 2022

English (en)

The procedures inc and dec increment or decrement a given variable by default by one.

usage

The first parameter specifies an ordinal value variable (e. g. an integer or enumeration type) and the second optional parameter may specify a different addend/subtrahend.

 1program incDecDemo(input, output, stderr);
 2
 3type
 4	primaryColor = (red, green, blue);
 5
 6var
 7	phase: primaryColor;
 8	x: longint;
 9
10begin
11	// enumeration type
12	phase := red;
13	inc(phase); // phase becomes green
14	writeLn(phase);
15	
16	// integer
17	x := 1;
18	inc(x, -1); // x becomes zero
19	writeLn(x);
20	
21	x := 1;
22	dec(x); // same as above: x becomes zero
23	writeLn(x);
24end.

background

In FPC’s system unit the inc and dec procedures are compiler procedures. They exist in order to optimize for certain architectures where dedicated inc and dec assembler instructions are available.

special behaviors

If {$rangeChecks} are turned on, those procedures may generate a run-time error (RTE 201). Also {$overflowChecks} may be generated (with a small difference in TP mode).

If pointer arithmetics are allowed by the {$pointerMath} compiler switch, inc and dec work on pointers, too.

In the case of typed pointers, e. g. a pointer to a record, the target’s type size is considered automatically. For instance:

 1program pointerIncDemo(input, output, stderr);
 2
 3{$pointerMath on}
 4
 5var
 6	p: PQWord;
 7
 8begin
 9	inc(p);
10	inc(p, 3);
11end.

will generate (excerpt):

13; [pointerIncDemo.pas]
14; [8] begin
15	leaq    -8(%rsp),%rsp
16.Lc3:
17; Var p located in register rax
18	call    FPC_INITIALIZEUNITS
19	movq    $0,%rax
20; [9] inc(p);
21	addq    $8,%rax
22; [10] inc(p, 3);
23	addq    $24,%rax
24; [11] end.
25	call    FPC_DO_EXIT
26	leaq    8(%rsp),%rsp
27	ret

see also