可以引用函数返回类型的属性吗?

发布于 2024-12-29 16:52:30 字数 1317 浏览 0 评论 0原文

我想知道是否可以以某种方式从返回不受约束类型的函数内部引用返回类型/值的属性。 (约束是否会传播到函数中?有些东西告诉我它们不会,并且返回类型的约束是由函数内实际返回的内容决定的)例如:

function f return std_logic_vector is begin
    -- how do we access attributes of what we are returning?
    -- "return_type" is a placeholder for it here, it would be nice if
    -- this information propagated up into the function based on the way
    -- the function's result is used when invoked
    return (return_type'high - 2 downto return_type'low + 1 => '0', others => '1');
end function;
signal x: std_logic_vector(7 downto 0);
signal y: std_logic_vector(11 downto 0);
[...]
x <= f; -- expect x = "11000001"
y <= f; -- expect y = "110000000001"

我知道这可以通过传递来破解函数所需类型的东西作为参数,就像这样,但如果可能的话我想避免它:

function f(hint: std_logic_vector) return std_logic_vector is
    variable retval: std_logic_vector(hint'range);
begin
    retval := (hint'high - 2 downto hint'low + 1 => '0', others => '1');
    return retval;
end function;
signal x: std_logic_vector(7 downto 0);
signal y: std_logic_vector(11 downto 0);
[...]
x <= f(x); -- x = "11000001"
y <= f(y); -- y = "110000000001"

如果重要的话,我正在使用 Quartus 并且这需要综合。第二个代码块应该可以正常工作(假设我没有犯任何错误),但我想知道是否有更好的方法来完成此任务。

请注意,这些都是人为的示例,当然还有更简单的方法来分配这些值;我问的是,一般来说是否有一种很好的方法来避免传递额外参数的黑客行为。

I'm wondering if it's possible to somehow reference attributes of the return type/value from inside a function returning an unconstrained type. (Do the constraints even propagate up into the function at all? Something tells me they don't, and that the return type's constraints are determined by whatever is actually returned inside the function) For example:

function f return std_logic_vector is begin
    -- how do we access attributes of what we are returning?
    -- "return_type" is a placeholder for it here, it would be nice if
    -- this information propagated up into the function based on the way
    -- the function's result is used when invoked
    return (return_type'high - 2 downto return_type'low + 1 => '0', others => '1');
end function;
signal x: std_logic_vector(7 downto 0);
signal y: std_logic_vector(11 downto 0);
[...]
x <= f; -- expect x = "11000001"
y <= f; -- expect y = "110000000001"

I know this could be hacked around by passing something of desired type to the function as a parameter, like so, but I'd like to avoid it if possible:

function f(hint: std_logic_vector) return std_logic_vector is
    variable retval: std_logic_vector(hint'range);
begin
    retval := (hint'high - 2 downto hint'low + 1 => '0', others => '1');
    return retval;
end function;
signal x: std_logic_vector(7 downto 0);
signal y: std_logic_vector(11 downto 0);
[...]
x <= f(x); -- x = "11000001"
y <= f(y); -- y = "110000000001"

If it matters, I'm using Quartus and this needs to synthesize. The second block of code SHOULD work fine (assuming I haven't made any mistakes), but I wonder if there is a better way to accomplish this.

Note that these are contrived examples and of course there are much simpler ways to assign those values; I am asking if in general there is a nice way to avoid the hack of passing an extra parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回梦里 2025-01-05 16:52:30

使用无约束数组作为返回类型,您的函数负责决定它返回什么,通常以某种方式基于输入的范围。返回范围可能与您的输入范围相同,或者如果需要也可以不同(也许您正在连接多个数组,增加位宽以避免结果溢出,或者其他什么)。

在你设计的示例中你不能轻易地做到这一点,因为你的函数没有输入......我想这会使其成为一个常量。 :)

如果你的代码发生变化,你需要做什么就更明显了:

-- from:
x <= f; 

-- to:
x <= f(x);

With an unconstrained array as a return type, your function is responsible for deciding what it returns, typically based in some way on the range(s) of your input(s). The return range may be the same as your input ranges, or different if desired (perhaps you are concatenating multiple arrays, increasing bit-width to avoid overflow of the result, or whatever).

You can't easily do this in your contrived example because your function has no inputs...which I guess would make it a constant. :)

It's a little more obvious what you need to do if your code changes:

-- from:
x <= f; 

-- to:
x <= f(x);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文