comp.lang.ada
 help / color / mirror / Atom feed
* no code generation for c strings
@ 2016-06-03  8:14 Luke A. Guest
  2016-06-03  8:25 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 13+ messages in thread
From: Luke A. Guest @ 2016-06-03  8:14 UTC (permalink / raw)




Hi,

In my current binding efforts I'm having to have a bunch of c strings which
denote the function name that needs to be requested from the API. If I
create an Ada string and convert it to c then whole application gets two
copies of each string which is overkill.

What I would like to have is have the compiler recognise that I've declared
a static char_array and just not generate a call to the secondary stack to
allocate a new string. Is this actually possible? -O2/3 still generate the
call.

S : constant char_array := to_c ("hello" & nul);


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

* Re: no code generation for c strings
  2016-06-03  8:14 no code generation for c strings Luke A. Guest
@ 2016-06-03  8:25 ` Dmitry A. Kazakov
  2016-06-03  9:23   ` Luke A. Guest
  2016-06-03 16:30   ` Simon Wright
  0 siblings, 2 replies; 13+ messages in thread
From: Dmitry A. Kazakov @ 2016-06-03  8:25 UTC (permalink / raw)


On 03/06/2016 10:14, Luke A. Guest wrote:

> What I would like to have is have the compiler recognise that I've declared
> a static char_array and just not generate a call to the secondary stack to
> allocate a new string. Is this actually possible? -O2/3 still generate the
> call.
>
> S : constant char_array := to_c ("hello" & nul);

S : constant char_array := "hello" & char'val (0);

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: no code generation for c strings
  2016-06-03  8:25 ` Dmitry A. Kazakov
@ 2016-06-03  9:23   ` Luke A. Guest
  2016-06-03  9:37     ` Dmitry A. Kazakov
  2016-06-03 16:30   ` Simon Wright
  1 sibling, 1 reply; 13+ messages in thread
From: Luke A. Guest @ 2016-06-03  9:23 UTC (permalink / raw)


Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote:

>> 
>> S : constant char_array := to_c ("hello" & nul);
> 
> S : constant char_array := "hello" & char'val (0);
> 

Are you saying this will produce the desired code?

Luke


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

* Re: no code generation for c strings
  2016-06-03  9:23   ` Luke A. Guest
@ 2016-06-03  9:37     ` Dmitry A. Kazakov
  2016-06-03 14:27       ` G.B.
  0 siblings, 1 reply; 13+ messages in thread
From: Dmitry A. Kazakov @ 2016-06-03  9:37 UTC (permalink / raw)


On 03/06/2016 11:23, Luke A. Guest wrote:
> Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote:
>
>>> S : constant char_array := to_c ("hello" & nul);
>>
>> S : constant char_array := "hello" & char'val (0);
>
> Are you saying this will produce the desired code?

In any case it should be better than calling a function, Ada still does 
not have compile-time ones.

A decent compiler/linker should use an initialized section here. If not, 
you could try an array aggregate instead...

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: no code generation for c strings
  2016-06-03  9:37     ` Dmitry A. Kazakov
@ 2016-06-03 14:27       ` G.B.
  2016-06-03 17:00         ` Lucretia
  0 siblings, 1 reply; 13+ messages in thread
From: G.B. @ 2016-06-03 14:27 UTC (permalink / raw)


On 03.06.16 11:37, Dmitry A. Kazakov wrote:
> On 03/06/2016 11:23, Luke A. Guest wrote:
>> Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote:
>>
>>>> S : constant char_array := to_c ("hello" & nul);
>>>
>>> S : constant char_array := "hello" & char'val (0);
>>
>> Are you saying this will produce the desired code?
>
> In any case it should be better than calling a function, Ada still does
> not have compile-time ones.

If "&" will be become a function call, then

  S : constant char_array := ('h', 'e', 'l', 'l', 'o', nul);



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

* Re: no code generation for c strings
  2016-06-03  8:25 ` Dmitry A. Kazakov
  2016-06-03  9:23   ` Luke A. Guest
@ 2016-06-03 16:30   ` Simon Wright
  2016-06-03 17:04     ` Lucretia
  1 sibling, 1 reply; 13+ messages in thread
From: Simon Wright @ 2016-06-03 16:30 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On 03/06/2016 10:14, Luke A. Guest wrote:
>
>> What I would like to have is have the compiler recognise that I've declared
>> a static char_array and just not generate a call to the secondary stack to
>> allocate a new string. Is this actually possible? -O2/3 still generate the
>> call.
>>
>> S : constant char_array := to_c ("hello" & nul);
>
> S : constant char_array := "hello" & char'val (0);

GCC 6.1.0:

   with Interfaces.C;
   package Char_Arrays is
      use type Interfaces.C.char;
      use type Interfaces.C.char_array;
      S : constant Interfaces.C.char_array :=
        "hello" & Interfaces.C.char'Val (0);
   end Char_Arrays;

generates (x86_64-apple-darwin15)

	.globl _char_arrays__s
	.const
	.align 3
_char_arrays__s:
	.ascii "hello\0"
	.space 2
	.globl _char_arrays_E
	.data
	.align 1
_char_arrays_E:
	.space 2
	.subsections_via_symbols

& similar for arm-eabi.


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

* Re: no code generation for c strings
  2016-06-03 14:27       ` G.B.
@ 2016-06-03 17:00         ` Lucretia
  2016-06-04  6:31           ` Georg Bauhaus
  0 siblings, 1 reply; 13+ messages in thread
From: Lucretia @ 2016-06-03 17:00 UTC (permalink / raw)


On Friday, 3 June 2016 15:27:59 UTC+1, G.B.  wrote:

> If "&" will be become a function call, then
> 
>   S : constant char_array := ('h', 'e', 'l', 'l', 'o', nul);

I want to avoid having to spell out all these function names like that thanks, and not really sure what you mean about the & there.

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

* Re: no code generation for c strings
  2016-06-03 16:30   ` Simon Wright
@ 2016-06-03 17:04     ` Lucretia
  2016-06-03 18:45       ` Simon Wright
  0 siblings, 1 reply; 13+ messages in thread
From: Lucretia @ 2016-06-03 17:04 UTC (permalink / raw)


On Friday, 3 June 2016 17:30:14 UTC+1, Simon Wright  wrote:
> "Dmitry A. Kazakov" writes:
> 
> > On 03/06/2016 10:14, Luke A. Guest wrote:
> >
> >> What I would like to have is have the compiler recognise that I've declared
> >> a static char_array and just not generate a call to the secondary stack to
> >> allocate a new string. Is this actually possible? -O2/3 still generate the
> >> call.
> >>
> >> S : constant char_array := to_c ("hello" & nul);
> >
> > S : constant char_array := "hello" & char'val (0);
> 
> GCC 6.1.0:
> 
>    with Interfaces.C;
>    package Char_Arrays is
>       use type Interfaces.C.char;
>       use type Interfaces.C.char_array;
>       S : constant Interfaces.C.char_array :=
>         "hello" & Interfaces.C.char'Val (0);
>    end Char_Arrays;
> 
> generates (x86_64-apple-darwin15)
> 
> 	.globl _char_arrays__s
> 	.const
> 	.align 3
> _char_arrays__s:
> 	.ascii "hello\0"
> 	.space 2
> 	.globl _char_arrays_E
> 	.data
> 	.align 1
> _char_arrays_E:
> 	.space 2
> 	.subsections_via_symbols
> 
> & similar for arm-eabi.


Interesting, I tried this and dumped the asm, similar there, but as soon as you add more code, it just gets confusing, because it never references the actual label for the string anywhere.

with Interfaces.C;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Text_Io; use Ada.Text_Io;

procedure C_Str is
   package C renames Interfaces.C;

   use type Interfaces.C.char;
   use type Interfaces.C.char_array;
   --  S1 : constant Interfaces.C.char_array := Interfaces.C.To_C ("hello" & Nul, Append_Nul => False);
--     S2 : constant C.char_array := C.To_C ("hello");
   S3 : constant C.char_array := "hello" & C.char'Val (0);
begin
--     Put_Line (C.To_Ada (S3));
   null;
end C_Str;

gnatmake -gnatG c_str.adb -cargs -S

gcc -c -gnatG -S c_str.adb
Source recreated from tree for C_Str (body)
-------------------------------------------

with ada;
with ada;
with ada.ada__characters;
with interfaces;
with interfaces.interfaces__c;
with ada.ada__characters.ada__characters__latin_1;
use ada.ada__characters.ada__characters__latin_1;
with ada.ada__text_io;
use ada.ada__text_io;

procedure c_str is
   c___XRP_interfaces__c___XE : _renaming_type;
   package c renames interfaces.interfaces__c;
   use type interfaces.interfaces__c.interfaces__c__char;
   use type interfaces.interfaces__c.interfaces__c__char_array;
   subtype c_str__Ts3S is interfaces__c__char_array (0 .. 5);
   s3 : constant interfaces__c__char_array (0 .. 5) := "hello["00"]";
begin
   null;
   return;
end c_str;

gnatmake: "c_str.ali" WARNING: ALI or object file not found after compile
gnatmake: "c_str.adb" compilation error

	.file	"c_str.adb"
	.text
	.globl	_ada_c_str
	.type	_ada_c_str, @function
_ada_c_str:
.LFB1:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	nop
	nop
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	_ada_c_str, .-_ada_c_str
	.section	.rodata
	.align 8
	.type	s3.3058, @object
	.size	s3.3058, 8
s3.3058:
	.string	"hello"
	.zero	2
	.ident	"GCC: (GNU) 4.9.2"
	.section	.note.GNU-stack,"",@progbits

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

* Re: no code generation for c strings
  2016-06-03 17:04     ` Lucretia
@ 2016-06-03 18:45       ` Simon Wright
  2016-06-03 20:30         ` Luke A. Guest
  0 siblings, 1 reply; 13+ messages in thread
From: Simon Wright @ 2016-06-03 18:45 UTC (permalink / raw)


Lucretia <laguest9000@googlemail.com> writes:

> Interesting, I tried this and dumped the asm, similar there, but as
> soon as you add more code, it just gets confusing, because it never
> references the actual label for the string anywhere.
[...]
>    S3 : constant C.char_array := "hello" & C.char'Val (0);
> begin
> --     Put_Line (C.To_Ada (S3));

You've commented out the only place where the variable is referenced --
here, it made up a label (LC1) and referenced that.


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

* Re: no code generation for c strings
  2016-06-03 18:45       ` Simon Wright
@ 2016-06-03 20:30         ` Luke A. Guest
  2016-06-03 21:25           ` Simon Wright
  0 siblings, 1 reply; 13+ messages in thread
From: Luke A. Guest @ 2016-06-03 20:30 UTC (permalink / raw)


Simon Wright <simon@pushface.org> wrote:
> Lucretia <laguest9000@googlemail.com> writes:
> 
>> Interesting, I tried this and dumped the asm, similar there, but as
>> soon as you add more code, it just gets confusing, because it never
>> references the actual label for the string anywhere.
> [...]
>> S3 : constant C.char_array := "hello" & C.char'Val (0);
>> begin
>> --     Put_Line (C.To_Ada (S3));
> 
> You've commented out the only place where the variable is referenced --
> here, it made up a label (LC1) and referenced that.
> 

Yeah that was on purpose as it produced a lot of stuff that complicated
looking at it, in hindsight I probably should've just done an assignment,
might've produced less. 

I'm still not sure why your way of doing this produced the desired effect
though.


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

* Re: no code generation for c strings
  2016-06-03 20:30         ` Luke A. Guest
@ 2016-06-03 21:25           ` Simon Wright
  2016-06-03 21:33             ` Luke A. Guest
  0 siblings, 1 reply; 13+ messages in thread
From: Simon Wright @ 2016-06-03 21:25 UTC (permalink / raw)


Luke A. Guest <laguest@archeia.com> writes:

> Simon Wright <simon@pushface.org> wrote:
>> Lucretia <laguest9000@googlemail.com> writes:
>> 
>>> Interesting, I tried this and dumped the asm, similar there, but as
>>> soon as you add more code, it just gets confusing, because it never
>>> references the actual label for the string anywhere.
>> [...]
>>> S3 : constant C.char_array := "hello" & C.char'Val (0);
>>> begin
>>> --     Put_Line (C.To_Ada (S3));
>> 
>> You've commented out the only place where the variable is referenced --
>> here, it made up a label (LC1) and referenced that.
>> 
>
> Yeah that was on purpose as it produced a lot of stuff that complicated
> looking at it, in hindsight I probably should've just done an assignment,
> might've produced less. 
>
> I'm still not sure why your way of doing this produced the desired effect
> though.

Possibly because it was in a spec? Yours was in a library-level
subprogram, so there would have been no need for a related symbol?


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

* Re: no code generation for c strings
  2016-06-03 21:25           ` Simon Wright
@ 2016-06-03 21:33             ` Luke A. Guest
  0 siblings, 0 replies; 13+ messages in thread
From: Luke A. Guest @ 2016-06-03 21:33 UTC (permalink / raw)


Simon Wright <simon@pushface.org> wrote:
> Luke A. Guest <laguest@archeia.com> writes:

>> I'm still not sure why your way of doing this produced the desired effect
>> though.
> 
> Possibly because it was in a spec? Yours was in a library-level
> subprogram, so there would have been no need for a related symbol?
> 

No, I mean I don't understand why this:

S : constant char_array := "hello" & char'val(0);

Produces the statically allocated string without using the secondary stack.
I know that using to_c will utilise it. Unsurprised the compiler doesn't
think that string is of type string. I tried something similar without the
end from the ampersand onwards and it complained of different types.



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

* Re: no code generation for c strings
  2016-06-03 17:00         ` Lucretia
@ 2016-06-04  6:31           ` Georg Bauhaus
  0 siblings, 0 replies; 13+ messages in thread
From: Georg Bauhaus @ 2016-06-04  6:31 UTC (permalink / raw)


On 03.06.16 19:00, Lucretia wrote:
> On Friday, 3 June 2016 15:27:59 UTC+1, G.B.  wrote:
>
>> If "&" will be become a function call, then
>>
>>    S : constant char_array := ('h', 'e', 'l', 'l', 'o', nul);
>
> I want to avoid having to spell out all these function names like that thanks, and not really sure what you mean about the & there.
>

By RM4.9(6) and RM4.9(20), "&" might be a static function. I'm not sure.
In any case, for S to become static, adding constraints seems to be sufficient:

package Static is
    S  : constant char_array          := "hello" & nul;
    S1 : constant Char_Array (0 .. 5) := "hello" & nul;
    
    X  : constant := S'Length;
    X1 : constant := S1'Length;
end Static;


      1. with Interfaces.C; use Interfaces.C;
      2.
      3. package Static is
      4.    S  : constant char_array          := "hello" & nul;
      5.    S1 : constant Char_Array (0 .. 5) := "hello" & nul;
      6.
      7.    X  : constant := S'Length;
                             |
         >>> non-static expression used in number declaration
         >>> prefix is non-static array (RM 4.9(8))

      8.    X1 : constant := S1'Length;
      9. end Static;


-- 
"HOTDOGS ARE NOT BOOKMARKS"
Springfield Elementary teaching staff

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

end of thread, other threads:[~2016-06-04  6:31 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-03  8:14 no code generation for c strings Luke A. Guest
2016-06-03  8:25 ` Dmitry A. Kazakov
2016-06-03  9:23   ` Luke A. Guest
2016-06-03  9:37     ` Dmitry A. Kazakov
2016-06-03 14:27       ` G.B.
2016-06-03 17:00         ` Lucretia
2016-06-04  6:31           ` Georg Bauhaus
2016-06-03 16:30   ` Simon Wright
2016-06-03 17:04     ` Lucretia
2016-06-03 18:45       ` Simon Wright
2016-06-03 20:30         ` Luke A. Guest
2016-06-03 21:25           ` Simon Wright
2016-06-03 21:33             ` Luke A. Guest

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