Questions about compiler generated code

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Questions about compiler generated code

Post by octal » Wed Jan 24, 2007 2:13 pm

Hi David,
I have two questions about the compiler:

1- Is there any cases where the compiler uses the Stack to pass parameters to functions or to store local variables of functions ? or does he use it only for storing the return address of functions call?

2- If a sub is NOT of type TEvent, we can get its address using the AddressOf() operator. From this address, is there anyway to call the function (the aim is to have Pointers to functions like functionnality)?
Can I just set the SP to this value?

Best regards
Octal

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Wed Jan 24, 2007 2:58 pm

Hi Octal

1 The Pic stack is only used in the normal way to act as a return address

2 Yes you can use the AddressOf() or the @ to find the address of a sub and I thought there was a standard method for SF to load the SP for you with the address but I might be wrong. I had some code that did that a while ago but I cannot find it.

Look at Events in the manual I thing there is something in there on it.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Wed Jan 24, 2007 3:18 pm

Ok TimB, thank you for your quick answer, I'll try to restaure the TOS (in an ASM block) from the AddressOf(...) return value and try to see if it gives correct results or no. I"'ll check the generated ASM code ... and try to simulate it in MPLab.

Now for the Context Save/Restore functions, can we use them outside an Interrupt routine ?
What are the registers saved by these two functions?

PS. I'm writing a Real Time Operating System fully written in SWFBasic and I'm fighting with the Context Switch (the scheduler). If I get good results I'll keep you informed and may be I'll publish it for very low price in order to collect sufficient money to buy a licence of swfCompiler :wink:

Best regards

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Wed Jan 24, 2007 3:47 pm

> 1- Is there any cases where the compiler uses the Stack to
> pass parameters to functions or to store local variables of
> functions ? or does he use it only for storing the return address
> of functions call?

No

> 2- If a sub is NOT of type TEvent, we can get its address using
> the AddressOf() operator.

Yes

> From this address, is there anyway to call the function

Yes, but it is tricky - just loading PCLATH and PCL will cause a jump to the sub address - the stack will be corruped on the RETURN.

> Now for the Context Save/Restore functions, can we use them
> outside an Interrupt routine ?

Yes

> What are the registers saved by these two functions?

parameters, local and working registers. No system registers are saved (unless told to do so). No SFRs are saved (unless told to do so)

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Wed Jan 24, 2007 3:57 pm

Thank you David, I'll try to analyse the generated code to see how I can do scheduling correctly.
For now I'm analysing procedures ot type TEvent and Interrupts context saving. I have a little question:
Intterupts are called OnTimer1, OnXXXXXX .... how can we deduce all the names of Interrupts ? are they always On...... and The name of the interrupt source for the specific target ?

Best regards

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Wed Jan 24, 2007 4:15 pm

Your should not assume anything about the name of a sub, function or interrupt. If the compiler changes its underlying code generation (which it might) it would break any code based on assumptions about labeling.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Wed Jan 24, 2007 4:24 pm

David Barker wrote:Your should not assume anything about the name of a sub, function or interrupt. If the compiler changes its underlying code generation (which it might) it would break any code based on assumptions about labeling.
I do not understand David...
Let's say that I'l going to use Timer1 interrupt ... When I declare an interrupt I have to use this syntax (for a LowPriority Intr):

Code: Select all

// program constants...

const
   TimerReloadValue = 50,
   TimerValue = 65536 - _clock * 2500
   

// timer 1...
dim Timer1 as word(TMR1L)
dim Timer1IF as PIR1.0
dim Timer1IE as PIE1.0
dim Timer1On as T1CON.0

// additional program variables...
dim LED as PORTD.0
dim TimerCounter as byte

// timer interrupt - blink LED every 500ms or so...
interrupt OnTimer()
   Timer1 = Timer1 + word(TimerValue) // force integer arithmetic
   dec(TimerCounter)
   if TimerCounter = 0 then
      TimerCounter = TimerReloadValue 
      toggle(LED)
   endif
   Timer1IF = 0
end interrupt
 
// configure and activate timer 1...
sub ActivateTimer()
   Timer1 = TimerValue
   T1CON = 0        
   Timer1IF = 0   
   Timer1IE = 1  
   Timer1On = 1 
   enable(OnTimer)
end sub
 

// program start...
low(LED)      
ActivateTimer
// loop forever...
while true
wend
So Why have we named the interrupt routine OnTimer ????
if I want to use another interrupt source, what name should have the Onxxxxx routine ????
in lib routine I have see that you use OnRX() for RS232 reception routine ...
What I do not understant is HOW the compiler associates the the Correct Interrupt Source of PIC to the Onxxxxx routine ?
In the previous code, if I named the routine OnMyIntrrupt, would it be associated to the Timer1 interrupt source ? why and where this association is made ?

Best regards
Octal

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Wed Jan 24, 2007 4:36 pm

Sorry, I misunderstood your question - I thought you were referring to names generated by the compiler.

As far as swordfish is concerned you can call an interrupt anything you like...It's just a name given to either a low or high interrupt source. What you do inside, and what interrupts are actually assigned, are entirely up to you...

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Wed Jan 24, 2007 4:50 pm

Ok thank you David, now I understand ...
If I have to respond to two interrupts sources with the same priority, for example Timer1 and Timer2, Both the ACTIVE TOGHETHER, have to handle them in the SAME interrupt handler (using a Case or If statement to react to each interrupt source separately). Is that correct ?

regards

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Wed Jan 24, 2007 5:05 pm

Yes, the PIC has only two possible interrupt levels (high and low). If two events share the same level, you need to differentiate in some way.

Post Reply