From fe9127ea91d54adb9f84228a66e15dbed627c36c Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Sat, 21 Mar 2015 22:35:11 +0700 Subject: [PATCH] compiler: add native_array_get and native_array_set Signed-off-by: Jean Privat --- src/compiler/abstract_compiler.nit | 8 ++++++++ src/compiler/global_compiler.nit | 13 +++++++++++++ src/compiler/separate_compiler.nit | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index c3952e3..88bbc2c 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -1126,6 +1126,14 @@ abstract class AbstractCompilerVisitor fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]) is abstract + # Return an element of a native array. + # The method is unsafe and is just a direct wrapper for the specific implementation of native arrays + fun native_array_get(native_array: RuntimeVariable, index: Int): RuntimeVariable is abstract + + # Store an element in a native array. + # The method is unsafe and is just a direct wrapper for the specific implementation of native arrays + fun native_array_set(native_array: RuntimeVariable, index: Int, value: RuntimeVariable) is abstract + # Evaluate `args` as expressions in the call of `mpropdef` on `recv`. # This method is used to manage varargs in signatures and returns the real array # of runtime variables to use in the call. diff --git a/src/compiler/global_compiler.nit b/src/compiler/global_compiler.nit index 9a89064..1aff84e 100644 --- a/src/compiler/global_compiler.nit +++ b/src/compiler/global_compiler.nit @@ -407,6 +407,19 @@ class GlobalCompilerVisitor return self.new_expr("NEW_{ret_type.c_name}({length})", ret_type) end + redef fun native_array_get(nat, i) + do + var recv = "((struct {nat.mcasttype.c_name}*){nat})->values" + var ret_type = nat.mcasttype.as(MClassType).arguments.first + return self.new_expr("{recv}[{i}]", ret_type) + end + + redef fun native_array_set(nat, i, val) + do + var recv = "((struct {nat.mcasttype.c_name}*){nat})->values" + self.add("{recv}[{i}]={val};") + end + redef fun calloc_array(ret_type, arguments) do self.ret(self.new_expr("NEW_{ret_type.c_name}({arguments[1]})", ret_type)) diff --git a/src/compiler/separate_compiler.nit b/src/compiler/separate_compiler.nit index 8cb75e7..83749b6 100644 --- a/src/compiler/separate_compiler.nit +++ b/src/compiler/separate_compiler.nit @@ -2043,6 +2043,22 @@ class SeparateCompilerVisitor end end + redef fun native_array_get(nat, i) + do + var nclass = mmodule.native_array_class + var recv = "((struct instance_{nclass.c_name}*){nat})->values" + # Because the objects are boxed, return the box to avoid unnecessary (or broken) unboxing/reboxing + var res = self.new_expr("{recv}[{i}]", compiler.mainmodule.object_type) + return res + end + + redef fun native_array_set(nat, i, val) + do + var nclass = mmodule.native_array_class + var recv = "((struct instance_{nclass.c_name}*){nat})->values" + self.add("{recv}[{i}]={val};") + end + fun link_unresolved_type(mclassdef: MClassDef, mtype: MType) do assert mtype.need_anchor var compiler = self.compiler -- 1.7.9.5