### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Changing a value in an inline routine  (Read 977 times)

#### Okoba

• Sr. Member
• Posts: 363
##### Changing a value in an inline routine
« on: September 17, 2022, 05:34:49 pm »
Hello,

I have a case that I can not explain except it is a bug, but it should not happen at all as it seems an obvious one and I never faced anything like this even though I am using this style of code a lot. So I am unsure if this is a bug or I am missing something?
As you can see Test1Inner must not change the C, but because both are inline it does!

Code: Pascal  [Select][+][-]
1. program project1;
2.
3.   procedure Test1Inner(V: Integer); inline;
4.   begin
5.     V := 0;
6.   end;
7.
8.   procedure Test1; inline;
9.   var
10.     V: Integer;
11.   begin
12.     V := 1;
13.     WriteLn('Before: ', V); //1
14.     Test1Inner(V);
15.     WriteLn('After: ', V); //0
16.   end;
17.
18.   procedure Test2Inner(V: Integer);
19.   begin
20.     V := 0;
21.   end;
22.
23.   procedure Test2; inline;
24.   var
25.     V: Integer;
26.   begin
27.     V := 1;
28.     WriteLn('Before: ', V); //1
29.     Test2Inner(V);
30.     WriteLn('After: ', V); //1
31.   end;
32.
33.   procedure Test3Inner(V: Integer); inline;
34.   begin
35.     V := 0;
36.   end;
37.
38.   procedure Test3;
39.   var
40.     V: Integer;
41.   begin
42.     V := 1;
43.     WriteLn('Before: ', V); //1
44.     Test3Inner(V);
45.     WriteLn('After: ', V); //1
46.   end;
47.
48. begin
49.   Test1;
50.   Test2;
51.   Test3;
53. end.

#### winni

• Hero Member
• Posts: 3188
##### Re: Changing a value in an inline routine
« Reply #1 on: September 17, 2022, 05:51:13 pm »
Hi!

Here is the error, change it to

Code: Pascal  [Select][+][-]
1. procedure Test2Inner(var V: Integer);

The same for procedure Test3Inner

Winni

#### Martin_fr

• Hero Member
• Posts: 8620
• Debugger - SynEdit - and more
##### Re: Changing a value in an inline routine
« Reply #2 on: September 17, 2022, 05:59:47 pm »
@winni: He does NOT want to change it.

@Okoba

I would say it's a bug is FPC.
And it is still present in FPC 3.3.1 from earlier this month.

#### PascalDragon

• Hero Member
• Posts: 4877
• Compiler Developer
##### Re: Changing a value in an inline routine
« Reply #3 on: September 17, 2022, 06:05:02 pm »
I have a case that I can not explain except it is a bug, but it should not happen at all as it seems an obvious one and I never faced anything like this even though I am using this style of code a lot. So I am unsure if this is a bug or I am missing something?

That is strange... from the assembly code it looks like Test1Inner should work like Test3Inner and in both cases V from the outer procedure should stay 1, but yet it doesn't in the first case... Please report as a bug.

#### Okoba

• Sr. Member
• Posts: 363
##### Re: Changing a value in an inline routine
« Reply #4 on: September 17, 2022, 06:19:40 pm »
Exactly. Weird enough I didn't see this before, it does happen only in this case for me as I use this type of code a lot, meaning inline routines change the inner value.
I would appreciate if you could look into it as I feel unsafe even looking at it
Reported, 39908

#### Martin_fr

• Hero Member
• Posts: 8620
• Debugger - SynEdit - and more
##### Re: Changing a value in an inline routine
« Reply #5 on: September 17, 2022, 06:20:03 pm »
That is strange... from the assembly code it looks like Test1Inner should work like Test3Inner and in both cases V from the outer procedure should stay 1, but yet it doesn't in the first case...

TestInner1 looks good, even Test1. But the inlined form of Test1 (in PASCALMAIN)
Code: Pascal  [Select][+][-]
1. # [49] Test1;
2.

That does indeed overwrite the value

"V1" is at
movl   \$1,-4(%rbp)

But after the call to   fpc_write_text_sint
"V1" from TestInner1
movl   \$0,-4(%rbp)

#### PascalDragon

• Hero Member
• Posts: 4877
• Compiler Developer
##### Re: Changing a value in an inline routine
« Reply #6 on: September 18, 2022, 04:14:47 pm »
That is strange... from the assembly code it looks like Test1Inner should work like Test3Inner and in both cases V from the outer procedure should stay 1, but yet it doesn't in the first case...

TestInner1 looks good, even Test1. But the inlined form of Test1 (in PASCALMAIN)

Ah, I didn't notice that Test1 is inlined as well! Yes, there is the difference, though the why is the next question of course... But since Okoba already reported it...

#### BobDog

• Sr. Member
• Posts: 366
##### Re: Changing a value in an inline routine
« Reply #7 on: September 18, 2022, 05:55:02 pm »

I get
Code: Pascal  [Select][+][-]
1. Before: 1
2. After: 1
3. Before: 1
4. After: 1
5. Before: 1
6. After: 1
7.
But if I change to
{\$mode objfpc} (or delphi)
I get
Code: Pascal  [Select][+][-]
1. Before: 1
2. After: 0
3. Before: 1
4. After: 1
5. Before: 1
6. After: 1
7.
(Geany ide, no default mode objfpc).
So mode FPC is OK.
(just an observation)

#### Okoba

• Sr. Member
• Posts: 363
##### Re: Changing a value in an inline routine
« Reply #8 on: September 19, 2022, 06:41:02 am »
I tested with all three fpc, objfpc and delphi modes and problem persists.
What is your version of FPC?

#### Zath

• Sr. Member
• Posts: 385
##### Re: Changing a value in an inline routine
« Reply #9 on: September 19, 2022, 10:04:36 am »

#### Okoba

• Sr. Member
• Posts: 363
##### Re: Changing a value in an inline routine
« Reply #10 on: September 19, 2022, 11:15:47 am »
Yes the problem is inlining.

#### Okoba

• Sr. Member
• Posts: 363
##### Re: Changing a value in an inline routine
« Reply #11 on: October 23, 2022, 03:26:53 pm »
Today I was struggling with a weird bug and after hours of debugging, checking assembly code and playing with inline, I found out that this bug is again the cause.
For now, I fixed it with a temporary var in the function, but this bug seems more dangerous than it looks.
I wished I had enough knowledge of the compiler to fix this, but for now I hope one of the members can fix this nasty one.

#### PascalDragon

• Hero Member
• Posts: 4877
• Compiler Developer
##### Re: Changing a value in an inline routine
« Reply #12 on: October 23, 2022, 04:55:56 pm »
I wished I had enough knowledge of the compiler to fix this, but for now I hope one of the members can fix this nasty one.

To be fair you'd probably need in depth knowledge for this.

#### Okoba

• Sr. Member
• Posts: 363
##### Re: Changing a value in an inline routine
« Reply #13 on: October 23, 2022, 05:18:09 pm »
I guessed but this bug frustrated me, as it hides itself and some times in some cases, in a function it happens.