optimization error

Beta Testing discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

optimization error

#1 Post by peterverkaik » 17 Dec 2009 00:00

When I compile the following routine

Code: Select all

'//This routine is called every 1.24 usec (49 cycles @79MHz). It provides 7 timeslots.
'//These run at priority 7 to ensure deterministic behaviour for all states.
'//Cycles is 16 + statefunction cycles
'//Only registers w0-w5 must be used by vp driver code because other w registers are not saved.
'//The interrupt frequency for each timeslot equals 1/(7*1.24) MHz = 115.2 kHz
sub procedure T4Interrupt org 0x003E '//On C30 this autogenerates push.s and pop.s statements)
  dim slot as ^vpBank rx '// asm("w5");
  slot = vpPtr                       '//this value is used in all statefunctions
  slot^.statePtr^                    '//call statefunction for slot
  vpPtr = slot^.nextVP               '//next vp
  IFS1.T4IF = 0                      '//clear T4 interruptflag
  asm
    push.s              ; //save w0-w3,status
    mov.d w4,[w15++]    ; //save w4,w5
    mov _vpPtr,w5       ; //slot = vpPtr; //this value is used in all statefunctions
    mov.w [w5+16],w0    ; //(slot->statePtr)(); //call statefunction for slot
    ;call w0             ;
    mov.w [w5+18],w0    ; //vpPtr = (vpBank *)slot->nextVP; //next vp
    mov w0,_vpPtr       ;
    bclr.b IFS1,#3      ; //IFS1bits.T4IF = 0; //clear T4 interruptflag
    mov.d [--w15],w4    ; //restore w4,w5
    pop.s               ; //restore w0-w3,status
  end asm
end sub
then it compiles into

Code: Select all

_T4Interrupt:
;RGBWSv0_01p.mbas,99 :: 		dim slot as ^vpBank rx '// asm("w5");
;RGBWSv0_01p.mbas,102 :: 		vpPtr = slot^.nextVP               '//next vp
0x0238	0x804000  	MOV	_vpPtr, W0
0x023A	0xECA000  	INC2	W0, 1
0x023C	0x780010  	MOV	[W0], W0
0x023E	0x884000  	MOV	W0, _vpPtr
;RGBWSv0_01p.mbas,103 :: 		IFS1.T4IF = 0                      '//clear T4 interruptflag
0x0240	0xA96087  	BCLR	IFS1, #11
;RGBWSv0_01p.mbas,105 :: 		push.s              ; //save w0-w3,status
0x0242	0xFEA000  	PUSH.S
;RGBWSv0_01p.mbas,106 :: 		mov.d w4,[w15++]    ; //save w4,w5
0x0244	0xBE9F84  	MOV.D	W4, [W15++]
;RGBWSv0_01p.mbas,107 :: 		mov _vpPtr,w5       ; //slot = vpPtr; //this value is used in all statefunctions
0x0246	0x804005  	MOV	_vpPtr, W5
;RGBWSv0_01p.mbas,108 :: 		mov.w [w5+16],w0    ; //(slot->statePtr)(); //call statefunction for slot
0x0248	0x900805  	MOV	[W5++16], W0
;RGBWSv0_01p.mbas,110 :: 		mov.w [w5+18],w0    ; //vpPtr = (vpBank *)slot->nextVP; //next vp
0x024A	0x900815  	MOV	[W5++18], W0
;RGBWSv0_01p.mbas,111 :: 		mov w0,_vpPtr       ;
0x024C	0x884000  	MOV	W0, _vpPtr
;RGBWSv0_01p.mbas,112 :: 		bclr.b IFS1,#3      ; //IFS1bits.T4IF = 0; //clear T4 interruptflag
0x024E	0xA96086  	BCLR.B	IFS1, #3
;RGBWSv0_01p.mbas,113 :: 		mov.d [--w15],w4    ; //restore w4,w5
0x0250	0xBE024F  	MOV.D	[--W15], W4
;RGBWSv0_01p.mbas,114 :: 		pop.s               ; //restore w0-w3,status
0x0252	0xFE8000  	POP.S
;RGBWSv0_01p.mbas,115 :: 		end asm
L_end_T4Interrupt:
0x0254	0x064000  	RETFIE
; end of _T4Interrupt
Compilation message shows
99 1011 Hint: Variable "slot" has been eliminated by optimizer RGBWSv0_01p.mbas

Due to the elemination, the call to stateFunction via pointer
is omitted. The asm statement shows the code as it should
be compiled.

Also like to know how I can reserve a specific workregister
in this case w5. Perhaps allow modifiers rx0 to rx15 as well as rx.

regards peter

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

#2 Post by peterverkaik » 17 Dec 2009 00:12

Also noted that asm instruction like
mov.w [w5+16],w0
is compiled into
MOV [W5++18], W0

Double ++ does not generate error.
Also the 18 should be 16

regards peter

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#3 Post by anikolic » 17 Dec 2009 08:56

Hi,
peterverkaik, thank you very much for this report. Could you please open the support ticket at http://www.mikroe.com/en/support/ and attach demonstration working version of this project that could be built, so our developers could take a look and resolve this issue. This would be very helpful to us, and to all users of final release.

Thank you.

Best regards,
Aleksandar
Web Department Manager

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#4 Post by milan » 17 Dec 2009 10:20

Hi,
peterverkaik wrote: Also noted that asm instruction like
mov.w [w5+16],w0
is compiled into
MOV [W5++18], W0
No, it is compiled correctly, see your listing :

Code: Select all

;RGBWSv0_01p.mbas,108 ::       mov.w [w5+16],w0    ; //(slot->statePtr)(); //call statefunction for slot
0x0248   0x900805     MOV   [W5++16], W0

;RGBWSv0_01p.mbas,110 ::       mov.w [w5+18],w0    ; //vpPtr = (vpBank *)slot->nextVP; //next vp
0x024A   0x900815     MOV   [W5++18], W0 
forget about double ++ , it will be corrected ...
SmartADAPT2 rules !

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

#5 Post by srdjan » 17 Dec 2009 12:38

Code: Select all

dim slot as ^vpBank rx '// asm("w5"); 
This was a bug, compiler was not properly allocating slot variable.
It is fixed now and will be available with the release.
Also like to know how I can reserve a specific workregister
in this case w5. Perhaps allow modifiers rx0 to rx15 as well as rx.
You can use absolute directive to allocate variable at specific register address. However, in this case compiler will not exclude this register from further use, so, you have to take care that compiler does not use specific register in scope you need it for something else.

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

#6 Post by peterverkaik » 17 Dec 2009 13:33

I simplified the code

Code: Select all

sub procedure T4Interrupt org 0x003E '//On C30 this autogenerates push.s and pop.s statements)
  'dim slot as ^vpBank rx '// asm("w5");
  w5 = vpPtr                      '//this value is used in all statefunctions
  vpPtr^.statePtr^                '//call statefunction for slot
  vpPtr = vpPtr^.nextVP           '//next vp
  IFS1.T4IF = 0                   '//clear T4 interruptflag
  asm
    push.s              ; //save w0-w3,status
    mov.d w4,[w15++]    ; //save w4,w5
    mov _vpPtr,w5       ; //slot = vpPtr; //this value is used in all statefunctions
    mov.w [w5+16],w0    ; //(slot->statePtr)(); //call statefunction for slot
    ;call w0             ;
    mov.w [w5+18],w0    ; //vpPtr = (vpBank *)slot->nextVP; //next vp
    mov w0,_vpPtr       ;
    bclr IFS1,#11       ; //IFS1bits.T4IF = 0; //clear T4 interruptflag
    mov.d [--w15],w4    ; //restore w4,w5
    pop.s               ; //restore w0-w3,status
  end asm
end sub
and this compiles into

Code: Select all

_T4Interrupt:
;RGBWSv0_01p.mbas,98 :: 		sub procedure T4Interrupt org 0x003E '//On C30 this autogenerates push.s and pop.s statements)
;RGBWSv0_01p.mbas,100 :: 		w5 = vpPtr                      '//this value is used in all statefunctions
0x0238	0x804005  	MOV	_vpPtr, W5
;RGBWSv0_01p.mbas,102 :: 		vpPtr = vpPtr^.nextVP           '//next vp
0x023A	0x804000  	MOV	_vpPtr, W0
0x023C	0x400072  	ADD	W0, #18, W0
0x023E	0x780010  	MOV	[W0], W0
0x0240	0x884000  	MOV	W0, _vpPtr
;RGBWSv0_01p.mbas,103 :: 		IFS1.T4IF = 0                   '//clear T4 interruptflag
0x0242	0xA96087  	BCLR	IFS1, #11
;RGBWSv0_01p.mbas,105 :: 		push.s              ; //save w0-w3,status
0x0244	0xFEA000  	PUSH.S
;RGBWSv0_01p.mbas,106 :: 		mov.d w4,[w15++]    ; //save w4,w5
0x0246	0xBE9F84  	MOV.D	W4, [W15++]
;RGBWSv0_01p.mbas,107 :: 		mov _vpPtr,w5       ; //slot = vpPtr; //this value is used in all statefunctions
0x0248	0x804005  	MOV	_vpPtr, W5
;RGBWSv0_01p.mbas,108 :: 		mov.w [w5+16],w0    ; //(slot->statePtr)(); //call statefunction for slot
0x024A	0x900805  	MOV	[W5++16], W0
;RGBWSv0_01p.mbas,110 :: 		mov.w [w5+18],w0    ; //vpPtr = (vpBank *)slot->nextVP; //next vp
0x024C	0x900815  	MOV	[W5++18], W0
;RGBWSv0_01p.mbas,111 :: 		mov w0,_vpPtr       ;
0x024E	0x884000  	MOV	W0, _vpPtr
;RGBWSv0_01p.mbas,112 :: 		bclr IFS1,#11       ; //IFS1bits.T4IF = 0; //clear T4 interruptflag
0x0250	0xA96087  	BCLR	IFS1, #11
;RGBWSv0_01p.mbas,113 :: 		mov.d [--w15],w4    ; //restore w4,w5
0x0252	0xBE024F  	MOV.D	[--W15], W4
;RGBWSv0_01p.mbas,114 :: 		pop.s               ; //restore w0-w3,status
0x0254	0xFE8000  	POP.S
;RGBWSv0_01p.mbas,115 :: 		end asm
L_end_T4Interrupt:
0x0256	0x064000  	RETFIE
; end of _T4Interrupt
Now w5 gets the value it should get without declaring slot.
The call via pointer is still omitted in the code.

Also, uncommenting 'call w0' in the asm block generates error
Identifier 'w0' was not declared

regards peter

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

#7 Post by janni » 17 Dec 2009 16:17

Also, uncommenting 'call w0' in the asm block generates error
Identifier 'w0' was not declared
This one is fixed for final release.

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#8 Post by anikolic » 18 Dec 2009 14:57

Now w5 gets the value it should get without declaring slot.
The call via pointer is still omitted in the code.

Also, uncommenting 'call w0' in the asm block generates error
Identifier 'w0' was not declared
I have spoken with our developers and I was given confirmation that this is all going to be resolved in the official release.

Best regards,
Aleksandar
Web Department Manager

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 Beta Testing”