[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C
From: |
Gaius Mulley |
Subject: |
Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C |
Date: |
Tue, 30 Jan 2024 15:39:24 +0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) |
Benjamin Kowarsch <trijezdci@gmail.com> writes:
> On Tue, 30 Jan 2024 at 22:55, Gaius Mulley wrote:
>
> > void p ( char str[], unsigned argc );
>
> just to confirm the prototype above is correct for gm2 (for both VAR and
> non VAR open array parameters)
>
> Gaius, how do you handle write access to a non-VAR open array parameter?
>
Hi Benjamin,
> Do you copy the array into temporary local storage into the local
> variable section on the stack and put a pointer to that into the
> contents field of the unbounded record,
yes it uses callee save (via alloca).
In gcc/m2/gm2-compiler/M2GenGCC.mod:1808
When creating the procedure prologue it checks whether the non var open
arrays must be saved:
(* callee saves non var unbounded parameter contents *)
SaveNonVarUnboundedParameters (tokenno, CurrentProcedure) ;
and eventually calls MakeCopyUse (which in turn performs alloca,
memcpy and assignment of the new pointer). It performs compile time
(and runtime) checking to see whether this is necessary
For clarity the compile time checking aggressively examines any write to
the array (or any use of ADR on the array etc) in which case it is
considered as written. It also generate runtime code to check for all
unbounded parameters, for example:
PROCEDURE StrConCat (VAR result: ARRAY OF CHAR; left, right: ARRAY OF CHAR) ;
in the prologue it checks to see if the block of content used by result
overlaps with left and right. Whether left overlaps with right etc
and if they overlap it will alloca/memcpy as appropriate.
So this allows for:
PROCEDURE StrLen (a: ARRAY OF CHAR) : CARDINAL ;
PROCEDURE WriteString (a: ARRAY OF CHAR) ;
VAR
result,
left,
right: ARRAY [0..20] OF CHAR ;
BEGIN
left := 'hello ' ;
right := 'world' ;
StrConCat (result, left, right) ;
WriteString (result)
to run without the need for any alloca/memcpy callee save.
But if the code were:
VAR
result,
right : ARRAY [0..20] OF CHAR ;
BEGIN
result := 'hello ' ;
right := 'world' ;
StrConCat (result, result, right) ; (* Would generate
alloca/memcpy. *)
WriteString (result)
then it would callee save result (2nd param) inside StrConCat
regards,
Gaius
Re: Passing parameters of type 'ARRAY[0..MAXINDEX] OF ElementType' to C, Gaius Mulley, 2024/01/29