Close

New C compiler for Lion32

A project log for Lion FPGA CPU/Computer

A 16-bit (and a 32bit) FPGA CPU I call Lion CPU and a computer, the Lion computer. Everything built from scratch.

leonLeon 05/19/2022 at 21:550 Comments

I made a new (kind of) port of a compiler for Lion32. This time it's the lcc. I compile the intermediate bytecode that a version of lcc produces to Lion32 assembly. The bytecode is intended for a virtual machine environment but it fits me nice. Now I have a full C compiler with floats and unions and structures and multi-dimension arrays and everything.

The only problem (now solved) was with some cases of the switch statement that I can't compile to relocatable code but it is bypassed by disabling a switch implementation optimization that used a  lookup table of addresses.

I did that in about 10 days this time,  including the c, system and math libraries. The code produced has many unnecessary push and pops but I included an optimizer in the compiler that detects and merges any needless push-pop pair to a single move instruction, it does a  good job.

Here one of the test programs that calculates and draws the mandelbrot in 32.8 seconds:

#include "clib.h"
#include "syslib.h"

float side=3.75f;
float real=-0.5f;
float imag=0.0f;
float aspect;
float dist,re,im,ci,cr,zr,zi,a,b; //float
int key,counter,colr,tim1,tim2,lcolr;
int scrw,scrh,maxiter;

void main()
{
int x,y,lim;
scrw=320;  scrh=200; maxiter=254;
aspect=(float)scrw/(float)scrh;
lim=8.0f; key=0;
dist=side/(float)scrw;
re=real-side/2.0f;
im=imag-side/aspect/2.0f;
IOout(24,1); // switches to graphics mode 1
Screen(0,15); Cls();
tim1=Timer(); lcolr=0;
for (y=0; 2*y-1<scrh; y++)
{
    ci=im+(float)y*dist;    
    for (x=0; x<scrw; x++)
    {
        cr=re+(float)x*dist;
        zr=cr;
        zi=ci;
        counter=64;
        do {
            a=zr*zr;
            b=zi*zi;
            zi=2.0f*zr*zi+ci;
            zr=a+cr-b;
            counter--;
        } while (counter>1 && 8.0f>(a+b));
        colr=65-counter;
        if (lcolr!=colr) { Screen( 0, colr); lcolr=colr; }
        Plot(x,y,1);
        Plot(x,scrh-y,1);
    }
}
tim2=Timer();
tim1=tim2-tim1;
Screen(0,30);
PosYX(21,46); printf("%d",tim1);
while (key!=32)    key=Inkey();
}

Discussions