@@ -433,40 +433,25 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
433433 if pcIdx < 0 or pcIdx >= co.opcodes.len: break
434434 let oc = co.opcodes[pcIdx]
435435
436- # additional debug output for stack operations
436+ # single debug output for opcode/ stack at top of loop
437437 when defined (hayaVmWriteStackOps):
438438 display (span (" exec_opcode:" , fgMagenta),
439439 span ($ oc), span (" chunk=" & $ currentChunk & " pcIdx=" & $ pcIdx & " stack=" & $ stack))
440440 case oc
441441 # Constants / simple pushes
442442 of opcPushNil:
443- # arg1 = object id (if you used it); produce a nil object placeholder
444443 stack.push (initObject (co.getArg1Int (pcIdx).uint16 , nilObject))
445-
446- when defined (hayaVmWriteStackOps):
447- echo " PushNil: id=" , co.getArg1Int (pcIdx)
448444 of opcPushI, opcPushF, opcPushS:
449445 co.pushConst (pcIdx, currentChunk, stack)
450446 of opcPushTrue:
451447 stack.push (initValue (true ))
452- when defined (hayaVmWriteStackOps):
453- echo " PushTrue"
454448 of opcPushFalse:
455449 stack.push (initValue (false ))
456- when defined (hayaVmWriteStackOps):
457- echo " PushFalse"
458450 of opcPushPointer:
459- discard # pointer literal already embedded (ignored here)
460-
461- when defined (hayaVmWriteStackOps):
462- echo " PushPointer"
451+ discard
463452 of opcPopPointer:
464453 if stack.len > 0 :
465454 discard stack.pop ()
466-
467-
468- when defined (hayaVmWriteStackOps):
469- echo " PopPointer"
470455 of opcFFIGetProc:
471456 # FFI dynamic call: arg1= symbol string index, arg2 = argc
472457 let symbolName = co.getArg1Str (pcIdx, currentChunk)
@@ -545,55 +530,30 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
545530 finally :
546531 dealloc (paramsMem)
547532 dealloc (argsMem)
548-
549533 # Variables
550534 of opcPushG:
551535 stack.push (vm.globals[co.getArg1Str (pcIdx, currentChunk)])
552-
553- when defined (hayaVmWriteStackOps):
554- echo " PushG: " , co.getArg1Str (pcIdx, currentChunk), " value=" , stack[^ 1 ]
555536 of opcPopG:
556537 let val = stack.pop ()
557538 vm.globals[co.getArg1Str (pcIdx, currentChunk)] = val
558-
559- when defined (hayaVmWriteStackOps):
560- echo " PopG: " , co.getArg1Str (pcIdx, currentChunk), " value=" , val
561539 of opcPushL:
562540 let idx = co.getArg1Int (pcIdx)
563541 ensureLocal (idx)
564542 stack.push (stack[stackBottom + idx])
565-
566- when defined (hayaVmWriteStackOps):
567- echo " PushL: idx=" , idx, " value=" , stack[^ 1 ]
568543 of opcPopL:
569544 let idx = co.getArg1Int (pcIdx)
570545 ensureLocal (idx)
571546 let val = stack.pop ()
572547 stack[stackBottom + idx] = val
573-
574- when defined (hayaVmWriteStackOps):
575- echo " PopL: idx=" , idx, " value=" , val
576548 # HTML generation
577549 of opcAttrClass:
578550 result .add (" class=\" " & co.getArg1Str (pcIdx, currentChunk) & " \" " )
579-
580- when defined (hayaVmWriteStackOps):
581- echo " AttrClass: " , co.getArg1Str (pcIdx, currentChunk)
582551 of opcAttrId:
583552 result .add (" id=\" " & co.getArg1Str (pcIdx, currentChunk) & " \" " )
584-
585- when defined (hayaVmWriteStackOps):
586- echo " AttrId: " , co.getArg1Str (pcIdx, currentChunk)
587553 of opcWSpace:
588554 result .add (" " )
589-
590- when defined (hayaVmWriteStackOps):
591- echo " WSpace"
592555 of opcAttrEnd:
593556 result .add (" >" )
594-
595- when defined (hayaVmWriteStackOps):
596- echo " AttrEnd"
597557 of opcAttr:
598558 let key = stack.pop ().stringVal[]
599559 let value = stack.pop ()
@@ -607,27 +567,15 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
607567 result .add (value.jsonVal.toString ())
608568 else : discard
609569 result .add (" \" " )
610-
611- when defined (hayaVmWriteStackOps):
612- echo " Attr: key=" , key, " value=" , value
613570 of opcAttrKey:
614571 let attr = stack.pop ()
615572 if attr.stringVal[].len > 0 :
616573 result .add (" " ) # leading space
617574 result .add (attr.stringVal[])
618-
619- when defined (hayaVmWriteStackOps):
620- echo " AttrKey: " , attr
621575 of opcBeginHtmlWithAttrs:
622576 result .add (" <" & co.getArg1Str (pcIdx, currentChunk))
623-
624- when defined (hayaVmWriteStackOps):
625- echo " BeginHtmlWithAttrs: " , co.getArg1Str (pcIdx, currentChunk)
626577 of opcBeginHtml:
627578 result .add (" <" & co.getArg1Str (pcIdx, currentChunk) & " >" )
628-
629- when defined (hayaVmWriteStackOps):
630- echo " BeginHtml: " , co.getArg1Str (pcIdx, currentChunk)
631579 of opcTextHtml:
632580 let v = stack.pop ()
633581 case v.typeId
@@ -637,20 +585,10 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
637585 of tyBool: result .add ($ (v.boolVal))
638586 of tyJsonStorage: result .add (v.jsonVal.toString ())
639587 else : discard
640-
641- when defined (hayaVmWriteStackOps):
642- echo " TextHtml: " , v
643588 of opcInnerHtml:
644589 discard
645-
646- when defined (hayaVmWriteStackOps):
647- echo " InnerHtml"
648590 of opcCloseHtml:
649591 result .add (" </" & co.getArg1Str (pcIdx, currentChunk) & " >" )
650-
651- when defined (hayaVmWriteStackOps):
652- echo " CloseHtml: " , co.getArg1Str (pcIdx, currentChunk)
653-
654592 # Arrays / Objects
655593 of opcConstrArray:
656594 let count = co.getArg1Int (pcIdx)
@@ -661,25 +599,16 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
661599 arr.objectVal.fields[i] = vals[i]
662600 stack.setLen (stack.len - count)
663601 stack.push (arr)
664-
665- when defined (hayaVmWriteStackOps):
666- echo " ConstrArray: count=" , count, " arr=" , arr
667602 of opcGetI:
668603 let idxVal = stack.pop ()
669604 let arr = stack.pop ()
670605 stack.push (arr.objectVal.fields[idxVal.intVal])
671-
672- when defined (hayaVmWriteStackOps):
673- echo " GetI: idx=" , idxVal.intVal, " value=" , stack[^ 1 ]
674606 of opcSetI:
675607 let
676608 val = stack.pop ()
677609 idxVal = stack.pop ()
678610 arr = stack.pop ()
679611 arr.objectVal.fields[idxVal.intVal] = val
680-
681- when defined (hayaVmWriteStackOps):
682- echo " SetI: idx=" , idxVal.intVal, " value=" , val
683612 of opcConstrObj:
684613 let count = co.getArg1Int (pcIdx)
685614 var obj = initObject (15 , count)
@@ -692,24 +621,16 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
692621 obj.objectVal.fields[i] = fieldValue
693622 stack.setLen (stack.len - (count * 2 ))
694623 stack.push (obj)
695- when defined (hayaVmWriteStackOps):
696- echo " ConstrObj: count=" , count, " obj=" , obj
697624 of opcGetF:
698625 let fld = co.getArg1Int (pcIdx)
699626 let obj = stack.pop ()
700627 stack.push (obj.objectVal.fields[fld])
701-
702- when defined (hayaVmWriteStackOps):
703- echo " GetF: field=" , fld, " value=" , stack[^ 1 ]
704628 of opcSetF:
705629 let
706630 fld = co.getArg1Int (pcIdx)
707631 val = stack.pop ()
708632 obj = stack.pop ()
709633 obj.objectVal.fields[fld] = val
710-
711- when defined (hayaVmWriteStackOps):
712- echo " SetF: field=" , fld, " value=" , val
713634 # JSON (placeholders)
714635 of opcGetJ:
715636 let
@@ -723,28 +644,16 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
723644 jsonValue = obj.jsonVal[key.stringVal[]]
724645 else : discard
725646 stack.push (initValue (jsonValue))
726-
727- when defined (hayaVmWriteStackOps):
728- echo " GetJ: key=" , key, " value=" , jsonValue
729647 of opcSetJ:
730648 discard # TODO
731-
732- when defined (hayaVmWriteStackOps):
733- echo " SetJ: not implemented"
734649 # Discard
735650 of opcDiscard:
736651 let n = co.getArg1Int (pcIdx)
737652 if n > 0 and stack.len >= n:
738653 stack.setLen (stack.len - n)
739-
740- when defined (hayaVmWriteStackOps):
741- echo " Discard: n=" , n, " stackLen=" , stack.len
742654 # Arithmetic
743655 of opcNegI:
744656 let a = stack.pop (); stack.push (initValue (- a.intVal))
745-
746- when defined (hayaVmWriteStackOps):
747- echo " NegI: a=" , a, " result=" , stack[^ 1 ]
748657 of opcAddI, opcSubI, opcMultI, opcDivI:
749658 let b = stack.pop () # rhs arg
750659 let a = stack.pop () # lhs arg
@@ -754,15 +663,9 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
754663 of opcMultI: stack.push (initValue (a.intVal * b.intVal))
755664 of opcDivI: stack.push (initValue (a.intVal div b.intVal))
756665 else : discard
757-
758- when defined (hayaVmWriteStackOps):
759- echo " IntArith: " , oc, " a=" , a.intVal, " b=" , b.intVal, " result=" , stack[^ 1 ]
760666 of opcNegF:
761667 let a = stack.pop ()
762668 stack.push (initValue (- a.floatVal))
763-
764- when defined (hayaVmWriteStackOps):
765- echo " NegF: a=" , a, " result=" , stack[^ 1 ]
766669 of opcAddF, opcSubF, opcMultF, opcDivF:
767670 let b = stack.pop ()
768671 let a = stack.pop ()
@@ -774,36 +677,18 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
774677 of opcMultF: stack.push (initValue (av * bv))
775678 of opcDivF: stack.push (initValue (av / bv))
776679 else : discard
777-
778- when defined (hayaVmWriteStackOps):
779- echo " FloatArith: " , oc, " a=" , av, " b=" , bv, " result=" , stack[^ 1 ]
780680 # Logic
781681 of opcInvB:
782682 let a = stack.pop (); stack.push (initValue (not a.boolVal))
783-
784- when defined (hayaVmWriteStackOps):
785- echo " InvB: a=" , a, " result=" , stack[^ 1 ]
786683 # Relational
787684 of opcEqB:
788685 let b = stack.pop (); let a = stack.pop (); stack.push (initValue (a.boolVal == b.boolVal))
789-
790- when defined (hayaVmWriteStackOps):
791- echo " EqB: a=" , a, " b=" , b, " result=" , stack[^ 1 ]
792686 of opcEqI:
793687 let b = stack.pop (); let a = stack.pop (); stack.push (initValue (a.intVal == b.intVal))
794-
795- when defined (hayaVmWriteStackOps):
796- echo " EqI: a=" , a, " b=" , b, " result=" , stack[^ 1 ]
797688 of opcLessI:
798689 let b = stack.pop (); let a = stack.pop (); stack.push (initValue (a.intVal < b.intVal))
799-
800- when defined (hayaVmWriteStackOps):
801- echo " LessI: a=" , a, " b=" , b, " result=" , stack[^ 1 ]
802690 of opcGreaterI:
803691 let b = stack.pop (); let a = stack.pop (); stack.push (initValue (a.intVal > b.intVal))
804-
805- when defined (hayaVmWriteStackOps):
806- echo " GreaterI: a=" , a, " b=" , b, " result=" , stack[^ 1 ]
807692 of opcEqF, opcLessF, opcGreaterF:
808693 let b = stack.pop ()
809694 let a = stack.pop ()
@@ -818,10 +703,6 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
818703 of opcGreaterF:
819704 stack.push (initValue (av > bv))
820705 else : discard
821-
822- when defined (hayaVmWriteStackOps):
823- echo " FloatRel: " , oc, " a=" , av, " b=" , bv, " result=" , stack[^ 1 ]
824-
825706 # Modules
826707 of opcImportModule:
827708 let chunkPath = co.getArg1Str (pcIdx, currentChunk)
@@ -831,48 +712,33 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
831712 stackBottom = stack.len
832713 inc (vm.lvl)
833714 switchTo (other.mainChunk, other)
834-
835715 when defined (hayaVmWriteStackOps):
836716 display (span (" import:" , fgGreen), span (chunkPath), span ($ currentChunk))
837-
838717 continue # jump to new chunk
839718 of opcImportModuleAlias, opcImportFromModule: discard # todo
840-
841719 #
842720 # Control Flow
843721 #
844722 of opcJumpFwd:
845723 let tgt = co.jumpTargets[pcIdx]
846724 if tgt >= 0 : pcIdx = tgt - 1
847-
848- when defined (hayaVmWriteStackOps):
849- echo " JumpFwd: tgt=" , tgt
850-
851725 of opcJumpFwdT:
852- # if stack.peek().boolVal:
853726 let cond = stack.pop ()
854727 if cond.boolVal:
855728 let tgt = co.jumpTargets[pcIdx]
856729 if tgt >= 0 : pcIdx = tgt - 1
857730 when defined (hayaVmWriteStackOps):
858731 echo " JumpFwdT: tgt=" , tgt, " cond=" , cond.boolVal
859732 of opcJumpFwdF:
860- # if not stack.peek().boolVal:
861733 let cond = stack.pop ()
862734 if not cond.boolVal:
863735 let tgt = co.jumpTargets[pcIdx]
864736 if tgt >= 0 : pcIdx = tgt - 1
865-
866-
867737 when defined (hayaVmWriteStackOps):
868- # echo "JumpFwdF: tgt=", tgt, " cond=", stack.peek().boolVal
869738 echo " JumpFwdF: tgt=" , tgt, " cond=" , cond.boolVal
870739 of opcJumpBack:
871740 let tgt = co.jumpTargets[pcIdx]
872741 if tgt >= 0 : pcIdx = tgt - 1
873-
874- when defined (hayaVmWriteStackOps):
875- echo " JumpBack: tgt=" , tgt
876742 of opcCallD:
877743 let chunkPath = co.getArg1Str (pcIdx, currentChunk)
878744 let procId = co.arg2[pcIdx].int
@@ -885,7 +751,6 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
885751 raise newException (ValueError ,
886752 " Not enough arguments on stack for call to " & p.name)
887753 stackBottom = stack.len - p.paramCount
888-
889754 when defined (hayaVmWriteStackOps):
890755 display (span (" opc:" , fgGreen), span (" <" & $ opcCallD & " >" ),
891756 span (" filePath=" & chunkPath & " procId=" & $ procId & " name=" & p.name & " paramCount=" & $ p.paramCount))
@@ -905,8 +770,6 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
905770 restoreFrame () # after foreign call
906771
907772 if p.hasResult: stack.push (callResult)
908-
909-
910773 when defined (hayaVmWriteStackOps):
911774 if callResult != nil :
912775 echo " Foreign call result: " , callResult
@@ -916,25 +779,17 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
916779 let rv = stack.pop ()
917780 restoreFrame ()
918781 stack.push (rv)
919-
920- when defined (hayaVmWriteStackOps):
921- echo " ReturnVal: rv=" , rv
922782 of opcReturnVoid:
923783 restoreFrame ()
924-
925784 when defined (hayaVmWriteStackOps):
926785 display (span (" return:" , fgGreen), span (" void" , fgCyan))
927786 of opcViewLoader:
928787 result .add (staticString.get ())
929-
930- when defined (hayaVmWriteStackOps):
931- echo " ViewLoader"
932788 of opcHalt:
933789 if stack.len > 0 :
934790 echo " Warning: stack not empty at halt, contains " , stack.len, " items."
935791 echo stack[^ 1 ].typeId
936792 stack.setLen (0 )
937-
938793 when defined (hayaVmWriteStackOps):
939794 display (span (" *** halt" , fgRed), span (" lvl=" & $ vm.lvl))
940795 if vm.lvl == 0 :
@@ -945,7 +800,4 @@ proc interpret*(vm: Vm, script: Script, startChunk: Chunk,
945800 of opcNoop: discard
946801 else :
947802 discard
948-
949- when defined (hayaVmWriteStackOps):
950- echo " Unknown opcode: " , oc
951803 inc (pcIdx)
0 commit comments