collapseos icon indicating copy to clipboard operation
collapseos copied to clipboard

C compiler?

Open tywkeene opened this issue 4 years ago • 22 comments

I realize, in the scheme of things, getting a C compiler running is a serious undertaking, but do you have any plans for it?

I don't have any experience with z80 assembly (Something I feel I could work on given some time, and this project to work on), but do know C.

If there were to be a C compiler, I'd love to help port over some userspace applications in the future.

Anyways, this is a really neat project, really caught my attention.

tywkeene avatar Oct 08 '19 19:10 tywkeene

Back in the 80's my office mate (who went on to head Netflix cloud services!) had a side project to get 'Small C' running on his kit computer (not Z80 but that doesn't matter). The point is he was hand coding it to the point where it would compile itself. So that would be a suitable route to getting C and many other tools on this platform - and there are likely people who have already written the code which is needed. I just did a quick search for Z80 C compiler and turned up this handy list of Z80 tools:

http://www.z80.info/z80sdt.htm

I am committed on other projects but will keep an eye over here in case I can offer any help.

happybeing avatar Oct 08 '19 19:10 happybeing

FUZIX has a SmallC implementation which would be a good start, but the problem is that C doesn't translate very well to z80. It is hard for a compiler to properly optimize register usage, so you end up with either inefficient binary or complicated C compiler. Either way, RAM usage gets big.

It would be nice to have, but not very high in my priorities.

hsoft avatar Oct 09 '19 01:10 hsoft

Always have the original system6 of old Unix as reference

agsb avatar Oct 09 '19 01:10 agsb

I imagine implementing BASIC would be much easier than C for an 8 bit processor

NoahGWood avatar Oct 09 '19 17:10 NoahGWood

@NoahGWood Yes, there are many many BASIC interpreters written in z80 asm. Eventually, one of them (that isn't copyright-ladden) could be integrated in Collapse OS.

hsoft avatar Oct 09 '19 23:10 hsoft

@hsoft perhaps something similar to this could be made: https://github.com/nanochess/bootBASIC

Midnoclose avatar Oct 10 '19 16:10 Midnoclose

One BASIC interpreter that could be somewhat useful was published in Soviet magazine 'Radio" in early 1980-s, under 8KB in size. It was for i8080, but Z80 can execute all i8080 instructions. The only requirement from hardware was to have a specific ROM-BIOS, but that ROM was 2K with only few input/output subroutines. Both were published as hex dump and since they were in a magazine they can be freely shared.

aaretali avatar Oct 10 '19 16:10 aaretali

There is CWDL CC386 C compiler, http://ladsoft.tripod.com/cc386_compiler.html. I used much earlier version of it and reworked code generator to produce Z80 assembly. The compiler was running as a cross-compiler on MS-DOS host, but it was supporting fairly good amount of C so it could be self-hosting (never tried this though). I chose it over Small-C and Micro-C for various reasons. If this sounds interesting enough, I could dig it out of my backups.

aaretali avatar Oct 10 '19 16:10 aaretali

Surprised nobody mentioned sdcc, it's regularly updated and frequently mentioned when Z80 and C comes up

http://sdcc.sourceforge.net/

But, I assume we would want it actually running on the Z80 :)

keithstellyes avatar Oct 11 '19 17:10 keithstellyes

You're right, it needs to run in 64KB address space on a Z80.

SDCC is just too fat for fitting into 64K of RAM. 4 largest source files in SDCC are between 100KB and 288KB. The entire generic source tree is over 1.5MB, Z80 code generator is another 650KB, one file of the code generator is 400KB alone.

This is why Small-C and others are much more viable. Desmet C http://desmet-c.com/ and BDS C https://www.bdsoft.com/resources/bdsc.html from CP/M world were open-sourced recently.

aaretali avatar Oct 14 '19 17:10 aaretali

I had a look at BDS C at the beginning of this year and it looked promising. Well written. A lot of fitting work will be necessary, however, to have this run on Collapse OS.

hsoft avatar Oct 16 '19 01:10 hsoft

If more people come with me, im up to design and write a new language and compiler that fit in the whole idea, i think we have time to do this before the collapse

nelsonrpn avatar Oct 16 '19 23:10 nelsonrpn

What about a simple lisp interpreter? https://github.com/kanaka/mal

Midnoclose avatar Oct 17 '19 02:10 Midnoclose

This is not a compiler that generates native code.

f4grx avatar Oct 17 '19 07:10 f4grx

@f4grx perhaps something like https://github.com/dubek/malc without llvm, written in z80 assembly

Midnoclose avatar Oct 17 '19 13:10 Midnoclose

SmallC is existent and developed on a CPM z80 system. its output is assembler and you assemble and link modules. It was self compiling on a minimum of 48K and preferable 56K system.

There were two other smaller Cs all ran in less than 48K, self compiling was not known as source was unavailable.

I used BDS-C as it was fast, small and it was also direct to assembled code system that could handle code broken into modules. BDS C relies on many of the CP/M BDOS APIs (borrow a PC term) to operate quite a few so you would need a source to build your own and knowledge of the CP/M APIs to work out what the required functions do. Doable but maybe writing a C compiler from SmallC is easier.

NOTE that is the TPA Transisent Program Area and it extends from the 0100h to the bottom of BIOS in high memory. A full boat system without bank switching can be as large as 61K but typical is more like 60K.

The compiler (SMALLC) also writes out to disk/mass storage so you need space there as well.

SDCC was never a resident on z80 systems, it was for PCs for cross platform work.

I havent got ther eyet but is there a uniform interface for applications programmer to use for file and character IO? does it provide functions similar to string input with editing, or print a string? What is the calling protocol, address of routine or function number though a common address?

Allison

Allisontheolder avatar Nov 12 '19 04:11 Allisontheolder

If one absolutely needed a C compiler for whatever reason, I know from experience to fit it into 16KB you'll want to write it in Assembly. Having done this myself, I must say I'll be porting the cc_* C compiler family and the M2-Planet C compiler to collapseOS. Which for those really worried will be glad to know are in the process of becoming capable of bootstrapping GNU Mes and thus GCC and GNU Guix, thus one would be able to completely restore the entire current software ecosystem provided sufficiently powerful hardware can be recovered.

oriansj avatar Dec 05 '19 21:12 oriansj

Small c is missing a lot, and there are no self hosted c compilers in source form. Bds is very fast and small, but has odd memory/linkage and knows nothing about z80 extensions. I've been working on a self hosted z80 c compiler for a while, but I've only got the top third done.

cm68 avatar Dec 12 '19 20:12 cm68

Small c is missing a lot, and there are no self hosted c compilers in source form. Bds is very fast and small, but has odd memory/linkage and knows nothing about z80 extensions. I've been working on a self hosted z80 c compiler for a while, but I've only got the top third done.

well if you look at my stage0 cc_x86, you'll see a trivial transition for the creation of the C compiler in assembly and only needs to support the subset required to build M2-Planet to get the full benefits.

oriansj avatar Dec 13 '19 00:12 oriansj

The Hyper C is a C compiler implemented as a byte code interpreter in the context of a Forth-like virtual machine on the Apple 2. It also has some similarities to Steve Wozniak's SWEET16.

This could serve as an invaluable point of reference for implementing compilers or interpreters on CollapseOS once the transition to Forth is complete. There does not appear to be a FOSS licence associated with the code, but it hardly matters. The C engine is documented well enough.

@tywkeene I'm not sure how much Forth you know or if you're into compiler design, but what do you think? I haven't had a chance to dive too deeply into the document yet, so I don't know if it has features @hsoft might want to bake into CollapseOS' Forth VM itself.

gitjeff2 avatar May 12 '20 13:05 gitjeff2

@gitjeff2 Oh man. I totally forgot about this thread.

Unfortunately I have way more obligations at work than I did when I was looking at this, and it seems that my eyes got a bit bigger than my skills. I was thinking of doing this as something to do between work obligations, but that's just not realistic for me anymore.

It is good to see I sparked a bit of a conversation though. Maybe it'll spark enough interest in someone with the time and actual skills.

tywkeene avatar May 12 '20 13:05 tywkeene

At this rate it should be possible to parse a C file in forth.

and here we go after less than one minute on google: https://www.bradrodriguez.com/papers/bnfparse.htm

Not a compiler, but people already tried it lol.

While we're at it here is a BASIC interpreter in forth http://home.iae.nl/users/mhx/basic.html

f4grx avatar May 12 '20 15:05 f4grx