Wednesday, February 04, 2009

Using C to talk to C

I've released the first version of a PLaneT package for working with C. It's based on an idea I got from Felix Klock's Scheme Workshop 2008 paper: rather than try to do manual pointer arithmetic based on the current architecture's ABI, you can find out byte offsets of data structures by creating a C program that tells you what they should be. So that's what c.plt allows you to do; using a system-installed C compiler, you can generate a representation of the binary layouts of data structures. Here's a sample interaction:
> (define time.h
(make-header
#reader (planet dherman/c/reader) {
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
}))
> (define time-abi
(compile-header time.h
(system-compiler #:include<> '("time.h") gcc)))
> (layout-size (time-abi 'tm))
36
> (layout-offset (time-abi 'tm) 'tm_sec)
0
> (layout-offset (time-abi 'tm) 'tm_year)
20

3 comments:

Ben Simon said...

That's a really slick approach.

I'll have to grab the code and poke around with it...

-Ben

Dave Herman said...

Thanks, Ben. All credit to Felix Klock for the cool idea.

There are lots of limitations and rough edges in the code, so feel free to poke me if there are particular areas you'd like to see improved.

Geek Squad said...

https://www.trover.com/u/geeksquadsr
https://git.iws.uni-stuttgart.de/geeksquadsr
https://www.trendiee.com/members/geeksquadsr/profile/classic/
https://atnd.org/users/337279
https://loop.frontiersin.org/people/814345/bio
https://www.interesante.com/interes/618130
https://enterprisedt.com/questions/index.php/user/geeksquadsr
http://www.thereminworld.com/user/geeksquadsr
https://www.panbo.com/user/geeksquadsr/
https://www.photovoltaikforum.com/core/user/113322-geeksquadsr/#about