forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
112 lines (95 loc) · 2.68 KB
/
format.ts
File metadata and controls
112 lines (95 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
Op,
NumFmt,
BytecodeFlag,
BinFmt,
OP_PROPS,
ObjectType,
OP_TYPES,
NumFmtSpecial,
} from "./bytecode"
import { toHex } from "./jdutil"
import { DebugInfo } from "@devicescript/interop"
export * from "./bytecode"
import type ts from "typescript"
import { ResolvedBuildConfig } from "@devicescript/interop"
import { CompileFlags } from "./compiler"
export interface SMap<T> {
[k: string]: T
}
export function opTakesNumber(op: Op) {
return !!(OP_PROPS.charCodeAt(op) & BytecodeFlag.TAKES_NUMBER)
}
export function opNumRealArgs(op: Op) {
return OP_PROPS.charCodeAt(op) & BytecodeFlag.NUM_ARGS_MASK
}
export function opNumArgs(op: Op) {
let n = opNumRealArgs(op)
if (opTakesNumber(op)) n++
return n
}
export function opType(op: Op): ObjectType {
return OP_TYPES.charCodeAt(op)
}
export function opIsStmt(op: Op) {
return !!(OP_PROPS.charCodeAt(op) & BytecodeFlag.IS_STMT)
}
export function exprIsStateful(op: Op) {
return (
opIsStmt(op) || !(OP_PROPS.charCodeAt(op) & BytecodeFlag.IS_STATELESS)
)
}
export function stmtIsFinal(op: Op) {
return opIsStmt(op) && OP_PROPS.charCodeAt(op) & BytecodeFlag.IS_FINAL_STMT
}
export interface InstrArgResolver {
describeCell?(fmt: string, idx: number): string
verboseDisasm?: boolean
forDiff?: boolean
}
export function bitSize(fmt: NumFmt) {
return 8 << (fmt & 0b11)
}
export function numfmtToString(v: number) {
const fmt = v & 0xf
const bitsz = bitSize(fmt)
const letter = ["u", "i", "f", "x"][fmt >> 2]
if (letter == "x") {
const idx = (v >> 4) | ((v & 3) << 4)
return NumFmtSpecial[idx] ?? "Spec." + idx
}
const shift = v >> 4
if (shift) return letter + (bitsz - shift) + "." + shift
else return letter + bitsz
}
export interface DevsDiagnostic extends ts.Diagnostic {
filename: string
line: number
column: number
endLine: number
endColumn: number
formatted: string
}
export interface Host {
write(filename: string, contents: Uint8Array | string): void
read(filename: string): string
resolvePath(filename: string): string
relativePath?(filename: string): string
log(msg: string): void
error?(err: DevsDiagnostic): void
getConfig(): ResolvedBuildConfig
verifyBytecode?(buf: Uint8Array, dbgInfo?: DebugInfo): void
isBasicOutput?(): boolean
getFlags?(): CompileFlags
}
export function parseImgVersion(v: number) {
return {
major: (v >> 24) & 0xff,
minor: (v >> 16) & 0xff,
patch: (v >> 0) & 0xffff,
}
}
export function runtimeVersion() {
const v = parseImgVersion(BinFmt.IMG_VERSION)
return `v${v.major}.${v.minor}.${v.patch}`
}