Jump to content

Recommended Posts

Hi,

 

This code:

bit SCK_TRIS		@ TRISB.5;
bit SDI_TRIS		@ TRISB.6;
bit CS_TRIS			@ TRISB.4;
bit SDO_TRIS		@ TRISC.7;
bit NIRQ_TRIS		@ TRISB.7;
bit LED_TRIS		@ TRISC.5;

#define 	OUTPUT	0
#define		INPUT	1


// ***
// This code compiles nice.
CS_TRIS 	= OUTPUT;
SDI_TRIS 	= OUTPUT;
SCK_TRIS 	= OUTPUT;
LED_TRIS 	= OUTPUT;
SDO_TRIS	= INPUT;
NIRQ_TRIS 	= INPUT;
// ***

Produces asm output which seems very fine:

	CS_TRIS 	= OUTPUT;
00F8  1206	  BCF gbl_CS_TRIS,4

SDI_TRIS 	= OUTPUT;
00F9  1306	  BCF gbl_SDI_TRIS,6

SCK_TRIS 	= OUTPUT;
00FA  1286	  BCF gbl_SCK_TRIS,5

LED_TRIS 	= OUTPUT;
00FB  1287	  BCF gbl_LED_TRIS,5

SDO_TRIS	= INPUT;
00FC  1787	  BSF gbl_SDO_TRIS,7

NIRQ_TRIS 	= INPUT;
00FD  1786	  BSF gbl_NIRQ_TRIS,7

 

However, writing it shorter:

// tris assignements and defines not shown
CS_TRIS = SDI_TRIS = SCK_TRIS = LED_TRIS = OUTPUT;
SDO_TRIS = NIRQ_TRIS = INPUT;

 

Produces:

CS_TRIS = SDI_TRIS = SCK_TRIS = LED_TRIS = OUTPUT;
00F8  1283	  BCF STATUS, RP0
00F9  01A5	  CLRF CompTempVar583
00FA  1683	  BSF STATUS, RP0
00FB  1287	  BCF gbl_LED_TRIS,5
00FC  1283	  BCF STATUS, RP0
00FD  01A6	  CLRF CompTempVar584
00FE  1683	  BSF STATUS, RP0
00FF  1286	  BCF gbl_SCK_TRIS,5
0100  1283	  BCF STATUS, RP0
0101  1C25	  BTFSS CompTempVar583,0
0102  2905	  GOTO	label13
0103  1683	  BSF STATUS, RP0
0104  1686	  BSF gbl_SCK_TRIS,5
0105  1283	  BCF STATUS, RP0
0106  1825	  BTFSC CompTempVar583,0
0107  0AA6	  INCF CompTempVar584, F
0108  01A7	  CLRF CompTempVar585
0109  1683	  BSF STATUS, RP0
010A  1306	  BCF gbl_SDI_TRIS,6
010B  1283	  BCF STATUS, RP0
010C  1C26	  BTFSS CompTempVar584,0
010D  2910	  GOTO	label14
010E  1683	  BSF STATUS, RP0
010F  1706	  BSF gbl_SDI_TRIS,6
0110  1283	  BCF STATUS, RP0
0111  1826	  BTFSC CompTempVar584,0
0112  0AA7	  INCF CompTempVar585, F
0113  1683	  BSF STATUS, RP0
0114  1206	  BCF gbl_CS_TRIS,4
0115  1283	  BCF STATUS, RP0
0116  1C27	  BTFSS CompTempVar585,0
0117  291A	  GOTO	label15
0118  1683	  BSF STATUS, RP0
0119  1606	  BSF gbl_CS_TRIS,4

SDO_TRIS = NIRQ_TRIS = INPUT;
011A  1283	  BCF STATUS, RP0
011B  01A5	  CLRF CompTempVar586
011C  0AA5	  INCF CompTempVar586, F
011D  1683	  BSF STATUS, RP0
011E  1786	  BSF gbl_NIRQ_TRIS,7
011F  1387	  BCF gbl_SDO_TRIS,7
0120  1283	  BCF STATUS, RP0
0121  1C25	  BTFSS CompTempVar586,0
0122  2925	  GOTO	label16
0123  1683	  BSF STATUS, RP0
0124  1787	  BSF gbl_SDO_TRIS,7

 

That is 6 vs. 45 instructions. I would excpect these two C blocks to be equal. Is this normal or is SourceBoost making bad optimisation? I'm using BoostC 6.95.

Link to post
Share on other sites

Quoting from an old copy of K&R's 'The C programming language'

7.14 Assignment operators

... The value is the value stored in the left operand after the assignment has taken place. ...

 

Some SFRs read back the same value as was written to them, some dont.

 

Your short form is NOT exactly equivalent!

 

In the long form, each bit is assigned the value of a constant. In the short form, the compiler *must* use the value of the previous bit after the assignment has been performed.

 

For SFRs where the value written is also the value read back, this *could* be optimised to a simple bit set or clear, but I think your are expecting too much of the optimiser.

 

I recommend reading Getting the Least Out of Your C Compiler.

Link to post
Share on other sites

Ian,

 

I think what you have said is completely correct.

 

Saying that, BoostC does not conform to the rules of K&R, ANSI C, C99 etc. and contains addition features of C++ such as reference variables and function overloading.

 

I think it is good practice to have only a single variable (or FSR) assignment per statement.

 

The user can be confused as to the operation of the compiler/optimiser when he sees something like this:

	porta = portb = 0;
007C  6A81		  CLRF gbl_portb
007E  0E00		  MOVLW 0x00
0080  6A80		  CLRF gbl_porta

 

Ignoring the redundant movlw instruction, both porta and portb are declared as volatile but porta is not dependant on the value read from portb but is also assigned the constant of 0. Here you can see the confusion when the compiler is inconsistant.

 

Cheers

 

Reynard

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...
×
×
  • Create New...