-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbitcode.ts
More file actions
103 lines (76 loc) · 2.67 KB
/
bitcode.ts
File metadata and controls
103 lines (76 loc) · 2.67 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
import { Buffer } from 'buffer';
import { Attribute, AttributeList } from './attribute-list';
import { CallingConv } from './calling-conv';
import { Linkage } from './linkage';
import * as passes from './passes';
import * as types from './types';
import * as values from './values';
import Type = types.Type;
import constants = values.constants;
const CHAR_WIDTH = 8;
export class Builder {
// Types
public static void(): types.Void {
return new types.Void();
}
public static i(width: number, signed: boolean = false): types.Int {
return new types.Int(width, signed);
}
public static signature(returnType: Type, params: Type[]): types.Signature {
return new types.Signature(returnType, params);
}
public static array(length: number, elemType: Type): types.Array {
return new types.Array(length, elemType);
}
public static struct(name?: string): types.Struct {
return new types.Struct(name);
}
// Values
public static global(ty: types.Type, name: string, init?: constants.Constant)
: values.Global {
return new values.Global(ty, name, init);
}
public static cstring(value: string): constants.Array {
const len = Buffer.byteLength(value);
const blob = Buffer.alloc(len + 1);
blob.write(value);
return Builder.blob(Buffer.from(blob));
}
public static blob(buffer: Buffer): constants.Array {
const elemTy = Builder.i(CHAR_WIDTH);
const ty = Builder.array(buffer.length, elemTy);
const elems = Array.from(buffer).map((ch) => elemTy.val(ch));
return ty.val(elems);
}
// Metadata
public static metadata(value: constants.MetadataValue): constants.Metadata {
return new constants.Metadata(value);
}
// Convenience methods
public void(): types.Void { return Builder.void(); }
public i(width: number): types.Int { return Builder.i(width); }
public signature(returnType: Type, params: Type[]): types.Signature {
return Builder.signature(returnType, params);
}
public array(length: number, elemType: Type): types.Array {
return Builder.array(length, elemType);
}
public struct(name?: string): types.Struct { return Builder.struct(name); }
public global(ty: types.Type, name: string, init?: constants.Constant)
: values.Global {
return Builder.global(ty, name, init);
}
public cstring(value: string): constants.Array {
return Builder.cstring(value);
}
public blob(buffer: Buffer): constants.Array {
// TODO(indutny): cache results?
return Builder.blob(buffer);
}
public metadata(value: constants.MetadataValue): constants.Metadata {
return Builder.metadata(value);
}
}
export {
passes, types, values, Attribute, AttributeList, CallingConv, Linkage,
};