Rudimentary vCPU support for the lcc C compiler

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice.
User avatar
marcelk
Posts: 304
Joined: 13 May 2018, 08:26

Re: Rudimentary vCPU support for the lcc C compiler

Post by marcelk » 27 Apr 2019, 19:48

You're famous now: https://hackaday.com/2019/04/26/the-no- ... -compiler/

Screenshot 2019-04-27 at 21.13.01.png
Screenshot 2019-04-27 at 21.13.01.png (151.19 KiB) Viewed 445 times

User avatar
marcelk
Posts: 304
Joined: 13 May 2018, 08:26

Re: Rudimentary vCPU support for the lcc C compiler

Post by marcelk » 29 Apr 2019, 20:17

I've just merged Pat's branch into the Gigatron repo, with preservation of its git history. Hopefully I didn't goof up because I forgot how I did it last time. (For future reference, I used instructions from here.)

We "promoted" it directly to Utils/lcc/, even though it's still very much work in progress. It helps collaboration to have it in a place where we want it to end up eventually. Besides, there's little expectation that these changes will ever find their way back upstream into the original lcc code base...

Libs/ will be a place for libraries. Makefiles and README's etcetera still need attention.

Expect things to break once in a while :D

User avatar
marcelk
Posts: 304
Joined: 13 May 2018, 08:26

Re: Rudimentary vCPU support for the lcc C compiler

Post by marcelk » 29 Apr 2019, 21:15

With one command from the root directory of the gigatron-rom repository we can build lcc:

Code: Select all

$ make lcc
mkdir -p Utils/lcc/build
cd Utils/lcc && env HOSTFILE=etc/gt1h.c make all
cc -g -c -Isrc -o build/main.o src/main.c
cc -g -c -Isrc -o build/alloc.o src/alloc.c
cc -g -c -Isrc -o build/bind.o src/bind.c
cc -g -c -Isrc -o build/dag.o src/dag.c
[
 ...lots of output and compiler warnings snipped...
]
cp rt.py build/rt.py
cp asm.py build/asm.py
$
All of lcc is now in a the directory Utils/lcc/build/. The compiler has many subprograms and files, with lcc itself being the driver for it all. We can simply keep everything there.

Next compile the test program. The Gigatron backend requires Python 3.6 or up to be installed on the system.

Code: Select all

$ make Libs/Example.gt1
Utils/lcc/build/lcc -ILibs -c Libs/sys/ClearScreen.c -o Libs/sys/ClearScreen.o
Utils/lcc/build/lcc -ILibs -c Libs/sys/Newline.c -o Libs/sys/Newline.o
Utils/lcc/build/lcc -ILibs -c Libs/sys/Random.c -o Libs/sys/Random.o
Utils/lcc/build/lcc -ILibs -c Libs/stdio/putchar.c -o Libs/stdio/putchar.o
Utils/lcc/build/lcc -ILibs -c Libs/stdio/puts.c -o Libs/stdio/puts.o
Utils/lcc/build/lcc -ILibs -c Libs/Example.c -o Libs/Example.o
Utils/lcc/build/lcc -ILibs Libs/sys/ClearScreen.o Libs/sys/Newline.o Libs/sys/Random.o Libs/stdio/putchar.o Libs/stdio/puts.o Libs/Example.o -o Libs/Example.gt1
$ ls -l Libs/Example.gt1
-rw-r-----  1 marcelk  staff  743 Apr 29 22:59 Libs/Example.gt1
$
Now we have a program that we can send to a Gigatron with Utils/sendFile.py (Python 2...!), or load into an emulator.

[Edit: updated paths for changes made afterwards]

User avatar
marcelk
Posts: 304
Joined: 13 May 2018, 08:26

Re: Rudimentary vCPU support for the lcc C compiler

Post by marcelk » 09 May 2019, 06:46

Our new C library now has rudimentary versions for sprintf() and printf(). From the GitHub commit message:
libc: printf/sprintf family
- Example.c: demonstrate sprintf and printf for %d, %u, %s and %c
- lcc: change argument order to support va_arg() (issue #62)
- lcc: caller of variadic function pops the "extra" arguments
- lcc: fix bug in swapping comparison operands (issue #63)
- stdarg: va_start, va_arg, v_end (and va_sarg, see comments issue #62)
- ctype: isdigit
- stdio: fprintf, fputc, fputs, printf, putchar, puts, snprint, vfprintf, vprintf, vsnprintf, vsprintf
- Makefile: fix mixup between LCC and native compiler
Libs/Example.c uses these functions to write a welcome message to the screen and format some numbers before turning itself into a TV Typewriter. Many of the flags and format specifiers are working already. For example:

Code: Select all

  ClearScreen();

  // Demo printf and varargs
  printf("%d %d %u\n",          1972, -327, UINT_MAX);
  printf("%07d %07d %07u\n",    1972, -327, UINT_MAX);
  printf("%+7d %+7d %+7u\n",    1972, -327, UINT_MAX);
  printf("%+07d %+07d %+07u\n", 1972, -327, UINT_MAX);

  puts("Ready");
Output works as expected:

Screenshot 2019-05-09 at 08.42.03.png
Screenshot 2019-05-09 at 08.42.03.png (130.35 KiB) Viewed 368 times
Attachments
Example.gt1
Compiler Example.c
(3.61 KiB) Downloaded 17 times
Example.c
Using printf/sprintf
(2.21 KiB) Downloaded 24 times

User avatar
marcelk
Posts: 304
Joined: 13 May 2018, 08:26

Re: Rudimentary vCPU support for the lcc C compiler

Post by marcelk » 19 May 2019, 09:44

Some updates since last week:
  • Signed and unsigned division and remainder are working(*). Signed / and % follow the C99 rules for negative operands (ANSI C was less demanding in this area)
  • stdout is now a true FILE object, with a tiny one-line buffer. fflush() works
  • The size argument for vsnprintf() works, and with that, we have snprintf() obeying it as well
  • The assembler can do offset calculations. This enables the whole assortment of constant expression initialisers
  • Some bugs solved regarding code generation for page hopping
  • The linker gives an error message when a function remains undefined, instead of a Python stack dump
(*) There is one annoying caveat: the multiplicative operators won't nest yet. A register spilling issue causes the compiler to run wild. So something like

Code: Select all

a * (b / c); // won't work yet.
This holds for all operators implemented with helper functions: <<, >>, *, / and %.

jurkstas
Posts: 7
Joined: 05 Aug 2019, 17:06

Re: Rudimentary vCPU support for the lcc C compiler

Post by jurkstas » 06 Aug 2019, 16:36

Hi all, my first post here. Thank you for doing this. It is fascinating!
marcelk wrote:
29 Apr 2019, 21:15
With one command from the root directory of the gigatron-rom repository we can build lcc:

Code: Select all

$ make lcc
mkdir -p Utils/lcc/build
cd Utils/lcc && env HOSTFILE=etc/gt1h.c make all
cc -g -c -Isrc -o build/main.o src/main.c
cc -g -c -Isrc -o build/alloc.o src/alloc.c
cc -g -c -Isrc -o build/bind.o src/bind.c
cc -g -c -Isrc -o build/dag.o src/dag.c
[
 ...lots of output and compiler warnings snipped...
]
cp rt.py build/rt.py
cp asm.py build/asm.py
$
All of lcc is now in a the directory Utils/lcc/build/. The compiler has many subprograms and files, with lcc itself being the driver for it all. We can simply keep everything there.

Next compile the test program. The Gigatron backend requires Python 3.6 or up to be installed on the system.

Code: Select all

$ make Libs/Example.gt1
Utils/lcc/build/lcc -ILibs -c Libs/sys/ClearScreen.c -o Libs/sys/ClearScreen.o
Utils/lcc/build/lcc -ILibs -c Libs/sys/Newline.c -o Libs/sys/Newline.o
Utils/lcc/build/lcc -ILibs -c Libs/sys/Random.c -o Libs/sys/Random.o
Utils/lcc/build/lcc -ILibs -c Libs/stdio/putchar.c -o Libs/stdio/putchar.o
Utils/lcc/build/lcc -ILibs -c Libs/stdio/puts.c -o Libs/stdio/puts.o
Utils/lcc/build/lcc -ILibs -c Libs/Example.c -o Libs/Example.o
Utils/lcc/build/lcc -ILibs Libs/sys/ClearScreen.o Libs/sys/Newline.o Libs/sys/Random.o Libs/stdio/putchar.o Libs/stdio/puts.o Libs/Example.o -o Libs/Example.gt1
$ ls -l Libs/Example.gt1
-rw-r-----  1 marcelk  staff  743 Apr 29 22:59 Libs/Example.gt1
$
Now we have a program that we can send to a Gigatron with Utils/sendFile.py (Python 2...!), or load into an emulator.

[Edit: updated paths for changes made afterwards]
This also works on Ubuntu desktop and even Ubuntu on Windows 10 (Linux shell). First I tried to clone the repository on Windows, but gcc threw many strange errors. After that, cloned it right on the Linux shell, installed bison (sudo apt install bison) and it worked. Not sure what else needs to be installed, because I already had Python et al installed there.

Post Reply