Dialog optimizer curiosity

I was curious about Dialog’s optimizer, so I tried a little experiment. Here are two files, indirection1.dg and indirection2.dg:

(program entry point)
	(alpha)

(alpha)
	Hello world!
(program entry point)
	(alpha)

(alpha)
	(beta)

(beta)
	Hello world!

With the current version of the 1b/01-dev compiler, these produce absolutely identical code, down to the byte:

-rw-rw-r-- 1 daniel daniel 5924 Mar 14 18:51 indirection1.z5
-rw-rw-r-- 1 daniel daniel 5924 Mar 14 18:52 indirection2.z5
$ diff indirection1.z5 indirection2.z5 && echo "Identical"
Identical

If I add one more layer of indirection, though:

(program entry point)
	(alpha)

(alpha)
	(beta)

(beta)
	(gamma)

(gamma)
	Hello world!

The file grows by four bytes.

I haven’t been able to figure out what exactly changes, or why. But I’m putting it here in case anyone more familiar with Z-machine bytecode might understand.

indirection1.z5 (5.8 KB)
indirection2.z5 (5.8 KB)

1 Like

I think the issue might be related to the routine address calculation of TERPTEST (the packed address differs by 1, changing the byte address by 4 bytes):

MAIN ROUTINE 13a9
-  13a9: 8f 05 b0                 call_1n          #16c0 
+  13a9: 8f 05 b1                 call_1n          #16c4
-000016b0: 00b1 0000           00da 8f36 05c6 2d14
+000016b0: 00b1 0000 008b 05ae 00da 8f36 05c7 2d14
# Commands I used, in case they're helpful
zd -d indirection1.z5 > indir1.txt
zd -d indirection2.z5 > indir2.txt
diffoscope indir1.txt indir2.txt
diffoscope indirection1.z5 indirection2.z5

The Å-machine backend produces an extra tail call:

L000010: 87                   tail               
         87                   tail               
         60 00                print_a_str_a      "Hello world!"
         03                   proceed
1 Like