v4.80 bèta:parameter passing problem + MemManager lib error

Beta Testing discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

v4.80 bèta:parameter passing problem + MemManager lib error

#1 Post by Dany » 28 Apr 2011 12:42

Hi, while using the new memory manager library I discovered it is not defragmenting the heap well. I then tried it in my own MemManager and to my surprise the same error occurred.

The reason is that elements of array of any type higher than byte parameters are not well passed through to functions:

extract from my MemManager code:
Function:

Code: Select all

function MM_CheckBlocks(P1: ^byte; S1: word; P2: ^byte; S2: word): byte;
begin
  Result := 0;      // default: no adjacent blocks
  if P1 = 0  then exit;
  if P2 = 0  then exit;
  if P1 = P2 then exit;
     
  if P1 + S1 = P2 then Result := 1  // block P1 is adjacent beneath block P2
  else
  if P2 + S2 = P1 then Result := 2; // block P2 is adjacent beneath block P1
end;
Call to that function:

Code: Select all

procedure FreeMem(var P: ^byte; ActualSize: word);
....
Tmp := MM_CheckBlocks(P, ActualSize, MM_FreeMemTable[I].Pointer, MM_FreeMemTable[I].Size);
...
I saw that only the low byte of "MM_FreeMemTable.Pointer" was to be seen in "P2" in the routine "MM_CheckBlocks".

The "MM_FreeMemTable" was declared as:

Code: Select all

Type  TFreeMemBlock = record
        Pointer: ^byte;
        Size   : Word;
      end; 
var   MM_FreeMemTable: array[HEAP_NR_FREE_BLOCKS] of TFreeMemBlock;
The problem could be solved by changing (adding a typecast) the call to:

Code: Select all

procedure FreeMem(var P: ^byte; ActualSize: word);
....
Tmp := MM_CheckBlocks(P, ActualSize, ^byte(MM_FreeMemTable[I].Pointer), MM_FreeMemTable[I].Size);
...
but this typecast should not be necessary at all! Both the procedure function paremeter and the actual parameter value are "^byte" types!

I did not test if the problem only occurs with record member arguments...

I think this is a major issue. :? :?
Last edited by Dany on 28 Apr 2011 17:41, edited 2 times in total.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: v4.80 problems with pointer parameters? MemManager lib e

#2 Post by janni » 28 Apr 2011 14:00

Dany wrote:I saw that only the low byte of "MM_FreeMemTable.Pointer" was to be seen in "P2" in the routine "MM_CheckBlocks".
It's the same with the other parameter (size) :( . Looks like compiler 'forgets' the record fields' types when they're indicated with variable index (constant index does not produce such effect). Interestingly, everything works fine when call is made from main, not from a procedure.

P.S. This behaviour is not limited to record fields - passing elements of array of any type higher than byte leads to the same problem.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: v4.80 problems with pointer parameters? MemManager lib e

#3 Post by Dany » 28 Apr 2011 17:33

janni wrote:
Dany wrote:I saw that only the low byte of "MM_FreeMemTable.Pointer" was to be seen in "P2" in the routine "MM_CheckBlocks".
It's the same with the other parameter (size) :( . Looks like compiler 'forgets' the record fields' types when they're indicated with variable index (constant index does not produce such effect). Interestingly, everything works fine when call is made from main, not from a procedure.

P.S. This behaviour is not limited to record fields - passing elements of array of any type higher than byte leads to the same problem.
Thanks Janni. O, this is a huge one... Sigh :( :(
I have adapted the title of the thread.
I am sure it was OK in older mP versions, I tested my MemManager thouroughly. :?
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: v4.80 bèta:parameter passing problem + MemManager lib er

#4 Post by janni » 29 Apr 2011 02:19

After some detailed checking it seems that the problem comes to function result type, or type of a variable the result is being assigned to. Parameters will not be cut down, if the function result is typecast to their size,like

Code: Select all

Tmp := word(MM_CheckBlocks(P, ActualSize, MM_FreeMemTable[I].Pointer, MM_FreeMemTable[I].Size));
Seems like simplest workaround before this bug is fixed.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: v4.80 bèta:parameter passing problem + MemManager lib er

#5 Post by Dany » 29 Apr 2011 11:56

janni wrote:After some detailed checking it seems that the problem comes to function result type, or type of a variable the result is being assigned to. Parameters will not be cut down, if the function result is typecast to their size,like

Code: Select all

Tmp := word(MM_CheckBlocks(P, ActualSize, MM_FreeMemTable[I].Pointer, MM_FreeMemTable[I].Size));
Seems like simplest workaround before this bug is fixed.
Thanks Janni, it is indeed a solution, as is the typecasting of the parameters themselves.

I am curious now:
- the variable "Tmp" is a byte variable, does the typecasting of the function's result to word does not give problems? This typecast seems to be totally illogical (but it works! :shock: )
- why is the typecasting of the function's result a solution? I do not understand the mechanism behind that.
The solution with the typecasting of the parameter values I can also not uderstand (why it is needed), but this seems to be a bit more logical.
Last edited by Dany on 12 Aug 2011 20:49, edited 1 time in total.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: v4.80 bèta:parameter passing problem + MemManager lib er

#6 Post by janni » 29 Apr 2011 16:21

Dany wrote:- the variable "Tmp" is a byte variable, does the typecasting of the function's result to word does not give problems?
No, just one additional assembly instruction will be added that does nothing. Actually, if Tmp were of word type, the error would not show up, either.
- why is the typecasting of the function's result a solution? I do not understand the mechanism behind that.
It looks like side effect of expressions evaluation rules - in an assigment of function result, some function parameters (only array elements indexed by a variable) are treated like expression arguments.
I am also sure this bug was not present in some of the older versions of mP PRO.
I'm affraid it was there for some time already, just weren't noticed as it's quite specific (for example, none of library functions seem to be affected by it - at least until Memory Manager lib was introduced).
I did check through older versions of the command-line compiler and the error was already there - at least from v. 3.80. Still, it may be even more specific than I think and some small code changes may mask it.

Anyway, discovered error is a fixed error :) . Final release of v 4.80 will surely be free of it. Good catch, Dany.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: v4.80 bèta:parameter passing problem + MemManager lib er

#7 Post by Dany » 30 Apr 2011 09:19

janni wrote:
Dany wrote:- the variable "Tmp" is a byte variable, does the typecasting of the function's result to word does not give problems?
No, just one additional assembly instruction will be added that does nothing. Actually, if Tmp were of word type, the error would not show up, either.
- why is the typecasting of the function's result a solution? I do not understand the mechanism behind that.
It looks like side effect of expressions evaluation rules - in an assigment of function result, some function parameters (only array elements indexed by a variable) are treated like expression arguments.
I am also sure this bug was not present in some of the older versions of mP PRO.
I'm affraid it was there for some time already, just weren't noticed as it's quite specific (for example, none of library functions seem to be affected by it - at least until Memory Manager lib was introduced).
I did check through older versions of the command-line compiler and the error was already there - at least from v. 3.80. Still, it may be even more specific than I think and some small code changes may mask it.

Anyway, discovered error is a fixed error :) . Final release of v 4.80 will surely be free of it.
Thanks Janni. :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

User avatar
srdjan
mikroElektronika team
Posts: 1552
Joined: 28 Dec 2005 12:47
Location: Serbia

Re: v4.80 bèta:parameter passing problem + MemManager lib er

#8 Post by srdjan » 06 May 2011 16:16

Hi,
Fixed :wink:
Thanks.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: v4.80 bèta:parameter passing problem + MemManager lib er

#9 Post by Dany » 06 May 2011 20:19

srdjan wrote:Hi,
Fixed :wink:
Thanks.
Thanks! Keep up the good work! :D :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroPascal PRO for PIC Beta Testing”