@aptre/v86 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/LICENSE +22 -0
  2. package/LICENSE.MIT +22 -0
  3. package/Readme.md +237 -0
  4. package/dist/v86.browser.js +26666 -0
  5. package/dist/v86.browser.js.map +7 -0
  6. package/dist/v86.js +26632 -0
  7. package/dist/v86.js.map +7 -0
  8. package/gen/generate_analyzer.ts +512 -0
  9. package/gen/generate_interpreter.ts +522 -0
  10. package/gen/generate_jit.ts +624 -0
  11. package/gen/rust_ast.ts +107 -0
  12. package/gen/util.ts +35 -0
  13. package/gen/x86_table.ts +1836 -0
  14. package/lib/9p.ts +1547 -0
  15. package/lib/filesystem.ts +1879 -0
  16. package/lib/marshall.ts +168 -0
  17. package/lib/softfloat/softfloat.c +32501 -0
  18. package/lib/zstd/zstddeclib.c +13520 -0
  19. package/package.json +75 -0
  20. package/src/acpi.ts +267 -0
  21. package/src/browser/dummy_screen.ts +106 -0
  22. package/src/browser/fake_network.ts +1771 -0
  23. package/src/browser/fetch_network.ts +361 -0
  24. package/src/browser/filestorage.ts +124 -0
  25. package/src/browser/inbrowser_network.ts +57 -0
  26. package/src/browser/keyboard.ts +564 -0
  27. package/src/browser/main.ts +3415 -0
  28. package/src/browser/mouse.ts +255 -0
  29. package/src/browser/network.ts +142 -0
  30. package/src/browser/print_stats.ts +336 -0
  31. package/src/browser/screen.ts +978 -0
  32. package/src/browser/serial.ts +316 -0
  33. package/src/browser/speaker.ts +1223 -0
  34. package/src/browser/starter.ts +1688 -0
  35. package/src/browser/wisp_network.ts +332 -0
  36. package/src/browser/worker_bus.ts +64 -0
  37. package/src/buffer.ts +652 -0
  38. package/src/bus.ts +78 -0
  39. package/src/const.ts +128 -0
  40. package/src/cpu.ts +2891 -0
  41. package/src/dma.ts +474 -0
  42. package/src/elf.ts +251 -0
  43. package/src/floppy.ts +1778 -0
  44. package/src/ide.ts +3455 -0
  45. package/src/io.ts +504 -0
  46. package/src/iso9660.ts +317 -0
  47. package/src/kernel.ts +250 -0
  48. package/src/lib.ts +645 -0
  49. package/src/log.ts +149 -0
  50. package/src/main.ts +199 -0
  51. package/src/ne2k.ts +1589 -0
  52. package/src/pci.ts +815 -0
  53. package/src/pit.ts +406 -0
  54. package/src/ps2.ts +820 -0
  55. package/src/rtc.ts +537 -0
  56. package/src/rust/analysis.rs +101 -0
  57. package/src/rust/codegen.rs +2660 -0
  58. package/src/rust/config.rs +3 -0
  59. package/src/rust/control_flow.rs +425 -0
  60. package/src/rust/cpu/apic.rs +658 -0
  61. package/src/rust/cpu/arith.rs +1207 -0
  62. package/src/rust/cpu/call_indirect.rs +2 -0
  63. package/src/rust/cpu/cpu.rs +4501 -0
  64. package/src/rust/cpu/fpu.rs +923 -0
  65. package/src/rust/cpu/global_pointers.rs +112 -0
  66. package/src/rust/cpu/instructions.rs +2486 -0
  67. package/src/rust/cpu/instructions_0f.rs +5261 -0
  68. package/src/rust/cpu/ioapic.rs +316 -0
  69. package/src/rust/cpu/memory.rs +351 -0
  70. package/src/rust/cpu/misc_instr.rs +613 -0
  71. package/src/rust/cpu/mod.rs +16 -0
  72. package/src/rust/cpu/modrm.rs +133 -0
  73. package/src/rust/cpu/pic.rs +402 -0
  74. package/src/rust/cpu/sse_instr.rs +361 -0
  75. package/src/rust/cpu/string.rs +701 -0
  76. package/src/rust/cpu/vga.rs +175 -0
  77. package/src/rust/cpu_context.rs +69 -0
  78. package/src/rust/dbg.rs +98 -0
  79. package/src/rust/gen/analyzer.rs +3807 -0
  80. package/src/rust/gen/analyzer0f.rs +3992 -0
  81. package/src/rust/gen/interpreter.rs +4447 -0
  82. package/src/rust/gen/interpreter0f.rs +5404 -0
  83. package/src/rust/gen/jit.rs +5080 -0
  84. package/src/rust/gen/jit0f.rs +5547 -0
  85. package/src/rust/gen/mod.rs +14 -0
  86. package/src/rust/jit.rs +2443 -0
  87. package/src/rust/jit_instructions.rs +7881 -0
  88. package/src/rust/js_api.rs +6 -0
  89. package/src/rust/leb.rs +46 -0
  90. package/src/rust/lib.rs +29 -0
  91. package/src/rust/modrm.rs +330 -0
  92. package/src/rust/opstats.rs +249 -0
  93. package/src/rust/page.rs +15 -0
  94. package/src/rust/paging.rs +25 -0
  95. package/src/rust/prefix.rs +15 -0
  96. package/src/rust/profiler.rs +155 -0
  97. package/src/rust/regs.rs +38 -0
  98. package/src/rust/softfloat.rs +286 -0
  99. package/src/rust/state_flags.rs +27 -0
  100. package/src/rust/wasmgen/mod.rs +2 -0
  101. package/src/rust/wasmgen/wasm_builder.rs +1047 -0
  102. package/src/rust/wasmgen/wasm_opcodes.rs +221 -0
  103. package/src/rust/zstd.rs +105 -0
  104. package/src/sb16.ts +1928 -0
  105. package/src/state.ts +359 -0
  106. package/src/uart.ts +472 -0
  107. package/src/vga.ts +2791 -0
  108. package/src/virtio.ts +1756 -0
  109. package/src/virtio_balloon.ts +273 -0
  110. package/src/virtio_console.ts +372 -0
  111. package/src/virtio_net.ts +326 -0
@@ -0,0 +1,107 @@
1
+ import assert from 'node:assert/strict'
2
+
3
+ export interface SwitchCase {
4
+ conditions: (string | number)[]
5
+ body: Statement[]
6
+ }
7
+
8
+ export interface DefaultCase {
9
+ varname?: string
10
+ body: Statement[]
11
+ }
12
+
13
+ export interface SwitchStatement {
14
+ type: 'switch'
15
+ condition: string
16
+ cases: SwitchCase[]
17
+ default_case?: DefaultCase
18
+ }
19
+
20
+ export interface IfBlock {
21
+ condition: string
22
+ body: Statement[]
23
+ }
24
+
25
+ export interface IfElseStatement {
26
+ type: 'if-else'
27
+ if_blocks: IfBlock[]
28
+ else_block?: { body: Statement[] }
29
+ }
30
+
31
+ export type Statement = string | SwitchStatement | IfElseStatement
32
+
33
+ function indent(lines: string[], how_much: number): string[] {
34
+ return lines.map((line) => ' '.repeat(how_much) + line)
35
+ }
36
+
37
+ export function print_syntax_tree(statements: Statement[]): string[] {
38
+ const code: string[] = []
39
+
40
+ for (const statement of statements) {
41
+ if (typeof statement === 'string') {
42
+ code.push(statement)
43
+ } else if (statement.type === 'switch') {
44
+ assert(statement.condition)
45
+
46
+ const cases: string[] = []
47
+
48
+ for (const case_ of statement.cases) {
49
+ assert(case_.conditions.length >= 1)
50
+
51
+ cases.push(case_.conditions.join(' | ') + ' => {')
52
+ cases.push(...indent(print_syntax_tree(case_.body), 4))
53
+ cases.push(`},`)
54
+ }
55
+
56
+ if (statement.default_case) {
57
+ const varname = statement.default_case.varname || '_'
58
+ cases.push(`${varname} => {`)
59
+ cases.push(
60
+ ...indent(
61
+ print_syntax_tree(statement.default_case.body),
62
+ 4,
63
+ ),
64
+ )
65
+ cases.push(`}`)
66
+ }
67
+
68
+ code.push(`match ${statement.condition} {`)
69
+ code.push(...indent(cases, 4))
70
+ code.push(`}`)
71
+ } else if (statement.type === 'if-else') {
72
+ assert(statement.if_blocks.length >= 1)
73
+
74
+ const first_if_block = statement.if_blocks[0]
75
+
76
+ code.push(`if ${first_if_block.condition} {`)
77
+ code.push(...indent(print_syntax_tree(first_if_block.body), 4))
78
+ code.push(`}`)
79
+
80
+ for (let i = 1; i < statement.if_blocks.length; i++) {
81
+ const if_block = statement.if_blocks[i]
82
+
83
+ code.push(`else if ${if_block.condition} {`)
84
+ code.push(...indent(print_syntax_tree(if_block.body), 4))
85
+ code.push(`}`)
86
+ }
87
+
88
+ if (statement.else_block) {
89
+ code.push(`else {`)
90
+ code.push(
91
+ ...indent(print_syntax_tree(statement.else_block.body), 4),
92
+ )
93
+ code.push(`}`)
94
+ }
95
+ } else {
96
+ assert(
97
+ false,
98
+ 'Unexpected type: ' +
99
+ (statement as { type: string }).type +
100
+ ' In: ' +
101
+ JSON.stringify(statement),
102
+ )
103
+ }
104
+ }
105
+
106
+ return code
107
+ }
package/gen/util.ts ADDED
@@ -0,0 +1,35 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import process from 'node:process'
4
+
5
+ const CYAN_FMT = '\x1b[36m%s\x1b[0m'
6
+
7
+ export function hex(n: number, pad: number = 0): string {
8
+ let s = n.toString(16).toUpperCase()
9
+ while (s.length < pad) s = '0' + s
10
+ return s
11
+ }
12
+
13
+ export function get_switch_value(arg_switch: string): string | null {
14
+ const argv = process.argv
15
+ const switch_i = argv.indexOf(arg_switch)
16
+ const val_i = switch_i + 1
17
+ if (switch_i > -1 && val_i < argv.length) {
18
+ return argv[switch_i + 1]
19
+ }
20
+ return null
21
+ }
22
+
23
+ export function get_switch_exist(arg_switch: string): boolean {
24
+ return process.argv.includes(arg_switch)
25
+ }
26
+
27
+ export function finalize_table_rust(
28
+ out_dir: string,
29
+ name: string,
30
+ contents: string,
31
+ ): void {
32
+ const file_path = path.join(out_dir, name)
33
+ fs.writeFileSync(file_path, contents)
34
+ console.log(CYAN_FMT, `[+] Wrote table ${name}.`)
35
+ }