/
node-helpers.js
115 lines (99 loc) · 2.31 KB
/
node-helpers.js
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
// @flow
import {
parse32F,
parse64F,
parse32I,
parse64I,
parseU32,
isNanLiteral,
isInfLiteral
} from "@webassemblyjs/wast-parser";
import { longNumberLiteral, floatLiteral, numberLiteral, instr } from "./nodes";
export function numberLiteralFromRaw(
rawValue: number | string,
instructionType: Valtype = "i32"
): NumericLiteral {
const original = rawValue;
// Remove numeric separators _
if (typeof rawValue === "string") {
rawValue = rawValue.replace(/_/g, "");
}
if (typeof rawValue === "number") {
return numberLiteral(rawValue, String(original));
} else {
switch (instructionType) {
case "i32": {
return numberLiteral(parse32I(rawValue), String(original));
}
case "u32": {
return numberLiteral(parseU32(rawValue), String(original));
}
case "i64": {
return longNumberLiteral(parse64I(rawValue), String(original));
}
case "f32": {
return floatLiteral(
parse32F(rawValue),
isNanLiteral(rawValue),
isInfLiteral(rawValue),
String(original)
);
}
// f64
default: {
return floatLiteral(
parse64F(rawValue),
isNanLiteral(rawValue),
isInfLiteral(rawValue),
String(original)
);
}
}
}
}
export function instruction(
id: string,
args: Array<Expression> = [],
namedArgs?: Object = {}
): Instr {
return instr(id, undefined, args, namedArgs);
}
export function objectInstruction(
id: string,
object: Valtype,
args: Array<Expression> = [],
namedArgs?: Object = {}
): Instr {
return instr(id, object, args, namedArgs);
}
/**
* Decorators
*/
export function withLoc(n: Node, end: Position, start: Position): Node {
const loc = {
start,
end
};
n.loc = loc;
return n;
}
export function withRaw(n: NumericLiteral, raw: string): Node {
n.raw = raw;
return n;
}
export function funcParam(valtype: Valtype, id: ?string): FuncParam {
return {
id,
valtype
};
}
export function indexLiteral(value: number | string): Index {
// $FlowIgnore
const x: NumberLiteral = numberLiteralFromRaw(value, "u32");
return x;
}
export function memIndexLiteral(value: number): Memidx {
// $FlowIgnore
const x: U32Literal = numberLiteralFromRaw(value, "u32");
return x;
}