gpt4 book ai didi

ada - 将结构/记录从汇编器传递到 Ada

转载 作者:行者123 更新时间:2023-12-02 07:50:47 25 4
gpt4 key购买 nike

我正在尝试将结构从 (x86) 汇编器传递到堆栈上的 Ada。我已经能够在 C 中成功地使用此模式来接受将从程序集传递的大量参数包装在结构内,我想知道这在 Ada 中是否也能以类似的方式工作。

这是一个(人为的、最小的)示例:

当我这样做时,调试被调用者显示传递的记录包含未初始化的数据。尽管有导出指令,Ada 似乎还是以不同的方式解释 C 调用约定。RM 包含有关从 Ada 向 C 传递结构的信息,表示它将自动将记录作为指针类型传递,但反之似乎并非如此。如果您接受单个 access 类型,它将简单地填充堆栈上的第一个值,正如 cdecl 所期望的那样。

(请原谅任何小错误,这不是我的实际代码。)

#####################################################################
# Caller
#
# This pushes the values onto the stack and calls the Ada function
#####################################################################
.global __example_function
.type __example_function, @function
__example_function:
push $1
push $2
push $3
push $4
call accepts_struct
ret
----------------------------------------------------------------------------
-- Accepts_Struct
--
-- Purpose:
-- Attempts to accept arguments pass on the stack as a struct.
----------------------------------------------------------------------------
procedure Accepts_Struct (
Struct : Struct_Passed_On_Stack
)
with Export,
Convention => C,
External_Name => "accepts_struct";

----------------------------------------------------------------------------
-- Ideally the four variables passed on the stack would be accepted as
-- the values of this struct.
----------------------------------------------------------------------------
type Struct_Passed_On_Stack is
record
A : Unsigned_32;
B : Unsigned_32;
C : Unsigned_32;
D : Unsigned_32;
end record
with Convention => C;

另一方面,这工作得很好:

procedure Accepts_Struct (
A : Unsigned_32;
B : Unsigned_32;
C : Unsigned_32;
D : Unsigned_32
)
with Export,
Convention => C,
External_Name => "accepts_struct";

在这个最小的情况下这不是什么大问题,但如果我传递 16 个或更多变量,那就有点麻烦了。如果您想知道我为什么要这样做,它是一个异常处理程序,处理器自动将变量传递到堆栈上以显示寄存器状态。

如果您能提供任何帮助,我们将不胜感激。

最佳答案

记录版本不起作用,因为记录未存储在堆栈中。相反,4 个 Unsigned_32 元素存储在堆栈上。如果您确实想使用记录而不是四个单独的无符号整数值,您可以在调用“accepts_struct”时将这四个值分配给记录的成员。Ada 期望堆栈中的第一个条目是一条记录,而不是 unsigned_32。Ada LRM 第 6.4.1 节规定:

For the evaluation of a parameter_association: The actual parameter is first evaluated. For an access parameter, the access_definition is elaborated, which creates the anonymous access type. For a parameter (of any mode) that is passed by reference (see 6.2), a view conversion of the actual parameter to the nominal subtype of the formal parameter is evaluated, and the formal parameter denotes that conversion. For an in or in out parameter that is passed by copy (see 6.2), the formal parameter object is created, and the value of the actual parameter is converted to the nominal subtype of the formal parameter and assigned to the formal.

另外,参数的传递方式在6.2节中描述:

6.2 Formal Parameter Modes

A parameter_specification declares a formal parameter of mode in, in out, or out. Static Semantics

A parameter is passed either by copy or by reference. When a parameter is passed by copy, the formal parameter denotes a separate object from the actual parameter, and any information transfer between the two occurs only before and after executing the subprogram. When a parameter is passed by reference, the formal parameter denotes (a view of) the object denoted by the actual parameter; reads and updates of the formal parameter directly reference the actual parameter object.

A type is a by-copy type if it is an elementary type, or if it is a descendant of a private type whose full type is a by-copy type. A parameter of a by-copy type is passed by copy, unless the formal parameter is explicitly aliased.

A type is a by-reference type if it is a descendant of one of the following:

a tagged type;

a task or protected type;

an explicitly limited record type;

a composite type with a subcomponent of a by-reference type;

a private type whose full type is a by-reference type.

A parameter of a by-reference type is passed by reference, as is an explicitly aliased parameter of any type. Each value of a by-reference type has an associated object. For a parenthesized expression, qualified_expression, or type_conversion, this object is the one associated with the operand. For a conditional_expression, this object is the one associated with the evaluated dependent_expression.

For other parameters, it is unspecified whether the parameter is passed by copy or by reference.

看来您的编译器正在尝试通过引用而不是通过复制来传递结构。在 C 中,所有参数都是通过复制传递的。

关于ada - 将结构/记录从汇编器传递到 Ada,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58403903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com