Questions about compiler generated code
Moderators: David Barker, Jerry Messina
Questions about compiler generated code
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
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
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.
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.
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
Best regards
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
Best regards
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
> 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)
> 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)
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
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
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
I do not understand David...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.
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
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
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
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...
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...
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
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
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact: