Hackaday 之问:CPU 里的 C 究竟意味着什么Hackaday Asks: What Does C in a CPU Really Mean?


AI free token/AI免费积分

Hackaday 之问:CPU 里的 C 究竟意味着什么

有一次我参加会议,主持人让我们按常规信息做自我介绍,还希望我们分享自己最喜欢的爱好。我愣了一秒,琢磨该说哪个爱好才不至于听起来像个十足的书呆子。随后我意识到这无所谓。我所有的爱好都很极客,包括设计 CPU 及其指令集。

其实我小时候就开始草拟一套指令集了,那时还没人有家用电脑。我很高兴把那个笔记本弄丢了:今天再看一定会让人难受。不过,我放到 FPGA 上的第一个 CPU 并不是自己设计的。我实现了 Caxton Foster 所著《Computer Architecture》一书中的 BLUE,还对它做了大量扩展和现代化改造。我特别想给它配一个 C 编译器。

问题恰恰就在这里。C 曾经是一门简单的语言,但现代 C 远非简单,编译器都是庞大的工程。我以前研究过把 GCC 移植到不同架构上,看起来就令人生畏。还有 LLVM,它稍微现代一些,但移植起来同样不轻松。有几个老编译器应该更容易些,可它们往往又不够现代。此外,如果你的 CPU 足够大——BLUE 并不算大——GCC 或类似的现代编译器就很有吸引力,因为你可以用它把 Linux 跑起来。

不过要记住,我说的不是在 BLUE 上运行的 C 编译器,而只是一个交叉编译器。但我的 OneDer CPU 是 32 位的,可以承载一个 C 编译器。这也是我下定决心去做一次“大型”编译器移植的另一个理由。因为即便 BLUE 没那么大,别的 CPU 可很大,而我很想让自己拥有一次 GCC 移植的经历。

那么……我该怎么办呢?我有几个想法供大家考虑,当然也欢迎其他建议。BLUE 最大的问题之一是它是一台面向字(word)的机器。例如,地址 0 是一个 16 位的字,地址 1 是另一个不重叠的 16 位字。

在碰到字符串(也就是字符数组)之前,这一切都很好。字符指针要不要特殊处理?还是说每个字只放一个字符、白白浪费空间?地址空间并不大,所以你可以让字符指针在运行时进行转换,但当然这会带来性能代价。又或者,你可以把所有指针都做成字节指针,代价则更大。

显然,最好的结果是移植 GCC 或类似的编译器。事实表明,GCC 有一个 PDP/11 移植版本,而 BLUE 与精简版低端 PDP/11 并没有太大差别。看来一个思路是拿到 PDP/11 的 GCC 源代码,把它当作起点。

当然,有些编译器天生就更容易移植。例如 SDCC 面向寄存器较少的 CPU。Clang/LLVM 和 LCC 也是为便于移植而设计的编译器。

这里的吸引力显而易见。如果我能让 GCC 或 LLVM 生成高质量的 BLUE 代码,我就学会了如何面向主流编译器基础设施之一进行开发。这些知识可以迁移到以后的 CPU 上,而且在未来处理器性能足够强的情况下,还可能带来 Linux 移植之类的成果。

缺点也很明显:这可能是在解决一个远比 BLUE 实际需要更大的问题。

还有一些小型编译器,比如 AT&T 的 pcc,它本应是可移植的。Amsterdam Compiler Kit 虽然古老,但能用。不过它们都不具备 GCC 那样的现代特性。

另一类“小型”编译器是输出某种易于理解的中间格式的编译器。当然,用 LLVM 也能做到:让它输出 IR 代码,然后直接转换,无需再回到工具链里去做代码生成后优化之类的事情。QBE 是另一个候选,它已经带有 C 前端。

也曾有过一些为虚拟机生成代码的编译器。移植这类编译器只需你创建一个你希望足够简单的 VM 和加载器。较早的商业产品 C-Flea 就用了这个思路,此外还有 subc 等若干方案。不过这些往往不是功能完备的编译器。

既然有 PDP/11 移植版,一个思路就是直接用 BLUE 去模拟一台 PDP/11。性能大概不会好,但应该相对快就能做出来。这绝对算个取巧的 hack,也绝对谈不上令人满意。不过这大概是最容易走的路,尤其是如果你能弄清 gcc 程序实际用到哪些指令,就不再模拟代码用不到的部分。

如果你想要更好的性能,可以把 GCC 输出的 PDP/11 汇编转译(transpile)成 BLUE 的汇编代码。同样,这大概也不算太难,但确实显得不够过瘾。你不需要转换整套指令集,只需要转换 gcc 会生成的部分。

正如我说过的,早期的 C 相当简单。可以写出某种工具,把现代 C 改写成较老的结构,然后再编写或移植一个更简单的编译器。这看起来是最吃力不讨好的工作,但确实可行。之后你可以移植 Small C 或 8cc 这类相当简单的编译器,甚至干脆自己写一个编译器。

无论走哪条路,BLUE 是一颗基于 FPGA 的 CPU 这一点意味着,如果需要,我可以添加指令,让承载 C 程序变得更容易。

那么现状是:你有一台基于累加器的 16 位 CPU,配有少量面向字的内存;你有一个可用的、支持重定位的汇编器和一个软件模拟器。你只需决定如何做出 C 编译器。你会怎么做?欢迎在评论区告诉我们。

如果你想看看早期版本的 BLUE 是如何运行的,可以看看下面那段年代久远的视频。

本文来源:https://hackaday.com/2026/11/24/ask-hackaday-putting-the-c-in-cpu/
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Hackaday Asks: What Does C in a CPU Really Mean?

I was once in a meeting where the facilitator asked us to introduce ourselves with the usual information, but also wanted us to share our favorite hobby. I spent a second trying to think of which hobby I could say to not sound like a total nerd. Then I realized it didn’t matter. All my hobbies are nerdy, including designing CPUs and their instruction sets.

I actually started sketching out an instruction set when I was a kid, back when no one had home computers. I am glad I lost that notebook: it would be painful to see today. The first CPU I put on an FPGA wasn’t of my own design, though. I implemented BLUE from Caxton Foster’s Computer Architecture book . I also extended it and modernized it a lot. I really wanted a C compiler for it.

That’s really the problem. C was once a simple language, but modern C is far from simple, and compilers are huge projects. I have looked before at porting GCC to a different architecture, and it looks daunting. There’s LLVM, which is a bit more modern, but also non-trivial to port. There are a few older compilers that ought to be easier, but they often aren’t quite modern enough. Plus, if your CPU were big enough, which BLUE isn’t, GCC or a similar modern compiler would be attractive because you could use it to get Linux going.

Keep in mind, though, I’m not talking about a C compiler that runs on BLUE. Just a cross-compiler. My OneDer CPU is 32-bit and could host a C compiler, though. That’s another reason to bite the bullet and do a “big” compiler port. Because even if BLUE isn’t that big, other CPUs are, and I’d love to have a GCC port under my belt.

So… what do I do? I have a few ideas for you to consider, and of course I’m open to other suggestions. One of the biggest problems with BLUE is that it is a word-oriented machine. Address 0, for example, is a 16-bit word. Address 1 is another 16-bit word that does not overlap.

This is great until you get into strings: that is, character arrays. Should a character pointer be special? Or should you waste space by putting one character per word? The address space isn’t very large, so you could make character pointers that get converted at runtime, but there is a penalty for that, of course. Or, you could make everything a byte pointer and pay an even bigger penalty.

Obviously, the best result would be to port GCC or a similar compiler. It turns out, GCC has a PDP/11 port, and BLUE is not very dissimilar from a stripped-down low-end PDP/11. It seems like one idea would be to get the PDP/11 GCC source code and use it as a starting point.

Of course, some compilers are made to be easier to port. SDCC handles CPUs with few registers, for example. Clang/LLVM and LCC are also compilers made to be easier to port.

There is obvious appeal here. If I get GCC or LLVM producing good BLUE code, I’ve learned how to target one of the major compiler infrastructures. That knowledge might transfer to later CPUs and, in the case of a sufficiently capable future processor, could lead to things like Linux ports.

The downside is obvious too: this may be solving a much larger problem than BLUE actually requires.

There are a few little compilers like pcc from AT&T, which was supposed to be portable. The Amsterdam Compiler Kit is old, but works. But these don’t have the modern features that GCC has.

Another category of “little” compilers would be compilers that output some well-understood intermediate format. You could do this with LLVM, of course. Have it output IR code, then convert it without going back into the toolchain to do things like post-code generation optimization. QBE is another candidate that already has a C front end.

There have been a few compilers that generate code for virtual machines. A port for these only requires you to create what you hope is a simple VM and loader. An older commercial product, C-Flea, used this idea. There’s also subc and a few others . These often are not full-featured compilers, though.

Since there is a PDP/11 port, one idea is to just emulate a PDP/11 with BLUE. The performance probably won’t be great, but it should be relatively quick. Definitely a hack and definitely not very satisfying. However, it is probably the easiest way to go, especially if you identify what instructions gcc programs use and drop emulation of things that the code won’t use.

If you want better performance, you could take PDP/11 assembly from GCC and transpile it to BLUE’s assembler. Again, this probably isn’t that hard, but it does seem unsatisfying. You wouldn’t need to convert the entire instruction set, just the things gcc emits.

As I said, early C was pretty simple. It should be possible to write something that mangles modern C into older constructs and then write or port a simpler compiler. This seems like the most unproductive work, but it is possible. You could then do something like port Small C or 8cc, which are pretty simple, and port them. You might even just write your own compiler.

Regardless of the path, the idea that BLUE is an FPGA-based CPU means that I can add instructions, if I need them, to make hosting C programs easier.

So. You have a 16-bit accumulator-based CPU with a small amount of word-oriented memory. You have a working assembler with relocation support and a software emulator. You just need to decide how to create the C compiler. What would you do? Let us know in the comments.

If you want to see an older version of BLUE in action, check out the very old video below.

本文来源:https://hackaday.com/2026/11/24/ask-hackaday-putting-the-c-in-cpu/
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry