Compare commits

...

2 Commits

Author SHA1 Message Date
Danila Fedorin 4ce503d02d Clean up some code. 2018-07-29 22:29:50 -07:00
Danila Fedorin dff0c8078c Remove useless assignment. 2018-07-29 21:46:10 -07:00
8 changed files with 16 additions and 20 deletions

View File

@ -5,7 +5,7 @@ module Chalk
def initialize(@param_count) def initialize(@param_count)
end end
def generate!(into) def generate!(codegen)
end end
end end

View File

@ -22,7 +22,6 @@ module Chalk
end end
def generate!(tree, function : InlineFunction, table, target, free) def generate!(tree, function : InlineFunction, table, target, free)
start = free
function.generate!(self, tree.params, table, target, free) function.generate!(self, tree.params, table, target, free)
end end

View File

@ -54,8 +54,9 @@ module Chalk
private def create_code(tree : TreeFunction, table) private def create_code(tree : TreeFunction, table)
generator = CodeGenerator.new table, tree generator = CodeGenerator.new table, tree
optimizer = Optimizer.new
@logger.debug("Generating code for #{tree.name}") @logger.debug("Generating code for #{tree.name}")
return generator.generate! return optimizer.optimize generator.generate!
end end
private def create_code(tree : BuiltinFunction, table) private def create_code(tree : BuiltinFunction, table)

View File

@ -1,30 +1,27 @@
module Chalk module Chalk
class Optimizer class Optimizer
def initialize(instructions : Array(Instruction)) private def check_dead(inst)
@instructions = instructions.dup
end
private def check_dead(inst)
if inst.is_a?(LoadRegInstruction) if inst.is_a?(LoadRegInstruction)
return inst.from == inst.into return inst.from == inst.into
end end
return false return false
end end
private def optimize!(range) private def optimize!(instructions, range)
offset = 0 offset = 0
range.each do |index| range.each do |index|
if check_dead(@instructions[index + offset]) if check_dead(instructions[index + offset])
@instructions.delete_at(index + offset) instructions.delete_at(index + offset)
offset -= 1 offset -= 1
end end
end end
return offset return offset
end end
private def optimize! def optimize(instructions)
block_boundaries = [@instructions.size] instructions = instructions.dup
@instructions.each_with_index do |inst, i| block_boundaries = [instructions.size]
instructions.each_with_index do |inst, i|
if inst.is_a?(JumpRelativeInstruction) if inst.is_a?(JumpRelativeInstruction)
block_boundaries << (inst.offset + i) block_boundaries << (inst.offset + i)
end end
@ -35,9 +32,10 @@ module Chalk
offset = 0 offset = 0
block_boundaries.each do |boundary| block_boundaries.each do |boundary|
range = (previous + offset)...(boundary + offset) range = (previous + offset)...(boundary + offset)
offset += optimize!(range) offset += optimize!(instructions, range)
previous = boundary previous = boundary
end end
return instructions
end end
end end
end end

View File

@ -1,8 +1,8 @@
require "./builder.cr" require "./parser_builder.cr"
module Chalk module Chalk
class Parser class Parser
include Builder include ParserBuilder
private def create_type private def create_type
either(type(TokenType::KwU0), type(TokenType::KwU8), type(TokenType::KwU12)) either(type(TokenType::KwU0), type(TokenType::KwU8), type(TokenType::KwU12))

View File

@ -2,7 +2,7 @@ require "./lexer.cr"
require "./parsers.cr" require "./parsers.cr"
module Chalk module Chalk
module Builder module ParserBuilder
def type(type) : BasicParser(Token) def type(type) : BasicParser(Token)
return TypeParser.new(type).as(BasicParser(Token)) return TypeParser.new(type).as(BasicParser(Token))
end end

View File

@ -1,5 +1,3 @@
require "./builder.cr"
module Chalk module Chalk
abstract class BasicParser(T) abstract class BasicParser(T)
abstract def parse?(tokens : Array(Token), abstract def parse?(tokens : Array(Token),