[fpc-pascal] Question about functions returning a string

Sven Barth pascaldragon at googlemail.com
Sat Nov 29 14:46:53 CET 2014


On 29.11.2014 13:56, Ewald wrote:
> On 11/29/2014 12:41 AM, Flávio Etrusco wrote:
>> Are you sure it's not the result that is passed in the first parameter?
>>
>> http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl
>
> Good question, but I don't think so. The first few lines of assembly
> from this function seem to read three arguments you see:
>
>    44b360:    53                       push   %rbx
>    44b361:    41 54                    push   %r12
>    44b363:    41 55                    push   %r13
>    44b365:    48 89 fb                 mov    %rdi,%rbx        <-- first
> argument, %rdi
>    44b368:    41 89 f4                 mov    %esi,%r12d       <-- second
> argument, %rsi
>    44b36b:    49 89 d5                 mov    %rdx,%r13        <-- third
> argument, %rdx
>
> What I forgot to mention was that this is on a x86_64 architecture, so I
> based myself on the table found at
> http://wiki.osdev.org/Calling_Conventions .
>

Just compile with "-al" and you'll see what the compiler uses the 
parameters for (the following example is for i386):

=== listing begin ===

.section .text
	.balign 16,0x90
.globl	P$TCALLCONV_SOMEFUNCTION$PCHAR$LONGWORD$$ANSISTRING
	.type	P$TCALLCONV_SOMEFUNCTION$PCHAR$LONGWORD$$ANSISTRING, at function
P$TCALLCONV_SOMEFUNCTION$PCHAR$LONGWORD$$ANSISTRING:
.Lc1:
# Temps allocated between ebp-524 and ebp-12
# [callconv.pp]
# [6] begin
	pushl	%ebp
.Lc3:
.Lc4:
	movl	%esp,%ebp
.Lc5:
	subl	$524,%esp
# Var aData located at ebp-4
# Var aLen located at ebp-8
# Var $result located at ebp-12
	movl	%eax,-4(%ebp)
	movl	%edx,-8(%ebp)
	movl	%ecx,-12(%ebp)
# [7] Result := Copy(StrPas(aData), 1, aLen);
	leal	-268(%ebp),%eax
	pushl	%eax
	leal	-524(%ebp),%edx
	movl	-4(%ebp),%eax
	call	SYSTEM_STRPAS$PCHAR$$SHORTSTRING
	leal	-524(%ebp),%eax
	movl	-8(%ebp),%ecx
	movl	$1,%edx
	call	fpc_shortstr_copy
	leal	-268(%ebp),%eax
	movl	-12(%ebp),%edx
	call	fpc_shortstr_to_ansistr
# [8] end;
	leave
	ret
.Lc2:
.Le0:
	.size	P$TCALLCONV_SOMEFUNCTION$PCHAR$LONGWORD$$ANSISTRING, .Le0 - 
P$TCALLCONV_SOMEFUNCTION$PCHAR$LONGWORD$$ANSISTRING

=== listing end ===

In general managed types (which a (Ansi)String is) are passed as parameters.

Regards,
Sven



More information about the fpc-pascal mailing list