Skip to content

Commit

Permalink
runtime: add runtime.nanotime
Browse files Browse the repository at this point in the history
This function returns the current timestamp, or 0 at compile time.

runtime.nanotime is used at package initialization by the time package
starting with Go 1.12.
  • Loading branch information
aykevl authored and deadprogram committed Mar 23, 2019
1 parent 9c41011 commit 4d82f42
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions interp/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ func (fr *frame) evalBasicBlock(bb, incoming llvm.BasicBlock, indent string) (re
case callee.Name() == "runtime.makeInterface":
uintptrType := callee.Type().Context().IntType(fr.TargetData.PointerSize() * 8)
fr.locals[inst] = &LocalValue{fr.Eval, llvm.ConstPtrToInt(inst.Operand(0), uintptrType)}
case callee.Name() == "runtime.nanotime":
fr.locals[inst] = &LocalValue{fr.Eval, llvm.ConstInt(fr.Mod.Context().Int64Type(), 0, false)}
case strings.HasPrefix(callee.Name(), "runtime.print") || callee.Name() == "runtime._panic":
// This are all print instructions, which necessarily have side
// effects but no results.
Expand Down
3 changes: 3 additions & 0 deletions interp/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (e *Eval) hasSideEffects(fn llvm.Value) *sideEffectResult {
case "runtime.alloc":
// Cannot be scanned but can be interpreted.
return &sideEffectResult{severity: sideEffectNone}
case "runtime.nanotime":
// Fixed value at compile time.
return &sideEffectResult{severity: sideEffectNone}
case "runtime._panic":
return &sideEffectResult{severity: sideEffectLimited}
}
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ func sleep(d int64) {
sleepTicks(timeUnit(d / tickMicros))
}

func nanotime() int64 {
return int64(ticks()) * tickMicros
}

//go:linkname now time.now
func now() (sec int64, nsec int32, mono int64) {
mono = int64(ticks()) * tickMicros
mono = nanotime()
sec = mono / (1000 * 1000 * 1000)
nsec = int32(mono - sec*(1000*1000*1000))
return
Expand Down

0 comments on commit 4d82f42

Please sign in to comment.