gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gm2] Compiling for stand-alone programs on the RPi


From: Gaius Mulley
Subject: Re: [Gm2] Compiling for stand-alone programs on the RPi
Date: Tue, 28 Apr 2015 09:27:07 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

JD <address@hidden> writes:

> Gaius,
> I've got back to trying to compile stand-alone programs for the
> Raspberry Pi, but I'm still having no success with more than one
> module.  I'd appreciate any ideas of yours.
>
> I'm using the native compiler, built about a month ago.
>
> If I do a "standard" compile, e.g..
>     gm2 -fmakeall main.mod
> then I get a clean compile and about 300Kb of binary for the approx 20
> lines of M2.
>
> This binary has in it many calls to the runtime support modules such
> as Storage, System, M2RTS and RTExceptions and many library calls,
> mainly for I/O.  At the moment I don't need any of these and I'd like
> to get rid of them (no offence!) and then put them in explicitly when
> I use them.  Of course, it doesn't run without Linux.
>
> I tried the option -nostartfiles and that reduced the binary to
> 1.5Kb(!)  But there was a very simple trick to that reduction: there
> was no code at all, just data.
>
> Those calls (the _init's and _finish's) clutter up the binary but
> presumably are benign if I don't use them.  I that case I could live
> with them included.
>
> The big stumbling block is the same one as a few months ago: setting
> up the registers, crucially the sp, before my code runs.  I would also
> like to zero the bss section.
>
> Presumably I don't need the 3 C runtime binaries (crt?.o) and so I
> have tried replacing crt1.o with my own code that sets up the sp. The
> result of the full compilation is a 4.7Kb binary that contains my
> crt1.s code but no other code.
>
>
> Do you know of other gcc options that would help me?  Up to now I must
> admit that I've avoided looking at such a long list.
>
> Would a different binutils be appropriate?
>
> I look forward to your thoughts!
>
> Regards,
> John

Hi John,

this should be possible - here is the command line I use to compile
for an embedded target (Atmega 328p).  It only has 2KB ram and 32KB
flash, so I use min lib (-flibs=min):

$ avr-gm2 -mmcu=atmega328 -g -Os -fno-exceptions -O2 -c flashled328.mod \
  -flibs=min
$ avr-gm2 -mmcu=atmega328 -g -Os -fno-exceptions -O2 -fno-pth -fonlylink \
  flashled328.mod -flibs=min -o flashled328.elf

$ avr-size flashled328.elf
   text     data            bss     dec     hex filename
   370        0               0     370     172     flashled328.elf

included at the end is the source code and disassembly of the final
linked executable.

Options which might also be useful are -fm2-whole-program -O2 which will
parse all modules and optimise across all.  Also -fmakelist which
generates a list of all modules it would like to link against.  You
could edit this, comment out using '#' modules or reorder them.  Then
you can tell gm2 to link against this list using: -fuselist.  There are
some critical modules which gm2 will by default attempt to include
(Storage,SYSTEM,M2RTS,RTExceptions,IOLink) if they are used, however you
can override this with the option: -fruntime-modules=foo,bar which
indicates that the gm2 scaffold will call _M2_foo_init before
_M2_bar_init and link foo.o and bar.o with your program and any imported
modules.

Anyway here is the flashing led program for the 328p and the disassembly
of the final executable.

regards,
Gaius


$ cat flashled328.mod
MODULE flashled328 ;

PROCEDURE Turn (on: BOOLEAN) ;
BEGIN
   IF on
   THEN
      (* turn LED on *)
      ASM VOLATILE ("cbi 8,5");
   ELSE
      (* turn LED off *)
      ASM VOLATILE ("sbi 8,5");
   END
END Turn ;

(*
   InitLed - initialize pin 0 as an output
*)


PROCEDURE InitLed ;
BEGIN
   ASM VOLATILE ("sbi 7,5")
END InitLed ;


CONST
   Delay = 400 ;

VAR
   i, j: CARDINAL ;
BEGIN
   InitLed ;
   Turn(FALSE) ;
   LOOP
      FOR i := 0 TO Delay DO
         FOR j := 0 TO Delay DO
            ASM VOLATILE ("nop")
         END
      END ;
      Turn(TRUE) ;
      FOR i := 0 TO Delay DO
         FOR j := 0 TO Delay DO
            ASM VOLATILE ("nop")
         END
      END ;
      Turn(FALSE)
   END
END flashled328.


flashled328.elf:     file format elf32-avr


Disassembly of section .text:

00000000 <__vectors>:
   0:   0c 94 34 00     jmp     0x68    ; 0x68 <__ctors_end>
   4:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
   8:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
   c:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  10:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  14:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  18:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  1c:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  20:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  24:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  28:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  2c:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  30:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  34:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  38:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  3c:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  40:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  44:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  48:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  4c:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  50:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  54:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  58:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  5c:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  60:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>
  64:   0c 94 3e 00     jmp     0x7c    ; 0x7c <__bad_interrupt>

00000068 <__ctors_end>:
  68:   11 24           eor     r1, r1
  6a:   1f be           out     0x3f, r1        ; 63
  6c:   cf ef           ldi     r28, 0xFF       ; 255
  6e:   d8 e0           ldi     r29, 0x08       ; 8
  70:   de bf           out     0x3e, r29       ; 62
  72:   cd bf           out     0x3d, r28       ; 61
  74:   0e 94 9f 00     call    0x13e   ; 0x13e <main>
  78:   0c 94 9e 00     jmp     0x13c   ; 0x13c <exit>

0000007c <__bad_interrupt>:
  7c:   0c 94 00 00     jmp     0       ; 0x0 <__vectors>

00000080 <_M2_flashled328_init>:
  80:   cf 93           push    r28
  82:   df 93           push    r29
  84:   cd b7           in      r28, 0x3d       ; 61
  86:   de b7           in      r29, 0x3e       ; 62
  88:   3d 9a           sbi     0x07, 5 ; 7
  8a:   45 9a           sbi     0x08, 5 ; 8
  8c:   21 e9           ldi     r18, 0x91       ; 145
  8e:   31 e0           ldi     r19, 0x01       ; 1
  90:   00 00           nop
  92:   80 e9           ldi     r24, 0x90       ; 144
  94:   91 e0           ldi     r25, 0x01       ; 1
  96:   00 00           nop
  98:   01 97           sbiw    r24, 0x01       ; 1
  9a:   e9 f7           brne    .-6             ; 0x96 
<_M2_flashled328_init+0x16>
  9c:   21 50           subi    r18, 0x01       ; 1
  9e:   31 09           sbc     r19, r1
  a0:   b9 f7           brne    .-18            ; 0x90 
<_M2_flashled328_init+0x10>
  a2:   45 98           cbi     0x08, 5 ; 8
  a4:   21 e9           ldi     r18, 0x91       ; 145
  a6:   31 e0           ldi     r19, 0x01       ; 1
  a8:   00 00           nop
  aa:   80 e9           ldi     r24, 0x90       ; 144
  ac:   91 e0           ldi     r25, 0x01       ; 1
  ae:   00 00           nop
  b0:   01 97           sbiw    r24, 0x01       ; 1
  b2:   e9 f7           brne    .-6             ; 0xae 
<_M2_flashled328_init+0x2e>
  b4:   21 50           subi    r18, 0x01       ; 1
  b6:   31 09           sbc     r19, r1
  b8:   b9 f7           brne    .-18            ; 0xa8 
<_M2_flashled328_init+0x28>
  ba:   e7 cf           rjmp    .-50            ; 0x8a 
<_M2_flashled328_init+0xa>

000000bc <_M2_flashled328_finish>:
  bc:   cf 93           push    r28
  be:   df 93           push    r29
  c0:   cd b7           in      r28, 0x3d       ; 61
  c2:   de b7           in      r29, 0x3e       ; 62
  c4:   df 91           pop     r29
  c6:   cf 91           pop     r28
  c8:   08 95           ret

000000ca <_M2_M2RTS_init>:
  ca:   cf 93           push    r28
  cc:   df 93           push    r29
  ce:   cd b7           in      r28, 0x3d       ; 61
  d0:   de b7           in      r29, 0x3e       ; 62
  d2:   df 91           pop     r29
  d4:   cf 91           pop     r28
  d6:   08 95           ret

000000d8 <_M2_M2RTS_finish>:
  d8:   cf 93           push    r28
  da:   df 93           push    r29
  dc:   cd b7           in      r28, 0x3d       ; 61
  de:   de b7           in      r29, 0x3e       ; 62
  e0:   df 91           pop     r29
  e2:   cf 91           pop     r28
  e4:   08 95           ret

000000e6 <M2RTS_ExecuteTerminationProcedures>:
  e6:   cf 93           push    r28
  e8:   df 93           push    r29
  ea:   cd b7           in      r28, 0x3d       ; 61
  ec:   de b7           in      r29, 0x3e       ; 62
  ee:   df 91           pop     r29
  f0:   cf 91           pop     r28
  f2:   08 95           ret

000000f4 <M2RTS_ExecuteInitialProcedures>:
  f4:   cf 93           push    r28
  f6:   df 93           push    r29
  f8:   cd b7           in      r28, 0x3d       ; 61
  fa:   de b7           in      r29, 0x3e       ; 62
  fc:   df 91           pop     r29
  fe:   cf 91           pop     r28
 100:   08 95           ret

00000102 <M2RTS_HALT>:
 102:   cf 93           push    r28
 104:   df 93           push    r29
 106:   cd b7           in      r28, 0x3d       ; 61
 108:   de b7           in      r29, 0x3e       ; 62
 10a:   df 91           pop     r29
 10c:   cf 91           pop     r28
 10e:   08 95           ret

00000110 <M2RTS_NoException>:
 110:   cf 93           push    r28
 112:   df 93           push    r29
 114:   cd b7           in      r28, 0x3d       ; 61
 116:   de b7           in      r29, 0x3e       ; 62
 118:   df 91           pop     r29
 11a:   cf 91           pop     r28
 11c:   08 95           ret

0000011e <_M2_SYSTEM_init>:
 11e:   cf 93           push    r28
 120:   df 93           push    r29
 122:   cd b7           in      r28, 0x3d       ; 61
 124:   de b7           in      r29, 0x3e       ; 62
 126:   df 91           pop     r29
 128:   cf 91           pop     r28
 12a:   08 95           ret

0000012c <_M2_SYSTEM_finish>:
 12c:   cf 93           push    r28
 12e:   df 93           push    r29
 130:   cd b7           in      r28, 0x3d       ; 61
 132:   de b7           in      r29, 0x3e       ; 62
 134:   df 91           pop     r29
 136:   cf 91           pop     r28
 138:   08 95           ret

0000013a <abort>:
 13a:   08 95           ret

0000013c <exit>:
 13c:   08 95           ret

0000013e <main>:
 13e:   ec 01           movw    r28, r24
 140:   8b 01           movw    r16, r22
 142:   0e 94 8f 00     call    0x11e   ; 0x11e <_M2_SYSTEM_init>
 146:   b8 01           movw    r22, r16
 148:   ce 01           movw    r24, r28
 14a:   0e 94 65 00     call    0xca    ; 0xca <_M2_M2RTS_init>
 14e:   0e 94 7a 00     call    0xf4    ; 0xf4 <M2RTS_ExecuteInitialProcedures>
 152:   b8 01           movw    r22, r16
 154:   ce 01           movw    r24, r28
 156:   0e 94 40 00     call    0x80    ; 0x80 <_M2_flashled328_init>
 15a:   0e 94 73 00     call    0xe6    ; 0xe6 
<M2RTS_ExecuteTerminationProcedures>
 15e:   0e 94 5e 00     call    0xbc    ; 0xbc <_M2_flashled328_finish>
 162:   0e 94 6c 00     call    0xd8    ; 0xd8 <_M2_M2RTS_finish>
 166:   0e 94 96 00     call    0x12c   ; 0x12c <_M2_SYSTEM_finish>
 16a:   80 e0           ldi     r24, 0x00       ; 0
 16c:   90 e0           ldi     r25, 0x00       ; 0
 16e:   0e 94 9e 00     call    0x13c   ; 0x13c <exit>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]