简体   繁体   English

用gcc扩展c编程语言

[英]extending the c programming language with gcc

I want to write my own programming language as an extension of the c programming language. 我想编写自己的编程语言作为c编程语言的扩展。 The entire programming language that I am inventing are simply shorthands which translate to valid c code. 我正在发明的整个编程语言只是简单的转换为有效的c代码。 For example: 例如:

namespace TcpConnection {
    void* connect(char *addr)
}

would translate to: 会转化为:

void* TcpConnection_connect(char *addr)

All that is done is a simple name replacement. 所做的一切都是简单的名称替换。 This is only one example of an extension which I want to provide. 这只是我想提供的扩展的一个例子。 Another simple extension would be function overloading (this would concatenate to the end of the function name the types of its arguments. 另一个简单的扩展是函数重载(这将连接到函数名称末尾的参数类型。

In any case, the result is perfectly valid C code. 在任何情况下,结果都是完全有效的C代码。 Is there any way to do this without going into gcc code? 有没有办法在不进入gcc代码的情况下这样做?

You could write a preprocessor that parses your language and compiles it to C, which is then passed on to gcc. 您可以编写一个预处理器来解析您的语言并将其编译为C,然后将其传递给gcc。 This is how early implementations of C++ worked. 这就是C ++早期实现的工作原理。 However, you may prefer to hack on LLVM's clang, which is designed to support several C-family languages and, as part of LLVM, is also designed to be more modular and easier to extend. 但是,您可能更喜欢破解LLVM的clang,它旨在支持多种C系列语言,并且作为LLVM的一部分,其设计也更加模块化,更易于扩展。

For prototyping, maybe just go with a preprocessor written in the language of your choice (C, Perl, Python...) and then build it into your Makefile rules. 对于原型设计,可能只需要使用您选择的语言编写的预处理器(C,Perl,Python ...),然后将其构建到Makefile规则中。 Just to get an easy, low-cost way to try it all out... 只是为了获得一个简单,低成本的方式来尝试一切......

Use a different file extension, and turn .foo into .c. 使用不同的文件扩展名,然后将.foo转换为.c。

You could hack something on top of http://cil.sourceforge.net/ 你可以在http://cil.sourceforge.net/上破解一些东西

Clang is another option, but it's not the most useful code rewriting tool, and modifying its parser frontend is not that easy. Clang是另一种选择,但它不是最有用的代码重写工具,修改其解析器前端并不容易。

I did something similar to embed an assembly language for a custom bytecode format, using some C99 macro magic and Perl. 我做了类似的事情,使用一些C99宏魔法和Perl为自定义字节码格式嵌入汇编语言。

The macros

#define x3_pragma_(...) _Pragma(#__VA_ARGS__)
#define x3_asm(...) ((const struct x3instruction []){ \
    x3_pragma_(X3 ASM __VA_ARGS__) \
})

transform 转变

x3_asm(exit = find val(0))

into

((const struct x3instruction []){
#pragma X3 ASM exit = find val(0)
})

which gets piped through a Perl script to get 通过Perl脚本获取管道

((const struct x3instruction []){
{ { { X3_OPFINDVAL, { .as_uint = (0) } }, { X3_OPEXIT, { 0 } } } },
})

A sample invocation of gcc and perl would look like this: gcc和perl的示例调用如下所示:

gcc -E foo.c | perl x3pp.pl | gcc -o foo.o -x c -

It's more complicated than stricly necessary, but I found it beneficial that the C preprocessor runs before my custom preprocessor, and I also liked that by using pragmas, the source stays legal C. 它比严格必要的更复杂,但我发现C预处理器在我的自定义预处理器之前运行是有益的,而且我也喜欢使用编译指示,源保持合法C.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM