comp.lang.ada
 help / color / mirror / Atom feed
* Return unconstrained types in ZFP
@ 2012-12-02  2:38 Rego, P.
  2012-12-02  3:29 ` Shark8
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Rego, P. @ 2012-12-02  2:38 UTC (permalink / raw)


Dear fellows, 

I am trying to implement, for an ATmega2560 (Arduino Mega) micro-controller using ZFP in GNAT-AVR, an USART unconstrained Get string like
      function Get (Port : USART_Port_Type; Size : Unsigned_8) return String_U8;

assuming that USART_Port_Type is an enumeration, and
      type String_U8 is array (Unsigned_8 range <>) of Character;

So I tried
      function Get (Port : USART_Port_Type; Size : Unsigned_8) return String_U8 is
         Curr_String : String_U8 (1 .. Size);
         Null_String : constant String_U8 (1 .. Size) := (others => ' ');
      begin
         for Index in 1 .. Curr_String'Length loop
            Curr_String (Unsigned_8 (Index)) := Get (Port); -- BTW, function Get (Port : USART_Port_Type) return Character; works ok!
         end loop;
         return Curr_String;
      exception
         when others => return Null_String;
      end Get;

and the compiler complained that I am not defining the Last_Chance_Handler. Now I found in GNAT documentation, that I should define it using
      procedure Last_Chance_Handler
        (Source_Location : System.Address; Line : Integer);
      pragma Export (C, Last_Chance_Handler, "__gnat_last_chance_handler");

and at least (for now) a null procedure would be enough
      procedure Last_Chance_Handler
        (Source_Location : System.Address; Line : Integer) is
      begin
         null;
      end Last_Chance_Handler; 

But now the compiler complains that I also did not define the Get_Secondary_Stack, and found no reference to memcpy. I found in GNAT documentation the Secondary Stack definition, but with no syntax information, and nothing else. And regarding to memcpy, found nothing.
   --    The Secondary Stack
   --      The secondary stack is a special storage pool that is used for
   --      this purpose. The called function places the result on the
   --      secondary stack, and the caller uses or copies the value from
   --      the secondary stack, and pops the secondary stack after the
   --      value is consumed. The secondary stack is outside the system
   --      ABI, and the important point is that although generally it is
   --      handled in a stack like manner corresponding to the subprogram
   --      call structure, a return from a function does NOT pop the stack.

Actually the compiler returned me
   In function `avr__atmega2560__usart__get__7':
   avr-atmega2560-usart.adb:(.text+0x164c): undefined reference to `memcpy'
   avr-atmega2560-usart.adb:(.text+0x1694): undefined reference to `memcpy'
   C:/GNAT/2012/lib/gcc/avr/4.5.4/rts-zfp\adalib\libgnat.a(s-secsta.o): In function `system__secondary_stack__ss_allocate':
   s-secsta.adb:(.text+0x8): undefined reference to `__gnat_get_secondary_stack'
   C:/GNAT/2012/lib/gcc/avr/4.5.4/rts-zfp\adalib\libgnat.a(s-secsta.o): In function `system__secondary_stack__ss_mark':
   s-secsta.adb:(.text+0x54): undefined reference to `__gnat_get_secondary_stack'
   C:/GNAT/2012/lib/gcc/avr/4.5.4/rts-zfp\adalib\libgnat.a(s-secsta.o): In function `system__secondary_stack__ss_release':
   s-secsta.adb:(.text+0x66): undefined reference to `__gnat_get_secondary_stack'
   collect2: ld returned 1 exit status
   avr-gnatlink: error when calling C:\GNAT\2012\bin\avr-gcc.exe
   avr-gnatmake: *** link failed.

I also tried to define null bodies like the Last_Chance_Handler, and at this point the code linked with success. But I am afraid that doing this I could make a big mess, so I did not burn the chip using this .hex (specially due to Memcpy null body). Thus, could someone give me a light on this? How can I implement correctly the Secondary_Stack procedure and Memcpy? Or maybe a better documentation or a tutorial would be very helpful.

Thanks.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-02  2:38 Return unconstrained types in ZFP Rego, P.
@ 2012-12-02  3:29 ` Shark8
  2012-12-04  1:08   ` Rego, P.
  2012-12-02 18:13 ` Luke A. Guest
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 13+ messages in thread
From: Shark8 @ 2012-12-02  3:29 UTC (permalink / raw)


That last-chance handler error came up for me when I was trying a RTL-less build for a bare-bones project (simple OS) -- I think it had to do w/ the compiler wanting to use RTL-features (denied because it's bare-bones) that were non-existent.

I'd bet that our resident compiler makers (Randy springs to mind) know a thing or two about bootstrapping, though I wouldn't expect Randy to know the GNAT details.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-02  2:38 Return unconstrained types in ZFP Rego, P.
  2012-12-02  3:29 ` Shark8
@ 2012-12-02 18:13 ` Luke A. Guest
  2012-12-04  0:59   ` Rego, P.
  2012-12-02 18:27 ` rrr.eee.27
  2012-12-03 12:25 ` Stephen Leake
  3 siblings, 1 reply; 13+ messages in thread
From: Luke A. Guest @ 2012-12-02 18:13 UTC (permalink / raw)


You need to define a small secondary stack, this can be defined in the linker
script and then imported into the correct package. This is the only way to
return unconstrained types. If you look in s-secsta you will see what needs to
be defined, you can implement it in a very basic way, it does not need to be
as complex as the system version.

Also, you need the memory allocation functions, if AVR Ada provides memcpy,
bzero, etc. just import them, see the runtime sources again for how it does this
on Linux. Otherwise, you will have to write your own.

Luke.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-02  2:38 Return unconstrained types in ZFP Rego, P.
  2012-12-02  3:29 ` Shark8
  2012-12-02 18:13 ` Luke A. Guest
@ 2012-12-02 18:27 ` rrr.eee.27
  2012-12-04  0:54   ` Rego, P.
  2012-12-03 12:25 ` Stephen Leake
  3 siblings, 1 reply; 13+ messages in thread
From: rrr.eee.27 @ 2012-12-02 18:27 UTC (permalink / raw)


On Sunday, December 2, 2012 3:38:22 AM UTC+1, Rego, P. wrote:
> Dear fellows, 
> 
> I am trying to implement, for an ATmega2560 (Arduino Mega) micro-controller using ZFP in GNAT-AVR, an USART unconstrained Get string like


Returning unconstrained objects from functions requires the so-called secondary stack in GNAT.  If you want to use it you need the files s-secsta.ad(s|b) and s-stoele.ad(s|b).  (Then you cannot call your run time system a "zero footprint" system anymore as the footprint is > 0)

BTW, AVR-Ada already provides the secondary stack for those who want to use this Ada feature without looking at the generated Asm.  The ATmega2560 is relatively big, you probably dont have to care.  Unconstrained arrays are nice syntax sugar.  If you are short on RAM or flash (I always am) you don't want to use that feature, however.

HTH
    Rolf



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-02  2:38 Return unconstrained types in ZFP Rego, P.
                   ` (2 preceding siblings ...)
  2012-12-02 18:27 ` rrr.eee.27
@ 2012-12-03 12:25 ` Stephen Leake
  2012-12-04  1:15   ` Rego, P.
  2012-12-04  8:40   ` Stephen Leake
  3 siblings, 2 replies; 13+ messages in thread
From: Stephen Leake @ 2012-12-03 12:25 UTC (permalink / raw)


"Rego, P." <pvrego@gmail.com> writes:

> I am trying to implement, for an ATmega2560 (Arduino Mega) micro-controller using ZFP in GNAT-AVR, an USART unconstrained Get string like
>       function Get (Port : USART_Port_Type; Size : Unsigned_8) return
>       String_U8;

Is "Size" a maximum here, or is the result expected to be exactly Size
bytes long?

Another alternative is to use a procedure:

      procedure Get 
         (Port      : in USART_Port_Type; 
          Data      :    out String_U8; 
          Data_Last :    out Unsigned_8);

Then the user must allocate space for Data:

declare
   Data      : String_U8 (1 .. Size);
   Data_Last : Unsigned_8;
begin
   Get (Port, Data, Data_Last);
   ... use Data
end;

This way, you don't need to implement the runtime code.

It is more of a burden for the user, but that's the point of ZFP; it's
easier to implement, harder to use.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-02 18:27 ` rrr.eee.27
@ 2012-12-04  0:54   ` Rego, P.
  0 siblings, 0 replies; 13+ messages in thread
From: Rego, P. @ 2012-12-04  0:54 UTC (permalink / raw)


> Returning unconstrained objects from functions requires the so-called secondary stack in GNAT.  If you want to use it you need the files s-secsta.ad(s|b) and s-stoele.ad(s|b). (Then you cannot call your run time system a "zero footprint" system anymore as the footprint is > 0)

I took a look in these files, at first it would be a good try. But you made a good point... if I were to use the secondary stack I could not call my rts a "zero footprint" (so I thought twice...)

> The ATmega2560 is relatively big, you probably dont have to care.  Unconstrained arrays are nice syntax sugar.  If you are short on RAM or flash (I always am) you don't want to use that feature, however.

Now I am not short of RAM and flash, but I'm sure I will be soon. You are right.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-02 18:13 ` Luke A. Guest
@ 2012-12-04  0:59   ` Rego, P.
  0 siblings, 0 replies; 13+ messages in thread
From: Rego, P. @ 2012-12-04  0:59 UTC (permalink / raw)


On Sunday, December 2, 2012 4:13:39 PM UTC-2, Luke A. Guest wrote:
> You need to define a small secondary stack, this can be defined in the linker
> script and then imported into the correct package. This is the only way to
> return unconstrained types. If you look in s-secsta you will see what needs to
> be defined, you can implement it in a very basic way, it does not need to be
> as complex as the system version.
> 
> Also, you need the memory allocation functions, if AVR Ada provides memcpy,
> bzero, etc. just import them, see the runtime sources again for how it does this on Linux. Otherwise, you will have to write your own.

I will very keep this info, it sure can be very useful in near future (but now Stephen made a very good suggestion, I am very inclined to use it :-) ). Thanks Luke.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-02  3:29 ` Shark8
@ 2012-12-04  1:08   ` Rego, P.
  0 siblings, 0 replies; 13+ messages in thread
From: Rego, P. @ 2012-12-04  1:08 UTC (permalink / raw)


On Sunday, December 2, 2012 1:29:32 AM UTC-2, Shark8 wrote:
> That last-chance handler error came up for me when I was trying a RTL-less build for a bare-bones project (simple OS) -- I think it had to do w/ the compiler wanting to use RTL-features (denied because it's bare-bones) that were non-existent.
> I'd bet that our resident compiler makers (Randy springs to mind) know a thing or two about bootstrapping, though I wouldn't expect Randy to know the GNAT details.

Thanks Shark8. I found also a very good tutorial for a bare-bones project "Ada Bare bones" in http://wiki.osdev.org/Ada_Bare_bones.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-03 12:25 ` Stephen Leake
@ 2012-12-04  1:15   ` Rego, P.
  2012-12-04  8:40   ` Stephen Leake
  1 sibling, 0 replies; 13+ messages in thread
From: Rego, P. @ 2012-12-04  1:15 UTC (permalink / raw)


> Another alternative is to use a procedure:
>       procedure Get 
>          (Port      : in USART_Port_Type; 
>           Data      :    out String_U8; 
>           Data_Last :    out Unsigned_8);
> Then the user must allocate space for Data:
> declare
>    Data      : String_U8 (1 .. Size);
>    Data_Last : Unsigned_8;
> begin
>    Get (Port, Data, Data_Last);
>    ... use Data
> end;
> This way, you don't need to implement the runtime code.
> It is more of a burden for the user, but that's the point of ZFP; it's
> easier to implement, harder to use.

Stephe, you made a very good suggestion. So I don't need to implement the rt code, at least for now (I'm sure I will miss RAM if I do it now). Thanks!

> Is "Size" a maximum here, or is the result expected to be exactly Size
> bytes long?

At first I would put the Size to set the exact size of the output String_U8, but if the user allocate it first, it fits the idea well. 

So, I used something like

   procedure Get
     (Port : in USART_Port_Type;
      Data : out String_U8);
...
   procedure Get 
     (Port : in USART_Port_Type;
      Data : out String_U8) is
   begin
      for Index in 1 .. Data'Length loop
         Data (Unsigned_8 (Index)) := Get (Port);
      end loop;
   exception
      when others => null; -- no need for this now
   end Get;
 
Thanks again Stephe, Rolf, Luke and Shark8.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-03 12:25 ` Stephen Leake
  2012-12-04  1:15   ` Rego, P.
@ 2012-12-04  8:40   ` Stephen Leake
  2012-12-04 13:21     ` Britt
  1 sibling, 1 reply; 13+ messages in thread
From: Stephen Leake @ 2012-12-04  8:40 UTC (permalink / raw)


Stephen Leake <stephen_leake@stephe-leake.org> writes:

> <snip>
>
> This way, you don't need to implement the runtime code.
>
> It is more of a burden for the user, but that's the point of ZFP; it's
> easier to implement, harder to use.

There should be a Restriction pragma setting that can warn about code
that requires a runtime. There doesn't seem to be one in LRM 13.12. Ah;
there are a bunch more in Annex D.

Apparently the only list of restrictions in GNAT is in the source code
(not a manual); lib/gcc/i686-pc-mingw32/4.5.4/adainclude/s-rident.ads

The following seem likely to help:

No_Implicit_Heap_Allocations
No_Secondary_Stack
No_Tasking

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-04  8:40   ` Stephen Leake
@ 2012-12-04 13:21     ` Britt
  2012-12-05  3:34       ` Stephen Leake
  0 siblings, 1 reply; 13+ messages in thread
From: Britt @ 2012-12-04 13:21 UTC (permalink / raw)


On Tuesday, December 4, 2012 3:40:47 AM UTC-5, Stephen Leake wrote:
> Stephen Leake <> writes:
> Apparently the only list of restrictions in GNAT is in the source code (not a manual);

No, see http://docs.adacore.com/gnat-unw-docs/html/gnat_rm_4.html#SEC202 for the full list.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-04 13:21     ` Britt
@ 2012-12-05  3:34       ` Stephen Leake
  2012-12-05 13:48         ` Britt
  0 siblings, 1 reply; 13+ messages in thread
From: Stephen Leake @ 2012-12-05  3:34 UTC (permalink / raw)


Britt <britt.snodgrass@gmail.com> writes:

> On Tuesday, December 4, 2012 3:40:47 AM UTC-5, Stephen Leake wrote:
>> Stephen Leake <> writes:
>> Apparently the only list of restrictions in GNAT is in the source code (not a manual);
>
> No, see
> http://docs.adacore.com/gnat-unw-docs/html/gnat_rm_4.html#SEC202 for
> the full list.

Hmm. I wonder what version of GNAT that manual is from?

That section (on Restrictions) is missing in the info and html versions
in GNAT 7.0.1.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Return unconstrained types in ZFP
  2012-12-05  3:34       ` Stephen Leake
@ 2012-12-05 13:48         ` Britt
  0 siblings, 0 replies; 13+ messages in thread
From: Britt @ 2012-12-05 13:48 UTC (permalink / raw)


On Tuesday, December 4, 2012 10:34:04 PM UTC-5, Stephen Leake wrote:
> Hmm. I wonder what version of GNAT that manual is from? That section (on Restrictions) is missing in the info and html versions in GNAT 7.0.1.

I suppose I cheated. AdaCore's "Live Docs" on the Web are usually bleeding edge advance versions. In this case the cover page says:

GNAT Pro Reference Manual 

The GNAT Pro Ada Compiler
GNAT Pro Version 7.2.0w
Document revision level 287265
Date: 2012/11/29 




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2012-12-05 13:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-02  2:38 Return unconstrained types in ZFP Rego, P.
2012-12-02  3:29 ` Shark8
2012-12-04  1:08   ` Rego, P.
2012-12-02 18:13 ` Luke A. Guest
2012-12-04  0:59   ` Rego, P.
2012-12-02 18:27 ` rrr.eee.27
2012-12-04  0:54   ` Rego, P.
2012-12-03 12:25 ` Stephen Leake
2012-12-04  1:15   ` Rego, P.
2012-12-04  8:40   ` Stephen Leake
2012-12-04 13:21     ` Britt
2012-12-05  3:34       ` Stephen Leake
2012-12-05 13:48         ` Britt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox