On Fri, 17 Aug 2012, Erich Waelde wrote:

> Hello Marcin,
> 
> I have made tests
> 1. compile avra from git
> commit a6e8b2957953810dae6467eeb4905bfc5ea6c33e
> Author: Marcin Cieslak <sa...@saper.info>
> Date:   Wed Aug 8 17:01:30 2012 +0200

> Does this help?

Yes, thank you.

It seems the problem described in this thread:

http://thread.gmane.org/gmane.comp.lang.forth.amforth/1353/focus=1354

is related to calculation of forward referenced labels in a macro.


Consider the following code (correctly assembled):

                .device atmega32
          PFA_COLD:
C:000000 c004           rjmp    PFA_DOPLUSLOOP3
C:000001 0000           nop
C:000002 0000           nop
C:000003 0000           nop
C:000004 0000           nop
          PFA_DOPLUSLOOP3:      
C:000005 9508           ret
Used memory blocks:
   code      :  Start = 0x0000, End = 0x0005, Length = 0x0006 (6 words), 
Overlap=N


Segment usage:
   Code      :         6 words (12 bytes)
   Data      :         0 bytes
   EEPROM    :         0 bytes


But this:

                .device atmega32
                  .macro jmp_
                         ; a more flexible macro
                        .ifdef @0
                        .if (@0-pc > 2040) || (pc-@0>2040)
                          jmp @0
                          .else   
                          rjmp @0
                          .endif
                          .else
                          jmp @0
                          .endif
                  .endmacro
          PFA_COLD:
C:000000   +    jmp_    PFA_DOPLUSLOOP3
         

          .ifdef PFA_DOPLUSLOOP3
          .if (PFA_DOPLUSLOOP3-pc > 2040) || (pc-PFA_DOPLUSLOOP3>2040)
:000000 c005      rjmp PFA_DOPLUSLOOP3
          .endif
          .else
C:000001 0000           nop
C:000002 0000           nop
C:000003 0000           nop
C:000004 0000           nop
          PFA_DOPLUSLOOP3:      
C:000005 9508           ret
Used memory blocks:
   code      :  Start = 0x0000, End = 0x0006, Length = 0x0007 (7 words), 
Overlap=N


Segment usage:
   Code      :         7 words (14 bytes)
   Data      :         0 bytes
   EEPROM    :         0 bytes

Assembly completed with no errors.



Generated jump goes one word too far (it is "jump 5 words forward"
and not "jump 4 words forward"). It can even be seen
that the length of a macro expansion is wrongly estimated
(it says the whole code is 7 words, while it is only 6).

avrasm2 generally does not allow using not-yet-defind
labels in macros like this.

There are two forward jumps in the code of amforth:

1. There is no problem with "jmp_ PFA_COLD" since the 
distance to PFA_COLD does to depend on the size of the
macro expanded.

2. Only "jmp_ PFA_DOPLUSLOOP3" is problematic since the label 
address depends on the size of the macro - and this can
change .. depending on the target address of the label.

Amforth 4.4 works because it had "rjmp PFA_DOPLUSLOOP3"
hardcoded and not jmp_ macro.


So there are two workarounds for amforth:

1) revert to amforth 4.4 hardcoded "rjmp" since we can
   assume the distance is pretty small here.

2) add a nop after "rjmp" in the "jmp_" macro to keep
   sizes of the macro expansion constant.


Solutions:

1) AVRA may disallow forward refences in the macros.

2) Detecting the impossibility to determine
   the value.

3) ...

//Marcin

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Amforth-devel mailing list for http://amforth.sf.net/
Amforth-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/amforth-devel

Reply via email to