Dictu icon indicating copy to clipboard operation
Dictu copied to clipboard

Chunk constant

Open Jason2605 opened this issue 5 years ago • 6 comments

Currently a chunk is only allowed 255 constants at a time.

Jason2605 avatar Mar 06 '20 16:03 Jason2605

I ran into this over the holiday break while writing a language translation module. I'd love to give this a go if that's alright.

briandowns avatar Jan 10 '23 02:01 briandowns

That’s absolutely fine @briandowns! I think there are examples of how we could handle this in Wren which I can find for you if you need them.

Jason2605 avatar Jan 16 '23 11:01 Jason2605

Yes, please! Any and all help/hints are appreciated. 😀

briandowns avatar Jan 16 '23 14:01 briandowns

Ran into this fun one again. Trying some things now. Any points or suggestions are very welcome.

briandowns avatar Sep 10 '23 03:09 briandowns

Ah yeah there’s a hard 255 limit at the moment as that’s all we’re allowed to pass with our constant byte code. We’d probably want a new opcode that deals with passing 2 bytes for the index once we get over 255

Jason2605 avatar Sep 10 '23 14:09 Jason2605

Bit more context, if we use the same opcode and just change it to be 2 bytes instead:

  • https://github.com/dictu-lang/Dictu/blob/develop/src/vm/vm.c#L841 Here we'd change it to READ_SHORT()

Here (and essentially all other places that make constants) we'd need to change it to emit two bytes instead (like how loops work)

  • https://github.com/dictu-lang/Dictu/blob/develop/src/vm/compiler.c#L139-L141
int constant = makeConstant(compiler, value);
emitByte(compiler, OP_CONSTANT);
emitBytes((constant >> 8) & 0xff, constant & 0xff);

And then change the check to be the new limit 1 << 16

Jason2605 avatar Sep 10 '23 14:09 Jason2605