Add int float casts to vm

This commit is contained in:
Andrii Riabushenko
2018-10-24 11:24:23 +01:00
parent 72370c8dfa
commit a7c9533a73
3 changed files with 40 additions and 3 deletions

View File

@@ -488,6 +488,18 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
of opcAsgnFloat:
decodeB(rkFloat)
regs[ra].floatVal = regs[rb].floatVal
of opcAsgnIntFromFloat32:
decodeB(rkFloat)
regs[ra].intVal = cast[int32](float32(regs[rb].floatVal))
of opcAsgnIntFromFloat64:
decodeB(rkFloat)
regs[ra].intVal = cast[int64](regs[rb].floatVal)
of opcAsgnFloat32FromInt:
decodeB(rkInt)
regs[ra].floatVal = cast[float32](int32(regs[rb].intVal))
of opcAsgnFloat64FromInt:
decodeB(rkInt)
regs[ra].floatVal = cast[float64](int64(regs[rb].intVal))
of opcAsgnComplex:
asgnComplex(regs[ra], regs[instr.regB])
of opcAsgnRef:

View File

@@ -35,6 +35,10 @@ type
opcAsgnStr,
opcAsgnFloat,
opcAsgnRef,
opcAsgnIntFromFloat32, # int and float must be of the same byte size
opcAsgnIntFromFloat64, # int and float must be of the same byte size
opcAsgnFloat32FromInt, # int and float must be of the same byte size
opcAsgnFloat64FromInt, # int and float must be of the same byte size
opcAsgnComplex,
opcNodeToReg,

View File

@@ -769,7 +769,7 @@ proc genCard(c: PCtx; n: PNode; dest: var TDest) =
c.gABC(n, opcCard, dest, tmp)
c.freeTemp(tmp)
proc genIntCast(c: PCtx; n: PNode; dest: var TDest) =
proc genCastIntFloat(c: PCtx; n: PNode; dest: var TDest) =
const allowedIntegers = {tyInt..tyInt64, tyUInt..tyUInt64, tyChar}
var signedIntegers = {tyInt8..tyInt32}
var unsignedIntegers = {tyUInt8..tyUInt32, tyChar}
@@ -809,8 +809,29 @@ proc genIntCast(c: PCtx; n: PNode; dest: var TDest) =
c.freeTemp(tmp)
c.freeTemp(tmp2)
c.freeTemp(tmp3)
elif src_size == getSize(c.config, dst) and src.kind in allowedIntegers and
dst.kind in {tyFloat, tyFloat32, tyFloat64}:
let tmp = c.getTemp(n.sons[1].typ)
if dest < 0: dest = c.getTemp(n[0].typ)
if dst.kind == tyFloat32:
c.gABC(n, opcAsgnFloat32FromInt, dest, tmp)
else:
c.gABC(n, opcAsgnFloat64FromInt, dest, tmp)
c.freeTemp(tmp)
elif src_size == getSize(c.config, dst) and src.kind in {tyFloat, tyFloat32, tyFloat64} and
dst.kind in allowedIntegers:
let tmp = c.getTemp(n.sons[1].typ)
if dest < 0: dest = c.getTemp(n[0].typ)
if src.kind == tyFloat32:
c.gABC(n, opcAsgnIntFromFloat32, dest, tmp)
else:
c.gABC(n, opcAsgnIntFromFloat64, dest, tmp)
c.freeTemp(tmp)
else:
globalError(c.config, n.info, "VM is only allowed to 'cast' between integers of same size")
globalError(c.config, n.info, "VM is only allowed to 'cast' between integers and/or floats of same size")
proc genVoidABC(c: PCtx, n: PNode, dest: TDest, opcode: TOpcode) =
unused(c, n, dest)
@@ -2008,7 +2029,7 @@ proc gen(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}) =
if allowCast in c.features:
genConv(c, n, n.sons[1], dest, opcCast)
else:
genIntCast(c, n, dest)
genCastIntFloat(c, n, dest)
of nkTypeOfExpr:
genTypeLit(c, n.typ, dest)
of nkComesFrom: