Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Sep 24, 2024
1 parent 95fbaf7 commit b961821
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
34 changes: 31 additions & 3 deletions src/nifc/amd64/genasm_e.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ template unOp(opr) {.dirty.} =
genTypeof c, n.firstSon
genx c, t, n.firstSon, WantValue

proc genAsgn(c: var GeneratedCode; dest, src: Location) =
proc genMov(c: var GeneratedCode; dest, src: Location) =
if sameLocation(dest, src):
discard "don't generate `mov rax, rax` etc"
elif Indirect in src.flags:
Expand Down Expand Up @@ -186,7 +186,7 @@ proc into(c: var GeneratedCode; dest: var Location; src: Location) =
elif dest.kind == InFlag and src.kind == InFlag and dest.flag == NopT:
dest.flag = src.flag
else:
genAsgn c, dest, src
genMov c, dest, src

proc genCall(c: var GeneratedCode; t: Tree; n: NodePos; dest: var Location) =
var args: seq[NodePos] = @[] # so that we can also do it backwards
Expand Down Expand Up @@ -228,7 +228,7 @@ proc genCall(c: var GeneratedCode; t: Tree; n: NodePos; dest: var Location) =
if dest.kind == Undef:
dest = resultWin64(ts)
else:
c.genAsgn(dest, resultWin64(ts))
c.genMov(dest, resultWin64(ts))

const
AddrTyp = AsmSlot(kind: AInt, size: WordSize, align: WordSize, offset: 0)
Expand Down Expand Up @@ -353,6 +353,34 @@ proc genLoad(c: var GeneratedCode; dest: var Location; address: Location) =
c.buildTree Mem1T:
emitLoc c, address

proc genAsgn(c: var GeneratedCode; t: Tree; n: NodePos) =
let (a, b) = sons2(t, n)
# special case local variables as these can be in registers
# which have no address:
if t[a].kind == Sym:
let lit = t[a].litId
let def = c.m.defs.getOrDefault(lit)
case def.kind
of VarC, ParamC:
let d = c.locals[lit]

let y = gen(c, t, n)
genMov c, d, y
freeTemp c, y
return
var d = Location(kind: Undef)
genAddr c, t, a, d

let y = makeReg gen(c, t, n)

# XXX also handle case kind == AMem!
let opc = if address.typ.kind == AFloat: MovapdT else: MovT
c.buildTree opc:
c.buildTree Mem1T:
emitLoc c, d
emitLoc c, y
freeTemp c, y

proc genLvalue(c: var GeneratedCode; t: Tree; n: NodePos; dest: var Location) =
let info = t[n].info
case t[n].kind
Expand Down
10 changes: 0 additions & 10 deletions src/nifc/amd64/genasm_s.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,12 @@ proc genSwitch(c: var GeneratedCode; t: Tree; caseStmt: NodePos) =
c.buildTree LabT, t[caseStmt].info:
c.defineLabel endif, t[caseStmt].info

proc genAsgn(c: var GeneratedCode; t: Tree; n: NodePos) =
let (dest, src) = sons2(t, n)
let isAsgn = t[dest].kind == Sym
let opc = if isAsgn: AsgnT else: StoreT
c.buildTree opc, t[n].info:
genTypeof c, dest
genx c, t, dest, (if isAsgn: WantValue else: WantAddr)
genx c, t, src, WantValue

proc genReturn(c: var GeneratedCode; t: Tree; n: NodePos) =
c.buildTree RetT, t[n].info:
genTypeof c, n.firstSon
c.genx t, n.firstSon, WantValue

proc genStmt(c: var GeneratedCode; t: Tree; n: NodePos) =
c.stmtBegin = c.code.len
case t[n].kind
of Empty:
discard
Expand Down

0 comments on commit b961821

Please sign in to comment.