DavidKinder / Inform6

The latest version of the Inform 6 compiler, used for generating interactive fiction games.
http://inform-fiction.org/
Other
204 stars 34 forks source link

Assembly format for inline bytes (and words) #251

Closed erkyrath closed 1 year ago

erkyrath commented 1 year ago

As described in this thread: https://intfiction.org/t/i6-assembly-bytes-extension/64439 Covers https://github.com/DavidKinder/Inform6/issues/245

Two new statement types, handled as new assembly syntax:

@ -> BYTE BYTE BYTE ...;
@ --> WORD WORD WORD ...;

The given bytes or words are directly copied out into the function. (Words are two-byte chunks in Z-code, four-byte chunks in Glulx.) The compiler assumes the data forms valid opcodes, but verifying this is your problem.


Additional changes:

The --trace asm=2 output used to show the bytecode like this:

    8  +00011 <*> aload        long_16 (internal array: foo) zero_ local_0 (val) 
                               48 03 09 00{06} 00 00 10 00 

The {06} is the backpatch marker (referring to that byte and the following three). This wasn't very legible and it was only implemented for Glulx. I have now made it more readable, and it works in both G and Z:

[Glulx]
    8  +00011 <*> aload        long_16 (internal array: foo) zero_ local_0 (val) 
                               48 03 09 {arr}00 00 00 10 00

[Z-code]
    8  +00009 <*> loadw        long_488 (internal array: foo) short_0 -> val 
                               cf 1f {arr}01 e8 00 01 

The marker is now a short text label which appears before the two-byte/four-byte sequence. (As opposed to appearing after the first byte.) To support this, I've added a describe_mv_short() function to bpatch.c -- this is just like describe_mv() but prints an abbreviation.

I reindented the parse_assembly_g() function, so there's a lot of diff lines that are whitespace-only.


Future work:

My original post mentioned the idea of letting game code get a pointer to function code, perhaps through a label. This would let you compile data into a function. (Of course in Zcode this would only work for functions whose real address was under $FFFF.)

I still like this idea, but I haven't implemented it yet. It would require both new syntax and new backpatching logic. For now, if you want static data for a function, use the Array static declaration.