Skip to content

Commit 98f463b

Browse files
✨ Update Debug Console, the type inference for struct type is buggy (can't obtain the typedef information from bc0)!
1 parent a817b07 commit 98f463b

19 files changed

+46724
-65
lines changed

public/index.js

Lines changed: 46422 additions & 2 deletions
Large diffs are not rendered by default.

src/exec/exec.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { CloneType, getType, maybeStringType, maybeValueType, String2Type } from "../types/c0type_utility";
21
import * as Arithmetic from "../utility/arithmetic";
2+
import * as TypeUtil from "../types/c0type_utility";
3+
34
import { build_c0_ptrValue, build_c0_value, js_cvt2_c0_value, is_same_value, build_c0_stringValue } from "../utility/c0_value";
45
import { c0_memory_error, c0_user_error, vm_error } from "../utility/errors";
56
import { read_ptr, shift_ptr } from "../utility/pointer_ops";
67
import { loadString } from "../utility/string_utility";
78
import { 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+
*/
917
export 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

src/exec/state.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ import { createHeap, VM_Memory } from "../utility/memory";
33
import parse from "../parser/parse";
44
import { loadStringPool } from "../utility/string_utility";
55

6+
/**
7+
* The C0 Virtual Machine Runtime with interface of operation
8+
*/
69
export default class C0VM_RuntimeState implements C0VM_RT{
710
public code: C0ByteCode;
811
public state: VM_State;
912
public allocator: C0HeapAllocator;
1013

14+
/**
15+
* Creating a new C0VM Runtime
16+
* @param rawByteCode The bc0 bytecode string
17+
* @param heapSize Heap size (in bytes), optional, if not explicitly designated, then use the
18+
* GlobalThis.MEM_POOL_DEFAULT_SIZE as the size.
19+
*/
1120
constructor(rawByteCode: string, heapSize?: number) {
1221
this.code = parse(rawByteCode);
1322
this.allocator = createHeap(VM_Memory, heapSize);
@@ -29,10 +38,19 @@ export default class C0VM_RuntimeState implements C0VM_RT{
2938
};
3039
}
3140

41+
/**
42+
* Step forward for the C0VM Runtime State.
43+
* @returns If the runtime state is able to perform next "step forward"
44+
*/
3245
public step_forward(): boolean {
3346
return step(this.state, this.allocator);
3447
}
3548

49+
/**
50+
* Re-initialize the state **without passing in bytecode file**
51+
* The parsed bytecode is stored in the .code property of runtime and is
52+
* loaded again when this function is called.
53+
*/
3654
public restart(): void {
3755
this.allocator.clear();
3856
const str_ptr = loadStringPool(this.code.stringPool, this.allocator);
@@ -53,7 +71,11 @@ export default class C0VM_RuntimeState implements C0VM_RT{
5371
};
5472
}
5573

74+
/**
75+
* A "peek hole" to help debug the VM
76+
* @returns The memory dump when DEBUG is activated
77+
*/
5678
public debug(): any {
57-
return this.allocator.debug_getMemPool();
79+
if (globalThis.DEBUG) return this.allocator.debug_getMemPool();
5880
}
5981
}

src/gui/code_editor_setup.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import LoadDocumentPlugin from "./extensions/loader_ui";
1313
import { onViewUpdate } from "./extensions/on_view_update";
1414
import { EditorState } from "@codemirror/state";
1515

16+
/**
17+
* Initialize an editor state with given content
18+
* @param s The string the editor will show in content area
19+
* @returns Editor State
20+
*/
1621
export function new_editor_state(s: string): EditorState {
1722
return EditorState.create({
1823
extensions: [
@@ -35,11 +40,13 @@ export function new_editor_state(s: string): EditorState {
3540

3641
// Auto-detect language
3742
// https://codemirror.net/examples/config/
43+
/**
44+
* Initialize the editor
45+
*/
3846
export function editor_init() {
3947
globalThis.EDITOR_VIEW = new EditorView({
4048
parent: document.getElementById(globalThis.UI_INPUT_ID),
4149
state: new_editor_state("")
4250
});
4351
console.log(`[C0VM.ts] C0 Editor Initialized.`);
4452
}
45-

src/gui/console_emitter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* A message emitter that emits message to console. Used for test & debug.
3+
* Or used in "node" ENVIRN_MODE
34
*/
45
export default class ConsoleEmitter implements MessageEmitter {
56
err(msg: string, detail?: string): void {

0 commit comments

Comments
 (0)