[m-rev.] for review: Arrays for the .NET backend (library part)

Tyson Dowd trd at cs.mu.OZ.AU
Fri Aug 3 02:22:07 AEST 2001


Hi,

Here's the library portion of the Arrays for .NET change.
It's getting late so I haven't quite implemented shrink and resize,
although I think they are not very hard to do.

make_empty_array and copy are much harder, and will not be done in the
very near future.

I decided to implement this code in C#, because it is much cleaner.

The MC++ interface still relies on LLDS backend type mappings, so it
uses MR_Word for arrays in the generated C code strings).
The MC++ interface sucks.

One annoying thing about the C# interface is that it gives warnings
about the unused "succeeded" variable.  We can live with this for a
little longer.

===================================================================


Estimated hours taken: 2
Branches: main

Implement the array operations (in C#).
Add flags for handling compiling C# files in the library directory.

library/Mmakefile:
	Add /t:module to the C# compiler flags.
	We need this because by default C# creates assemblies, but we
	expect the library to be composed of modules.

library/array.m:
	Implement most of the functionality of arrays in C#.
	We still need to implement
		array__make_empty_array/1
		array__resize/4
		array__shrink/3
		array__copy/3
	array__make_empty_array requires more RTTI support (to figure
	out what type of array to create), and array__copy requires an
	implementation of deep_copy.

	You need the latest changes to the compiler which map array(T)
	to System.Array or T[] for this to compile.
	
scripts/Mmake.rules:
	Add ALL_MS_CSCFLAGS to the C# compiler flags.

scripts/Mmake.vars.in:
	Add MS_CSCFLAGS and associated flags.
	

Index: library/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/mercury/library/Mmakefile,v
retrieving revision 1.75
diff -u -r1.75 Mmakefile
--- library/Mmakefile	2001/07/27 06:19:54	1.75
+++ library/Mmakefile	2001/08/02 16:15:26
@@ -94,6 +94,7 @@
 	# -AI sets the assembly search path (just like -I for assemblies)
 MS_CLFLAGS  =	-AI$(RUNTIME_DIR) -I$(RUNTIME_DIR)
 MS_CL_NOASM=:noAssembly
+MS_CSCFLAGS=/t:module
 LDFLAGS	=	-L$(BOEHM_GC_DIR) -L$(RUNTIME_DIR)
 ALL_LDFLAGS =	$(LDFLAGS) $(EXTRA_LDFLAGS)
 LDLIBS	=	-l$(RT_LIB_NAME) \
Index: library/array.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/array.m,v
retrieving revision 1.94
diff -u -r1.94 array.m
--- library/array.m	2001/07/03 08:16:24	1.94
+++ library/array.m	2001/08/02 16:15:26
@@ -747,22 +747,19 @@
 	Array = (MR_Word) ML_make_array(0, 0);
 ").
 
-:- pragma foreign_proc("MC++", 
+:- pragma foreign_proc("C#", 
 		array__init(Size::in, Item::in, Array::array_uo),
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-		// XXX still need to do init
-	Array = (MR_Word) System::Array::CreateInstance(Item->GetType(), Size);
+	Array = System.Array.CreateInstance(Item.GetType(), Size);
+	for (int i = 0; i < Size; i++) {
+		Array.SetValue(Item, i);
+	}
 ").
 
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__make_empty_array(Array::array_uo),
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-		// XXX this is inefficient.
-	Array = (MR_Word) 
-		System::Array::CreateInstance(
-			(new System::Object)->GetType(), 0);
+        mercury.runtime.Errors.SORRY(""foreign code for this predicate"");
 ").
 
 
@@ -781,17 +778,15 @@
 	Min = 0;
 ").
 
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__min(Array::array_ui, Min::out),
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
 	/* Array not used */
 	Min = 0;
 ").
-:- pragma foreign_proc("MC++", 
+:- pragma foreign_proc("C#", 
 		array__min(Array::in, Min::out),
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
 	/* Array not used */
 	Min = 0;
 ").
@@ -806,17 +801,15 @@
 		[will_not_call_mercury, thread_safe], "
 	Max = ((MR_ArrayType *)Array)->size - 1;
 ").
-:- pragma foreign_proc("MC++", 
+:- pragma foreign_proc("C#", 
 		array__max(Array::array_ui, Max::out), 
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-	Max = Array->get_Length() - 1;
+	Max = Array.Length - 1;
 ").
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__max(Array::in, Max::out), 
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-	Max = Array->get_Length() - 1;
+	Max = Array.Length - 1;
 ").
 
 
@@ -837,17 +830,15 @@
 	Max = ((MR_ArrayType *)Array)->size;
 ").
 
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__size(Array::array_ui, Max::out),
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-	Max = Array->get_Length() - 1;
+	Max = Array.Length;
 ").
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__size(Array::in, Max::out),
 		[will_not_call_mercury, thread_safe], "
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-	Max = Array->get_Length() - 1;
+	Max = Array.Length;
 ").
 
 
@@ -900,17 +891,15 @@
 	Item = array->elements[Index];
 }").
 
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__lookup(Array::array_ui, Index::in, Item::out),
 		[will_not_call_mercury, thread_safe], "{
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-	Item = dynamic_cast<MR_Word>(Array->GetValue(Index));
+	Item = Array.GetValue(Index);
 }").
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__lookup(Array::in, Index::in, Item::out),
 		[will_not_call_mercury, thread_safe], "{
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
-	Item = dynamic_cast<MR_Word>(Array->GetValue(Index));
+	Item = Array.GetValue(Index);
 }").
 
 
@@ -931,13 +920,12 @@
 	Array = Array0;
 }").
 
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__set(Array0::array_di, Index::in,
 		Item::in, Array::array_uo),
 		[will_not_call_mercury, thread_safe], "{
-	Array0->SetValue(Item, Index);	/* destructive update! */
+	Array0.SetValue(Item, Index);	/* destructive update! */
 	Array = Array0;
-        mercury::runtime::Errors::SORRY(""foreign code for this predicate"");
 }").
 
 
@@ -989,10 +977,10 @@
 	Array = (MR_Word) ML_resize_array(
 				(MR_ArrayType *) Array0, Size, Item);
 ").
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__resize(_Array0::array_di, _Size::in, _Item::in,
 		_Array::array_uo), [will_not_call_mercury, thread_safe], "
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+	mercury.runtime.Errors.SORRY(""foreign code for this function"");
 ").
 
 
@@ -1041,10 +1029,10 @@
 	Array = (MR_Word) ML_shrink_array(
 				(MR_ArrayType *) Array0, Size);
 ").
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__shrink(_Array0::array_di, _Size::in, _Array::array_uo),
 		[will_not_call_mercury, thread_safe], "
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+	mercury.runtime.Errors.SORRY(""foreign code for this function"");
 ").
 
 
@@ -1093,19 +1081,19 @@
 	Array = (MR_Word) ML_copy_array((MR_ArrayType *) Array0);
 ").
 
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__copy(Array0::array_ui, Array::array_uo),
 		[will_not_call_mercury, thread_safe], "
 		// XXX need to deep copy it
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+	mercury.runtime.Errors.SORRY(""foreign code for this function"");
 	Array = Array0;
 
 ").
 
-:- pragma foreign_proc("MC++",
+:- pragma foreign_proc("C#",
 		array__copy(Array0::in, Array::array_uo),
 		[will_not_call_mercury, thread_safe], "
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+	mercury.runtime.Errors.SORRY(""foreign code for this function"");
 		// XXX need to deep copy it
 	Array = Array0;
 ").
Index: scripts/Mmake.rules
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/Mmake.rules,v
retrieving revision 1.107
diff -u -r1.107 Mmake.rules
--- scripts/Mmake.rules	2001/07/31 10:08:03	1.107
+++ scripts/Mmake.rules	2001/08/02 16:15:26
@@ -259,8 +259,8 @@
 	rm -f $*.obj
 
 $(os_subdir)%.dll : %.cs
-	csc /t:library /lib:`cygpath -w $(MERC_DLL_DIR)` /out:$@ \
-		$(CSHARP_ASSEMBLY_REFS-$*) $<
+	$(MS_CSC) /t:library /lib:`cygpath -w $(MERC_DLL_DIR)` /out:$@ \
+		$(CSHARP_ASSEMBLY_REFS-$*) $(ALL_MS_CSCFLAGS) $<
 
 $(os_subdir)%.dll : %.il
 	$(MS_ILASM) $(ALL_MS_ILASMFLAGS) /dll /quiet /OUT=$@ $<
@@ -368,8 +368,8 @@
 	rm -f $*.obj
 
 .cs.dll:
-	csc /t:library /lib:`cygpath -w $(MERC_DLL_DIR)` /out:$@ \
-		$(CSHARP_ASSEMBLY_REFS-$*) $<
+	$(MS_CSC) /t:library /lib:`cygpath -w $(MERC_DLL_DIR)` /out:$@ \
+		$(CSHARP_ASSEMBLY_REFS-$*) $(EXTRA_CSCFLAGS) $<
 
 .cpp.exe:
 	$(MS_CL) -CLR($MS_CL_NOASM) -I$(MERCURY_LIBRARY_PATH) $< -link -entry:main $(MS_CL_LIBS) -out:$@
Index: scripts/Mmake.vars.in
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/Mmake.vars.in,v
retrieving revision 1.53
diff -u -r1.53 Mmake.vars.in
--- scripts/Mmake.vars.in	2001/07/27 16:06:29	1.53
+++ scripts/Mmake.vars.in	2001/08/02 16:15:27
@@ -174,6 +174,14 @@
 # assemblies.
 MS_AL		= al
 
+# MS_CSC is the command line version of C# compiler
+MS_CSC		= csc
+ALL_MS_CSCFLAGS	= $(MS_CSCFLAGS) $(EXTRA_MS_CSCFLAGS) $(TARGET_MS_CSCFLAGS) \
+		$(LIB_MS_CSCFLAGS)
+MS_CSCFLAGS	=
+EXTRA_MS_CSCFLAGS =
+LIB_MS_CSCFLAGS	= 
+
 ML		= ml
 ALL_MLFLAGS	= $(MLFLAGS) $(EXTRA_MLFLAGS) $(TARGET_MLFLAGS) $(LIB_MLFLAGS)
 MLFLAGS		= $(EXTRA_MLFLAGS)
@@ -376,6 +384,11 @@
   $(maybe-base-MS_CLFLAGS-$(findstring undefined,$(origin MS_CLFLAGS-$*)))
 maybe-base-MS_CLFLAGS- = $(MS_CLFLAGS-$*)
 maybe-base-MS_CLFLAGS-undefined =
+
+TARGET_MS_CSCFLAGS = \
+  $(maybe-base-MS_CSCFLAGS-$(findstring undefined,$(origin MS_CSCFLAGS-$*)))
+maybe-base-MS_CSCFLAGS- = $(MS_CSCFLAGS-$*)
+maybe-base-MS_CSCFLAGS-undefined =
 
 TARGET_MS_ILASMFLAGS = \
    $(maybe-base-MS_ILASMFLAGS-$(findstring undefined,$(origin MS_ILASMFLAGS-$*)))

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list