mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 12:33:42 +00:00
cgo: add support for bitfields using generated getters and setters
This commit is contained in:
committed by
Ron Evans
parent
23c8d15847
commit
1d7cc2c242
Vendored
+1
@@ -18,6 +18,7 @@ short globalArray[3] = {5, 6, 7};
|
||||
joined_t globalUnion;
|
||||
int globalUnionSize = sizeof(globalUnion);
|
||||
option_t globalOption = optionG;
|
||||
bitfield_t globalBitfield = {244, 15, 1, 2, 47, 5};
|
||||
|
||||
int fortytwo() {
|
||||
return 42;
|
||||
|
||||
Vendored
+13
@@ -64,6 +64,11 @@ func main() {
|
||||
println("union global data:", C.globalUnion.data[0], C.globalUnion.data[1], C.globalUnion.data[2])
|
||||
println("union field:", printUnion(C.globalUnion).f)
|
||||
var _ C.union_joined = C.globalUnion
|
||||
printBitfield(&C.globalBitfield)
|
||||
C.globalBitfield.set_bitfield_a(7)
|
||||
C.globalBitfield.set_bitfield_b(0)
|
||||
C.globalBitfield.set_bitfield_c(0xff)
|
||||
printBitfield(&C.globalBitfield)
|
||||
|
||||
// elaborated type
|
||||
p := C.struct_point{x: 3, y: 5}
|
||||
@@ -108,3 +113,11 @@ func printUnion(union C.joined_t) C.joined_t {
|
||||
func mul(a, b C.int) C.int {
|
||||
return a * b
|
||||
}
|
||||
|
||||
func printBitfield(bitfield *C.bitfield_t) {
|
||||
println("bitfield a:", bitfield.bitfield_a())
|
||||
println("bitfield b:", bitfield.bitfield_b())
|
||||
println("bitfield c:", bitfield.bitfield_c())
|
||||
println("bitfield d:", bitfield.d)
|
||||
println("bitfield e:", bitfield.e)
|
||||
}
|
||||
|
||||
Vendored
+12
@@ -65,6 +65,17 @@ typedef enum {
|
||||
option3A = 21,
|
||||
} option3_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned char start;
|
||||
unsigned char a : 5;
|
||||
unsigned char b : 1;
|
||||
unsigned char c : 2;
|
||||
unsigned char :0; // new field
|
||||
unsigned char d : 6;
|
||||
unsigned char e : 3;
|
||||
// Note that C++ allows bitfields bigger than the underlying type.
|
||||
} bitfield_t;
|
||||
|
||||
// test globals and datatypes
|
||||
extern int global;
|
||||
extern int unusedGlobal;
|
||||
@@ -85,6 +96,7 @@ extern short globalArray[3];
|
||||
extern joined_t globalUnion;
|
||||
extern int globalUnionSize;
|
||||
extern option_t globalOption;
|
||||
extern bitfield_t globalBitfield;
|
||||
|
||||
// test duplicate definitions
|
||||
int add(int a, int b);
|
||||
|
||||
Vendored
+10
@@ -31,6 +31,16 @@ union local data: 5 8 1
|
||||
union s: true
|
||||
union f: +6.280000e+000
|
||||
union field: +6.280000e+000
|
||||
bitfield a: 15
|
||||
bitfield b: 1
|
||||
bitfield c: 2
|
||||
bitfield d: 47
|
||||
bitfield e: 5
|
||||
bitfield a: 7
|
||||
bitfield b: 0
|
||||
bitfield c: 3
|
||||
bitfield d: 47
|
||||
bitfield e: 5
|
||||
struct: 3 5
|
||||
n in chain: 3
|
||||
n in chain: 6
|
||||
|
||||
Reference in New Issue
Block a user