Class: SFRP::Mono::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrp/mono/function.rb

Defined Under Namespace

Classes: FType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, param_strs, ftype, exp = nil, ffi_str = nil) ⇒ Function

Returns a new instance of Function.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
# File 'lib/sfrp/mono/function.rb', line 10

def initialize(str, param_strs, ftype, exp = nil, ffi_str = nil)
  raise ArgumentError if exp.nil? && ffi_str.nil?
  raise ArgumentError unless param_strs.size == ftype.param_type_strs.size
  @str = str
  @param_strs = param_strs
  @ftype = ftype
  @exp = exp
  @ffi_str = ffi_str
end

Instance Attribute Details

#strObject (readonly)

Returns the value of attribute str.



8
9
10
# File 'lib/sfrp/mono/function.rb', line 8

def str
  @str
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
# File 'lib/sfrp/mono/function.rb', line 24

def ==(other)
  comp == other.comp
end

#compObject



20
21
22
# File 'lib/sfrp/mono/function.rb', line 20

def comp
  [@str, @param_strs, @ftype, @exp, @ffi_str]
end

#gen(src_set, dest_set) ⇒ Object

Generate function in C for this function.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sfrp/mono/function.rb', line 69

def gen(src_set, dest_set)
  return if @ffi_str
  env = Environment.new
  type = src_set.type(@ftype.return_type_str)
  dest_set << L.function(@str, type.low_type_str) do |f|
    @param_strs.zip(@ftype.param_type_strs).map do |p_str, t_str|
      f.append_param(src_set.type(t_str).low_type_str, p_str)
    end
    stmt = L.stmt("return #{@exp.to_low(src_set, env)}")
    env.each_declared_vars do |var_str, type_str|
      f << L.stmt("#{src_set.type(type_str).low_type_str} #{var_str}")
    end
    f << stmt
  end
end

#low_call_exp(low_arg_exps) ⇒ Object

Return low-expression to call this function.



37
38
39
40
# File 'lib/sfrp/mono/function.rb', line 37

def low_call_exp(low_arg_exps)
  return L.call_exp(@ffi_str, low_arg_exps) if @ffi_str
  L.call_exp(@str, low_arg_exps)
end

#low_call_exp_in_exp(set, env, low_arg_exps) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sfrp/mono/function.rb', line 42

def low_call_exp_in_exp(set, env, low_arg_exps)
  if @ffi_str
    type = set.type(@ftype.return_type_str)
    if @ffi_str !~ /[a-zA-Z]/
      if low_arg_exps.size == 2
        return "(#{low_arg_exps[0]}) #{@ffi_str} (#{low_arg_exps[1]})"
      end
      if low_arg_exps.size == 1
        return "#{@ffi_str} (#{low_arg_exps[0]})"
      end
      raise InvalidTypeOfForeignFunctionError.new(@ffi_str)
    end
    if type.native?
      return L.call_exp(@ffi_str, low_arg_exps)
    end
    if type.linear?(set)
      var = env.new_var(@ftype.return_type_str)
      pointers = type.low_member_pointers_for_single_vconst(set, var)
      call_exp = L.call_exp(@ffi_str, low_arg_exps + pointers)
      return "(#{var} = #{type.low_allocator_str}(0), #{call_exp}, #{var})"
    end
    raise InvalidTypeOfForeignFunctionError.new(@ffi_str)
  end
  L.call_exp(@str, low_arg_exps)
end

#memory(set) ⇒ Object

Return max needed memory size to call this function once. If this func is a foreign function, this size is assumed as max needed memory size to hold return-type.



31
32
33
34
# File 'lib/sfrp/mono/function.rb', line 31

def memory(set)
  return set.type(@ftype.return_type_str).memory(set) if @ffi_str
  @exp.memory(set)
end