130 likes | 342 Views
LOLCODE to 6502 Compiler. HOW DUZ I PUSH YR POP? MAH TABLES IZ A STACKZ LDA #$1337 ; WAT U SAY?. Of VM’s and SM’s: Problem. LOLCODE is a dynamic, interpreted language The 6502 is an 8-bit, 3 register, limited CPU Instruction sets inherently different and perpendicular How to solve this?.
E N D
LOLCODE to 6502 Compiler HOW DUZ I PUSH YR POP? MAH TABLES IZ A STACKZ LDA #$1337 ; WAT U SAY?
Of VM’s and SM’s: Problem • LOLCODE is a dynamic, interpreted language • The 6502 is an 8-bit, 3 register, limited CPU • Instruction sets inherently different and perpendicular • How to solve this?
Of VM’s and SM’s: Solution • Use a virtual machine built on top of the 6502 • Create a custom instruction set for the LOLCODE IR tree – SM assembly • Create a custom stack machine to execute instructions on a ‘stack’ • Abstracts memory addressing with stack
Of SM’s and VM’s: Emulate? • LOL SM is not a pure emulated stack machine • Only stack/heap/frame are emulated • Why? Emulating an entire machine is slow, snail pace on the 6502 • End result: bastardization of emulation and pure 6502 assembly
Of SM’s and VM’s: The Machine • What is the machine then? • Stack, Heap, ‘IT’ register, Frames • SP (stack pointer), FP (frame pointer), HP (heap pointer) • Most instructions operate on stack • Strings (objects) operate on heap • Function calls manipulate frame
Of SM’s and VM’s: Limitations • The 6502 CPU limits what we can do, and forces workarounds • Absolute addressing is not possible with the stack • Stack must be indirectly referenced with an offset counter (no negative offsets) • Operations using the SP are tricky
Of VM’s and SM’s: Example • LOLCODEnum R SUM OF 0 AN 1numnum IZ NOW A TROOF
Of SM’s and VM’s: Example • LOLCODE IRASSIGN |- ID:num |- ADD |- INT:0 |- INT:1ASSIGN |- ID:IT |- ID:numRECAST |- ID:num |- LTBOOL
Of SM’s and VM’s: Example • SM Assemblypush_const:0push_const:1pop_var:numpush_var:numpop_itpush_var:numto_boolpop_var:num
Of SM’s and VM’s: Example • 6502 Assembly pop_var: ; X is offset of var, known at compile TAX ; push the X register onto stack PHA LDX #$1 JMP dec_sp ; decrement the SP by 3 bytes PLA ; pop the X register off the stack TXA LDY #$0 LDA (SP), Y ; grab first byte off SM stack STA (FP, X) ; store into FP + offset X INX ; go to next byte INY ; go to next byte ; Repeat for next two bytes
Compiler Report: Progress • Most LOLCODE IR converted into a mix of macro SM, and pure assembly • 75% done when realized that current OffsetTable counter was broken • Rewrote table, but have to rewrite most of the conversions • Now supports local variables/new frames
Compiler Report: Progress • Several macro instructions already written in 6502 assembly • Variable type conversions still need to finish (because they are tricky) • String/heap operations require more analysis (creation/deletion) • Math operations require clarification
Compiler Report: The Last Mile • All LOLCODE IR converted to macro instructions (big) • All SM assembly written in 6502 ASM (bigger) • Actual heap/string/stdio working with garbage collection (small) • Code examples (tiny)