Skip to content

Commit

Permalink
Basic ALU instructions placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
udzura committed Nov 3, 2024
1 parent c975bc9 commit 685104f
Show file tree
Hide file tree
Showing 15 changed files with 1,341 additions and 79 deletions.
127 changes: 127 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,139 @@ task :generate do
libdir = File.expand_path("../lib", __FILE__)

GenAlu.execute(libdir + "/wardite/alu_i32.generated.rb", prefix: "i32", defined_ops: [
:load,
:load8_s,
:load8_u,
:load16_s,
:load16_u,
:store,
:store8,
:store16,
:const,
:eqz,
:eq,
:ne,
:lts,
:ltu,
:gts,
:gtu,
:les,
:leu,
:ges,
:geu,
:clz,
:ctz,
:popcnt,
:add,
:sub,
:mul,
:div_s,
:div_u,
:rem_s,
:rem_u,
:and,
:or,
:xor,
:shl,
:shr_s,
:shr_u,
:rotl,
:rotr,
])
GenAlu.execute(libdir + "/wardite/alu_i64.generated.rb", prefix: "i64", defined_ops: [
:load,
:load8_s,
:load8_u,
:load16_s,
:load16_u,
:load32_s,
:load32_u,
:store,
:store8,
:store16,
:store32,
:const,
:eqz,
:eq,
:ne,
:lts,
:ltu,
:gts,
:gtu,
:les,
:leu,
:ges,
:geu,
:clz,
:ctz,
:popcnt,
:add,
:sub,
:mul,
:div_s,
:div_u,
:rem_s,
:rem_u,
:and,
:or,
:xor,
:shl,
:shr_s,
:shr_u,
:rotl,
:rotr,
])
GenAlu.execute(libdir + "/wardite/alu_f32.generated.rb", prefix: "f32", defined_ops: [
:load,
:store,
:const__f,
:eqz,
:eq,
:ne,
:lt,
:gt,
:le,
:ge,
:abs,
:neg,
:ceil,
:floor,
:trunc,
:nearest,
:sqrt,
:add,
:sub,
:mul,
:div,
:min,
:max,
:copysign,
])
GenAlu.execute(libdir + "/wardite/alu_f64.generated.rb", prefix: "f64", defined_ops: [
:load,
:store,
:const__f,
:eqz,
:eq,
:ne,
:lt,
:gt,
:le,
:ge,
:abs,
:neg,
:ceil,
:floor,
:trunc,
:nearest,
:sqrt,
:add,
:sub,
:mul,
:div,
:min,
:max,
:copysign,
])
end

Expand Down
1 change: 1 addition & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target :lib do
signature "sig"
check "lib"

library "pp"
# configure_code_diagnostics(Steep::Diagnostic::Ruby.strict)
end

Expand Down
15 changes: 15 additions & 0 deletions lib/wardite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,12 @@ def eval_insn(frame, insn)
case insn.namespace
when :i32
return Evaluator.i32_eval_insn(self, frame, insn)
when :i64
return Evaluator.i64_eval_insn(self, frame, insn)
when :f32
return Evaluator.f32_eval_insn(self, frame, insn)
when :f64
return Evaluator.f64_eval_insn(self, frame, insn)
end

# unmached namespace...
Expand Down Expand Up @@ -921,7 +927,16 @@ def eval_insn(frame, insn)
end
stack_unwind(old_frame.sp, old_frame.arity)
end

else
raise "TODO! unsupported #{insn.inspect}"
end

rescue => e
require "pp"
$stderr.puts "frame: #{frame.pretty_inspect}"
$stderr.puts "stack: #{stack.pretty_inspect}"
raise e
end

# @rbs ops: Array[Op]
Expand Down
159 changes: 159 additions & 0 deletions lib/wardite/alu_f32.generated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# rbs_inline: enabled
require_relative "value"

module Wardite
module Evaluator
# @rbs runtime: Runtime
# @rbs frame: Frame
# @rbs insn: Op
# @rbs return: void
def self.f32_eval_insn(runtime, frame, insn)
case insn.code

when :f32_load
raise "TODO! unsupported #{insn.inspect}"


when :f32_store
_align = insn.operand[0] # TODO: alignment support?
offset = insn.operand[1]
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)

value = runtime.stack.pop
addr = runtime.stack.pop
if !value.is_a?(F32) || !addr.is_a?(I32)
raise EvalError, "maybe stack too short"
end

at = addr.value + offset
data_end = at + value.packed.size
memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
memory.data[at...data_end] = value.packed


when :f32_const
const = insn.operand[0]
if !const.is_a?(Float)
raise EvalError, "invalid type of operand"
end
runtime.stack.push(F32(const))


when :f32_eqz
raise "TODO! unsupported #{insn.inspect}"


when :f32_eq
raise "TODO! unsupported #{insn.inspect}"


when :f32_ne
raise "TODO! unsupported #{insn.inspect}"


when :f32_lt
right, left = runtime.stack.pop, runtime.stack.pop
if !right.is_a?(F32) || !left.is_a?(F32)
raise EvalError, "maybe empty or invalid stack"
end
value = (left.value < right.value) ? 1 : 0
runtime.stack.push(I32(value))


when :f32_gt
right, left = runtime.stack.pop, runtime.stack.pop
if !right.is_a?(F32) || !left.is_a?(F32)
raise EvalError, "maybe empty or invalid stack"
end
value = (left.value > right.value) ? 1 : 0
runtime.stack.push(I32(value))


when :f32_le
right, left = runtime.stack.pop, runtime.stack.pop
if !right.is_a?(F32) || !left.is_a?(F32)
raise EvalError, "maybe empty or invalid stack"
end
value = (left.value <= right.value) ? 1 : 0
runtime.stack.push(I32(value))


when :f32_ge
right, left = runtime.stack.pop, runtime.stack.pop
if !right.is_a?(F32) || !left.is_a?(F32)
raise EvalError, "maybe empty or invalid stack"
end
value = (left.value >= right.value) ? 1 : 0
runtime.stack.push(I32(value))


when :f32_abs
raise "TODO! unsupported #{insn.inspect}"


when :f32_neg
raise "TODO! unsupported #{insn.inspect}"


when :f32_ceil
raise "TODO! unsupported #{insn.inspect}"


when :f32_floor
raise "TODO! unsupported #{insn.inspect}"


when :f32_trunc
raise "TODO! unsupported #{insn.inspect}"


when :f32_nearest
raise "TODO! unsupported #{insn.inspect}"


when :f32_sqrt
raise "TODO! unsupported #{insn.inspect}"


when :f32_add
right, left = runtime.stack.pop, runtime.stack.pop
if !right.is_a?(F32) || !left.is_a?(F32)
raise EvalError, "maybe empty or invalid stack"
end
runtime.stack.push(F32(left.value + right.value))


when :f32_sub
right, left = runtime.stack.pop, runtime.stack.pop
if !right.is_a?(F32) || !left.is_a?(F32)
raise EvalError, "maybe empty or invalid stack"
end
runtime.stack.push(F32(left.value - right.value))


when :f32_mul
raise "TODO! unsupported #{insn.inspect}"


when :f32_div
raise "TODO! unsupported #{insn.inspect}"


when :f32_min
raise "TODO! unsupported #{insn.inspect}"


when :f32_max
raise "TODO! unsupported #{insn.inspect}"


when :f32_copysign
raise "TODO! unsupported #{insn.inspect}"


else
raise "Unknown opcode for namespace #{insn.namespace}: #{insn.code}"
end
end
end
end
Loading

0 comments on commit 685104f

Please sign in to comment.