Clean up some code.
This commit is contained in:
parent
dff0c8078c
commit
4ce503d02d
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
module Chalk
|
module Chalk
|
||||||
class Optimizer
|
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)
|
if inst.is_a?(LoadRegInstruction)
|
||||||
return inst.from == inst.into
|
return inst.from == inst.into
|
||||||
|
@ -11,20 +7,21 @@ module Chalk
|
||||||
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
|
||||||
|
|
|
@ -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))
|
||||||
|
|
|
@ -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
|
|
@ -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),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user