Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.compilers > #1171
| From | noitalmost <noitalmost@cox.net> |
|---|---|
| Newsgroups | comp.compilers |
| Subject | Trouble implementing a standard library -- Calling C functions from within my language |
| Date | 2014-03-19 18:28 -0400 |
| Organization | Compilers Central |
| Message-ID | <14-03-049@comp.compilers> (permalink) |
I need some small-scale examples, like a toy compiler would do if it
needed access to some libc functions.
Let's say I have a module (library) called Std that provides
procedure ln (x : float) float;
procedure open(fname : string; mode : int) int;
procedure read(fd : int; buf : ^byte; cnt : int) bool;
On a Linux system, I want to just wrap the libc functions
double log(double x);
int open(const char *fname, int mode);
ssize_t read(int fd, void *buf, int cnt);
In prior projects, I always output assembly and just did system calls.
This time it's different. First, I have an intermediate form that can
produce C source code. Second, I want to be able to access functions
(say from math.h) that aren't Linux system calls.
I don't think the intermediate form matters for this discussion. It
can be assumed that module Std gets parsed into an AST, and that I can
walk the AST to generate C code.
How is this usually handled? I could put a CCall node in my AST and have the
parser recognize
procedure ln (x : float) :
return CCall( ln, x );
end ln;
And then have
class CCall : public Expr
{
CCall(string name, vector<Expr*> params);
string genC();
...
}
where, Expr is a AstNode. But CCall::genC() would be a really
complicated list of special cases. Maybe I also need to pass the C
return type and the C types of the parameters? And I'm on my way
toward an Ada-style foreign-function interface. Eek! Isn't there a
simpler way?
Back to comp.compilers | Previous | Next — Next in thread | Find similar | Unroll thread
Trouble implementing a standard library -- Calling C functions from within my language noitalmost <noitalmost@cox.net> - 2014-03-19 18:28 -0400 Re: Trouble implementing a standard library -- Calling C functions from within my language Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2014-03-20 03:37 +0100 Re: Trouble implementing a standard library -- Calling C functions from within my language alexfrunews@gmail.com - 2014-03-19 21:41 -0700 Re: Trouble implementing a standard library -- Calling C functions from within my language noitalmost <noitalmost@cox.net> - 2014-03-21 14:49 -0400
csiph-web