From 5f3f1feecad53ad5582f4af1dd9d240b4af2516f Mon Sep 17 00:00:00 2001 From: ASVIEST Date: Fri, 22 Dec 2023 21:02:27 +0300 Subject: [PATCH 1/7] inlineAsmSyntax pragma It can't generate different asm code for ICC because asmStmtFrmt is just string. --- compiler/ccgstmts.nim | 15 +++++++++++++++ compiler/pragmas.nim | 32 ++++++++++++++++++-------------- compiler/wordrecg.nim | 2 +- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 7bbc408902d2d..0fc0583a8fb10 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -1537,6 +1537,21 @@ proc genAsmStmt(p: BProc, t: PNode) = assert(t.kind == nkAsmStmt) genLineDir(p, t) var s = newRopeAppender() + + var inlineAsmSyntax = "" + if (let p = t[0]; p).kind == nkPragma: + for i in p: + if whichPragma(i) == wInlineAsmSyntax: + inlineAsmSyntax = i[1].strVal + + if inlineAsmSyntax != "" and + not ( + inlineAsmSyntax == "gcc" and hasGnuAsm in CC[p.config.cCompiler].props or + inlineAsmSyntax == "vcc" and hasGnuAsm notin CC[p.config.cCompiler].props): + localError( + p.config, t.info, + "Your compiler does not support the specified inline assembler") + genAsmOrEmitStmt(p, t, isAsmStmt=true, s) # see bug #2362, "top level asm statements" seem to be a mis-feature # but even if we don't do this, the example in #2362 cannot possibly diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 001af6ae7fec8..596d36d02dc50 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -153,20 +153,6 @@ proc pragmaEnsures(c: PContext, n: PNode) = n[1] = c.semExpr(c, n[1]) closeScope(c) -proc pragmaAsm*(c: PContext, n: PNode): char = - result = '\0' - if n != nil: - for i in 0.. Date: Sat, 23 Dec 2023 00:49:18 +0300 Subject: [PATCH 2/7] doc --- doc/manual_experimental.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index 765a69a0fab42..1a99b33e33000 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -2595,3 +2595,20 @@ method foo(x: Base) {.base.} = discard ``` It gives an error: method `foo` can be defined only in the same module with its type (Base). + + +inlineAsmSyntax pragma +============= + +`inlineAsmSyntax` pragma allowing specify target inline assembler syntax in `asm` stmt. + +It prevents compiling code with different of the target CC inline asm syntax, i.e. it will not allow gcc inline asm code to be compiled with vcc. + +```nim +proc nothing() = + asm {.inlineAsmSyntax: "gcc".}""" + nop + """ +``` + +The current C(C++) backend implementation cannot generate code for gcc and for vcc at the same time. For example, `{.inlineAsmSyntax: "vcc".}` with the ICC compiler will not generate code with intel asm syntax, even though ICC can use both gcc-like asm and vcc-like. From e1980116c5659f5d8580043295b692349de74dc5 Mon Sep 17 00:00:00 2001 From: ASVIEST <71895914+ASVIEST@users.noreply.github.com> Date: Sat, 23 Dec 2023 10:43:53 +0300 Subject: [PATCH 3/7] fix doc --- doc/manual_experimental.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index 1a99b33e33000..dbe432a38cf45 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -2598,7 +2598,7 @@ It gives an error: method `foo` can be defined only in the same module with its inlineAsmSyntax pragma -============= +====================== `inlineAsmSyntax` pragma allowing specify target inline assembler syntax in `asm` stmt. From 6c5c3c9c3aad6f3a5db3e3691d91cd51a30cc7d3 Mon Sep 17 00:00:00 2001 From: ASVIEST <71895914+ASVIEST@users.noreply.github.com> Date: Sun, 24 Dec 2023 17:36:41 +0300 Subject: [PATCH 4/7] Update compiler/ccgstmts.nim Co-authored-by: Andreas Rumpf --- compiler/ccgstmts.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 0fc0583a8fb10..b9d6e18f0215e 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -1539,7 +1539,7 @@ proc genAsmStmt(p: BProc, t: PNode) = var s = newRopeAppender() var inlineAsmSyntax = "" - if (let p = t[0]; p).kind == nkPragma: + if (let p = t[0]; p.kind == nkPragma): for i in p: if whichPragma(i) == wInlineAsmSyntax: inlineAsmSyntax = i[1].strVal From eec8eddb14f3eb5dacf872485026cab514052922 Mon Sep 17 00:00:00 2001 From: ASVIEST Date: Sun, 24 Dec 2023 17:46:46 +0300 Subject: [PATCH 5/7] rename inlineAsmSyntax to asmSyntax --- compiler/ccgstmts.nim | 12 ++++++------ compiler/pragmas.nim | 2 +- compiler/wordrecg.nim | 2 +- doc/manual_experimental.md | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index b9d6e18f0215e..28c0851aa6b0e 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -1538,16 +1538,16 @@ proc genAsmStmt(p: BProc, t: PNode) = genLineDir(p, t) var s = newRopeAppender() - var inlineAsmSyntax = "" + var asmSyntax = "" if (let p = t[0]; p.kind == nkPragma): for i in p: - if whichPragma(i) == wInlineAsmSyntax: - inlineAsmSyntax = i[1].strVal + if whichPragma(i) == wAsmSyntax: + asmSyntax = i[1].strVal - if inlineAsmSyntax != "" and + if asmSyntax != "" and not ( - inlineAsmSyntax == "gcc" and hasGnuAsm in CC[p.config.cCompiler].props or - inlineAsmSyntax == "vcc" and hasGnuAsm notin CC[p.config.cCompiler].props): + asmSyntax == "gcc" and hasGnuAsm in CC[p.config.cCompiler].props or + asmSyntax == "vcc" and hasGnuAsm notin CC[p.config.cCompiler].props): localError( p.config, t.info, "Your compiler does not support the specified inline assembler") diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 596d36d02dc50..a4c5397992794 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -304,7 +304,7 @@ proc pragmaAsm*(c: PContext, n: PNode): char = of wSubsChar: if it[1].kind == nkCharLit: result = chr(int(it[1].intVal)) else: invalidPragma(c, it) - of wInlineAsmSyntax: + of wAsmSyntax: let s = expectStrLit(c, it) if s notin ["gcc", "vcc"]: invalidPragma(c, it) else: invalidPragma(c, it) diff --git a/compiler/wordrecg.nim b/compiler/wordrecg.nim index 2804df0dd59e8..39e0b2e25a44a 100644 --- a/compiler/wordrecg.nim +++ b/compiler/wordrecg.nim @@ -84,7 +84,7 @@ type wComputedGoto = "computedGoto", wExperimental = "experimental", wDoctype = "doctype", wWrite = "write", wGensym = "gensym", wInject = "inject", wDirty = "dirty", wInheritable = "inheritable", wThreadVar = "threadvar", wEmit = "emit", - wAsmNoStackFrame = "asmNoStackFrame", wInlineAsmSyntax = "inlineAsmSyntax", wImplicitStatic = "implicitStatic", + wAsmNoStackFrame = "asmNoStackFrame", wAsmSyntax = "asmSyntax", wImplicitStatic = "implicitStatic", wGlobal = "global", wCodegenDecl = "codegenDecl", wUnchecked = "unchecked", wGuard = "guard", wLocks = "locks", wPartial = "partial", wExplain = "explain", wLiftLocals = "liftlocals", wEnforceNoRaises = "enforceNoRaises", wSystemRaisesDefect = "systemRaisesDefect", diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index dbe432a38cf45..fe36a78d15356 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -2597,18 +2597,18 @@ method foo(x: Base) {.base.} = discard It gives an error: method `foo` can be defined only in the same module with its type (Base). -inlineAsmSyntax pragma +asmSyntax pragma ====================== -`inlineAsmSyntax` pragma allowing specify target inline assembler syntax in `asm` stmt. +`asmSyntax` pragma allowing specify target inline assembler syntax in `asm` stmt. It prevents compiling code with different of the target CC inline asm syntax, i.e. it will not allow gcc inline asm code to be compiled with vcc. ```nim proc nothing() = - asm {.inlineAsmSyntax: "gcc".}""" + asm {.asmSyntax: "gcc".}""" nop """ ``` -The current C(C++) backend implementation cannot generate code for gcc and for vcc at the same time. For example, `{.inlineAsmSyntax: "vcc".}` with the ICC compiler will not generate code with intel asm syntax, even though ICC can use both gcc-like asm and vcc-like. +The current C(C++) backend implementation cannot generate code for gcc and for vcc at the same time. For example, `{.asmSyntax: "vcc".}` with the ICC compiler will not generate code with intel asm syntax, even though ICC can use both gcc-like asm and vcc-like. From 9ad95e900394c4595eb966994d20d709b58b393e Mon Sep 17 00:00:00 2001 From: ASVIEST Date: Sun, 24 Dec 2023 17:51:12 +0300 Subject: [PATCH 6/7] fix doc --- doc/manual_experimental.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index fe36a78d15356..335436cfca9f1 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -2598,7 +2598,7 @@ It gives an error: method `foo` can be defined only in the same module with its asmSyntax pragma -====================== +================ `asmSyntax` pragma allowing specify target inline assembler syntax in `asm` stmt. From 11cfc8b220720228e43ff2bb5ea7c66f75f16a3e Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 25 Dec 2023 07:12:44 +0100 Subject: [PATCH 7/7] Apply suggestions from code review --- doc/manual_experimental.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index 335436cfca9f1..9b52fbd2a4702 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -2600,7 +2600,7 @@ It gives an error: method `foo` can be defined only in the same module with its asmSyntax pragma ================ -`asmSyntax` pragma allowing specify target inline assembler syntax in `asm` stmt. +The `asmSyntax` pragma is used to specify target inline assembler syntax in an `asm` statement. It prevents compiling code with different of the target CC inline asm syntax, i.e. it will not allow gcc inline asm code to be compiled with vcc. @@ -2611,4 +2611,4 @@ proc nothing() = """ ``` -The current C(C++) backend implementation cannot generate code for gcc and for vcc at the same time. For example, `{.asmSyntax: "vcc".}` with the ICC compiler will not generate code with intel asm syntax, even though ICC can use both gcc-like asm and vcc-like. +The current C(C++) backend implementation cannot generate code for gcc and for vcc at the same time. For example, `{.asmSyntax: "vcc".}` with the ICC compiler will not generate code with intel asm syntax, even though ICC can use both gcc-like and vcc-like asm.