Question about constant pointer typecasts.

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:

Question about constant pointer typecasts.

#1 Post by Dany » 24 Jun 2012 12:06

Hi,

why does the expression

Code: Select all

Ptr := ^const TStateTransition(Machine.TransitionTable);
give an error "syntac error: expected "pointer to record (?T57)" but "pointer to array()" found,
while the expression

Code: Select all

Ptr := ^const TStateTransition(^const byte(Machine.TransitionTable));
compiles without error?

Apparently 2 subsequent type castings are needed here?

The types used are:

Code: Select all

type
     TGetEventProc = function: byte;
     
     TActionProc = procedure(Id, From, Towards, Event: byte);
     
     TStateTransition =
     record
       Fromstate, ToState, Event: byte;
       Action: ^TActionProc;
     end;
     
     TStateMachineTable = array[1] of TStateTransition; // dummy size
     
     TStateMachine =
     record
       Ident           : byte;
       TransitionTable : ^const TStateMachineTable;
       NrTransitions   : byte;
       GetEventProc    : ^TGetEventProc;
       Running         : boolean;  // running or stopped
       CurrentState    : byte;     // the current state of the state machine
       CurrentEvent    : byte;     // the current event to react upon
       NextState       : byte;     // the future state of the state machine
     end;
The usage of the above types in the expression:

Code: Select all

procedure StateMachine_Step(var Machine: TStateMachine);
var Index      : byte;
    Routine    : ^TActionProc;
    Ptr         : ^const TStateTransition;
    Found      : boolean;
begin
  ....      
      Ptr := ^const TStateTransition(^const byte(Machine.TransitionTable)); // point to first entry in the State Transition table  
  ...
end;
Thanks in advance!
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)

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: Question about constant pointer typecasts.

#2 Post by JimKueneman » 24 Jun 2012 16:00

I want to know for the dsPIC compiler as well. What appears two pointer types are compatible. I am doing this:

TBase = record
State: byte;
Count: byte;
end;
PBase = ^TBase;

TData = record
State: byte;
Count: byte;
Data: array[0..50] of char;
end;
PData = ^TData;

..... More records expanding TBase

Now I code what type it is in the state field;

Base: PBase;
Data: PData;

if Base^.State and STATE_IS_TDATA <> 0 then
Data := PData( Base);

gives the same type of error. I have to cast it to a non pointer type then back:

if Base^.State and STATE_IS_TDATA <> 0 then
Data := PData( Word( Base));

Then all is well, sort of because now the code is assuming a 16 bit pointer for the dsPIC and is not portable between micros. I have fixed this by creating a pointer type:

{ifdef P30}Pointer = Word;{endif}
{ifdef P32}Pointer = DWord;{endif}
etc...

so now I do this:

if Base^.State and STATE_IS_TDATA <> 0 then
Data := PData( Pointer( Base));

so it "looks" like I am casting it to a generic pointer but not really.

That said I would like to suggest a generic pointer type so it is automatically based microcontroller you are compiling for and the size of the pointer.

Jim

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

Re: Question about constant pointer typecasts.

#3 Post by srdjan » 27 Jun 2012 08:04

Dany wrote:Hi,

why does the expression

Code: Select all

Ptr := ^const TStateTransition(Machine.TransitionTable);
give an error "syntac error: expected "pointer to record (?T57)" but "pointer to array()" found,
Should be an error here, these types are not compatible and the error message is correct.
Dany wrote: while the expression

Code: Select all

Ptr := ^const TStateTransition(^const byte(Machine.TransitionTable));
compiles without error?
Byte pointer acts as an generic pointer, it can be passed to any other pointer type and vice verse with the exception of function pointers.
Therefore, by forcing this 'two step' casting, compiler makes sure that this is something you
wanted to do, not something you have done by mistake.

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

Re: Question about constant pointer typecasts.

#4 Post by srdjan » 27 Jun 2012 08:10

JimKueneman wrote: so it "looks" like I am casting it to a generic pointer but not really.

That said I would like to suggest a generic pointer type so it is automatically based microcontroller you are compiling for and the size of the pointer.
We already have an pascal version with generic type pointers, just needs to be released :)
Unfortunately, it will take some time to properly test this one :(
For now, you can use byte pointers as generic pointers like Danny did.
This will take care of your pointer size issue mentioned here.

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

Re: Question about constant pointer typecasts.

#5 Post by Dany » 27 Jun 2012 09:03

srdjan wrote:We already have an pascal version with generic type pointers, just needs to be released :)
Unfortunately, it will take some time to properly test this one :(
For now, you can use byte pointers as generic pointers like Danny did.
This will take care of your pointer size issue mentioned here.
Thanks!
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”