Jump to content
Sign in to follow this  
chedzoy

Enhance Use Of References In Boostc?

Recommended Posts

Any chance of adding this in to the BoostC compiler?

 

 

//--------------------------------------------------------------------------

This is a cut down example of the kind of thing I would like to do..

 

The scenario goes like this..

 

I receive a message on the UART which contains a command byte, followed by a payload of say 32 bytes.

My code then wants to interpret the payload part of the message by using the command byte to select the true data

type of the payload.

 

If I'm using pointers I would just cast the payload address to the correct type and then access it through the

cast pointer. But pointer access is quite bulky on the asm code.

 

So I would like to make a reference variable of the type defined by the command byte to the payload memory.

That way when the compiler generates code to access the payload via the reference it generates code that accesses

the same data space as the payload, but uses offsets into the data that are defined by the type of the reference.

Should be a lot less code hungery.

 

This is an example piece of g++ code which shows what I would like to do. Am I right in thinking this would generate much less code than an equivalent using pointers?

 

 

 

#include <stdio.h>

#include <stdlib.h>

 

typedef struct _rs232_msg

{

char payload_type;

char payload[32];

} rs232_msg_t;

 

typedef struct _cmd0

{

int i;

int j;

} cmd0_t;

 

 

typedef struct _cmd1

{

char i;

char j[20];

} cmd1_t;

 

typedef struct _cmd2

{

long i;

} cmd2_t;

 

void receive_msg (rs232_msg_t &msg)

{

// pretend to get a message from UART

for (int i = 0; i < 32; i++) msg.payload = i;

msg.payload_type = 2;

}

 

int main()

{

rs232_msg_t y;

 

receive_msg(y);

 

switch (y.payload_type)

{

case 0:

{

cmd0_t &x = reinterpret_cast<cmd0_t&>(y.payload);

// process cmd0 message using x

break;

}

case 1:

{

cmd1_t &x = reinterpret_cast<cmd1_t&>(y.payload);

// process cmd1 message using x

break;

}

case 2:

{

cmd2_t &x = reinterpret_cast<cmd2_t&>(y.payload);

// process cmd2 message using x

break;

}

default: break;// invalid message type...

}

return 0;

}

Share this post


Link to post
Share on other sites

What about this code that does the same but doesn't use reinterpret_cast and references:

 

...
int main()
{
   rs232_msg_t y;

   receive_msg(y);

   switch (y.payload_type)
   {
       case 0:
       {
       cmd0_t x;
       memcpy( &x, y.payload, sizeof(x) );
       // process cmd0 message using x
       break;
       }
       case 1:
       {
       cmd1_t x;
       memcpy( &x, y.payload, sizeof(x) );
       // process cmd1 message using x
       break;
       }
       case 2:
       {
       cmd2_t x;
       memcpy( &x, y.payload, sizeof(x) );
       // process cmd2 message using x
       break;
       }
       default: break;// invalid message type...
   }
   return 0;
}

 

This code functionally does same what reinterpret_cat would do if it was implemented in BoostC.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
What about this code that does the same but doesn't use reinterpret_cast and references:

 

...
int main()
{
   rs232_msg_t y;

   receive_msg(y);

   switch (y.payload_type)
   {
       case 0:
       {
       cmd0_t x;
       memcpy( &x, y.payload, sizeof(x) );
       // process cmd0 message using x
       break;
       }
       case 1:
       {
       cmd1_t x;
       memcpy( &x, y.payload, sizeof(x) );
       // process cmd1 message using x
       break;
       }
       case 2:
       {
       cmd2_t x;
       memcpy( &x, y.payload, sizeof(x) );
       // process cmd2 message using x
       break;
       }
       default: break;// invalid message type...
   }
   return 0;
}

 

This code functionally does same what reinterpret_cat would do if it was implemented in BoostC.

 

Regards,

Pavel

 

Except it uses upto twice as much memory and how ever much code space memcpy needs.

 

Being able to do..

 

rs232_msg_t rs232;

 

cmd1_t &cmd1 = (cmd1_t &) rs232.payload;

 

would make it much more efficient.

 

All its doing is overlaying a cmd1_t structure on top of the memory at rs232.payload.

 

At the moment Im doing this by fixing the addresses of the structures in the PIC memory.

 

So

 

t_rs232_message rs232_rxmsg@0x20;

#define RS232_RX_PAYLOAD 0x22

 

t_cmd1 cmd1@RS232_RX_PAYLOAD;

t_cmd2 cmd2@RS232_RX_PAYLOAD;

t_cmd3 cmd3@RS232_RX_PAYLOAD;

 

Which is fine, except Id rather the compiler looked after where variables are placed in the address map. I may get it wrong !!

 

In hope !!

 

Chris.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...