@@ -123,7 +123,8 @@ extension Sp {
123123 return UntypedValue ( storage: self [ Int ( index) ] )
124124 }
125125 nonmutating set {
126- return self [ Int ( index) ] = newValue. storage
126+ self [ Int ( index) ] = newValue. storage
127+ return
127128 }
128129 }
129130
@@ -302,7 +303,10 @@ extension Execution {
302303 handle: InternalFunction ,
303304 type: FunctionType
304305 ) throws {
305- var sp : Sp = sp, md : Md = nil , ms : Ms = 0 , pc = pc
306+ var sp : Sp = sp
307+ var md : Md = nil
308+ var ms : Ms = 0
309+ var pc = pc
306310 ( pc, sp) = try invoke (
307311 function: handle,
308312 callerInstance: nil ,
@@ -334,82 +338,82 @@ extension Execution {
334338 }
335339 }
336340
337- #if EngineStats
338- /// A helper structure for collecting instruction statistics.
339- /// - Note: This is used only when the `EngineStats` flag is enabled.
340- struct StatsCollector {
341- struct Trigram : Hashable {
342- var a : UInt64
343- var b : UInt64
344- var c : UInt64
345- }
341+ #if EngineStats
342+ /// A helper structure for collecting instruction statistics.
343+ /// - Note: This is used only when the `EngineStats` flag is enabled.
344+ struct StatsCollector {
345+ struct Trigram : Hashable {
346+ var a : UInt64
347+ var b : UInt64
348+ var c : UInt64
349+ }
346350
347- struct CircularBuffer < T> {
348- private var buffer : [ T ? ]
349- private var index : Int = 0
351+ struct CircularBuffer < T> {
352+ private var buffer : [ T ? ]
353+ private var index : Int = 0
350354
351- init ( capacity: Int ) {
352- buffer = Array ( repeating: nil , count: capacity)
353- }
355+ init ( capacity: Int ) {
356+ buffer = Array ( repeating: nil , count: capacity)
357+ }
354358
355- /// Accesses the element at the specified position counted from the oldest element.
356- subscript( _ index: Int ) -> T ? {
357- get {
358- return buffer [ ( self . index + index) % buffer. count]
359+ /// Accesses the element at the specified position counted from the oldest element.
360+ subscript( _ index: Int ) -> T ? {
361+ get {
362+ return buffer [ ( self . index + index) % buffer. count]
363+ }
364+ set {
365+ buffer [ ( self . index + index) % buffer. count] = newValue
366+ }
359367 }
360- set {
361- buffer [ ( self . index + index) % buffer. count] = newValue
368+
369+ mutating func append( _ value: T ) {
370+ buffer [ index] = value
371+ index = ( index + 1 ) % buffer. count
362372 }
363373 }
364374
365- mutating func append( _ value: T ) {
366- buffer [ index] = value
367- index = ( index + 1 ) % buffer. count
375+ /// A dictionary that stores the count of each trigram pattern.
376+ private var countByTrigram : [ Trigram : Int ] = [ : ]
377+ /// A circular buffer that stores the last three instructions.
378+ private var buffer = CircularBuffer < UInt64 > ( capacity: 3 )
379+
380+ /// Tracks the given instruction index. This function is called for each instruction execution.
381+ mutating func track( _ opcode: UInt64 ) {
382+ buffer. append ( opcode)
383+ if let a = buffer [ 0 ] , let b = buffer [ 1 ] , let c = buffer [ 2 ] {
384+ let trigram = Trigram ( a: a, b: b, c: c)
385+ countByTrigram [ trigram, default: 0 ] += 1
386+ }
368387 }
369- }
370388
371- /// A dictionary that stores the count of each trigram pattern.
372- private var countByTrigram : [ Trigram : Int ] = [ : ]
373- /// A circular buffer that stores the last three instructions.
374- private var buffer = CircularBuffer < UInt64 > ( capacity: 3 )
375-
376- /// Tracks the given instruction index. This function is called for each instruction execution.
377- mutating func track( _ opcode: UInt64 ) {
378- buffer. append ( opcode)
379- if let a = buffer [ 0 ] , let b = buffer [ 1 ] , let c = buffer [ 2 ] {
380- let trigram = Trigram ( a: a, b: b, c: c)
381- countByTrigram [ trigram, default: 0 ] += 1
389+ func dump< TargetStream: TextOutputStream > ( target: inout TargetStream , limit: Int ) {
390+ print ( " Instruction statistics: " , to: & target)
391+ for (trigram, count) in countByTrigram. sorted ( by: { $0. value > $1. value } ) . prefix ( limit) {
392+ print ( " \( Instruction . name ( opcode: trigram. a) ) -> \( Instruction . name ( opcode: trigram. b) ) -> \( Instruction . name ( opcode: trigram. c) ) = \( count) " , to: & target)
393+ }
382394 }
383- }
384395
385- func dump < TargetStream : TextOutputStream > ( target : inout TargetStream , limit : Int ) {
386- print ( " Instruction statistics: " , to : & target )
387- for (trigram , count ) in countByTrigram . sorted ( by : { $0 . value > $1 . value } ) . prefix ( limit ) {
388- print ( " \( Instruction . name ( opcode : trigram . a ) ) -> \( Instruction . name ( opcode : trigram . b ) ) -> \( Instruction . name ( opcode : trigram . c ) ) = \( count ) " , to : & target )
396+ /// Dumps the instruction statistics to the standard error output stream.
397+ func dump ( limit : Int = 10 ) {
398+ var target = _Stderr ( )
399+ dump ( target : & target , limit : limit )
389400 }
390401 }
391-
392- /// Dumps the instruction statistics to the standard error output stream.
393- func dump( limit: Int = 10 ) {
394- var target = _Stderr ( )
395- dump ( target: & target, limit: limit)
396- }
397- }
398- #endif
402+ #endif
399403
400404 /// Starts the main execution loop using the token threading model.
401405 /// Be careful when modifying this function as it is performance-critical.
402406 @inline ( __always)
403407 mutating func runTokenThreaded( sp: inout Sp , pc: inout Pc , md: inout Md , ms: inout Ms ) throws {
404- #if EngineStats
405- var stats = StatsCollector ( )
406- defer { stats. dump ( ) }
407- #endif
408+ #if EngineStats
409+ var stats = StatsCollector ( )
410+ defer { stats. dump ( ) }
411+ #endif
408412 var opcode = pc. read ( OpcodeID . self)
409413 while true {
410- #if EngineStats
411- stats. track ( inst)
412- #endif
414+ #if EngineStats
415+ stats. track ( inst)
416+ #endif
413417 opcode = try doExecute ( opcode, sp: & sp, pc: & pc, md: & md, ms: & ms)
414418 }
415419 }
0 commit comments