Beginning of a multibyte conversion helper

This commit is contained in:
Productive2 2021-01-04 23:49:30 +01:00
parent 383af42d76
commit 67d4b77c8f
3 changed files with 18 additions and 3 deletions

View File

@ -31,6 +31,7 @@ import types/methods
import tables
import config
import memory
import multibyte
when isMainModule:
import util/debug
@ -716,7 +717,7 @@ proc patchJump(self: Compiler, offset: int) =
if jump > (int uint16.high):
self.compileError("too much code to jump over")
else:
let casted = cast[array[2, uint8]](jump)
let casted = toDouble(jump)
self.currentChunk.code[offset] = casted[0]
self.currentChunk.code[offset + 1] = casted[1]
@ -751,8 +752,9 @@ proc emitLoop(self: Compiler, start: int) =
if offset > (int uint16.high):
self.compileError("loop body is too large")
else:
self.emitByte(uint8 offset and 0xff)
self.emitByte(uint8 (offset shr 8) and 0xff)
let offsetBytes = toDouble(offset)
self.emitByte(offsetBytes[0])
self.emitByte(offsetBytes[1])
proc endLooping(self: Compiler) =

7
src/multibyte.nim Normal file
View File

@ -0,0 +1,7 @@
proc toDouble*(input: int | uint | uint16): array[2, uint8] =
cast[array[2, uint8]](uint16(input))
proc fromDouble*(input: array[2, uint8]): uint16 =
copyMem(result.addr, unsafeAddr(input), sizeof(uint16))

6
tests/nim/multibyte.nim Normal file
View File

@ -0,0 +1,6 @@
import ../../src/multibyte
for i in countup(0, int(uint16.high())):
assert fromDouble(toDouble(i)) == uint16(i)