Add int float casts to vm

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

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: