comp.lang.ada
 help / color / mirror / Atom feed
From: Lucretia <laguest9000@googlemail.com>
Subject: Re: no code generation for c strings
Date: Fri, 3 Jun 2016 10:04:56 -0700 (PDT)
Date: 2016-06-03T10:04:56-07:00	[thread overview]
Message-ID: <718de74d-ab15-4eee-983f-b78d8f8070f1@googlegroups.com> (raw)
In-Reply-To: <ly8tymqlpn.fsf@pushface.org>

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

  reply	other threads:[~2016-06-03 17:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
replies disabled

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