Tuesday, December 30, 2008

Jagging Away

The D programming language does not support multi-dimensional arrays.

Instead, multi-dimensional matrices can be implemented with arrays of arrays (aka jagged arrays), same as in C and C++.

When a static, multidimensional array needs to be initialized, in a statement such as:

int foo[3][4][5][6];

the native compiler back-end implicitly initializes the array by reserving the memory and filling it with zeros.

In the .NET back-end for the D compiler that I am working on, things are different: explicit newarr calls are required, in conjunction with navigating the data structure and initializing the individual elements.

And this is where it gets interesting. The array may have any arbitrary rank, and thus the compiler needs to figure out the types of the nested arrays; for the example above, they are:

int32 [][][][]
int32 [][][]
int32 [][]
int32 []

My implementation uses a runtime helper function in the dnetlib.dll assembly; rather than trying to determine the rank of the array and the types involved, the compiler back-end simply generates a call to the runtime helper, which does the heavy lifting. This solution works for jagged arrays of any rank.

The helper code itself is written in C# and uses generic recursion; it appends square brackets [] to the generic parameter at each recursion level, like shown below.

namespace runtime
{
public class Array
{
//helper for initalizing jagged array
static public void Init<T>(System.Array a, uint[] sizes, int start, int length)
{
if (length == 2)
{
uint n = sizes[start];
uint m = sizes[start + 1];
for (uint i = 0; i != n; ++i)
{
a.SetValue(new T[m], i);
}
}
else
{
--length;
//call recursively, changing template parameter from T to T[]
Init<T[]>(a, sizes, start, length);
uint n = sizes[start];
for (uint i = 0; i != n; ++i)
{
Init
<T>((System.Array)a.GetValue(i), sizes, start + 1, length);
}
}
}

//called at runtime
static public void Init<T>(System.Array a, uint[] sizes)
{
Init<T>(a, sizes, 0, sizes.Length);
}
}
} //namespace runtime

1 comment:

Jonathan Allen said...

Your compiler project seem pretty cool so I would like to interview you for the news site InfoQ. If you are interested, drop me a line at jonathan@infoq.com.