-
Notifications
You must be signed in to change notification settings - Fork 18
/
jacs_format.ts
120 lines (103 loc) · 3.03 KB
/
jacs_format.ts
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
113
114
115
116
117
118
119
120
namespace jacs {
export function opTakesNumber(op: Op) {
return !!(OP_PROPS.charCodeAt(op) & BytecodeFlag.TAKES_NUMBER)
}
export function exprIsStateful(op: Op) {
return !(OP_PROPS.charCodeAt(op) & BytecodeFlag.IS_STATELESS)
}
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 opIsStmt(op: Op) {
return !!(OP_PROPS.charCodeAt(op) & BytecodeFlag.IS_STMT)
}
export enum CellKind {
LOCAL = Op.EXPRx_LOAD_LOCAL,
GLOBAL = Op.EXPRx_LOAD_GLOBAL,
PARAM = Op.EXPRx_LOAD_PARAM,
FLOAT_CONST = Op.EXPRx_LITERAL_F64,
// these cannot be emitted directly
JD_EVENT = 0x100,
JD_REG = 0x101,
JD_ROLE = 0x102,
JD_VALUE_SEQ = 0x103,
JD_CURR_BUFFER = 0x104,
JD_COMMAND = 0x105,
JD_CLIENT_COMMAND = 0x106,
X_BUFFER = 0x124,
ERROR = 0x200,
}
export function storeStmt(k: CellKind) {
switch (k) {
case CellKind.LOCAL:
return Op.STMTx1_STORE_LOCAL
case CellKind.PARAM:
return Op.STMTx1_STORE_PARAM
case CellKind.GLOBAL:
return Op.STMTx1_STORE_GLOBAL
default:
return oops("bad kind")
}
}
export function loadExpr(k: CellKind) {
switch (k) {
case CellKind.LOCAL:
case CellKind.PARAM:
case CellKind.GLOBAL:
return k as any as Op
default:
return oops("bad kind")
}
}
export interface InstrArgResolver {
describeCell(t: string, idx: number): string
resolverPC: number
}
export interface FunctionDebugInfo {
name: string
// format is (line-number, start, len)
// start is offset in halfwords from the start of the function
// len is in halfwords
srcmap: number[]
locals: CellDebugInfo[]
}
export interface CellDebugInfo {
name: string
}
export interface RoleDebugInfo extends CellDebugInfo {
serviceClass: number
}
export interface DebugInfo {
functions: FunctionDebugInfo[]
roles: RoleDebugInfo[]
globals: CellDebugInfo[]
source: string
}
export function emptyDebugInfo(): DebugInfo {
return {
functions: [],
globals: [],
roles: [],
source: "",
}
}
export interface JacError {
filename: string
line: number
column: number
message: string
codeFragment: string
}
export function printJacError(err: JacError) {
let msg = `${err.filename || ""}(${err.line},${err.column}): ${
err.message
}`
if (err.codeFragment) msg += ` (${err.codeFragment})`
console.error(msg)
}
}