Clean up some code.

This commit is contained in:
Danila Fedorin 2018-07-29 22:29:50 -07:00
parent dff0c8078c
commit 4ce503d02d
7 changed files with 16 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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