Compare commits

...

2 Commits

Author SHA1 Message Date
Danila Fedorin 5c362d8e13 "Fix" register allocation. 2018-07-28 18:58:04 -07:00
Danila Fedorin 946db467ee Format code. 2018-07-28 17:53:07 -07:00
4 changed files with 113 additions and 113 deletions

View File

@ -1,21 +1,21 @@
module Chalk module Chalk
class BuiltinFunction class BuiltinFunction
getter param_count : Int32 getter param_count : Int32
def initialize(@param_count) def initialize(@param_count)
end end
def generate!(into) def generate!(into)
end end
end end
class InlineFunction class InlineFunction
getter param_count : Int32 getter param_count : Int32
def initialize(@param_count)
end
def generate!(codegen, params, table, target, free) def initialize(@param_count)
end end
def generate!(codegen, params, table, target, free)
end
end end
end end

View File

@ -22,45 +22,45 @@ module Chalk
end end
def generate!(tree, function : InlineFunction, table, target, free) def generate!(tree, function : InlineFunction, table, target, free)
start = free start = free
function.generate!(self, tree.params, table, target, free) function.generate!(self, tree.params, table, target, free)
end end
def generate!(tree, function : TreeFunction | BuiltinFunction, table, target, free) def generate!(tree, function : TreeFunction | BuiltinFunction, table, target, free)
start_at = free start_at = free
# Move I to stack # Move I to stack
setis setis
# Get to correct stack position # Get to correct stack position
addi STACK_REG addi STACK_REG
# Store variables # Store variables
store (start_at - 1) unless start_at == 0 store (start_at - 1) unless start_at == 0
# Increment I and stack position # Increment I and stack position
load free, start_at load free, start_at
opr TokenType::OpAdd, STACK_REG, free opr TokenType::OpAdd, STACK_REG, free
addi free addi free
# Calculate the parameters # Calculate the parameters
tree.params.each do |param| tree.params.each do |param|
generate! param, table, free, free + 1 generate! param, table, free, free + 1
free += 1 free += 1
end end
# Call the function # Call the function
tree.params.size.times do |time| tree.params.size.times do |time|
loadr time, time + start_at loadr time, time + start_at
end end
call tree.name call tree.name
# Reduce stack pointer # Reduce stack pointer
load free, start_at load free, start_at
opr TokenType::OpSub, STACK_REG, free opr TokenType::OpSub, STACK_REG, free
# Move I to stack # Move I to stack
setis setis
# Get to correct stack position # Get to correct stack position
addi STACK_REG addi STACK_REG
# Restore # Restore
restore (start_at - 1) unless start_at == 0 restore (start_at - 1) unless start_at == 0
# Get call value into target # Get call value into target
loadr target, RETURN_REG loadr target, RETURN_REG
end end
def generate!(tree, table, target, free) def generate!(tree, table, target, free)
@ -86,16 +86,17 @@ module Chalk
when TreeBlock when TreeBlock
table = Table.new(table) table = Table.new(table)
tree.children.each do |child| tree.children.each do |child|
free += generate! child, table, free, free free += generate! child, table, free, free + 1
end end
when TreeVar when TreeVar
entry = table[tree.name]? entry = table[tree.name]?
if entry == nil if entry == nil
entry = VarEntry.new free entry = VarEntry.new free
free += 1
table[tree.name] = entry table[tree.name] = entry
end end
raise "Unknown variable" unless entry.is_a?(VarEntry) raise "Unknown variable" unless entry.is_a?(VarEntry)
generate! tree.expr, table, entry.register, free + 1 generate! tree.expr, table, entry.register, free
return 1 return 1
when TreeAssign when TreeAssign
entry = table[tree.name]? entry = table[tree.name]?
@ -118,13 +119,13 @@ module Chalk
when TreeWhile when TreeWhile
before_cond = @instructions.size before_cond = @instructions.size
generate! tree.condition, table, free, free + 1 generate! tree.condition, table, free, free + 1
sne target, 0 sne free, 0
cond_jump = jr 0 cond_jump = jr 0
old_size = @instructions.size old_size = @instructions.size
generate! tree.block, table, free, free + 1 generate! tree.block, table, free, free + 1
after_jump = jr 0 after_jump = jr 0
cond_jump.offset = @instructions.size - old_size + 1 cond_jump.offset = @instructions.size - old_size + 1
after_jump.offset = before_cond - instructions.size + 1 after_jump.offset = before_cond - instructions.size + 1
when TreeReturn when TreeReturn

View File

@ -1,59 +1,58 @@
module Chalk module Chalk
class InlineDrawFunction < InlineFunction class InlineDrawFunction < InlineFunction
def initialize def initialize
@param_count = 3 @param_count = 3
end
def generate!(emitter, params, table, target, free)
if !params[2].is_a?(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 << DrawInstruction.new free, free + 1, params[2].as(TreeLit).lit.to_i32
end
end end
class InlineAwaitKeyFunction < InlineFunction def generate!(emitter, params, table, target, free)
def initialize if !params[2].is_a?(TreeLit)
@param_count = 0 raise "Third parameter must be a constant."
end end
emitter.generate! params[0], table, free, free + 1
emitter.generate! params[1], table, free + 1, free + 2
emitter.instructions << DrawInstruction.new free, free + 1, params[2].as(TreeLit).lit.to_i32
end
end
def generate!(emitter, params, table, target, free) class InlineAwaitKeyFunction < InlineFunction
emitter.instructions << AwaitKeyInstruction.new target def initialize
end @param_count = 0
end end
class InlineGetFontFunction < InlineFunction def generate!(emitter, params, table, target, free)
def initialize emitter.instructions << AwaitKeyInstruction.new target
@param_count = 1 end
end end
def generate!(emitter, params, table, target, free) class InlineGetFontFunction < InlineFunction
emitter.generate! params[0], table, free, free + 1 def initialize
emitter.instructions << GetFontInstruction.new free @param_count = 1
end
end end
class InlineSetDelayFunction < InlineFunction def generate!(emitter, params, table, target, free)
def initialize emitter.generate! params[0], table, free, free + 1
@param_count = 1 emitter.instructions << GetFontInstruction.new free
end end
end
def generate!(emitter, params, table, target, free) class InlineSetDelayFunction < InlineFunction
emitter.generate! params[0], table, free, free + 1 def initialize
emitter.instructions << SetDelayTimerInstruction.new free @param_count = 1
end
end end
class InlineGetDelayFunction < InlineFunction def generate!(emitter, params, table, target, free)
def initialize emitter.generate! params[0], table, free, free + 1
@param_count = 0 emitter.instructions << SetDelayTimerInstruction.new free
end
def generate!(emitter, params, table, target, free)
emitter.instructions << GetDelayTimerInstruction.new target
end
end end
end
class InlineGetDelayFunction < InlineFunction
def initialize
@param_count = 0
end
def generate!(emitter, params, table, target, free)
emitter.instructions << GetDelayTimerInstruction.new target
end
end
end end

View File

@ -295,15 +295,15 @@ module Chalk
end end
def to_s(io) def to_s(io)
io << "draw R" io << "draw R"
x.to_s(16, io) x.to_s(16, io)
io << " R" io << " R"
y.to_s(16, io) y.to_s(16, io)
io << " " << height io << " " << height
end end
def to_bin(i, index) def to_bin(i, index)
return 0xd000 | (@x << 8) | (@y << 4) | height return 0xd000 | (@x << 8) | (@y << 4) | height
end end
end end
@ -314,28 +314,28 @@ module Chalk
end end
def to_s(io) def to_s(io)
io << "getk R" io << "getk R"
@into.to_s(16, io) @into.to_s(16, io)
end end
def to_bin(i, index) def to_bin(i, index)
return 0xf00a | (@into << 8) return 0xf00a | (@into << 8)
end end
end end
class GetFontInstruction < Instruction class GetFontInstruction < Instruction
property from : Int32 property from : Int32
def initialize(@from) def initialize(@from)
end end
def to_s(io) def to_s(io)
io << "font R" io << "font R"
@from.to_s(16, io) @from.to_s(16, io)
end end
def to_bin(i, index) def to_bin(i, index)
return 0xf029 | (@from << 8) return 0xf029 | (@from << 8)
end end
end end
@ -346,12 +346,12 @@ module Chalk
end end
def to_s(io) def to_s(io)
io << "set_delay R" io << "set_delay R"
@from.to_s(16, io) @from.to_s(16, io)
end end
def to_bin(i, index) def to_bin(i, index)
return 0xf015 | (@from << 8) return 0xf015 | (@from << 8)
end end
end end
@ -362,12 +362,12 @@ module Chalk
end end
def to_s(io) def to_s(io)
io << "get_delay R" io << "get_delay R"
@into.to_s(16, io) @into.to_s(16, io)
end end
def to_bin(i, index) def to_bin(i, index)
return 0xf007 | (@into << 8) return 0xf007 | (@into << 8)
end end
end end
end end