Skip to content

jmikkola/writing-a-c-compiler-python

Repository files navigation

This implements the extra credit from the compiler.

So far, this completes chapter 18 including the extra credit.

Running

./compiler.sh <foo.c>

flags:

  • To control the stage, one of --lex, --parse, --validate, --tacky, --codegen, or --emit.
  • To print the results of a stage, add --print.
  • To create an object file instead of an executable, -c

Testing

./test.sh

This assumes that the tests are checked out at ../writing-a-c-compiler-tests/.

This passes along flags to the test command in that directory, such as --failfast, --latest-only, --stage lex, or --extra-credit.

GDB notes

To get gdb to remember history, add set history save on to ~/.gdbinit

Example commands to start a program:

layout reg
layout asm
break main
run

Stepping one instruction

si

Example of printing an integer value in the stack (at -0x24(%rbp)):

print *(int*)($rbp - 0x24)

Example of printing a floating value in a register:

print $xmm0.v2_double[0]

Debugging the parser

python -i parser.py

then

import lexer
tokens = lexer.tokenize('struct example')
p = Parser(tokens)
p.parse_type_specifier()

or

import lexer
Parser(lexer.tokenize('struct example eg;')).parse_block_item()

About

The result of following https://nostarch.com/writing-c-compiler using Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages