weblog d’un abbe


Hacking a codegen

Filed under: Experiences, Fun, Hacking, Yippee!! — Tags: , , , , , , — abbe @ 0456

So, finally I’ve hacked a basic AMD64 code generator, after so many tries. This code generator is hacked in Common Lisp, and is currently generating a code for a very simple toy like language. I’ve not written any grammar specification for it. It is a LISP like language. This piece of code generator is dedicated to one of my cool friend Edwin Jose, and is thus named as louzer. Following is an example of the language for which louzer generates code.

	 (let principal rate time amount x)
	 (= principal 1000)
	 (= rate 10)
	 (= time 100)
	 (= x 100)
	 (= amount
		(/ (* principal (* rate time))
	 (print "Amount (%d) - %d is %d.\\n"  amount x (- amount x))
	 (print "Hello World, louzer\\n"))

The language code is also embedded along with the source code in the LISP file. Following is how I’m using it with GNU clisp implementation:

% clisp louzer.lisp |tee test.S
.section .text
.extern printf
.type main,@function
.globl main
 pushq %rbp
 movq %rsp, %rbp
 subq $40, %rsp
 movq $1000, %rbx
 movq %rbx, -8(%rbp)
 movq $10, %rbx
 movq %rbx, -16(%rbp)
 movq $100, %rbx
 movq %rbx, -24(%rbp)
 movq $100, %rbx
 movq %rbx, -40(%rbp)
 movq -16(%rbp), %rbx
 imul -24(%rbp), %rbx
 imul -8(%rbp), %rbx
 movq %rbx, %rax
 movq $100, %rbx
 xorq %rdx,%rdx
 idiv %rbx
 movq %rax, -32(%rbp)
 leaq __string_0,%rdi
 movq -32(%rbp), %rsi
 movq -40(%rbp), %rdx
 movq -32(%rbp), %rbx
 subq -40(%rbp), %rbx
 movq %rbx, %rcx
 xorq %rax,%rax
 call printf
 leaq __string_1,%rdi
 xorq %rax,%rax
 call printf
 xorq %rax, %rax
 movq %rbp, %rsp
 popq %rbp
.section .rodata
__string_1: .string "Hello, louzer World\n"
__string_0: .string "Amount (%d) - %d is %d.\n"
/* Generated by louzer :) */

Above is the piece of AMD64 assembly code emitted by the louzer. So, now time to assemble and link the above assembly code and generate the output of the above code.

% cc -o test test.S
% ./test
Amount (10000) - 100 is 9900.
Hello, louzer World

Voila. Oh, sorry to keep you waiting, now you can download the louzer.lisp and have fun. BtW, code is not perfect and has couple of limitations, which I’ve not fixed due to lack of time, as I’ve an exam day after tomorrow. So, I’ll be able to work on it only after 20081219. Happy hacking codegens…;)

NOTE (for Grammar Nazis): Forgive me for any grammatical mistakes you encounter above, and ofcourse point out the mistake :).

P.S. Forgot to mention, you’ll need an AMD64 architecture CPU, toolchain and POSIX OS to test out above stuff.


1 Comment »

  1. Elegant..

    Comment by Edwin — 20081216 @ 1440

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: