1- import { CloneType , getType , maybeStringType , maybeValueType , String2Type } from "../types/c0type_utility" ;
21import * as Arithmetic from "../utility/arithmetic" ;
2+ import * as TypeUtil from "../types/c0type_utility" ;
3+
34import { build_c0_ptrValue , build_c0_value , js_cvt2_c0_value , is_same_value , build_c0_stringValue } from "../utility/c0_value" ;
45import { c0_memory_error , c0_user_error , vm_error } from "../utility/errors" ;
56import { read_ptr , shift_ptr } from "../utility/pointer_ops" ;
67import { loadString } from "../utility/string_utility" ;
78import { safe_pop_stack } from "./helpers" ;
89
10+ /**
11+ * Main function of C0VM.ts, perform transformation on VM_State and allocator
12+ * @param state Current state of virtual machine
13+ * @param allocator The heap memory allocator of current VM
14+ * @returns `true` if the VM is still able to "step forward"
15+ * @returns `false` if this step is the last step of current program
16+ */
917export function step ( state : VM_State , allocator : C0HeapAllocator ) : boolean {
1018 const F = state . CurrFrame . P ; // the function that is currently running on
1119 if ( globalThis . DEBUG_DUMP_STEP ) {
@@ -21,11 +29,11 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
2129 const v = safe_pop_stack ( state . CurrFrame . S ) ;
2230 const nv = {
2331 value : new DataView ( v . value . buffer . slice ( v . value . byteOffset , v . value . byteLength ) ) ,
24- type : CloneType ( v . type )
32+ type : TypeUtil . CloneType ( v . type )
2533 } ;
2634
2735 state . CurrFrame . S . push ( v ) ;
28- state . CurrFrame . S . push ( nv as C0Value < C0TypeClass > ) ;
36+ state . CurrFrame . S . push ( nv ) ;
2937 break ;
3038 }
3139
@@ -306,7 +314,7 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
306314 state . CurrFrame . PC += 1 ;
307315
308316 const str_ptr = safe_pop_stack ( state . CurrFrame . S ) ;
309- if ( ! maybeStringType ( str_ptr ) ) {
317+ if ( ! TypeUtil . maybeStringType ( str_ptr ) ) {
310318 throw new vm_error ( `Type unmatch: expected a pointer in C0Value, received a ${ str_ptr . type . type } ` ) ;
311319 }
312320 throw new c0_user_error ( loadString ( str_ptr , allocator ) ) ;
@@ -317,11 +325,11 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
317325 state . CurrFrame . PC += 1 ;
318326
319327 const str_ptr = safe_pop_stack ( state . CurrFrame . S ) ;
320- if ( ! maybeStringType ( str_ptr ) ) {
328+ if ( ! TypeUtil . maybeStringType ( str_ptr ) ) {
321329 throw new vm_error ( `Type unmatch: expected a string in C0Value, received a ${ str_ptr . type . type } ` ) ;
322330 }
323331 const val = safe_pop_stack ( state . CurrFrame . S ) ;
324- if ( ! maybeValueType ( val ) ) {
332+ if ( ! TypeUtil . maybeValueType ( val ) ) {
325333 throw new vm_error ( `Type unmatch: expected a value in C0Value, received a ${ str_ptr . type . type } ` ) ;
326334 }
327335 if ( val . value . getUint32 ( 0 ) === 0 ) {
@@ -489,7 +497,7 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
489497 state . CurrFrame . PC += 2 ;
490498
491499 const ptr = allocator . malloc ( s ) ;
492- const T = String2Type ( F . comment . get ( state . CurrFrame . PC - 2 ) . dataType ) ;
500+ const T = TypeUtil . String2Type ( F . comment . get ( state . CurrFrame . PC - 2 ) . dataType ) ;
493501 if ( T . type === C0TypeClass . ptr && T . kind === "struct" ) {
494502 state . CurrFrame . S . push (
495503 { value : ptr , type : T }
@@ -536,19 +544,18 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
536544 state . CurrFrame . PC += 1 ;
537545
538546 const a = safe_pop_stack ( state . CurrFrame . S ) ;
539- if ( a . type . type !== C0TypeClass . ptr && a . type . type !== C0TypeClass . unknown ) {
547+ if ( ! TypeUtil . maybePointerType ( a ) ) {
540548 throw new vm_error ( "Type unmatch, AMLOAD expect to receive a pointer" ) ;
541549 }
542550 const mem_block = allocator . amload ( a . value ) ;
543551 if ( a . type . type === C0TypeClass . ptr ) {
544552 if ( a . type . kind === "struct" ) {
545553 state . CurrFrame . S . push (
546- // @ts -ignore
547- { value : mem_block , type : ( getType ( a . type ) as C0Type < C0TypeClass > ) }
554+ { value : mem_block , type : ( TypeUtil . getType ( a . type ) ) }
548555 )
549556 } else {
550557 state . CurrFrame . S . push (
551- { value : mem_block , type : ( a . type . value as C0Type < C0TypeClass . ptr > ) }
558+ { value : mem_block , type : ( a . type . value ) }
552559 ) ;
553560 }
554561 } else {
@@ -583,7 +590,7 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
583590 state . CurrFrame . PC += 1 ;
584591
585592 const a = safe_pop_stack ( state . CurrFrame . S ) ;
586- if ( a . type . type !== C0TypeClass . ptr && a . type . type !== C0TypeClass . unknown ) {
593+ if ( ! TypeUtil . maybePointerType ( a ) ) {
587594 throw new vm_error ( "Type unmatch, CMLOAD expect to receive a pointer" ) ;
588595 }
589596 const mem_block = allocator . cmload ( a . value ) ;
@@ -620,13 +627,13 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
620627 state . CurrFrame . PC += 2 ;
621628
622629 const a = safe_pop_stack ( state . CurrFrame . S ) ;
623- if ( a . type . type !== C0TypeClass . ptr && a . type . type !== C0TypeClass . unknown ) {
630+ if ( ! TypeUtil . maybePointerType ( a ) ) {
624631 throw new vm_error ( "Type unmatch, AADDF expect to receive a pointer" ) ;
625632 }
626633 const off_ptr = shift_ptr ( a . value , f ) ;
627634
628635 if ( a . type . type === C0TypeClass . ptr ) {
629- const new_type = ( CloneType ( a . type ) as C0Type < C0TypeClass . ptr > ) ;
636+ const new_type = ( TypeUtil . CloneType ( a . type ) as C0Type < C0TypeClass . ptr > ) ;
630637 if ( new_type . kind !== "struct" ) throw new vm_error ( "AADDF should only be applied on a struct pointer" ) ;
631638 new_type . offset += f ;
632639 state . CurrFrame . S . push (
@@ -657,8 +664,16 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
657664
658665 allocator . deref ( ptr ) . setUint32 ( 0 , f ) ;
659666 state . CurrFrame . S . push (
660- build_c0_ptrValue ( ptr , "arr" , String2Type ( F . comment . get ( state . CurrFrame . PC - 2 ) . dataType ) )
667+ {
668+ value : ptr ,
669+ type : { type : C0TypeClass . ptr , kind : "arr" , value : TypeUtil . String2Type ( F . comment . get ( state . CurrFrame . PC - 2 ) . dataType ) }
670+ }
661671 ) ;
672+
673+ console . log ( "NEWARRAY" ) ;
674+ console . log ( F . comment . get ( state . CurrFrame . PC - 2 ) . dataType ) ;
675+ console . log ( TypeUtil . String2Type ( F . comment . get ( state . CurrFrame . PC - 2 ) . dataType ) ) ;
676+
662677 break ;
663678 }
664679
@@ -684,8 +699,7 @@ export function step(state: VM_State, allocator: C0HeapAllocator): boolean {
684699
685700 const i = safe_pop_stack ( state . CurrFrame . S ) ;
686701 const a = safe_pop_stack ( state . CurrFrame . S ) ;
687- if ( ( i . type . type !== C0TypeClass . value && i . type . type !== C0TypeClass . unknown )
688- || a . type . type !== C0TypeClass . ptr ) {
702+ if ( ! TypeUtil . maybeValueType ( i ) || a . type . type !== C0TypeClass . ptr ) {
689703 throw new vm_error ( "Type unmatch, expected to have {ptr, value}" ) ;
690704 }
691705
0 commit comments