#ifndef _ENTRY
#define _ENTRY
#include <cstdint>
#include "ap_int.h"
struct US0 {
ap_uint<1> tag;
union {
struct {
ap_uint<2l> v0;
} case1; // Some
};
};
US0 US0_0() { // None
US0 x;
x.tag = 0;
return x;
}
US0 US0_1(ap_uint<2l> v0) { // Some
US0 x;
x.tag = 1;
x.case1.v0 = v0;
return x;
}
int32_t entry(){
ap_uint<2l> v0;
v0 = 0;
US0 v1;
v1 = US0_1(v0);
return 0l;
}
#endif
我正在致力于将函数式语言编译到 FPGA 的 C++ 后端,经过多次调试,我终于意识到编译器拒绝将
ap_uint
放入联合中。如果我尝试编译这样的程序,我会在控制台中收到很多已删除的类型错误。
../../../../hello.hpp: In function 'US0 US0_0()':
../../../../hello.hpp:22:9: error: use of deleted function 'US0::US0()'
US0 x;
^
../../../../hello.hpp:13:8: note: 'US0::US0()' is implicitly deleted because the default definition would be ill-formed:
struct US0 {
^~~
../../../../hello.hpp:18:11: error: union member 'US0::<anonymous union>::case1' with non-trivial 'US0::<anonymous union>::<anonymous struct>::<constructor>()'
} case1; // Some
^~~~~
如何调整 ap 类型,以便它们可以在联合类型中使用。
#ifndef _ENTRY
#define _ENTRY
#include <cstdint>
#include "ap_int.h"
struct US0 {
ap_uint<1> tag;
union U {
struct {
ap_uint<2l> v0;
} case1; // Some
U() {}
} v;
US0() {}
US0(const US0 && x) {
this->tag = x.tag;
this->v.case1 = x.v.case1;
}
US0 & operator=(US0 & x) {
this->tag = x.tag;
this->v.case1 = x.v.case1;
return *this;
}
US0 & operator=(US0 && x) {
this->tag = x.tag;
this->v.case1 = x.v.case1;
return *this;
}
};
US0 US0_0() { // None
US0 x;
x.tag = 0;
return x;
}
US0 US0_1(ap_uint<2l> v0) { // Some
US0 x;
x.tag = 1;
x.v.case1.v0 = v0;
return x;
}
int32_t entry(){
ap_uint<2l> v0;
v0 = 0;
US0 v1;
v1 = US0_1(v0);
return 0l;
}
#endif
理查德在评论中的建议是正确的。使其工作的方法是定义各种构造函数并为此数据类型分配运算符。下面是一个如何完成此操作的示例。下一步是使它们自动化。