Python脚本自动生成C

313 views
Skip to first unread message

none_nobody

unread,
Apr 15, 2014, 10:52:23 AM4/15/14
to sh...@googlegroups.com
传说 : 【好奇号代码共250万行,程序语言是C,多是用Python脚本自动生成。】

这么强悍的蟒蛇,还要我这种代码狗做什么呢?


有谁知道如何用 python 自动生成 C 的?

要是编辑环境像 scratch 那样写代码也好啊。

Xu Xin

unread,
Apr 15, 2014, 11:47:40 AM4/15/14
to sh...@googlegroups.com
Cython 之类的吧,当然 NASA 有啥其他的黑科技也说不定。

印象中好奇号跑的 VxWorks?
- Xin
> --
> -- You received this message because you are subscribed to the Google Groups
> Shanghai Linux User Group group. To post to this group, send email to
> sh...@googlegroups.com. To unsubscribe from this group, send email to
> shlug+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/shlug?hl=zh-CN
> ---
> 您收到此邮件是因为您订阅了Google网上论坛中的“Shanghai Linux User Group”论坛。
> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
> 要查看更多选项,请访问https://groups.google.com/d/optout

Shell Xu

unread,
Apr 15, 2014, 10:05:46 PM4/15/14
to shlug
啊,以前我在gary那里就写过一个生成C的python。从一个简单的文件和符号导出表里面读取信息,然后生成一堆C代码。再混合一点自己的代码编译。


--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛中的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/blog/

liyaoshi

unread,
Apr 15, 2014, 10:09:04 PM4/15/14
to sh...@googlegroups.com
求扫盲

我一般多是1k行写出一个驱动原型,100行单元测试

liyaoshi

unread,
Apr 15, 2014, 10:10:16 PM4/15/14
to sh...@googlegroups.com
头文件不算

Shell Xu

unread,
Apr 15, 2014, 10:17:27 PM4/15/14
to shlug
没任何的花样,每个函数就是一个标准的东西,就是名字发生了变化。其实这个东西可以用宏来简化(实际上也用了),但是我需要从导出表来生成代码,而不是自己拿着编辑器去操作。
其实你可以看做每次我拿编辑器执行了一个很长的编辑宏,将导出表的每一行都扔到了一个C宏里面,生成了一个C文件,然后把这个文件加入编译。只是我把这个过程用python写,然后自动化封装到了编译过程里。于是拿到新的lib和定义调整,编译可以启动的进行,而不需要手工再整一遍。


在 2014年4月16日 上午10:09,liyaoshi <liya...@gmail.com>写道:

Liu Cheng

unread,
Apr 15, 2014, 10:30:05 PM4/15/14
to sh...@googlegroups.com
常见的一些生成的情况
像lex yacc之类的就是最常用的生成代码的玩意

还有诸如要生成一些支持多个参数的函数
int add2(int v1, int v2);
int add3(int v1, int v2, int v3);
...

liyaoshi

unread,
Apr 15, 2014, 10:35:49 PM4/15/14
to sh...@googlegroups.com
怎么定义函数逻辑?

Shell Xu

unread,
Apr 16, 2014, 1:52:38 AM4/16/14
to shlug
当然是人来写。

liyaoshi

unread,
Apr 16, 2014, 3:28:47 AM4/16/14
to sh...@googlegroups.com
那你这个
跟ctrl-c ctrl-v 有啥区别?

Grissiom

unread,
Apr 16, 2014, 3:31:10 AM4/16/14
to sh...@googlegroups.com
2014-04-16 15:28 GMT+08:00 liyaoshi <liya...@gmail.com>:
那你这个
跟ctrl-c ctrl-v 有啥区别?


用少量的复制就可以生成大量的代码吧……



--
Cheers,
Grissiom

Shell Xu

unread,
Apr 16, 2014, 4:52:09 AM4/16/14
to shlug

不用每次编译都来一遍。。。

from nexus 4

Qian Hong

unread,
Apr 16, 2014, 5:15:43 AM4/16/14
to sh...@googlegroups.com
2014-04-16 15:28 GMT+08:00 liyaoshi <liya...@gmail.com>:
> 跟ctrl-c ctrl-v 有啥区别?

我能想到的用代码生成代码的例子有:
Wine项目:
用C生成汇编:
winegcc一个gcc的wrapper,调用gcc生成objfile
winebuild是一个类似汇编器的东西,几句话说不清,man winebuild有说明

用C+IDL文件生成h头文件
widl是一个IDL编译器,和微软的MIDL兼容,可以把IDL编译为.h文件,widl本身是用C写的

用C生成Makefile:
Wine代码目录下有个tool/makedep.c
,makedep可以根据Makefile.in等文件批量生成Makefile的部分内容。Wine项目有超过五百个模块,好像没有更好的办法了。

用perl生成Makefile:
winemaker可以把VC++的dsp/sln项目文件转化为GNU Makefile

用C生成C:
有个工具叫winedump,其中一项功能是解析一个dll的导出函数,然后生成一个stub的dll。
假设你有个一个dll叫msvcrt.dll (Windows上的PE32格式的dll,不是Wine上的 ELF 32格式的
dll.so或者PE32的dll.fake)
那么:
$ winedump -I /usr/i686-w64-mingw32/include/ spec msvcrt.dll
就可以生成以下文件:
Makefile.in msvcrt_main.c msvcrt.spec

用Perl生成h:
tools/make_requests 可以把wineserver的协议定义文件翻译为h头文件

用Perl生成C:
tools/make_unicode 从unicode标准的字符数据生成Wine项目需要的C文件

用bison生成C:
Wine项目大量用到了bison做语法解析,如vbscript,jscript,msxml,widl等模块,不过bison太知名了,别的项目也用,放在这里算是凑数。

其他例子可以通过 git grep -i 'do not edit' 找到

在npactivex项目中,我看到了用python生成汇编的例子

在bash源码中,我看到了用def文件生成C的例子,但是我没看懂那么做的必要性。

Google 搜 state machine compiler
,可以找到另一类代码生成代码的使用场景和工具,例如http://www.complang.org/ragel/,和bison很像。

我感觉可能要亲自参与一个依赖代码生成代码的项目,才能了解这么做对于该项目的必要性,但是旁观另一个项目的时候又未必能一下子就明白。


--
Regards,
Qian Hong

-
http://www.winehq.org

liyaoshi

unread,
Apr 16, 2014, 5:19:19 AM4/16/14
to sh...@googlegroups.com
如果cmake生成的玩意也能算

autoconf 生成的也能算

要python生成他干吗?


--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。

Bamboo Hui

unread,
Apr 16, 2014, 5:44:27 AM4/16/14
to sh...@googlegroups.com

这个在量子物理,理论化学领域应该比较常见吧。

一些公式,矩阵运算使用的算符有规律性,人手动推导再写代码,又麻烦又容易出错,自动推导是最佳选择。国外又不少博士论文就是这个

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛中的“Shanghai Linux User Group”论坛。

Chaos Eternal

unread,
Apr 16, 2014, 11:35:35 AM4/16/14
to sh...@googlegroups.com
因为C的宏实在太弱了。如果能有hygenic macro system, 比如scheme那样的,也就不用python了。

从NASA的用例来说,我感觉这种驱动吧,有很多测试代码,这些测试代码手写确实很麻烦,不如用代码自动生成。

2014-04-16 10:17 GMT+08:00 Shell Xu <shell...@gmail.com>:

liyaoshi

unread,
Apr 16, 2014, 10:29:34 PM4/16/14
to sh...@googlegroups.com
这种宏,流行的办法不是xml么?
再用xml 解析得到变量
我见过人家用java做个界面,编辑变量
看这种


您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。

Zhao Quan

unread,
Apr 24, 2014, 4:10:54 AM4/24/14
to sh...@googlegroups.com
MS 有个技术 叫 T4 template 貌似也是类似的功能。

一般是几十行代码,生成几十个文件。
这么来玩的。
Reply all
Reply to author
Forward
0 new messages