Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIR: asm statement support, Wip #22992

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ddaea3b
Asm (gcc extended asm) for nir
ASVIEST Nov 27, 2023
73eb391
Update genasm.nim
ASVIEST Nov 27, 2023
73f9b60
Asm parsing now working on backends (WIP(
ASVIEST Nov 29, 2023
a4ddb3b
Emit targets
ASVIEST Nov 29, 2023
03af260
Remove asm instrs
ASVIEST Nov 29, 2023
545d35a
Update nirinsts.nim
ASVIEST Dec 1, 2023
5cefc65
verbatims
ASVIEST Dec 1, 2023
cd5de29
gcc asm (simple)
ASVIEST Dec 2, 2023
c0ab580
fix lastSon
ASVIEST Dec 2, 2023
78a4c61
Seperated file
ASVIEST Dec 2, 2023
e4af3f0
target properties
ASVIEST Dec 2, 2023
e9221ae
GCC asm c codegen
ASVIEST Dec 2, 2023
8419151
Small fix
ASVIEST Dec 2, 2023
57802ed
apply suggestions
ASVIEST Dec 3, 2023
a1f9c3f
msvc asm
ASVIEST Dec 3, 2023
f41557d
info's
ASVIEST Dec 3, 2023
fc8b91f
fix
ASVIEST Dec 3, 2023
68bc65d
fix
ASVIEST Dec 3, 2023
3d4f45e
Extended asm is now default inline asm syntax
ASVIEST Dec 3, 2023
119e15b
Merge branch 'devel' into nir-asm
ASVIEST Dec 3, 2023
24b6c55
support asmNoStackFrame
ASVIEST Dec 3, 2023
bb463f1
Merge branch 'nir-asm' of https://github.com/ASVIEST/Nim into nir-asm
ASVIEST Dec 3, 2023
5f58ec9
use strings for verbatims
ASVIEST Dec 4, 2023
330ae88
fix asmNoStackFrame
ASVIEST Dec 4, 2023
21ee838
fix
ASVIEST Dec 4, 2023
cc45710
small refactor
ASVIEST Dec 7, 2023
1b88194
Merge branch 'nim-lang:devel' into nir-asm
ASVIEST Dec 7, 2023
c7d3093
Merge branch 'nim-lang:devel' into nir-asm
ASVIEST Dec 12, 2023
11a6fe8
inlineAsmSyntax pragma
ASVIEST Dec 12, 2023
ec8beea
Merge branch 'nim-lang:devel' into nir-asm
ASVIEST Dec 12, 2023
540ef8f
simple dialect switch
ASVIEST Dec 13, 2023
5d6c354
Update cir.nim
ASVIEST Dec 13, 2023
f0efd4c
Update cir.nim
ASVIEST Dec 13, 2023
811e6ab
Revert "Update cir.nim"
ASVIEST Dec 13, 2023
94c7fa0
Merge branch 'nim-lang:devel' into nir-asm
ASVIEST Dec 13, 2023
747ead5
Merge branch 'nim-lang:devel' into nir-asm
ASVIEST Dec 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/nir/ast2ir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,8 @@ proc genComplexCall(c: var ProcCon; n: PNode; d: var Value) =
else:
genCall c, n, d

include genasm

proc gen(c: var ProcCon; n: PNode; d: var Value; flags: GenFlags = {}) =
when defined(nimCompilerStacktraceHints):
setFrameMsg c.config$n.info & " " & $n.kind & " " & $flags
Expand Down Expand Up @@ -2560,6 +2562,11 @@ proc gen(c: var ProcCon; n: PNode; d: var Value; flags: GenFlags = {}) =
unused(c, n, d)
genWhile(c, n)
of nkBlockExpr, nkBlockStmt: genBlock(c, n, d)
of nkAsmStmt:
if c.prc == nil:
genGlobalAsm(c, n)
else:
genInlineAsm(c, n)
of nkReturnStmt: genReturn(c, n)
of nkRaiseStmt: genRaise(c, n)
of nkBreakStmt: genBreak(c, n)
Expand Down
67 changes: 67 additions & 0 deletions compiler/nir/cir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type
CaseKeyword = "case "
DefaultKeyword = "default:"
BreakKeyword = "break"
AsmKeyword = "__asm__ "
NullPtr = "nullptr"
IfNot = "if (!("
ReturnKeyword = "return "
Expand Down Expand Up @@ -812,6 +813,72 @@ proc gen(c: var GeneratedCode; t: Tree; n: NodePos) =
of ForeignProcDecl: genProcDecl c, t, n, true
of PragmaPair, PragmaId, TestOf, Yld, SetExc, TestExc:
c.add "cannot interpret: " & $t[n].kind

of AsmGlobal:
c.add AsmKeyword
c.add ParLe
c.add NewLine
for ch in sons(t, n):
if t[ch].kind == StrVal:
let s = c.m.lit.strings[t[ch].litId]
var left = 0
for j in 0..s.high:
if s[j] == '\n':
c.add makeCString(s[left..j])
c.add NewLine
left = j + 1
else:
gen c, t, ch

c.add ParRi
c.add Semicolon
c.add NewLine
of Asm:
c.add AsmKeyword
c.add ParLe
gen c, t, n.firstSon #asmTemplate
for ch in sonsFrom1(t, n):
c.add Colon
gen c, t, ch
c.add NewLine
c.add ParRi
c.add Semicolon
c.add NewLine
of AsmTemplate:
c.add NewLine
for i in sons(t, n):
let s = c.m.lit.strings[t[i].litId]
var left = 0
for j in 0..s.high:
if s[j] == '\n':
# separate every '\n'
# into different string like:
# " mov %1, %0\n"
# " add $1, %0\n"
# to produce more readable code
c.add makeCString(s[left..j])
c.add NewLine
left = j + 1
of AsmOutputOperand, AsmInputOperand:
let (dest, asmSymbolicName, constraint) = sons3(t, n)
let
symbolicName = c.m.lit.strings[t[asmSymbolicName].litId]
constraintStr = c.m.lit.strings[t[constraint].litId]

if symbolicName != "":
c.add BracketLe
c.add '"' & symbolicName & '"'
c.add BracketRi

c.add '"' & constraintStr & '"'
gen c, t, dest
of AsmInjectExpr:
c.add ParLe
gen c, t, n.firstSon
c.add ParRi
of AsmClobber:
let clobber = t[n.firstSon]
c.add c.m.lit.strings[clobber.litId]

const
Prelude = """
Expand Down
Loading