190 likes | 564 Views
LOLCODE to 6502. I CAN HAS TIPE CASTZ? MAH CEE-PEE-YUU IZ 8 BITZ n00b: LDA $1337. Limitations. 8-bit A, X, Y registers Limited range of numbers Integers: -128 to 127 (signed) Floats: 4-bit mantissa, 4-bit exponent -8x10 8 to 8x10 8. Limitations.
E N D
LOLCODE to 6502 I CAN HAS TIPE CASTZ? MAH CEE-PEE-YUU IZ 8 BITZ n00b: LDA $1337
Limitations • 8-bit A, X, Y registers • Limited range of numbers • Integers:-128 to 127 (signed) • Floats:4-bit mantissa, 4-bit exponent-8x108 to 8x108
Limitations • Strings – a String variable is a pointer to a location in memory that holds character data • Problem; addresses are 16-bit • LOLCODE has dynamic variables (change types at run time) • How to handle this?
Solutions • Variable is represented by 3 8-bit bytes in memory • 1st byte = variable data type0 = undefined1 = integer (9 = negative integer)2 = float (10 = negative float) • 3 = boolean4 = string • Values 5 – F can be used for expansion
Solutions • Integers are now 17-bit (16-bit data, 1-bit sign) • Sign is stored in type-flag highest bit • 2nd byte stores low 8 bits, 3rd byte stores high 8 bits • Floats are now 16bit • 2nd byte holds mantissa, 3rd is exponents
Solutions • Strings are a 16-bit address stored in 2nd and 3rd bytes • Booleans – 2nd and 3rd bytes treated like an integer • Zero = false, Non-zero = true
Solutions • Variable type can change by simply changing its type value • Involves some additional code overhead when types change for most typecasts • May result in runtime conversion errors (i.e. String data is not a number, float too large/loss precision)
LOLCODE to 6502 • An LOLCODE input file is opened • Input is scanned for tokens, sent to parser • Parser constructs a parse-tree • Parse-tree is transformed into an IR-tree • Compiler reads IR-tree and outputs 6502 assembly • Assembler assembles assembly ;d
LOLCODE Example BTW Finds the greatest common divisor GCD of two numbers HAI HOW DUZ I gcd YR n1 AN YR n2 AN YR n3 BOTH OF BOTH SAEM 0 AN MOD OF n1 AN n3 AN BOTH SAEM 0 AN MOD OF n2 AN n3 O RLY? YA RLY food R n3 OIC FOUND YR gcd n1 n2 DIFF OF n3 AN 1 IF U SAY SO I HAS A n1 I HAS A n2 VISIBLE "First number " GIMMEH n1 VISIBLE "Second number " GIMMEH n2 VISIBLE "GCD of both is " gcd n1 n2 SMALLR OF n1 AN n2 KTHXBYE
LOLCODE Function Calling • LOLCODE does not have tokens to indicate that a function is called • Implied from both prefix argument notation (SUM OF x AN y, FUNC_ID a1 a2 a3) • At parse time, parser cannot distinguish a variable ID from a function ID • IDs must be transformed into CALLs, after parsing using a symbol table
LOLCODE “IT” • Conditional structures make use of a special variable called IT to determine outcome • Expression by itself will store result implicitly in IT • IT can be modified explicitly • IT must be type cast into appropriate type for use with conditionals
LOLCODE STDIO • A 6502 machine does not have native STDIO (depends on the machine/kernel) • 6502 Simulator emulates a text console • All IO is memory-mapped • Input -> Read from memory location • Output -> Write to memory location
LOLCODE:6502 Mapping • 0x0000 – 0x00FF: Zero-Page, fast memory accesses, used for temporary “registers” • 0x0100 – 0x01FF: Stack, used for storing function call arguments, return address • 0x0200 – 0x9FF: Variable range • 0x1000 – 0x3FFF: Heap/String range • 0x4000 – 0x7FFF: Program code • 0x8000 – 0xDFFF: Available for expansion • 0xE000 – 0xFFFF: STDIO mapped here, etc
LOLCODE IR-Tree • The parse-tree created from the input is transformed into an IR-tree using several things: • Expressions are broken into at most binomial equations using temporaries • a = b + c / d * e becomest1 = d * et2 = c / t1t3 = b + t2a = t3
LOLCODE IR-Tree • Variables are renamed into memory addresses (cat becomes 0x0200) • Function definitions are treated like code, and given a label to jump to • String constants are assigned a label to read from
LOLCODE Memory Accessing • The only way to load data into A, X, or Y from a 16-bit memory location is to use indirect memory addressing • For example, data at $1234 has value $5678 • Store low byte at a zero-page location, and high byte in the next location • $0020 = $34, $0021 = $12
LOLCODE Memory Accessing • LDA ($0020), X • Reads a memory address stored at $0020, $0021 • Adds X to that memory address • Fetches the data at the memory address, and stores in the accumulator
What’s next? • Create a memory manager, to handle heap/variables • Type conversion subroutines • String manipulation/storage, ties in with the MM • Code generation of all LOLCODE aspects • Optimization of flow structure, reduce memory accesses, etc