This commit is contained in:
Andreas Rumpf 2017-10-16 16:46:38 +02:00
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)`` # this unusal way of implementing it ensures that e.g. ``echo("hallo", 45)``
# is threadsafe. # is threadsafe.
internalAssert n.kind == nkBracket 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: if platform.targetOS == osGenode:
# bypass libc and print directly to the Genode LOG session # 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>") p.module.includeHeader("<base/log.h>")
linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args) linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args)
else: else:
p.module.includeHeader("<stdio.h>") var a: TLoc
linefmt(p, cpsStmts, "printf($1$2);$n", initLocExpr(p, n, a)
makeCString(repeat("%s", n.len) & tnl), args) linefmt(p, cpsStmts, "#echoBinSafe($1, $2);$n", a.rdLoc, n.len.rope)
linefmt(p, cpsStmts, "fflush(stdout);$n") 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) = proc gcUsage(n: PNode) =
if gSelectedGC == gcNone: message(n.info, warnGcMem, n.renderTree) if gSelectedGC == gcNone: message(n.info, warnGcMem, n.renderTree)

View file

@ -404,4 +404,16 @@ proc setStdIoUnbuffered() =
when declared(stdin): when declared(stdin):
discard c_setvbuf(stdin, nil, IONBF, 0) 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.} {.pop.}