Class: Mirah::JVM::Types::IntegerType

Inherits:
Number show all
Defined in:
lib/mirah/jvm/types/integers.rb

Constant Summary

Constants inherited from AST::TypeReference

AST::TypeReference::BlockType, AST::TypeReference::ErrorType, AST::TypeReference::NoType, AST::TypeReference::NullType, AST::TypeReference::UnreachableType

Instance Attribute Summary

Attributes inherited from Type

#inner_class

Attributes inherited from AST::TypeReference

#array, #meta

Attributes included from AST::Named

#name

Attributes inherited from AST::Node

#children, #inferred_type, #newline, #parent, #position

Instance Method Summary collapse

Methods inherited from Number

#add_delegates, #boolean_operator, #box, #compile_boolean_operator, #delegate_intrinsic, #invert_op, #math_operator, #suffix, #unary_operator

Methods inherited from PrimitiveType

#convertible_to?, #initialize, #interfaces, #newarray, #primitive?, #primitive_type, #superclass

Methods inherited from Type

#add_compiled_macro, #add_enumerable_macros, #add_macro, #add_method, #aload, #array?, #array_type, #assignable_from?, #astore, #basic_type, #compatible?, #component_type, #constructor, #declared_class_methods, #declared_constructors, #declared_instance_methods, #declared_intrinsics, #dynamic?, #expand_each, #field_getter, #field_setter, #full_name, #get_method, #include, #initialize, #inner_class?, #inner_class_getter, #inspect, #interface?, #interfaces, #intrinsics, #is_parent, #iterable?, #java_method, #jvm_type, #load_extensions, #log, #meta, #meta?, #newarray, #pop, #primitive?, #return, #store, #superclass, #to_source, #unmeta, #void?, #wide?, #wrap_with_scoped_body

Methods included from MethodLookup

#each_is_exact, #each_is_exact_or_subtype_or_convertible, #field_lookup, #find_jls, #find_method, #inner_class, #is_more_specific?, #log, #phase1, #phase2, #phase3, #primitive_convertible?

Methods inherited from AST::TypeReference

#==, #_dump, _load, #basic_type, #block?, #compatible?, #component_type, #eql?, #error?, #full_name, #hash, #initialize, #is_parent, #iterable?, #meta?, #narrow, #null?, #primitive?, #to_s, #type_reference, #unmeta, #unreachable?, #void?

Methods included from AST::Named

#string_value, #to_s, #validate_name

Methods inherited from AST::Node

#<<, ===, #[], #[]=, #_dump, _load, #_set_parent, child, child_name, #child_nodes, #each, #empty?, #expr?, #inferred_type!, #initialize, #initialize_copy, #insert, #inspect, #inspect_children, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #string_value, #temp, #to_s, #top_level?, #validate_child, #validate_children

Constructor Details

This class inherits a constructor from Mirah::JVM::Types::PrimitiveType

Instance Method Details

#add_intrinsicsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mirah/jvm/types/integers.rb', line 111

def add_intrinsics
  super
  math_operator('<<', 'shl')
  math_operator('>>', 'shr')
  math_operator('>>>', 'ushr')
  math_operator('|', 'or')
  math_operator('&', 'and')
  math_operator('^', 'xor')
  unary_operator('~', 'not')

  add_macro('downto', Int, Mirah::AST.block_type) do |transformer, call|
    build_loop(call.parent, call.position, transformer,
               call.block, call.target, call.parameters[0], false, true)
  end
  add_macro('upto', Int, Mirah::AST.block_type) do |transformer, call|
    build_loop(call.parent, call.position, transformer,
               call.block, call.target, call.parameters[0], true, true)
  end
  add_macro('times', Mirah::AST.block_type) do |transformer, call|
    build_loop(call.parent, call.position, transformer,
               call.block, Mirah::AST::fixnum(nil, call.position, 0),
               call.target, true, false)
  end
end

#box_typeObject



68
69
70
# File 'lib/mirah/jvm/types/integers.rb', line 68

def box_type
  java.lang.Integer
end

#build_loop(parent, position, duby, block, first_value, last_value, ascending, inclusive) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mirah/jvm/types/integers.rb', line 76

def build_loop(parent, position, duby, block, first_value,
               last_value, ascending, inclusive)
  if ascending
    comparison = "<"
    op = "+="
  else
    comparison = ">"
    op = "-="
  end
  comparison << "=" if inclusive
  forloop = Mirah::AST::Loop.new(parent, position, true, false) do |forloop|
    first, last = duby.tmp, duby.tmp
    init = duby.eval("#{first} = 0; #{last} = 0;")
    init.children[-2].value = first_value
    init.children[-1].value = last_value
    forloop.init << init

    var = (block.args.args || [])[0]
    if var
      forloop.pre << duby.eval(
          "#{var.name} = #{first}", '', forloop, first, last)
    end
    forloop.post << duby.eval("#{first} #{op} 1")
    [
      Mirah::AST::Condition.new(forloop, position) do |c|
        [duby.eval("#{first} #{comparison} #{last}",
                   '', forloop, first, last)]
      end,
      nil
    ]
  end
  forloop.body = block.body
  forloop
end

#init_value(builder) ⇒ Object



37
38
39
# File 'lib/mirah/jvm/types/integers.rb', line 37

def init_value(builder)
  builder.iconst_0
end

#jump_if(builder, op, label) ⇒ Object



72
73
74
# File 'lib/mirah/jvm/types/integers.rb', line 72

def jump_if(builder, op, label)
  builder.send "if_icmp#{op}", label
end

#literal(builder, value) ⇒ Object



33
34
35
# File 'lib/mirah/jvm/types/integers.rb', line 33

def literal(builder, value)
  builder.push_int(value)
end

#load(builder, index) ⇒ Object



41
42
43
# File 'lib/mirah/jvm/types/integers.rb', line 41

def load(builder, index)
  builder.iload(index)
end

#math_typeObject



64
65
66
# File 'lib/mirah/jvm/types/integers.rb', line 64

def math_type
  Int
end

#prefixObject



60
61
62
# File 'lib/mirah/jvm/types/integers.rb', line 60

def prefix
  'i'
end

#widen(builder, type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mirah/jvm/types/integers.rb', line 45

def widen(builder, type)
  case type
  when Byte, Short, Int
    # do nothing
  when Long
    builder.i2l
  when Float
    builder.i2f
  when Double
    builder.i2d
  else
    raise ArgumentError, "Invalid widening conversion from #{name} to #{type}"
  end
end