Writing a Python Compiler for Practice

I read quite a bit about processors and architectures recently; mainly opcodes, integrated circuits, etc. I have been a python developer for several years now and I would like to get some practice writing machine code.

I thought that I would compile a very simple python script in machinecode as a way to practice it. The script looks like this:

a = 2 b = 3 c = a + b print c 

I am writing a compiler in python because I am not as good in C as I am in python. I looked around a bit and I have the following python libraries that might help, i.e.

 binascii.hexify(hex(2)) <-- should convert 2 to binary, correct? file = open('/usr/local/bin/my_sample_program','wb') <-- should write the resulting binary file 

I still need to find opcodes for the Intel Core i5, but this should be easy.

My question is this:

1) How to write the operation code to a file? In other words, suppose that the operation code for setting the register that contains the value 2 is 0010, how to do it, is it the first four numbers in the first line of program execution?

2) How to tell OS, OS X or Ubuntu to load the program into physical memory? I assume that the first thing the compiler does is write instructions for the OS on the resulting binary?

3) Any resources you might know about this can help me.

+8
compiler-construction python
source share
2 answers

This is the project you are planning there. Besides learning how the compiler works, you also need to read tons of information about the details of the operating system on downloadable file formats such as ELF.

I would suggest that you would release the assembly file as the output of your compiler. You can then use the existing assembler to convert the file to machine code. In fact, this is what most C compilers (including GCC) do "below the surface".

EDIT: The output of the compiler or assembler is usually an object file. This is later combined with other object files using the linker. Writing an entire tool chain, compiler, assembler, linker, and other related tools would be easy for a few man-years. In this light, I don’t think you should see a straightforward solution, such as using an existing assembler and linker as a hoax.

+7
source share

Compiling python is not easy. You can see the pypy that the compiler has right at the point in time.

Another option is to start with python bytecode , which is saved in the .pyc file if the python program runs the standard Cpython interpreter. This has a limited number of instructions for which you will need to generate assembly / executable code for your processor.

Please note that you will also need to write a large amount of code to implement all the built-in types and functions!

+3
source share

All Articles