Compare commits

..

No commits in common. "4ce503d02d4b91919ac21f06bedf38d915133313" and "5c362d8e13cdd9b59220e2781dd8029422ce25f6" have entirely different histories.

8 changed files with 20 additions and 16 deletions

View File

@ -2,7 +2,7 @@ require "./lexer.cr"
require "./parsers.cr" require "./parsers.cr"
module Chalk module Chalk
module ParserBuilder module Builder
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

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

View File

@ -22,6 +22,7 @@ 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,9 +54,8 @@ 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 optimizer.optimize generator.generate! return generator.generate!
end end
private def create_code(tree : BuiltinFunction, table) private def create_code(tree : BuiltinFunction, table)

View File

@ -1,5 +1,9 @@
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
@ -7,21 +11,20 @@ module Chalk
return false return false
end end
private def optimize!(instructions, range) private def optimize!(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
def optimize(instructions) private def optimize!
instructions = instructions.dup block_boundaries = [@instructions.size]
block_boundaries = [instructions.size] @instructions.each_with_index do |inst, i|
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
@ -32,10 +35,9 @@ 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!(instructions, range) offset += optimize!(range)
previous = boundary previous = boundary
end end
return instructions
end end
end end
end end

View File

@ -1,8 +1,8 @@
require "./parser_builder.cr" require "./builder.cr"
module Chalk module Chalk
class Parser class Parser
include ParserBuilder include Builder
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

@ -1,3 +1,5 @@
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),