-
Notifications
You must be signed in to change notification settings - Fork 46
/
common.bal
executable file
·213 lines (172 loc) · 6.47 KB
/
common.bal
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import wso2/nballerina.comm.err;
// This contains the common definitions for both print.llvm and jni.llvm
// "i64" corresponds to LLVMInt64Type
// "i8" corresponds to LLVMInt8Type
// "i1" corresponds to LLVMInt1Type
public type IntType "i64"|"i32"|"i16"|"i8"|"i1";
public type FloatType "double";
// Used to constrain parameters that represent an alignment
public type Alignment 1|2|4|8|16;
// Corresponds to LLVMPointerType
public type PointerType readonly & record {|
Type pointsTo;
int addressSpace;
|};
public type IntegralType IntType|PointerType;
// Corresponds to LLVMPointerType function
public function pointerType(Type ty, int addressSpace = 0) returns PointerType {
return { pointsTo: ty, addressSpace };
}
# https://github.com/llvm/llvm-project/blob/ad4bb8280952c2cacf497e30560ee94c119b36e0/llvm/include/llvm/IR/Type.h#L259
# Valid type for a register in codegen. This includes all first-class types except struct and array types.
public type SingleValueType IntegralType|FloatType|PointerType;
// Corresponds to LLVMArrayType
public type ArrayType readonly & record {|
Type elementType;
int elementCount;
|};
public function arrayType(Type ty, int elementCount) returns ArrayType {
return { elementType: ty, elementCount: elementCount };
}
// Corresponds to llvm::StructType
public type StructType readonly & record {
Type[] elementTypes;
string? name = ();
};
public function structType(Type[] elementTypes) returns StructType {
return { elementTypes: elementTypes.cloneReadOnly() };
}
// Corresponds to LLVMMetadataRef
public type MetadataType "metadata";
public type Type IntType|FloatType|PointerType|StructType|ArrayType|FunctionType|MetadataType;
// A RetType is valid only as the return type of a function
public type RetType Type|"void";
# Corresponds to llvm::FunctionType class
public type FunctionType readonly & record {|
RetType returnType;
Type[] paramTypes;
|};
public function functionType(RetType returnType, Type[] paramTypes) returns FunctionType {
return { returnType, paramTypes: paramTypes.cloneReadOnly() };
}
// Corresponds to LLVMLinkage enum
public type Linkage "internal"|"external";
public type FunctionEnumAttribute "nocallback"|"nofree"|"nosync"|"readnone"|"noreturn"|"cold"|"nounwind"|"readnone"|"speculatable"|"willreturn";
public type ParamEnumAttribute "signext"|"zeroext"|"nest"|"readonly";
public type ReturnEnumAttribute "signext"|"zeroext"|"noalias";
public type MemoryAttribure "none";
public type EnumAttribute FunctionEnumAttribute | (readonly & ["param", int, ParamEnumAttribute]) | (readonly & ["return", ReturnEnumAttribute])| (readonly & ["memory", MemoryAttribure]);
// Subtype of LLVMOpcode
public type IntArithmeticOp "add"|"sub"|"mul";
public type FloatArithmeticOp "fadd"|"fsub"|"fmul"|"fdiv"|"frem";
public type IntArithmeticSignedOp "sdiv"|"srem";
public type IntBitwiseOp "xor"|"or"|"and"|"shl"|"ashr"|"lshr";
type IntOp IntArithmeticOp|IntArithmeticSignedOp|IntBitwiseOp;
type BinaryOp IntOp|FloatArithmeticOp;
// Corresponds to LLVMIntPredicate
public type IntPredicate "eq"|"ne"|"ugt"|"uge"|"ult"|"ule"|"sgt"|"sge"|"slt"|"sle";
// Corresponds to LLVMRealPredicate
public type FloatPredicate "false"|"oeq"|"ogt"|"oge"|"olt"|"ole"|"one"|"ord"|"ueq"|"ugt"|"uge"|"ult"|"ule"|"une"|"uno"|"true";
public type IntegerArithmeticIntrinsicName "sadd.with.overflow.i64.i64"|"ssub.with.overflow.i64.i64"|"smul.with.overflow.i64.i64";
public type GeneralIntrinsicName "ptrmask.p1.i64"|"expect.i1";
type DebugIntrinsicName "dbg.value"|"dbg.declare";
public type IntrinsicFunctionName IntegerArithmeticIntrinsicName|GeneralIntrinsicName;
public type TargetTriple string;
public type GlobalSymbolProperties record {|
boolean unnamedAddr = false;
int addressSpace = 0;
Linkage linkage = "external";
|};
public type GlobalProperties record {|
*GlobalSymbolProperties;
boolean isConstant = false;
int? align = ();
ConstValue|Function? initializer = ();
|};
// Corresponds to LLVMDWARFSourceLanguage
public type DWARFSourceLanguage "C99";
// Corresponds to LLVMDWARFEmissionKind
public type EmissionKind "none"|"full"|"lineTablesOnly";
public type CompileUnitProperties record {|
DWARFSourceLanguage language = "C99";
Metadata? file;
string? producer = ();
boolean isOptimized = false;
string? flags = ();
int runtimeVersion = 0;
string? splitName = ();
EmissionKind kind = "full";
int? DWOId = ();
boolean splitDebugInlining = false;
boolean? splitDebugInfoForProfiling = ();
string? sysRoot = ();
string? sdk = ();
|};
public type ModuleFlag ["Debug Info Version", int]|["Dwarf Version", int];
// Corresponds to LLVMModuleFlagBehavior
public type ModuleFlagBehavior "error"|"warning"|"require"|"override"|"append"|"appendUnique"|"max";
// Corresponds to LLVMDIFlags
public type DIFlag "zero";
public type FunctionMetadataProperties record {|
Metadata? scope;
string? name;
string? linkageName;
Metadata? file;
int lineNo;
Metadata? ty;
boolean isLocalToUnit = true;
boolean isDefinition = true;
int scopeLine;
DIFlag flag = "zero";
boolean isOptimized = false;
|};
// Corresponds to LLVMDWARFTypeEncoding
public type DwarfTypeEncoding "boolean"|"float"|"signed"|"signed_char";
public type BasicTypeMetadataProperties record {|
string name;
int sizeInBits;
DwarfTypeEncoding encoding;
DIFlag flag = "zero";
|};
public type VariableMetadataProperties record {|
Metadata scope;
string name;
Metadata file;
int lineNo;
Metadata ty;
DIFlag flag = "zero";
int? alignInBits=();
|};
public type ValueMetadataProperties record {|
Value value;
Metadata varInfo;
Metadata expr;
Metadata debugLoc;
BasicBlock block;
|};
public type PointerTypeMetdataProperties record {|
Metadata pointeeTy;
int sizeInBits;
Alignment? alignInBits = ();
int addressSpace = 0;
string? name = ();
|};
function getTypeAtIndex(StructType ty, int index, Context context) returns Type {
boolean isNamed = ty.name != ();
Type[] elementTypes = ty.elementTypes;
if isNamed {
elementTypes = context.getNamedStructBody(ty);
}
return elementTypes[index];
}
function sameNumberType(Value v1, Value v2) returns IntType|FloatType {
Type ty1 = v1.ty;
Type ty2 = v2.ty;
if ty1 != ty2 {
panic err:illegalArgument("expected same types");
}
else if ty1 is IntType || ty1 is FloatType {
return ty1;
}
panic err:illegalArgument("expected a number type");
}