I’ve gotten a few emails about the lack of posts, so rather than reply to everyone, I figure I’ll set it straight:
I’ve been working on a compiler for class. Boom!
Something kinda cool though:
I can take code that looks like this:
extern void print_int(int x);
extern void print_string(char x[]);
int two[2];
int five[5], one;
void love_me(void) {
print_string("You love me, don't you?n");
}
void main( void ) {
two = 4;
love_me();
}
And turn it into code that looks like this:
.data
two: .space 8
five: .space 20
one: .space 4
_string_0: .asciiz "You love me, dont you?n"
.text
jal love_me
la $sp, 4($sp)
love_me:
# Function prologue
sw $fp, -4($sp) # save old $fp
sw $ra, -8($sp) # save return address
la $fp, 0($sp) # set up frame pointer
la $sp, -12($sp) # Update stack pointer and allocate stack frame
# Load variables on to the stack
la $t9, _string_0
sw $t9, -4($sp)
la $sp, -4($sp)
# Call the method
jal print_string
la $sp, 4($sp)
# Function epilogue
la $sp, 0($fp) # Restore stack pointer
lw $ra, -8($sp) # Restore return address
lw $fp, -4($sp) # Restore frame pointer
jr $ra # Return
jal main
la $sp, 4($sp)
main:
# Function prologue
sw $fp, -4($sp) # save old $fp
sw $ra, -8($sp) # save return address
la $fp, 0($sp) # set up frame pointer
la $sp, -12($sp) # Update stack pointer and allocate stack frame
li $4, 4 # Load _tmp_0
sw $4, -16($fp)
lw $4, -16($fp) # Load two
sw $4, two
# Load variables on to the stack
# Call the method
jal love_me
la $sp, 4($sp)
# Function epilogue
la $sp, 0($fp) # Restore stack pointer
lw $ra, -8($sp) # Restore return address
lw $fp, -4($sp) # Restore frame pointer
jr $ra # Return
print_int:
li $v0, 1
lw $a0, 0($sp)
syscall
jr $ra
print_string:
li $v0, 4
lw $a0, 0($sp)
syscall
jr $ra
When I get it done, I’ll open-source it and be back for more ruby joy! So stay tuned amigos!
0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment