keyvank / 30cc

30 C Compiler
49 stars 6 forks source link
c compiler educational

30 C Compiler!

30cc (Pronounced as CCC, because in the Persian language, the number 30 is pronounced as C) is a toy C compiler written in C, with the goal of being able to compile itself 🤝 This is my first attempt in writing a self-hosting software! What is a self-hosting software?

30cc emits x86-64 assembly as its output. The outputs are totally unoptimized, but that's fine, the project aims to be educational.

Currently, this is the most complicated code 30cc is able to successfully parse and compile a simple implementation of a linked-list:

void *malloc(int sz);
void printf(char *s, ...);

struct node
{
    int val;
    void *next;
};

struct linked_list
{
    struct node *first;
};

struct linked_list *new_list()
{
    struct linked_list *ret = (struct linked_list *)malloc(8);
    return ret;
}

void add_val(struct linked_list *lst, int val)
{
    struct node *new = (struct node *)malloc(16);
    *((int *)(&new->next)) = new;
    new->val = val;
    if (lst->first)
    {
        struct node *curr = lst->first;
        while (curr->next)
        {
            curr = (struct node *)curr->next;
        }
        curr->next = (void *)new;
    }
    else
    {
        lst->first = new;
    }
}

int main()
{
    struct linked_list *lst = new_list();
    add_val(lst, 123);
    add_val(lst, 432);
    add_val(lst, 999);
    add_val(lst, 873);
    struct node *curr = lst->first;
    while (curr)
    {
        printf("%u\n", curr->val);
        curr = (struct node *)curr->next;
    }
}

Usage

make run program=./examples/inp.c argumnets=something

Contribute

Tests

To run tests use

python scripts/test.py update

Then check the output of the tests.

If you are on mac use ./scripts/test_mac.sh to run the tests in docker.