From 1289b81d780b7aaa237ad83564a62d57c399bac3 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 10 Aug 2015 14:12:21 -0400 Subject: [PATCH] src/interpreter: Added fixints to interpreter Signed-off-by: Lucas Bajolet --- src/interpreter/dynamic_loading_ffi/README.md | 5 + .../dynamic_loading_ffi/dynamic_loading_ffi.nit | 60 ++++ .../dynamic_loading_ffi/on_demand_compiler.nit | 5 + src/interpreter/naive_interpreter.nit | 375 ++++++++++++++++++++ 4 files changed, 445 insertions(+) diff --git a/src/interpreter/dynamic_loading_ffi/README.md b/src/interpreter/dynamic_loading_ffi/README.md index 3a07c84..1d66f23 100644 --- a/src/interpreter/dynamic_loading_ffi/README.md +++ b/src/interpreter/dynamic_loading_ffi/README.md @@ -41,6 +41,11 @@ This API is compose of 2 main elements: int value_Bool; uint32_t value_Char; uint8_t value_Byte; + int8_t value_Int8; + int16_t value_Int16; + uint16_t value_UInt16; + int32_t value_Int32; + uint32_t value_UInt32; double value_Float; void* value_Pointer; } nit_call_arg; diff --git a/src/interpreter/dynamic_loading_ffi/dynamic_loading_ffi.nit b/src/interpreter/dynamic_loading_ffi/dynamic_loading_ffi.nit index 3f1bf0d..b507c9d 100644 --- a/src/interpreter/dynamic_loading_ffi/dynamic_loading_ffi.nit +++ b/src/interpreter/dynamic_loading_ffi/dynamic_loading_ffi.nit @@ -32,6 +32,11 @@ in "C Header" `{ int value_Bool; uint32_t value_Char; uint8_t value_Byte; + int8_t value_Int8; + int16_t value_Int16; + uint16_t value_UInt16; + int32_t value_Int32; + uint32_t value_UInt32; double value_Float; void* value_Pointer; } nit_call_arg; @@ -77,6 +82,36 @@ private extern class CallArg `{ nit_call_arg* `} # The `Byte` held by this cell fun byte=(value: Byte) `{ self->value_Byte = value; `} + # The `Int` held by this cell + fun int8: Int8 `{ return self->value_Int8; `} + + # The `Int` held by this cell + fun int8=(value: Int8) `{ self->value_Int8 = value; `} + + # The `Int` held by this cell + fun int16: Int16 `{ return self->value_Int16; `} + + # The `Int` held by this cell + fun int16=(value: Int16) `{ self->value_Int16 = value; `} + + # The `Int` held by this cell + fun uint16: UInt16 `{ return self->value_UInt16; `} + + # The `Int` held by this cell + fun uint16=(value: UInt16) `{ self->value_UInt16 = value; `} + + # The `Int` held by this cell + fun int32: Int32 `{ return self->value_Int32; `} + + # The `Int` held by this cell + fun int32=(value: Int32) `{ self->value_Int32 = value; `} + + # The `Int` held by this cell + fun uint32: UInt32 `{ return self->value_UInt32; `} + + # The `Int` held by this cell + fun uint32=(value: UInt32) `{ self->value_UInt32 = value; `} + # The `Float` held by this cell fun float: Float `{ return self->value_Float; `} @@ -115,6 +150,21 @@ private extern class CallArg `{ nit_call_arg* `} else if static_type.name == "Byte" then assert value isa PrimitiveInstance[Byte] self.byte = value.val + else if static_type.name == "Int8" then + assert value isa PrimitiveInstance[Int8] + self.int8 = value.val + else if static_type.name == "Int16" then + assert value isa PrimitiveInstance[Int16] + self.int16 = value.val + else if static_type.name == "UInt16" then + assert value isa PrimitiveInstance[UInt16] + self.uint16 = value.val + else if static_type.name == "Int32" then + assert value isa PrimitiveInstance[Int32] + self.int32 = value.val + else if static_type.name == "UInt32" then + assert value isa PrimitiveInstance[UInt32] + self.uint32 = value.val else if static_type.name == "Float" then assert value isa PrimitiveInstance[Float] self.float = value.val @@ -145,6 +195,16 @@ private extern class CallArg `{ nit_call_arg* `} return v.char_instance(self.char) else if name == "Byte" then return v.byte_instance(self.byte) + else if name == "Int8" then + return v.int8_instance(self.int8) + else if name == "Int16" then + return v.int16_instance(self.int16) + else if name == "UInt16" then + return v.uint16_instance(self.uint16) + else if name == "Int32" then + return v.int32_instance(self.int32) + else if name == "UInt32" then + return v.uint32_instance(self.uint32) else if name == "Float" then return v.float_instance(self.float) else if name == "NativeString" then diff --git a/src/interpreter/dynamic_loading_ffi/on_demand_compiler.nit b/src/interpreter/dynamic_loading_ffi/on_demand_compiler.nit index 5953647..eba0678 100644 --- a/src/interpreter/dynamic_loading_ffi/on_demand_compiler.nit +++ b/src/interpreter/dynamic_loading_ffi/on_demand_compiler.nit @@ -143,6 +143,11 @@ typedef union nit_call_arg { int value_Bool; uint32_t value_Char; uint8_t value_Byte; + int8_t value_Int8; + int16_t value_Int16; + uint16_t value_UInt16; + int32_t value_Int32; + uint32_t value_UInt32; double value_Float; void* value_Pointer; } nit_call_arg; diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index 3d3aa12..1181cf0 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -216,6 +216,51 @@ class NaiveInterpreter return instance end + # Return the int8 instance associated with `val`. + fun int8_instance(val: Int8): Instance + do + var t = mainmodule.int8_type + var instance = new PrimitiveInstance[Int8](t, val) + init_instance_primitive(instance) + return instance + end + + # Return the int16 instance associated with `val`. + fun int16_instance(val: Int16): Instance + do + var t = mainmodule.int16_type + var instance = new PrimitiveInstance[Int16](t, val) + init_instance_primitive(instance) + return instance + end + + # Return the uint16 instance associated with `val`. + fun uint16_instance(val: UInt16): Instance + do + var t = mainmodule.uint16_type + var instance = new PrimitiveInstance[UInt16](t, val) + init_instance_primitive(instance) + return instance + end + + # Return the int32 instance associated with `val`. + fun int32_instance(val: Int32): Instance + do + var t = mainmodule.int32_type + var instance = new PrimitiveInstance[Int32](t, val) + init_instance_primitive(instance) + return instance + end + + # Return the uint32 instance associated with `val`. + fun uint32_instance(val: UInt32): Instance + do + var t = mainmodule.uint32_type + var instance = new PrimitiveInstance[UInt32](t, val) + init_instance_primitive(instance) + return instance + end + # Return the char instance associated with `val`. fun char_instance(val: Char): Instance do @@ -656,6 +701,26 @@ abstract class Instance # else aborts fun to_b: Byte do abort + # Return the integer value if the instance is a int8. + # else aborts + fun to_i8: Int8 do abort + + # Return the integer value if the instance is a int16. + # else aborts + fun to_i16: Int16 do abort + + # Return the integer value if the instance is a uint16. + # else aborts + fun to_u16: UInt16 do abort + + # Return the integer value if the instance is a int32. + # else aborts + fun to_i32: Int32 do abort + + # Return the integer value if the instance is a uint32. + # else aborts + fun to_u32: UInt32 do abort + # The real value encapsulated if the instance is primitive. # Else aborts. fun val: nullable Object do abort @@ -703,6 +768,16 @@ class PrimitiveInstance[E] redef fun to_f do return val.as(Float) redef fun to_b do return val.as(Byte) + + redef fun to_i8 do return val.as(Int8) + + redef fun to_i16 do return val.as(Int16) + + redef fun to_u16 do return val.as(UInt16) + + redef fun to_i32 do return val.as(Int32) + + redef fun to_u32 do return val.as(UInt32) end # Information about local variables in a running method @@ -907,6 +982,16 @@ redef class AMethPropdef return v.int_instance(recvval << args[1].to_i) else if pname == ">>" then return v.int_instance(recvval >> args[1].to_i) + else if pname == "to_i8" then + return v.int8_instance(recvval.to_i8) + else if pname == "to_i16" then + return v.int16_instance(recvval.to_i16) + else if pname == "to_u16" then + return v.uint16_instance(recvval.to_u16) + else if pname == "to_i32" then + return v.int32_instance(recvval.to_i32) + else if pname == "to_u32" then + return v.uint32_instance(recvval.to_u32) else if pname == "rand" then var res = recvval.rand return v.int_instance(res) @@ -945,6 +1030,16 @@ redef class AMethPropdef return v.byte_instance(recvval << args[1].to_i) else if pname == ">>" then return v.byte_instance(recvval >> args[1].to_i) + else if pname == "to_i8" then + return v.int8_instance(recvval.to_i8) + else if pname == "to_i16" then + return v.int16_instance(recvval.to_i16) + else if pname == "to_u16" then + return v.uint16_instance(recvval.to_u16) + else if pname == "to_i32" then + return v.int32_instance(recvval.to_i32) + else if pname == "to_u32" then + return v.uint32_instance(recvval.to_u32) else if pname == "byte_to_s_len" then return v.int_instance(recvval.to_s.length) end @@ -993,6 +1088,16 @@ redef class AMethPropdef return v.int_instance(recv.to_i) else if pname == "to_b" then return v.byte_instance(recv.to_b) + else if pname == "to_i8" then + return v.int8_instance(recv.to_i8) + else if pname == "to_i16" then + return v.int16_instance(recv.to_i16) + else if pname == "to_u16" then + return v.uint16_instance(recv.to_u16) + else if pname == "to_i32" then + return v.int32_instance(recv.to_i32) + else if pname == "to_u32" then + return v.uint32_instance(recv.to_u32) else if pname == "cos" then return v.float_instance(args[0].to_f.cos) else if pname == "sin" then @@ -1073,6 +1178,271 @@ redef class AMethPropdef recvval.copy_to(0, args[2].to_i, args[1].val.as(Array[Instance]), 0) return null end + else if cname == "Int8" then + var recvval = args[0].to_i8 + if pname == "unary -" then + return v.int8_instance(-recvval) + else if pname == "unary +" then + return args[0] + else if pname == "+" then + return v.int8_instance(recvval + args[1].to_i8) + else if pname == "-" then + return v.int8_instance(recvval - args[1].to_i8) + else if pname == "*" then + return v.int8_instance(recvval * args[1].to_i8) + else if pname == "%" then + return v.int8_instance(recvval % args[1].to_i8) + else if pname == "/" then + return v.int8_instance(recvval / args[1].to_i8) + else if pname == "<" then + return v.bool_instance(recvval < args[1].to_i8) + else if pname == ">" then + return v.bool_instance(recvval > args[1].to_i8) + else if pname == "<=" then + return v.bool_instance(recvval <= args[1].to_i8) + else if pname == ">=" then + return v.bool_instance(recvval >= args[1].to_i8) + else if pname == "<=>" then + return v.int_instance(recvval <=> args[1].to_i8) + else if pname == "to_f" then + return v.float_instance(recvval.to_f) + else if pname == "to_i" then + return v.int_instance(recvval.to_i) + else if pname == "to_b" then + return v.byte_instance(recvval.to_b) + else if pname == "to_i16" then + return v.int16_instance(recvval.to_i16) + else if pname == "to_u16" then + return v.uint16_instance(recvval.to_u16) + else if pname == "to_i32" then + return v.int32_instance(recvval.to_i32) + else if pname == "to_u32" then + return v.uint32_instance(recvval.to_u32) + else if pname == "<<" then + return v.int8_instance(recvval << (args[1].to_i)) + else if pname == ">>" then + return v.int8_instance(recvval >> (args[1].to_i)) + else if pname == "&" then + return v.int8_instance(recvval & args[1].to_i8) + else if pname == "|" then + return v.int8_instance(recvval | args[1].to_i8) + else if pname == "^" then + return v.int8_instance(recvval ^ args[1].to_i8) + else if pname == "unary ~" then + return v.int8_instance(~recvval) + end + else if cname == "Int16" then + var recvval = args[0].to_i16 + if pname == "unary -" then + return v.int16_instance(-recvval) + else if pname == "unary +" then + return args[0] + else if pname == "+" then + return v.int16_instance(recvval + args[1].to_i16) + else if pname == "-" then + return v.int16_instance(recvval - args[1].to_i16) + else if pname == "*" then + return v.int16_instance(recvval * args[1].to_i16) + else if pname == "%" then + return v.int16_instance(recvval % args[1].to_i16) + else if pname == "/" then + return v.int16_instance(recvval / args[1].to_i16) + else if pname == "<" then + return v.bool_instance(recvval < args[1].to_i16) + else if pname == ">" then + return v.bool_instance(recvval > args[1].to_i16) + else if pname == "<=" then + return v.bool_instance(recvval <= args[1].to_i16) + else if pname == ">=" then + return v.bool_instance(recvval >= args[1].to_i16) + else if pname == "<=>" then + return v.int_instance(recvval <=> args[1].to_i16) + else if pname == "to_f" then + return v.float_instance(recvval.to_f) + else if pname == "to_i" then + return v.int_instance(recvval.to_i) + else if pname == "to_b" then + return v.byte_instance(recvval.to_b) + else if pname == "to_i8" then + return v.int8_instance(recvval.to_i8) + else if pname == "to_u16" then + return v.uint16_instance(recvval.to_u16) + else if pname == "to_i32" then + return v.int32_instance(recvval.to_i32) + else if pname == "to_u32" then + return v.uint32_instance(recvval.to_u32) + else if pname == "<<" then + return v.int16_instance(recvval << (args[1].to_i)) + else if pname == ">>" then + return v.int16_instance(recvval >> (args[1].to_i)) + else if pname == "&" then + return v.int16_instance(recvval & args[1].to_i16) + else if pname == "|" then + return v.int16_instance(recvval | args[1].to_i16) + else if pname == "^" then + return v.int16_instance(recvval ^ args[1].to_i16) + else if pname == "unary ~" then + return v.int16_instance(~recvval) + end + else if cname == "UInt16" then + var recvval = args[0].to_u16 + if pname == "unary -" then + return v.uint16_instance(-recvval) + else if pname == "unary +" then + return args[0] + else if pname == "+" then + return v.uint16_instance(recvval + args[1].to_u16) + else if pname == "-" then + return v.uint16_instance(recvval - args[1].to_u16) + else if pname == "*" then + return v.uint16_instance(recvval * args[1].to_u16) + else if pname == "%" then + return v.uint16_instance(recvval % args[1].to_u16) + else if pname == "/" then + return v.uint16_instance(recvval / args[1].to_u16) + else if pname == "<" then + return v.bool_instance(recvval < args[1].to_u16) + else if pname == ">" then + return v.bool_instance(recvval > args[1].to_u16) + else if pname == "<=" then + return v.bool_instance(recvval <= args[1].to_u16) + else if pname == ">=" then + return v.bool_instance(recvval >= args[1].to_u16) + else if pname == "<=>" then + return v.int_instance(recvval <=> args[1].to_u16) + else if pname == "to_f" then + return v.float_instance(recvval.to_f) + else if pname == "to_i" then + return v.int_instance(recvval.to_i) + else if pname == "to_b" then + return v.byte_instance(recvval.to_b) + else if pname == "to_i8" then + return v.int8_instance(recvval.to_i8) + else if pname == "to_i16" then + return v.int16_instance(recvval.to_i16) + else if pname == "to_i32" then + return v.int32_instance(recvval.to_i32) + else if pname == "to_u32" then + return v.uint32_instance(recvval.to_u32) + else if pname == "<<" then + return v.uint16_instance(recvval << (args[1].to_i)) + else if pname == ">>" then + return v.uint16_instance(recvval >> (args[1].to_i)) + else if pname == "&" then + return v.uint16_instance(recvval & args[1].to_u16) + else if pname == "|" then + return v.uint16_instance(recvval | args[1].to_u16) + else if pname == "^" then + return v.uint16_instance(recvval ^ args[1].to_u16) + else if pname == "unary ~" then + return v.uint16_instance(~recvval) + end + else if cname == "Int32" then + var recvval = args[0].to_i32 + if pname == "unary -" then + return v.int32_instance(-recvval) + else if pname == "unary +" then + return args[0] + else if pname == "+" then + return v.int32_instance(recvval + args[1].to_i32) + else if pname == "-" then + return v.int32_instance(recvval - args[1].to_i32) + else if pname == "*" then + return v.int32_instance(recvval * args[1].to_i32) + else if pname == "%" then + return v.int32_instance(recvval % args[1].to_i32) + else if pname == "/" then + return v.int32_instance(recvval / args[1].to_i32) + else if pname == "<" then + return v.bool_instance(recvval < args[1].to_i32) + else if pname == ">" then + return v.bool_instance(recvval > args[1].to_i32) + else if pname == "<=" then + return v.bool_instance(recvval <= args[1].to_i32) + else if pname == ">=" then + return v.bool_instance(recvval >= args[1].to_i32) + else if pname == "<=>" then + return v.int_instance(recvval <=> args[1].to_i32) + else if pname == "to_f" then + return v.float_instance(recvval.to_f) + else if pname == "to_i" then + return v.int_instance(recvval.to_i) + else if pname == "to_b" then + return v.byte_instance(recvval.to_b) + else if pname == "to_i8" then + return v.int8_instance(recvval.to_i8) + else if pname == "to_i16" then + return v.int16_instance(recvval.to_i16) + else if pname == "to_u16" then + return v.uint16_instance(recvval.to_u16) + else if pname == "to_u32" then + return v.uint32_instance(recvval.to_u32) + else if pname == "<<" then + return v.int32_instance(recvval << (args[1].to_i)) + else if pname == ">>" then + return v.int32_instance(recvval >> (args[1].to_i)) + else if pname == "&" then + return v.int32_instance(recvval & args[1].to_i32) + else if pname == "|" then + return v.int32_instance(recvval | args[1].to_i32) + else if pname == "^" then + return v.int32_instance(recvval ^ args[1].to_i32) + else if pname == "unary ~" then + return v.int32_instance(~recvval) + end + else if cname == "UInt32" then + var recvval = args[0].to_u32 + if pname == "unary -" then + return v.uint32_instance(-recvval) + else if pname == "unary +" then + return args[0] + else if pname == "+" then + return v.uint32_instance(recvval + args[1].to_u32) + else if pname == "-" then + return v.uint32_instance(recvval - args[1].to_u32) + else if pname == "*" then + return v.uint32_instance(recvval * args[1].to_u32) + else if pname == "%" then + return v.uint32_instance(recvval % args[1].to_u32) + else if pname == "/" then + return v.uint32_instance(recvval / args[1].to_u32) + else if pname == "<" then + return v.bool_instance(recvval < args[1].to_u32) + else if pname == ">" then + return v.bool_instance(recvval > args[1].to_u32) + else if pname == "<=" then + return v.bool_instance(recvval <= args[1].to_u32) + else if pname == ">=" then + return v.bool_instance(recvval >= args[1].to_u32) + else if pname == "<=>" then + return v.int_instance(recvval <=> args[1].to_u32) + else if pname == "to_f" then + return v.float_instance(recvval.to_f) + else if pname == "to_i" then + return v.int_instance(recvval.to_i) + else if pname == "to_b" then + return v.byte_instance(recvval.to_b) + else if pname == "to_i8" then + return v.int8_instance(recvval.to_i8) + else if pname == "to_i16" then + return v.int16_instance(recvval.to_i16) + else if pname == "to_u16" then + return v.uint16_instance(recvval.to_u16) + else if pname == "to_i32" then + return v.int32_instance(recvval.to_i32) + else if pname == "<<" then + return v.uint32_instance(recvval << (args[1].to_i)) + else if pname == ">>" then + return v.uint32_instance(recvval >> (args[1].to_i)) + else if pname == "&" then + return v.uint32_instance(recvval & args[1].to_u32) + else if pname == "|" then + return v.uint32_instance(recvval | args[1].to_u32) + else if pname == "^" then + return v.uint32_instance(recvval ^ args[1].to_u32) + else if pname == "unary ~" then + return v.uint32_instance(~recvval) + end else if pname == "native_argc" then return v.int_instance(v.arguments.length) else if pname == "native_argv" then @@ -1512,6 +1882,11 @@ redef class AIntegerExpr do if value isa Int then return v.int_instance(value.as(Int)) if value isa Byte then return v.byte_instance(value.as(Byte)) + if value isa Int8 then return v.int8_instance(value.as(Int8)) + if value isa Int16 then return v.int16_instance(value.as(Int16)) + if value isa UInt16 then return v.uint16_instance(value.as(UInt16)) + if value isa Int32 then return v.int32_instance(value.as(Int32)) + if value isa UInt32 then return v.uint32_instance(value.as(UInt32)) return null end end -- 1.7.9.5