Compare commits
No commits in common. "4de89d98a1b7877eca07ec5510450ba9beea6ac2" and "f8320bcb82d2c9a301113f5e57d2490284522c24" have entirely different histories.
4de89d98a1
...
f8320bcb82
@ -4,8 +4,11 @@ module Chalk
|
||||
# that is provided by chalk's standard library, and therefore
|
||||
# has predefined output.
|
||||
abstract class BuiltinFunction
|
||||
# Gets the number of parameters this function has.
|
||||
getter param_count : Int32
|
||||
|
||||
# Creates a new function with *param_count* parameters.
|
||||
def initialize()
|
||||
def initialize(@param_count)
|
||||
end
|
||||
|
||||
# Uses the given `Compiler::Emitter` to output code.
|
||||
@ -19,8 +22,11 @@ module Chalk
|
||||
# function also accepts trees rather than register numbers,
|
||||
# and therefore can accept and manipulate trees.
|
||||
abstract class InlineFunction
|
||||
# Gets the number of parameters this function has.
|
||||
getter param_count : Int32
|
||||
|
||||
# Creates a new function with *param_count* parameters.
|
||||
def initialize()
|
||||
def initialize(@param_count)
|
||||
end
|
||||
|
||||
# Generates code like `Compiler::CodeGenerator` would.
|
||||
|
@ -8,6 +8,11 @@ module Chalk
|
||||
class CodeGenerator
|
||||
include Emitter
|
||||
|
||||
# The register into which the return value of a function is stored.
|
||||
RETURN_REG = 14
|
||||
# The register into which the "stack pointer" is stored.
|
||||
STACK_REG = 13
|
||||
|
||||
# Gets the instructions currently emitted by this code generator.
|
||||
getter instructions
|
||||
|
||||
@ -37,7 +42,10 @@ module Chalk
|
||||
# `#generate!` call.
|
||||
def generate!(tree, function : Trees::TreeFunction | Builtin::BuiltinFunction, table, target, free)
|
||||
start_at = free
|
||||
to_stack
|
||||
# Move I to stack
|
||||
setis
|
||||
# Get to correct stack position
|
||||
addi STACK_REG
|
||||
# Store variables
|
||||
store (start_at - 1) unless start_at == 0
|
||||
# Increment I and stack position
|
||||
@ -59,7 +67,10 @@ module Chalk
|
||||
# Reduce stack pointer
|
||||
load free, start_at
|
||||
opr TokenType::OpSub, STACK_REG, free
|
||||
to_stack
|
||||
# Move I to stack
|
||||
setis
|
||||
# Get to correct stack position
|
||||
addi STACK_REG
|
||||
# Restore
|
||||
restore (start_at - 1) unless start_at == 0
|
||||
# Get call value into target
|
||||
@ -85,9 +96,12 @@ module Chalk
|
||||
generate! tree.right, table, free, free + 1
|
||||
opr tree.op, target, free
|
||||
when Trees::TreeCall
|
||||
entry = table[tree.name]?.not_nil!
|
||||
raise "Unknown function" unless entry.is_a?(FunctionEntry)
|
||||
generate! tree, entry.function, table, target, free
|
||||
entry = table[tree.name]?
|
||||
raise "Unknown function" unless entry &&
|
||||
entry.is_a?(FunctionEntry)
|
||||
function = entry.function
|
||||
raise "Invalid call" if tree.params.size != function.param_count
|
||||
generate! tree, function, table, target, free
|
||||
when Trees::TreeBlock
|
||||
table = Table.new(table)
|
||||
tree.children.each do |child|
|
||||
|
@ -53,11 +53,11 @@ module Chalk
|
||||
end
|
||||
@logger.debug("Done creating symbol table")
|
||||
|
||||
table["draw"] = FunctionEntry.new Builtin::InlineDrawFunction.new
|
||||
table["get_key"] = FunctionEntry.new Builtin::InlineAwaitKeyFunction.new
|
||||
table["get_font"] = FunctionEntry.new Builtin::InlineGetFontFunction.new
|
||||
table["set_delay"] = FunctionEntry.new Builtin::InlineSetDelayFunction.new
|
||||
table["get_delay"] = FunctionEntry.new Builtin::InlineGetDelayFunction.new
|
||||
table["set_sound"] = FunctionEntry.new Builtin::InlineSetSoundFunction.new
|
||||
table["draw_number"] = FunctionEntry.new Builtin::InlineDrawNumberFunction.new
|
||||
return table
|
||||
end
|
||||
|
||||
|
@ -1,19 +1,8 @@
|
||||
module Chalk
|
||||
module Compiler
|
||||
# The register into which the return value of a function is stored.
|
||||
RETURN_REG = 14
|
||||
# The register into which the "stack pointer" is stored.
|
||||
STACK_REG = 13
|
||||
|
||||
# Module to emit instructions and store
|
||||
# them into an existing array.
|
||||
module Emitter
|
||||
# Moves I to the next available value on the stack.
|
||||
def to_stack
|
||||
setis
|
||||
addi STACK_REG
|
||||
end
|
||||
|
||||
# Emits an instruction to load a *value* into a register, *into*.
|
||||
def load(into, value)
|
||||
inst = Ir::LoadInstruction.new into, value.to_i32
|
||||
|
@ -3,8 +3,31 @@ require "./type"
|
||||
|
||||
module Chalk
|
||||
module Builtin
|
||||
# Inline function to draw sprite at address I.
|
||||
class InlineDrawFunction < InlineFunction
|
||||
def initialize
|
||||
@param_count = 3
|
||||
end
|
||||
|
||||
def generate!(emitter, params, table, target, free)
|
||||
if !params[2].is_a?(Trees::TreeLit)
|
||||
raise "Third parameter must be a constant."
|
||||
end
|
||||
emitter.generate! params[0], table, free, free + 1
|
||||
emitter.generate! params[1], table, free + 1, free + 2
|
||||
emitter.instructions << Ir::DrawInstruction.new free, free + 1, params[2].as(Trees::TreeLit).lit.to_i32
|
||||
end
|
||||
def type
|
||||
return Compiler::FunctionType.new([Compiler::Type::U8] * 3, Compiler::Type::U0)
|
||||
end
|
||||
end
|
||||
|
||||
# Inline function to await for a key and return it.
|
||||
class InlineAwaitKeyFunction < InlineFunction
|
||||
def initialize
|
||||
@param_count = 0
|
||||
end
|
||||
|
||||
def generate!(emitter, params, table, target, free)
|
||||
emitter.instructions << Ir::AwaitKeyInstruction.new target
|
||||
end
|
||||
@ -13,13 +36,31 @@ module Chalk
|
||||
end
|
||||
end
|
||||
|
||||
# Inline function to get font for a given value.
|
||||
class InlineGetFontFunction < InlineFunction
|
||||
def initialize
|
||||
@param_count = 1
|
||||
end
|
||||
|
||||
def generate!(emitter, params, table, target, free)
|
||||
emitter.generate! params[0], table, free, free + 1
|
||||
emitter.instructions << Ir::GetFontInstruction.new free
|
||||
end
|
||||
def type
|
||||
return Compiler::FunctionType.new([Compiler::Type::U8], Compiler::Type::U0)
|
||||
end
|
||||
end
|
||||
|
||||
# Inline function to set the delay timer.
|
||||
class InlineSetDelayFunction < InlineFunction
|
||||
def initialize
|
||||
@param_count = 1
|
||||
end
|
||||
|
||||
def generate!(emitter, params, table, target, free)
|
||||
emitter.generate! params[0], table, free, free + 1
|
||||
emitter.instructions << Ir::SetDelayTimerInstruction.new free
|
||||
end
|
||||
|
||||
def type
|
||||
return Compiler::FunctionType.new([Compiler::Type::U8], Compiler::Type::U0)
|
||||
end
|
||||
@ -27,6 +68,10 @@ module Chalk
|
||||
|
||||
# Inline function to get the delay timer.
|
||||
class InlineGetDelayFunction < InlineFunction
|
||||
def initialize
|
||||
@param_count = 0
|
||||
end
|
||||
|
||||
def generate!(emitter, params, table, target, free)
|
||||
emitter.instructions << Ir::GetDelayTimerInstruction.new target
|
||||
end
|
||||
@ -34,52 +79,5 @@ module Chalk
|
||||
return Compiler::FunctionType.new(([] of Compiler::Type), Compiler::Type::U8)
|
||||
end
|
||||
end
|
||||
|
||||
# Function to set the sound timer.
|
||||
class InlineSetSoundFunction < InlineFunction
|
||||
def generate!(emitter, params, table, target, free)
|
||||
emitter.generate! params[0], table, free, free + 1
|
||||
emitter.instructions << Ir::SetSoundTimerInstruction.new free
|
||||
end
|
||||
|
||||
def type
|
||||
return Compiler::FunctionType.new([Compiler::Type::U8], Compiler::Type::U0)
|
||||
end
|
||||
end
|
||||
|
||||
# Function to draw numbers.
|
||||
class InlineDrawNumberFunction < InlineFunction
|
||||
def generate!(emitter, params, table, target, free)
|
||||
emitter.to_stack
|
||||
# Save variables from R0-R2
|
||||
emitter.store 0x2
|
||||
emitter.load free, 0x3
|
||||
emitter.addi free
|
||||
# Write BCD values to I
|
||||
emitter.generate! params[0], table, free, free + 1
|
||||
emitter.instructions << Ir::BCDInstruction.new free
|
||||
emitter.restore 0x2
|
||||
# Get the coordinates
|
||||
free = 3 if free < 3
|
||||
emitter.generate! params[1], table, free, free + 1
|
||||
emitter.generate! params[2], table, free + 1, free + 2
|
||||
# Draw
|
||||
emitter.instructions << Ir::GetFontInstruction.new 0x0
|
||||
emitter.instructions << Ir::DrawInstruction.new free, free + 1, 5
|
||||
emitter.op(Compiler::TokenType::OpAdd, free, 6)
|
||||
emitter.instructions << Ir::GetFontInstruction.new 0x1
|
||||
emitter.instructions << Ir::DrawInstruction.new free, free + 1, 5
|
||||
emitter.op(Compiler::TokenType::OpAdd, free, 6)
|
||||
emitter.instructions << Ir::GetFontInstruction.new 0x2
|
||||
emitter.instructions << Ir::DrawInstruction.new free, free + 1, 5
|
||||
# Load variables from RO-R2 back
|
||||
emitter.to_stack
|
||||
emitter.restore 0x2
|
||||
end
|
||||
|
||||
def type
|
||||
return Compiler::FunctionType.new([Compiler::Type::U8] * 3, Compiler::Type::U0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -12,33 +12,6 @@ module Chalk
|
||||
end
|
||||
end
|
||||
|
||||
# Instruction to clear the screen
|
||||
class ClearInstruction < Instruction
|
||||
def to_s(io)
|
||||
io << "clear"
|
||||
end
|
||||
|
||||
def to_bin(table, stack, index)
|
||||
return 0x00e0
|
||||
end
|
||||
end
|
||||
|
||||
# Instruction to assign a random value to a register.
|
||||
class RandomInstruction < Instruction
|
||||
def initialize(@register : Int32, @value : Int32)
|
||||
end
|
||||
|
||||
def to_s(io)
|
||||
io << "rand R"
|
||||
@register.to_s(16, io)
|
||||
io << " " << @value
|
||||
end
|
||||
|
||||
def to_bin(table, stack, index)
|
||||
return 0xc000 | (@register << 8) | (@value)
|
||||
end
|
||||
end
|
||||
|
||||
# Instruction to load a value into a register.
|
||||
class LoadInstruction < Instruction
|
||||
def initialize(@register : Int32, @value : Int32)
|
||||
@ -394,34 +367,5 @@ module Chalk
|
||||
return 0xf007 | (@into << 8)
|
||||
end
|
||||
end
|
||||
|
||||
# Instruction to set the sound timer to a value.
|
||||
class SetSoundTimerInstruction < Instruction
|
||||
def initialize(@from : Int32)
|
||||
end
|
||||
|
||||
def to_s(io)
|
||||
io << "set_sound R"
|
||||
@from.to_s(16, io)
|
||||
end
|
||||
|
||||
def to_bin(table, stack, index)
|
||||
return 0xf018 | (@from << 8)
|
||||
end
|
||||
end
|
||||
|
||||
class BCDInstruction < Instruction
|
||||
def initialize(@from : Int32)
|
||||
end
|
||||
|
||||
def to_s(io)
|
||||
io << "bcd R"
|
||||
@from.to_s(16, io)
|
||||
end
|
||||
|
||||
def to_bin(table, stack, index)
|
||||
return 0xf033 | (@from << 8)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user