This commit is contained in:
Andreas Rumpf
2017-10-16 16:46:38 +02:00
parent 309838c6ed
commit 7eeef4aae7
2 changed files with 28 additions and 12 deletions

View File

@@ -965,23 +965,27 @@ proc genEcho(p: BProc, n: PNode) =
# this unusal way of implementing it ensures that e.g. ``echo("hallo", 45)``
# is threadsafe.
internalAssert n.kind == nkBracket
var args: Rope = nil
var a: TLoc
for i in countup(0, n.len-1):
if n.sons[i].skipConv.kind == nkNilLit:
add(args, ", \"nil\"")
else:
initLocExpr(p, n.sons[i], a)
addf(args, ", $1? ($1)->data:\"nil\"", [rdLoc(a)])
if platform.targetOS == osGenode:
# bypass libc and print directly to the Genode LOG session
var args: Rope = nil
var a: TLoc
for i in countup(0, n.len-1):
if n.sons[i].skipConv.kind == nkNilLit:
add(args, ", \"nil\"")
else:
initLocExpr(p, n.sons[i], a)
addf(args, ", $1? ($1)->data:\"nil\"", [rdLoc(a)])
p.module.includeHeader("<base/log.h>")
linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args)
else:
p.module.includeHeader("<stdio.h>")
linefmt(p, cpsStmts, "printf($1$2);$n",
makeCString(repeat("%s", n.len) & tnl), args)
linefmt(p, cpsStmts, "fflush(stdout);$n")
var a: TLoc
initLocExpr(p, n, a)
linefmt(p, cpsStmts, "#echoBinSafe($1, $2);$n", a.rdLoc, n.len.rope)
when false:
p.module.includeHeader("<stdio.h>")
linefmt(p, cpsStmts, "printf($1$2);$n",
makeCString(repeat("%s", n.len) & tnl), args)
linefmt(p, cpsStmts, "fflush(stdout);$n")
proc gcUsage(n: PNode) =
if gSelectedGC == gcNone: message(n.info, warnGcMem, n.renderTree)

View File

@@ -404,4 +404,16 @@ proc setStdIoUnbuffered() =
when declared(stdin):
discard c_setvbuf(stdin, nil, IONBF, 0)
when declared(stdout):
proc echoBinSafe(args: openArray[string]) {.compilerProc.} =
proc flockfile(f: File) {.importc, noDecl.}
proc funlockfile(f: File) {.importc, noDecl.}
flockfile(stdout)
for s in args:
discard c_fwrite(s.cstring, s.len, 1, stdout)
const linefeed = "\n" # can be 1 or more chars
discard c_fwrite(linefeed.cstring, linefeed.len, 1, stdout)
discard c_fflush(stdout)
funlockfile(stdout)
{.pop.}