Skip to content

Commit

Permalink
compiler flags for slices vs offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioAriasC committed Jan 29, 2022
1 parent e6a632b commit 17f55c5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 67 deletions.
7 changes: 1 addition & 6 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#!/usr/bin/env bash
#shards build --release --verbose --progress --stats --define=no_opt
#shards build --release --verbose --progress --stats --define=no_opt --define=gc_none
#shards build --release --verbose --progress --stats --define=cache --define=gc_none
#shards build --release --verbose --progress --stats --define=gc_none
#shards build --release --verbose --progress --stats --define=cache
#shards build --release --verbose --progress --stats --define=slices
#shards build --release --verbose --progress --stats --define=slice
shards build --release --verbose --progress --stats
4 changes: 4 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,15 @@ private def assert_instructions(expected : Instructions, actual : Instructions)
end

def concat(instructions : Array(Instructions)) : Instructions
{% if flag?(:slice) %}
mem = IO::Memory.new
instructions.each do |ins|
mem.write(ins)
end
return mem.to_slice
{% else %}
return instructions.sum
{% end %}
end

def test_compile_result(input : String, expected_constants : Array, expected_instructions : Array(Instructions))
Expand Down
76 changes: 16 additions & 60 deletions src/code.cr
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
macro ret_ins
puts typeof(ex)
puts ex.message
{% if flag?(:slice)%}
return Instructions.empty
{% else %}
return [] of UInt8
{% end%}
end

# macro cache_arrays(cache, v)
# key = {ins, i}
# cached_instruction = {{cache}}[key]?
# if cached_instruction.nil?
# cached_instruction = {{v}}
# {{cache}}[key] = cached_instruction
# end
# return cached_instruction
# end

module Code
extend self
enum Opcode
Expand Down Expand Up @@ -94,8 +88,11 @@ module Code
Opcode::OpCurrentClosure => "OpCurrentClosure".to_definition,
}

{% if flag?(:slice) %}
alias Instructions = Bytes

{% else %}
alias Instructions = Array(UInt8)
{% end%}
def lookup(op : Opcode) : Definition
definition = DEFINITIONS[op]
if !definition.nil?
Expand Down Expand Up @@ -129,68 +126,27 @@ module Code

def make(op : Opcode) : Instructions
lookup(op)
{% if flag?(:slice) %}
return Instructions[op.to_u8]
{% else %}
return [op.to_u8]
{% end %}
rescue ex
ret_ins
end

# struct InstructionsCache
# # A two level cache
# @base = {} of UInt64 => Array(Instructions?)
#
# def []=(key : Tuple(Instructions, Int32), value : Instructions)
# key_0 = key[0].object_id
# inner = @base[key_0]?
# if inner.nil?
# inner = Array.new(key[0].size) { nil.as(Instructions?) }
# @base[key_0] = inner
# end
# inner[key[1]] = value
# end
#
# def []?(key : Tuple(Instructions, Int32)) : Instructions?
# inner = @base[key[0].object_id]?
# if inner.nil?
# return nil
# else
# return inner[key[1]]?
# end
# end
# end
#
# private ONSET_CACHE = InstructionsCache.new
# private OFFSET_CACHE = InstructionsCache.new

def onset(ins : Instructions, i : Int32) : Instructions
# {% if !flag?(:no_opt) %}
# cache_arrays ONSET_CACHE, ins[0..(i - 1)]
# {% else %}
ins[0..(i - 1)]
# {% end %}
end

alias MBytes = OffsetArray | Instructions | Bytes

private def offset(ins : Instructions, i : Int32) : MBytes
# {% if !flag?(:no_opt) %}
# {% if !flag?(:cache) %}
# {% if !flag?(:slices)%}
# OffsetArray.new(ins, i)
# {% else %}
# offset_slice(ins, i)
# {% end %}
# {% else %}
# cache_arrays OFFSET_CACHE, ins[i...(ins.size)]
# {% end %}
# {% else %}
{% if flag?(:slice) %}
ins[i...(ins.size)]
# {% end %}
end

private def offset_slice(ins : Instructions, i : Int32) : Bytes
# ptr = Pointer.malloc(ins.size - i){|j| ins[i + j]}
# return Slice.new(ptr, ins.size - i)
Slice.new(ins.size - i){|j| ins[i + j]}
{% else %}
OffsetArray.new(ins, i)
{% end %}
end

def read_int(ins : Instructions, i : Int32) : Int32
Expand Down
12 changes: 12 additions & 0 deletions src/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ module Compilers
property last_instruction
property previous_instruction

{% if flag?(:slice) %}
def initialize(@instructions : Instructions = Instructions.empty, @last_instruction = EmittedInstruction.new, @previous_instruction = EmittedInstruction.new)
end
{% else %}
def initialize(@instructions : Instructions = [] of UInt8, @last_instruction = EmittedInstruction.new, @previous_instruction = EmittedInstruction.new)
end
{% end %}
end

struct MCompiler
Expand Down Expand Up @@ -229,16 +234,23 @@ module Compilers
private def add_instruction(ins : Instructions) : Int32
pos = current_instructions.size
current_scope
{% if flag?(:slice) %}
current_scope.instructions = concat(current_scope.instructions, ins)
{% else %}
current_scope.instructions += ins
{% end %}
return pos
end

{% if flag?(:slice) %}
private def concat(a : Instructions, b : Instructions) : Instructions
mem = IO::Memory.new
mem.write(a)
mem.write(b)
mem.to_slice
end
{% end %}


private def remove_last_pop
last = current_scope.last_instruction
Expand Down
2 changes: 1 addition & 1 deletion tests.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
#crystal spec --verbose --define=slices
#crystal spec --verbose --define=slice
crystal spec --verbose

0 comments on commit 17f55c5

Please sign in to comment.