Skip to content

Commit

Permalink
String is VALUE not P_CHAR
Browse files Browse the repository at this point in the history
  • Loading branch information
miura1729 committed Aug 14, 2009
1 parent 8972c72 commit 1844bda
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 25 deletions.
4 changes: 4 additions & 0 deletions lib/llvmbuilder.rb
Expand Up @@ -34,6 +34,10 @@ def to_label_aux(s)
ns.gsub!(/@/, "_a")
ns.gsub!(/\?/, "_q")
ns.gsub!(/\+/, "_p")
ns.gsub!(/\//, "_d")
ns.gsub!(/\*/, "_A")
ns.gsub!(/\%/, "_P")
ns.gsub!(/\-/, "_m")
ns
end

Expand Down
4 changes: 2 additions & 2 deletions lib/llvmutil.rb
Expand Up @@ -757,10 +757,10 @@ def do_cfunction(receiver, info, ins, local_vars, args, mname)
rettype = funcinfo[:rettype]
argtype = funcinfo[:argtype]
unless rettype.is_a?(RubyType) then
rettype = RubyType.new(rettype,
rettype = RubyType.from_llvm(rettype,
info[3],
"return type of #{mname} in c call")
argtype = funcinfo[:argtype].map {|ts| RubyType.new(ts, info[3])}
argtype = funcinfo[:argtype].map {|ts| RubyType.from_llvm(ts, info[3], "")}
end
cname = funcinfo[:cname]
send_self = funcinfo[:send_self]
Expand Down
23 changes: 14 additions & 9 deletions lib/type.rb
Expand Up @@ -395,6 +395,17 @@ def self.unsafe(lno = nil, name = nil, treat = nil)
RubyType.new(type, lno, name, :"YARV2LLVM::LLVMLIB::Unsafe")
end

def self.from_llvm(llvm, lno, name)
case llvm
when Type::Int32Ty
RubyType.new(llvm, lno, name, :Fixnum)
when Type::DoubleTy
RubyType.new(llvm, lno, name, :Float)
else
raise "Unkown type"
end
end

def self.from_sym(sym, lno, name)
case sym
when :Fixnum
Expand Down Expand Up @@ -886,21 +897,15 @@ def inspect2
end

def to_value(val, b, context)
ftype = Type.function(VALUE, [P_CHAR])
func = context.builder.external_function('rb_str_new_cstr', ftype)
b.call(func, val)
val
end

def from_value(val, b, context)
ftype = Type.function(P_CHAR, [P_VALUE])
func = context.builder.external_function('rb_string_value_ptr', ftype)
strp = b.alloca(VALUE, 1)
b.store(val, strp)
b.call(func, strp)
val
end

def llvm
P_CHAR
VALUE
end
end

Expand Down
75 changes: 62 additions & 13 deletions lib/vmtraverse.rb
Expand Up @@ -1124,7 +1124,13 @@ def visit_getconstant(code, ins, local_vars, ln, info)
end
@expstack.push [type,
lambda {|b, context|
if val then
if !UNDEF.equal?(type.type.constant) then
context.rc = type.type.constant.llvm

elsif !UNDEF.equal?(type.type.content) then
context.rc = type.type.content.llvm

elsif val then
context.rc = val.llvm
else
slf = Object.llvm
Expand Down Expand Up @@ -1299,7 +1305,7 @@ def visit_putobject(code, ins, local_vars, ln, info)
when VALUE
context.rc = orgtype.to_value(p1.llvm, b, context)
when P_CHAR
context.rc = p1.llvm(b)
context.rc = p1.llvm2(b)
else
context.rc = p1.llvm
end
Expand All @@ -1316,17 +1322,23 @@ def visit_putiseq(code, ins, local_vars, ln, info)

def visit_putstring(code, ins, local_vars, ln, info)
p1 = ins[1]
@expstack.push [RubyType.typeof(p1, info[3], p1),
type = RubyType.typeof(p1, info[3], p1)
type.type.constant = p1
type.type.content = p1

@expstack.push [type,
lambda {|b, context|
context.rc = p1.llvm(b)
ftype = Type.function(VALUE, [P_CHAR])
func = context.builder.external_function('rb_str_new_cstr', ftype)
context.rc = b.call(func, p1.llvm2(b))
context.org = p1
context
}]
end

def visit_concatstrings(code, ins, local_vars, ln, info)
nele = ins[1]
rett = RubyType.value(info[3], "return type tostring", String)
rett = RubyType.value(info[3], "return type concatstring", String)
eles = []
nele.times do
eles.push @expstack.pop
Expand Down Expand Up @@ -2201,11 +2213,11 @@ def visit_opt_plus(code, ins, local_vars, ln, info)
return context
end

case stype[0].type.llvm
when Type::DoubleTy, Type::Int32Ty
case stype[0].klass
when :Fixnum, :Float
context.rc = b.add(sval[0], sval[1])

when P_CHAR
when :String
ftype = Type.function(VALUE, [P_CHAR])
fname = 'rb_str_new_cstr'
funcnewstr = context.builder.external_function(fname, ftype)
Expand Down Expand Up @@ -2690,8 +2702,12 @@ def visit_opt_ltlt(code, ins, local_vars, ln, info)
s2 = @expstack.pop
s1 = @expstack.pop
rettype = s1[0].dup_type
if s1[0].type and s1[0].type.klass != Array then
rettype = check_same_type_2arg_static(s1, s2)
if s1[0].type
if s1[0].type.klass == :String then
rettype = check_same_type_2arg_static(s1, s2)
elsif s1[0].type.klass != :Array then
rettype = check_same_type_2arg_static(s1, s2)
end
end

@expstack.push [rettype,
Expand All @@ -2712,7 +2728,17 @@ def visit_opt_ltlt(code, ins, local_vars, ln, info)
context.rc = b.shl(sval[0], sval[1])

when :Array
raise "Unsupported type #{s1[0].inspect2}"
ftype = Type.function(VALUE, [VALUE, VALUE])
func = context.builder.external_function('rb_ary_push', ftype)
context.rc = b.call(func, sval[0], sval[1])

when :String
ftype = Type.function(VALUE, [VALUE, VALUE])
func = context.builder.external_function('rb_str_concat', ftype)
sval[0] = stype[0].type.to_value(sval[0], b, context)
sval[1] = stype[1].type.to_value(sval[1], b, context)
rcvalue = b.call(func, sval[0], sval[1])
context.rc = rettype.type.from_value(rcvalue, b, context)

else
raise "Unsupported type #{s1[0].inspect2}"
Expand Down Expand Up @@ -2921,7 +2947,30 @@ def visit_opt_aref(code, ins, local_vars, ln, info)
end

# opt_aset
# opt_length

def visit_opt_length(code, ins, local_vars, ln, info)
rec = @expstack.pop
rettype = RubyType.fixnum(info[3], "return type of length", Fixnum)
@expstack.push [rettype,
lambda {|b, context|
case rec[0].type.klass
when :String
context = rec[1].call(b, context)
recval = context.rc
recval = rec[0].type.to_value(recval, b, context)
ftype = Type.function(VALUE, [VALUE])
func = context.builder.external_function('rb_str_length', ftype)
rcvalue = b.call(func, recval)
context.rc = rettype.type.from_value(rcvalue, b, context)

else
raise "Not support type #{lst[0].type.inspect2} in length"
end

context
}]
end

# opt_succ
# opt_not
# opt_regexpmatch1
Expand Down Expand Up @@ -3096,7 +3145,7 @@ def gen_define_ruby(builder)
MethodDefinition::RubyMethodStub.each do |name, klasstab|
klasstab.each do |rec, m|
unless m[:outputp]
nameptr = name.llvm(b)
nameptr = name.llvm2(b)
stubval = b.ptr_to_int(m[:stub], VALUE)
if rec then
recptr = Object.const_get(rec)
Expand Down
2 changes: 1 addition & 1 deletion lib/yarv2llvm.rb
Expand Up @@ -98,7 +98,7 @@ def llvm
end

class String
def llvm(b)
def llvm2(b)
b.create_global_string_ptr(self)
end
end
Expand Down

0 comments on commit 1844bda

Please sign in to comment.