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:

#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
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:

#include <stdio.h>

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

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

    .file   "main.c"
    .section        .rodata
    .string "Hello, CompileGPT"
    .globl  main
    .type   main, @function
    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
    .cfi_def_cfa 7, 8
    .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:

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:

ld -m elf_x86_64 -dynamic-linker /lib/ -o a a.o /usr/lib/crt1.o /usr/lib/ /usr/lib/crti.o /usr/lib/crtn.o

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

$ ./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.

cat a.c | bash
as a.S -o a.o
ld -m elf_x86_64 -dynamic-linker /lib/ -o a a.o /usr/lib/crt1.o /usr/lib/ /usr/lib/crti.o /usr/lib/crtn.o ./a
Share on

Pranav Gade
Pranav Gade
Software Hacker