4 ms·
Semi-related, besides TCC, are there any other C-ish compilers that are easily ported to a new ISA any of you can recommend? Been playing with FPGA softcores,
by magicalhippo 5y ago
Semi-related, besides TCC, are there any other C-ish compilers that are easily ported to a new ISA any of you can recommend?
Been playing with FPGA softcores, and while writing a compiler can be fun it kinda detracts from the main project.
- bajsejohannes 5y agoAre you making your own softcores? Do you have a recommendation for getting started with that? I've recently gotten interested in making CPUs from logic gates, but I have no idea how to transfer that knowledge to FPGAs.
- magicalhippo 5y agoI'm fairly fresh to this field, so hopefully someone else can come along and give a better answer. I just picked up an iCE40[1] and Cyclone IV dev board[2] and started hacking Verilog. I know Verilog isn't the best option around, and I'm planning on moving onto something better but for now it was easy to get started, and from what I could see many of those better tools end up generating Verilog so would be nice to be able to debug that. If you know how to think in logic-gate-terms then FPGAs won't be that difficult I suspect. Essentially either you have plain logic gates where the output is purely a function of the inputs, or you have edge-triggered logic thanks to flip-flops. Just think of it as writing code that specifies how to wire up set of logic gates, muliplexers and flip-flops. As a brief example, here's an excerpt from my very first ALU: wire [8:0] res_and; wire [8:0] res_xor; wire [8:0] res_add; assign res_and = (bus_a & bus_b); assign res_xor = (bus_a ^ bus_b); assign res_add = (bus_a + bus_b); always @(posedge clk) case (op_sel) OP_AND: result <= res_and; OP_XOR: result <= res_xor; OP_ADD: result <= res_add; default: result <= 0; endcase Here "bus_a" and "bus_b" are wires (which may come from registers), while "result" is a register. The wires don't hold state, while registers can. The <= means to latch the right side into the left side, in this case on a positive edge of clk. The case statement is just a multiplexer. At least to me, it feels very much like "writing a circuit". [1]: https://www.latticesemi.com/en/Products/DevelopmentBoardsAndKits/iCE40UltraPlusBreakoutBoard https://www.latticesemi.com/en/Products/DevelopmentBoardsAnd... [2]: https://www.aliexpress.com/item/32949281189.html https://www.aliexpress.com/item/32949281189.html
- bajsejohannes 5y agoThanks! I feel like I actually understand that verilog code! Do you ever see the "rendered" state on the FPGA, or is that all abstracted away? Would you recommend one of those FPGAs over another? I know iCE40 from this amazing project (https://github.com/nickmqb/fpga_craft https://github.com/nickmqb/fpga_craft) so maybe I'll just get that!
- thrtythreeforty 5y agoYou can certainly inspect the synthesized primitives, either graphically post-place&route, or in a netlist which is usually a tangled mess. Typically you're doing so either because you're curious, you're debugging a timing problem, or you're trying to make sure the toolchains inferred a particular primitive you are trying to use on purpose. It's not terrifically informative otherwise.
- magicalhippo 5y agoI've posted a few examples here[1]. It's from the same ALU code that I had trimmed to give you an example. It just outputs some results to an RGB led. It shows the logical view, the allocated blocks and the routing in Lattice Radiant IDE. I haven't done much with the Cyclon IV yet, but I found the iCE40 very easy to get rolling with and the freely available tools have been great so far. It's not a huge FPGA though so if you want to do large projects maybe consider something else. But as a starter I think it's been great. [1]: https://imgur.com/a/ZaV9Wvr https://imgur.com/a/ZaV9Wvr
- GeorgeTirebiter 5y agoI use the iCE40 with 8K LUTs. It's 'old' but the open-source chain is nice, the vendor-supplied chain is fine, and it's 'big enough' to do fairly serious stuff. Like the subc compiler I mentioned, the tradeoffs working with an older FPGA like that are ok to me. GL! p.s. I mostly used the TinyFPGA BX to learn about the iCE40, but it doesn't have enough pins to make even a semi-decent full machine (with wide memory bus) so I use the Lattice HX 8K dev board. It's also easy to use.
- GeorgeTirebiter 5y ago
- foxhill 5y agoC4 comes to mind (C in 4 functions), https://github.com/rswier/c4 https://github.com/rswier/c4. have you considered adding a backend for LLVM? perhaps a bit heavyweight, but it could be a good way to get C/C++, fortran, rust, etc. if that's something you'd like!
- magicalhippo 5y ago> C4 comes to mind Seems like an interpreter? But maybe that could be tweaked... A bit messy code though. > have you considered adding a backend for LLVM? Just to the extent that I kinda dismissed it out of the gate due to seeming to be a fairly complex and dynamic target. Haven't used it personally but a good buddy maintains a backend.
- rwmj 5y agoThe plan9 compiler: https://www.geeklan.co.uk/files/oshug69-Miller-criscv.pdf https://www.geeklan.co.uk/files/oshug69-Miller-criscv.pdf
- magicalhippo 5y agoThat sounds encouraging, thanks!
- girvo 5y agoThe article itself mentions this: https://c9x.me/compile/ https://c9x.me/compile/
- atgreen 5y agoGCC isn't all that bad. I wrote this guide a while ago, but GCC hasn't changed too much since then: https://atgreen.github.io/ggx/ https://atgreen.github.io/ggx/
- dig1 5y agoCheck PCC [1]. Official release was 2014, but development is still active [2]. [1] http://pcc.ludd.ltu.se/ http://pcc.ludd.ltu.se/ [2] https://marc.info/?l=pcc-commit-list https://marc.info/?l=pcc-commit-list
- aseipp 5y agoThere isn't really any value in doing that kind of stuff honestly unless you just like really boring and annoying work. Like, if you are designing your own ISA, you can punt that down to basically the end of the line, because all the actual important stuff comes first. There's a kind of great irony to compiler work like this where you need some outside thing to drive the design, but if the design being driven ultimately isn't very bespoke, you eventually realize everyone else has done all the work already so everything you have to do is boring (e.g. wire up a new ABI to some existing codegen... how riveting...) Alternatively you have something unique, and find out the compiler is actually the hard part of the whole problem, because now you're on your own. If you want a tool for early programming your own ISA I recommend using Lisp or something else as a macro assembler to exercise the system. If you simply insist on arbitrary C code, something like WebAssembly might actually be better than patching some random 3rd party code generator (i.e. just write a simple one-shot one-pass compiler that does no advanced regalloc or optimization and just turns it into a binary. This is relatively easy and does not require advanced algorithms and there are already a bunch of WASM compilers available.)
- samatman 5y ago> If you want a tool for early programming your own ISA I recommend using Lisp or something else as a macro assembler to exercise the system. That something else should be, and hear me out, Forth. Yes I'm perfectly serious, if you look around you can find some interactive AVR Forth assemblers which are in the 2-4K installed range. This is Forth's niche, it's a surprisingly capable language for many things but interactive programming a bespoke ISA, this is where Forth comes from. Firm recommendation to at least read A Problem Oriented Language if working in this space, a combination of memoir and a distinctive philosophy of software development.
- magicalhippo 5y agoYeah I figured it's a long shot. Just wanted to see if I had missed something which would allow me to get going without much effort. > If you want a tool for early programming your own ISA I recommend using Lisp Good point, maybe this is the time for me to learn Lisp...
- 5y ago
- GeorgeTirebiter 5y agoI've been porting subc https://www.t3x.org/subc/ https://www.t3x.org/subc/ to my 36-bit RISC machine arch. It's by no means 'full' C, however: 1. It is Public Domain. I like that. 2. There is a book ($) that explains it in detail (and it's good). I respect the design choices made, and the compiler can compile itself once bootstrapped. It's really fast, but code gen is only 'ok'. That's a tradeoff that works for me.
- jecel 5y agoOne alternative is LCC: https://en.wikipedia.org/wiki/LCC_(compiler) https://en.wikipedia.org/wiki/LCC_(compiler) It is half way in complexity between TCC and GCC or LLVM and was created to be educational. I think there are major differences between more recent versions and what it described in the book.
- muth02446 5y agoNot a full-blown compiler but a backend similar to qbe or llvm: https://github.com/robertmuth/Cwerg https://github.com/robertmuth/Cwerg
- MaxBarraclough 5y agoPerhaps the lightweight qbe compiler backend. [0] There's a C frontend for qbe called cproc. [1][2] See also this talk [3] which discusses the relative ease of porting qbe, at 11:14. [0] https://c9x.me/compile/ https://c9x.me/compile/ [1] https://sr.ht/~mcf/cproc/ https://sr.ht/~mcf/cproc/ [2] Official GitHub mirror: https://github.com/michaelforney/cproc https://github.com/michaelforney/cproc [3] https://spacepub.space/w/pjgvVL74xtFwqnaKuoCXZj?start=11m14s https://spacepub.space/w/pjgvVL74xtFwqnaKuoCXZj?start=11m14s