This page looks best with JavaScript enabled

CompileGPT - the perfect compiler

 ·  ☕ 3 min read

I love compilers. They make modern software possible, by taking source code humans can work with, and transforming it into code computers can actually execute. However, compilers have a problem - they throw error messages all the time. Wrong syntax? ERROR! Forgot a bracket? ERROR! Missing semicolon? ERROR! This gets really annoying, and is a critical bottleneck in developer productivity. Imagine the thousands of developer-hours that could be saved if we never had to deal with this problem again, especially halfway through perfectly good build pipelines.

Luckily for us, we live in the age of AI! So, we just need to ask OpenAI’s GPT nicely to compile programs for us. So, that’s exactly what I did. Once we have a compiled program, we can simply assemble, link, and run it. For demonstration, consider this C program, which has a few obvious problems:

1
2
3
4
5
6
7
#include <stdio.h

int main()
{
    printf("Hello, world!\n")
    return 0
}

I modified my gpt-chat-cli script to include a system prompt asking GPT to behave like a compiler, and save the output to a file a.S, which can be found here. Running the script on a simple C program looks something like this:

$ cat a.c | bash compilegpt.sh
GPT:
I found two errors in your code:

1. Missing '>' in '#include <stdio.h'
2. Missing ';' after 'printf("hello, CompileGPT")' and 'return 0'

Here's the corrected version of your code:

```c
#include <stdio.h>

int main() {
    printf("Hello, CompileGPT");
    return 0;
}
```

Now, let's compile it into assembly code using AT&T syntax:

```assembly
    .file   "main.c"
    .section        .rodata
.LC0:
    .string "Hello, CompileGPT"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $.LC0, %edi
    call    puts
    movl    $0, %eax
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (GNU) 12.2.1 20220107 (CompileGPT v4.0.0)"
    .section        .note.GNU-stack,"",@progbits
```

We can then compile the code written to a.S with gas, the gnu assembler:

1
as a.S -o a.o

This gives us an assembled object file, but it hasn’t been linked yet. So, it does not know what anything outside the program itself looks like - it has no idea about what printf is referring to, for example. To link it, we can use ld, the GNU linker:

1
ld -m elf_x86_64 -dynamic-linker /lib/ld-linux.so.2 -o a a.o /usr/lib/crt1.o /usr/lib/libc.so /usr/lib/crti.o /usr/lib/crtn.o

We can now execute our binary, written to the binary:

$ ld.so ./a
Hello, CompileGPT

And it works!

To try CompileGPT yourself, you can download the script to call OpenAI APIs here, and run the following commands to experience the next generation of compiler technology.

1
2
3
4
cat a.c | bash compilegpt.sh
as a.S -o a.o
ld -m elf_x86_64 -dynamic-linker /lib/ld-linux.so.2 -o a a.o /usr/lib/crt1.o /usr/lib/libc.so /usr/lib/crti.o /usr/lib/crtn.o
ld.so ./a
Share on

Pranav Gade
WRITTEN BY
Pranav Gade
Software Hacker