From 4ce503d02d4b91919ac21f06bedf38d915133313 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 29 Jul 2018 22:29:50 -0700 Subject: [PATCH] Clean up some code. --- src/chalk/builtin.cr | 2 +- src/chalk/compiler.cr | 3 ++- src/chalk/{ui.cr => config.cr} | 0 src/chalk/optimizer.cr | 22 ++++++++++----------- src/chalk/parser.cr | 4 ++-- src/chalk/{builder.cr => parser_builder.cr} | 2 +- src/chalk/parsers.cr | 2 -- 7 files changed, 16 insertions(+), 19 deletions(-) rename src/chalk/{ui.cr => config.cr} (100%) rename src/chalk/{builder.cr => parser_builder.cr} (98%) diff --git a/src/chalk/builtin.cr b/src/chalk/builtin.cr index 066767a..568d58a 100644 --- a/src/chalk/builtin.cr +++ b/src/chalk/builtin.cr @@ -5,7 +5,7 @@ module Chalk def initialize(@param_count) end - def generate!(into) + def generate!(codegen) end end diff --git a/src/chalk/compiler.cr b/src/chalk/compiler.cr index c028df4..1e6ca27 100644 --- a/src/chalk/compiler.cr +++ b/src/chalk/compiler.cr @@ -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) diff --git a/src/chalk/ui.cr b/src/chalk/config.cr similarity index 100% rename from src/chalk/ui.cr rename to src/chalk/config.cr diff --git a/src/chalk/optimizer.cr b/src/chalk/optimizer.cr index 0a27f4f..364d7cb 100644 --- a/src/chalk/optimizer.cr +++ b/src/chalk/optimizer.cr @@ -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 diff --git a/src/chalk/parser.cr b/src/chalk/parser.cr index f7ac9d1..2d78bdb 100644 --- a/src/chalk/parser.cr +++ b/src/chalk/parser.cr @@ -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)) diff --git a/src/chalk/builder.cr b/src/chalk/parser_builder.cr similarity index 98% rename from src/chalk/builder.cr rename to src/chalk/parser_builder.cr index 43dec6c..c91de47 100644 --- a/src/chalk/builder.cr +++ b/src/chalk/parser_builder.cr @@ -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 diff --git a/src/chalk/parsers.cr b/src/chalk/parsers.cr index 5ea58de..fbb6f9f 100644 --- a/src/chalk/parsers.cr +++ b/src/chalk/parsers.cr @@ -1,5 +1,3 @@ -require "./builder.cr" - module Chalk abstract class BasicParser(T) abstract def parse?(tokens : Array(Token),