forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support read/write to structs originating in C
- Loading branch information
Showing
5 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from ltypes import (i8, dataclass, i32, f32, c32, f64, i16, i64, c64, | ||
ccall, CPtr, c_p_pointer, Pointer) | ||
|
||
@dataclass | ||
class buffer_struct: | ||
buffer8: i8 | ||
buffer1: i32 | ||
buffer2: f32 | ||
buffer3: c32 | ||
buffer4: f64 | ||
buffer5: i16 | ||
buffer6: i64 | ||
buffer7: c64 | ||
|
||
@ccall | ||
def get_buffer() -> CPtr: | ||
pass | ||
|
||
@ccall | ||
def fill_buffer(buffer_cptr: CPtr): | ||
pass | ||
|
||
def f(): | ||
b: CPtr = get_buffer() | ||
pb: Pointer[buffer_struct] = c_p_pointer(b, buffer_struct) | ||
pb.buffer8 = i8(3) | ||
pb.buffer1 = i32(4) | ||
pb.buffer2 = f32(5) | ||
pb.buffer3 = c32(9) | ||
pb.buffer4 = f64(6) | ||
pb.buffer5 = i16(7) | ||
pb.buffer6 = i64(8) | ||
pb.buffer7 = c64(10) | ||
print(pb.buffer8) | ||
print(pb.buffer1) | ||
print(pb.buffer2) | ||
print(pb.buffer3) | ||
print(pb.buffer4) | ||
print(pb.buffer5) | ||
print(pb.buffer6) | ||
print(pb.buffer7) | ||
assert pb.buffer8 == i8(3) | ||
assert pb.buffer1 == i32(4) | ||
assert pb.buffer2 == f32(5) | ||
assert pb.buffer3 == c32(9) | ||
assert pb.buffer4 == f64(6) | ||
assert pb.buffer5 == i16(7) | ||
assert pb.buffer6 == i64(8) | ||
assert pb.buffer7 == c64(10) | ||
|
||
fill_buffer(b) | ||
print(pb.buffer8) | ||
print(pb.buffer1) | ||
print(pb.buffer2) | ||
print(pb.buffer3) | ||
print(pb.buffer4) | ||
print(pb.buffer5) | ||
print(pb.buffer6) | ||
print(pb.buffer7) | ||
assert pb.buffer8 == i8(8) | ||
assert pb.buffer1 == i32(9) | ||
assert pb.buffer2 == f32(10) | ||
assert pb.buffer3 == c32(14) + c32(15j) | ||
assert pb.buffer4 == f64(11) | ||
assert pb.buffer5 == i16(12) | ||
assert pb.buffer6 == i64(13) | ||
assert pb.buffer7 == c64(16) + c64(17j) | ||
|
||
|
||
f() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <stdlib.h> | ||
#include <complex.h> | ||
#include "structs_19b.h" | ||
|
||
|
||
struct buffer_c { | ||
int8_t buffer8; | ||
int32_t buffer1; | ||
float buffer2; | ||
float complex buffer3; | ||
double buffer4; | ||
int16_t buffer5; | ||
int64_t buffer6; | ||
double complex buffer7; | ||
}; | ||
|
||
void fill_buffer(void* buffer_cptr) { | ||
struct buffer_c* buffer_clink_ = (struct buffer_c*) buffer_cptr; | ||
buffer_clink_->buffer8 = 8; | ||
buffer_clink_->buffer1 = 9; | ||
buffer_clink_->buffer2 = 10.0; | ||
buffer_clink_->buffer3 = CMPLXF(14.0, 15.0); | ||
buffer_clink_->buffer4 = 11.0; | ||
buffer_clink_->buffer5 = 12; | ||
buffer_clink_->buffer6 = 13; | ||
buffer_clink_->buffer7 = CMPLXL(16.0, 17.0); | ||
} | ||
|
||
void* get_buffer() { | ||
return malloc(sizeof(struct buffer_c)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include <inttypes.h> | ||
|
||
void fill_buffer(void* buffer_cptr); | ||
void* get_buffer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters