I've implemented arbitrary dimensional arrays in the compiler and got everything working at run-time using a Copy On Write system that utilises the reference counting built into the VM. For what it is worth:
[source]
char[][][] c={ { "hello","world" },{ "hi","there" } };
char[][][] d=c;
out c[0][1],"\n";
out c[1][1],"\n";
out d[0][1],"\n";
out d[1][1],"\n";
c[0][1]="paul";
d[1][1]="froob";
out c[0][1],"\n";
out c[1][1],"\n";
out d[0][1],"\n";
out d[1][1],"\n";
[/source]
Compiles to (deep breath):
[source]
setrxi 2
push
setrxi 4
push
muli
pop
alloc
setrxi 1
setbt
movar
push
movar
push
setrxi 2
push
setrxi 4
push
muli
pop
alloc
setrxi 1
setbt
movar
push
movar
push
setrxi string1
push
axstm 8
get
movra
pop
put
pop
addax 4
movar
push
setrxi string2
push
axstm 8
get
movra
pop
put
pop
axstm 4
get
movra
incref
axstm 8
get
movra
pop
put
pop
addax 4
movar
push
setrxi 2
push
setrxi 4
push
muli
pop
alloc
setrxi 1
setbt
movar
push
movar
push
setrxi string3
push
axstm 8
get
movra
pop
put
pop
addax 4
movar
push
setrxi string4
push
axstm 8
get
movra
pop
put
pop
axstm 4
get
movra
incref
axstm 8
get
movra
pop
put
pop
axstm 4
get
movra
incref
axstm 4
get
push
axstm 4
get
movra
incref
axstm 8
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 8
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 8
get
movra
detach
movar
axstm 8
put
setrxi string6
push
axstm 12
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
push
get
movra
decref
pop
movra
pop
put
push
pop
gc
axstm 4
get
movra
detach
movar
axstm 4
put
setrxi string7
push
axstm 8
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
push
get
movra
decref
pop
movra
pop
put
push
pop
gc
axstm 8
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 8
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
pop
movra
decref
pop
movra
decref
gc
exit
-1 1 0 string5: 10b
-1 5 0 string7: "froob"
-1 5 0 string1: "hello"
-1 2 0 string3: "hi"
-1 4 0 string6: "paul"
-1 5 0 string4: "there"
-1 5 0 string2: "world"
[/source]
And correctly outputs what you would expect if deep copies of the arrays were being made. In fact the two arrays point to the same memory until the c[0][1]="paul" line, at which point a clone is created which c points to. When d[1][1]="froob" is executed, the same happens but as d only now has one reference, the cloning is skipped.
My god this stuff is hard to get right and to debug when it goes wrong. Wish my project was proceeding at the pace that Deyja's VM/compiler project was, but then I've written lots of these before and really wanted to see if I could get this nonsense to work.
This is an example of the VM running in debug mode, just to give you an idea of what I am staring at while going mad trying to work out what is broken.
[source]
Om 1.0 Copyright(C)2011 Aardvajk Software
REGISTERS:
pc : 5
ir : setrxi (11)
st : 1402
rx.i: 2
rx.f: 2.8026e-045
ax : 2004270470
cx : -1
ALLOCATED BLOCKS:
FREE BLOCKS:
2426: 1024
MEMORY SIZE: 3450
REGISTERS:
pc : 6
ir : push (21)
st : 1406
rx.i: 2
rx.f: 2.8026e-045
ax : 2004270470
cx : -1
ALLOCATED BLOCKS:
FREE BLOCKS:
2426: 1024
MEMORY SIZE: 3450
REGISTERS:
pc : 11
ir : setrxi (11)
st : 1406
rx.i: 4
rx.f: 5.60519e-045
ax : 2004270470
cx : -1
ALLOCATED BLOCKS:
FREE BLOCKS:
2426: 1024
MEMORY SIZE: 3450
ALLOCATED BLOCKS:
STACK:
1402: 2 (2)
1403: 0
1404: 0
1405: 0
[/source]
Need to think about array resizing next really. Not much point having dynamic arrays in a language if they, er, aren't.