-
Notifications
You must be signed in to change notification settings - Fork 239
/
bril.ts
149 lines (128 loc) · 2.91 KB
/
bril.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* The definition of the Bril language.
*/
/**
* A variable name.
*/
export type Ident = string;
/**
* Primitive types.
*/
export type PrimType = "int" | "bool" | "float" | "char";
/**
* Parameterized types. (We only have pointers for now.)
*/
export type ParamType = {ptr: Type};
/**
* Value types.
*/
export type Type = PrimType | ParamType;
/**
* An (always optional) source code position.
*/
export type Position = {row: number, col: number};
/**
* Common fields in any operation.
*/
interface Op {
args?: Ident[];
funcs?: Ident[];
labels?: Ident[];
pos?: Position;
}
/**
* An instruction that does not produce any result.
*/
export interface EffectOperation extends Op {
op: "br" | "jmp" | "print" | "ret" | "call" |
"store" | "free" |
"speculate" | "guard" | "commit";
}
/**
* An operation that produces a value and places its result in the
* destination variable.
*/
export interface ValueOperation extends Op {
op: "add" | "mul" | "sub" | "div" |
"id" | "nop" |
"eq" | "lt" | "gt" | "ge" | "le" | "not" | "and" | "or" |
"call" |
"load" | "ptradd" | "alloc" |
"fadd" | "fmul" | "fsub" | "fdiv" |
"feq" | "flt" | "fle" | "fgt" | "fge" |
"ceq" | "clt" | "cle" | "cgt" | "cge" |
"char2int" | "int2char" |
"phi";
dest: Ident;
type: Type;
}
/**
* The type of Bril values that may appear in constants.
*/
export type Value = number | boolean | string;
/**
* An instruction that places a literal value into a variable.
*/
export interface Constant {
op: "const";
value: Value;
dest: Ident;
type: Type;
pos?: Position;
}
/**
* Operations take arguments, which come from previously-assigned identifiers.
*/
export type Operation = EffectOperation | ValueOperation;
/**
* Instructions can be operations (which have arguments) or constants (which
* don't). Both produce a value in a destination variable.
*/
export type Instruction = Operation | Constant;
/**
* Both constants and value operations produce results.
*/
export type ValueInstruction = Constant | ValueOperation;
/**
* The valid opcodes for value-producing instructions.
*/
export type ValueOpCode = ValueOperation["op"];
/**
* The valid opcodes for effecting operations.
*/
export type EffectOpCode = EffectOperation["op"];
/**
* All valid operation opcodes.
*/
export type OpCode = ValueOpCode | EffectOpCode;
/**
* Jump labels just mark a position with a name.
*/
export interface Label {
label: Ident;
pos?: Position;
}
/*
* An argument has a name and a type.
*/
export interface Argument {
name: Ident;
type: Type;
}
/**
* A function consists of a sequence of instructions.
*/
export interface Function {
name: Ident;
args?: Argument[];
instrs: (Instruction | Label)[];
type?: Type;
pos?: Position;
}
/**
* A program consists of a set of functions, one of which must be named
* "main".
*/
export interface Program {
functions: Function[];
}