-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathnumber.bal
368 lines (327 loc) · 14.8 KB
/
number.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import wso2/nballerina.bir;
import wso2/nballerina.types as t;
import wso2/nballerina.print.llvm;
final llvm:StructType LLVM_INT_WITH_OVERFLOW = llvm:structType(["i64", "i1"]);
final RuntimeFunction floatToIntFunction = {
name: "float_to_int",
ty: {
returnType: LLVM_INT_WITH_OVERFLOW,
paramTypes: ["double"]
},
attrs: ["nounwind", "readnone", "speculatable", "willreturn"]
};
final RuntimeFunction decimalNegFunction = {
name: "decimal_neg",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [LLVM_TAGGED_PTR]
},
attrs: [["param", 0, "readonly"]]
};
final RuntimeFunction convertToFloatFunction = {
name: "convert_to_float",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [LLVM_TAGGED_PTR]
},
attrs: [["param", 0, "readonly"]]
};
final RuntimeFunction decimalToIntFunction = {
name: "decimal_to_int",
ty: {
returnType: LLVM_INT_WITH_OVERFLOW,
paramTypes: [LLVM_TAGGED_PTR]
},
attrs: [["param", 0, "readonly"]]
};
final RuntimeFunction convertToIntFunction = {
name: "convert_to_int",
ty: {
returnType: llvm:structType([LLVM_TAGGED_PTR, "i1"]),
paramTypes: [LLVM_TAGGED_PTR]
},
attrs: [["param", 0, "readonly"]]
};
final RuntimeFunction decimalFromIntFunction = {
name: "decimal_from_int",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [LLVM_INT]
},
attrs: []
};
final RuntimeFunction decimalFromFloatFunction = {
name: "decimal_from_float",
ty: {
returnType: LLVM_TAGGED_WITH_PANIC_CODE,
paramTypes: [LLVM_FLOAT]
},
attrs: []
};
final RuntimeFunction convertToDecimalFunction = {
name: "convert_to_decimal",
ty: {
returnType: LLVM_TAGGED_WITH_PANIC_CODE,
paramTypes: [LLVM_TAGGED_PTR]
},
attrs: [["param", 0, "readonly"]]
};
final readonly & map<RuntimeFunction> decimalArithmeticFuncs = createDecimalArithmeticFuncs();
function createDecimalArithmeticFuncs() returns readonly & map<RuntimeFunction> {
map<RuntimeFunction> m = {};
readonly & llvm:FunctionType ty = {
returnType: llvm:structType([LLVM_TAGGED_PTR, "i64"]),
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
};
foreach var [op, name] in decimalArithmeticFuncNames.entries() {
m[op] = { name: "decimal_" + name, ty, attrs: [["param", 0, "readonly"]] };
}
return m.cloneReadOnly();
}
final RuntimeFunction decimalToFloatFunction = {
name: "decimal_to_float",
ty: {
returnType: "double",
paramTypes: [LLVM_TAGGED_PTR]
},
attrs: [["param", 0, "readonly"]]
};
function buildArithmeticBinary(llvm:Builder builder, Scaffold scaffold, bir:IntArithmeticBinaryInsn insn) {
llvm:IntrinsicFunctionName? intrinsicName = buildBinaryIntIntrinsic(insn.op);
llvm:Value lhs = buildInt(builder, scaffold, insn.operands[0]);
llvm:Value rhs = buildInt(builder, scaffold, insn.operands[1]);
llvm:Value result;
llvm:BasicBlock? joinBlock = ();
if intrinsicName != () {
llvm:FunctionDecl intrinsicFunction = scaffold.getIntrinsicFunction(intrinsicName);
// XXX better to distinguish builder.call and builder.callVoid
llvm:Value resultWithOverflow = <llvm:Value>builder.call(intrinsicFunction, [lhs, rhs]);
llvm:BasicBlock continueBlock = scaffold.addBasicBlock();
llvm:BasicBlock overflowBlock = scaffold.addBasicBlock();
builder.condBr(builder.extractValue(resultWithOverflow, 1), overflowBlock, continueBlock);
builder.positionAtEnd(overflowBlock);
builder.store(buildErrorForConstPanic(builder, scaffold, PANIC_ARITHMETIC_OVERFLOW, insn.pos), scaffold.panicAddress());
builder.br(scaffold.getOnPanic());
builder.positionAtEnd(continueBlock);
result = builder.extractValue(resultWithOverflow, 0);
}
else {
llvm:BasicBlock zeroDivisorBlock = scaffold.addBasicBlock();
llvm:BasicBlock continueBlock = scaffold.addBasicBlock();
builder.condBr(builder.iCmp("eq", rhs, constInt(scaffold, 0)), zeroDivisorBlock, continueBlock);
builder.positionAtEnd(zeroDivisorBlock);
builder.store(buildErrorForConstPanic(builder, scaffold, PANIC_DIVIDE_BY_ZERO, insn.pos), scaffold.panicAddress());
builder.br(scaffold.getOnPanic());
builder.positionAtEnd(continueBlock);
continueBlock = scaffold.addBasicBlock();
llvm:BasicBlock overflowBlock = scaffold.addBasicBlock();
builder.condBr(builder.iBitwise("and",
builder.iCmp("eq", lhs, constInt(scaffold, int:MIN_VALUE)),
builder.iCmp("eq", rhs, constInt(scaffold, -1))),
overflowBlock,
continueBlock);
builder.positionAtEnd(overflowBlock);
llvm:IntArithmeticSignedOp op;
if insn.op == "/" {
op = "sdiv";
builder.store(buildErrorForConstPanic(builder, scaffold, PANIC_ARITHMETIC_OVERFLOW, insn.pos), scaffold.panicAddress());
builder.br(scaffold.getOnPanic());
}
else {
builder.store(constInt(scaffold, 0), scaffold.address(insn.result));
llvm:BasicBlock b = scaffold.addBasicBlock();
builder.br(b);
joinBlock = b;
op = "srem";
}
builder.positionAtEnd(continueBlock);
result = builder.iArithmeticSigned(op, lhs, rhs);
}
buildStoreInt(builder, scaffold, result, insn.result);
if joinBlock != () {
builder.br(joinBlock);
builder.positionAtEnd(joinBlock);
}
}
function buildNoPanicArithmeticBinary(llvm:Builder builder, Scaffold scaffold, bir:IntNoPanicArithmeticBinaryInsn insn) {
llvm:Value lhs = buildInt(builder, scaffold, insn.operands[0]);
llvm:Value rhs = buildInt(builder, scaffold, insn.operands[1]);
llvm:IntArithmeticOp op = intArithmeticOps.get(insn.op);
llvm:Value result = builder.iArithmeticNoWrap(op, lhs, rhs);
buildStoreInt(builder, scaffold, result, insn.result);
}
function buildFloatArithmeticBinary(llvm:Builder builder, Scaffold scaffold, bir:FloatArithmeticBinaryInsn insn) {
llvm:Value lhs = buildFloat(builder, scaffold, insn.operands[0]);
llvm:Value rhs = buildFloat(builder, scaffold, insn.operands[1]);
llvm:FloatArithmeticOp op = floatArithmeticOps.get(insn.op);
llvm:Value result = builder.fArithmetic(op, lhs, rhs);
buildStoreFloat(builder, scaffold, result, insn.result);
}
function buildDecimalArithmeticBinary(llvm:Builder builder, Scaffold scaffold, bir:DecimalArithmeticBinaryInsn insn) {
llvm:Value lhs = buildDecimal(builder, scaffold, insn.operands[0]);
llvm:Value rhs = buildDecimal(builder, scaffold, insn.operands[1]);
llvm:Value resultWithErr = buildRuntimeFunctionCall(builder, scaffold, decimalArithmeticFuncs.get(insn.op), [lhs, rhs]);
buildStoreDecimal(builder, scaffold, buildCheckPanicCode(builder, scaffold, resultWithErr, insn.pos), insn.result);
}
function buildFloatNegate(llvm:Builder builder, Scaffold scaffold, bir:FloatNegateInsn insn) {
llvm:Value operand = buildFloat(builder, scaffold, insn.operand);
llvm:Value result = builder.fNeg(operand);
buildStoreFloat(builder, scaffold, result, insn.result);
}
final readonly & map<llvm:IntBitwiseOp> binaryBitwiseOp = {
"&": "and",
"^": "xor",
"|": "or",
"<<": "shl",
">>": "ashr",
">>>" : "lshr"
};
function buildBitwiseBinary(llvm:Builder builder, Scaffold scaffold, bir:IntBitwiseBinaryInsn insn) {
llvm:Value lhs = buildInt(builder, scaffold, insn.operands[0]);
llvm:Value rhs = buildInt(builder, scaffold, insn.operands[1]);
if insn.op is bir:BitwiseShiftOp {
rhs = builder.iBitwise("and", constInt(scaffold, 0x3F), rhs);
}
llvm:IntBitwiseOp op = binaryBitwiseOp.get(insn.op);
llvm:Value result = builder.iBitwise(op, lhs, rhs);
buildStoreInt(builder, scaffold, result, insn.result);
}
function buildConvertToInt(llvm:Builder builder, Scaffold scaffold, bir:ConvertToIntInsn insn) returns BuildError? {
var [repr, val] = check buildReprValue(builder, scaffold, insn.operand);
if repr.base == BASE_REPR_FLOAT {
buildConvertFloatToInt(builder, scaffold, val, insn);
return;
}
else if repr is TaggedRepr && repr.subtype == t:DECIMAL {
buildConvertDecimalToInt(builder, scaffold, val, insn);
return;
}
// convert to int form tagged pointer
llvm:Value resultWithErr = buildRuntimeFunctionCall(builder, scaffold, convertToIntFunction, [val]);
buildStoreTagged(builder, scaffold, buildCheckOverflow(builder, scaffold, resultWithErr, insn), insn.result);
}
function buildConvertFloatToInt(llvm:Builder builder, Scaffold scaffold, llvm:Value floatVal, bir:ConvertToIntInsn insn) {
llvm:Value resultWithErr = buildRuntimeFunctionCall(builder, scaffold, floatToIntFunction, [floatVal]);
buildStoreInt(builder, scaffold, buildCheckOverflow(builder, scaffold, resultWithErr, insn), insn.result);
}
function buildConvertDecimalToInt(llvm:Builder builder, Scaffold scaffold, llvm:Value decimalVal, bir:ConvertToIntInsn insn) {
llvm:Value resultWithErr = buildRuntimeFunctionCall(builder, scaffold, decimalToIntFunction, [decimalVal]);
buildStoreInt(builder, scaffold, buildCheckOverflow(builder, scaffold, resultWithErr, insn), insn.result);
}
function buildCheckOverflow(llvm:Builder builder, Scaffold scaffold, llvm:Value valWithErr, bir:ConvertToIntInsn insn) returns llvm:Value {
llvm:BasicBlock continueBlock = scaffold.addBasicBlock();
llvm:BasicBlock errBlock = scaffold.addBasicBlock();
builder.condBr(builder.extractValue(valWithErr, 1), errBlock, continueBlock);
builder.positionAtEnd(errBlock);
builder.store(buildErrorForConstPanic(builder, scaffold, PANIC_ARITHMETIC_OVERFLOW, insn.pos), scaffold.panicAddress());
builder.br(scaffold.getOnPanic());
builder.positionAtEnd(continueBlock);
return builder.extractValue(valWithErr, 0);
}
function buildConvertToFloat(llvm:Builder builder, Scaffold scaffold, bir:ConvertToFloatInsn insn) returns BuildError? {
var [repr, val] = check buildReprValue(builder, scaffold, insn.operand);
if repr.base == BASE_REPR_INT {
buildConvertIntToFloat(builder, scaffold, val, insn);
return;
}
else if repr is TaggedRepr && repr.subtype == t:DECIMAL {
buildConvertDecimalToFloat(builder, scaffold, val, insn);
return;
}
// convert to float form tagged pointer
llvm:Value result = buildRuntimeFunctionCall(builder, scaffold, convertToFloatFunction, [val]);
buildStoreTagged(builder, scaffold, result, insn.result);
}
function buildConvertIntToFloat(llvm:Builder builder, Scaffold scaffold, llvm:Value intVal, bir:ConvertToFloatInsn insn) {
buildStoreFloat(builder, scaffold, builder.sIToFP(intVal, LLVM_FLOAT), insn.result);
}
function buildConvertDecimalToFloat(llvm:Builder builder, Scaffold scaffold, llvm:Value decimalVal, bir:ConvertToFloatInsn insn) {
llvm:Value result = buildRuntimeFunctionCall(builder, scaffold, decimalToFloatFunction, [decimalVal]);
buildStoreFloat(builder, scaffold, result, insn.result);
}
function buildConvertToDecimal(llvm:Builder builder, Scaffold scaffold, bir:ConvertToDecimalInsn insn) returns BuildError? {
var [repr, val] = check buildReprValue(builder, scaffold, insn.operand);
if repr.base == BASE_REPR_INT {
buildConvertIntToDecimal(builder, scaffold, val, insn);
return;
}
else if repr.base == BASE_REPR_FLOAT {
buildConvertFloatToDecimal(builder, scaffold, val, insn);
return;
}
// convert to decimal from tagged pointer
llvm:Value resultWithErr = buildRuntimeFunctionCall(builder, scaffold, convertToDecimalFunction, [val]);
buildStoreDecimal(builder, scaffold, buildCheckPanicCode(builder, scaffold, resultWithErr, insn.pos), insn.result);
}
function buildConvertIntToDecimal(llvm:Builder builder, Scaffold scaffold, llvm:Value intVal, bir:ConvertToDecimalInsn insn) {
llvm:BasicBlock continueBlock = scaffold.addBasicBlock();
llvm:BasicBlock errBlock = scaffold.addBasicBlock();
builder.condBr(constBoolean(scaffold, false), errBlock, continueBlock);
builder.positionAtEnd(errBlock);
builder.br(scaffold.getOnPanic());
builder.positionAtEnd(continueBlock);
buildStoreDecimal(builder, scaffold, buildRuntimeFunctionCall(builder, scaffold, decimalFromIntFunction, [intVal]), insn.result);
}
function buildConvertFloatToDecimal(llvm:Builder builder, Scaffold scaffold, llvm:Value floatVal, bir:ConvertToDecimalInsn insn) {
llvm:Value resultWithErr = buildRuntimeFunctionCall(builder, scaffold, decimalFromFloatFunction, [floatVal]);
buildStoreDecimal(builder, scaffold, buildCheckPanicCode(builder, scaffold, resultWithErr, insn.pos), insn.result);
}
function buildDecimalNegate(llvm:Builder builder, Scaffold scaffold, bir:DecimalNegateInsn insn) {
llvm:Value operand = buildDecimal(builder, scaffold, insn.operand);
llvm:Value result = buildRuntimeFunctionCall(builder, scaffold, decimalNegFunction, [operand]);
buildStoreDecimal(builder, scaffold, result, insn.result);
}
final readonly & map<llvm:IntrinsicFunctionName> binaryIntIntrinsics = {
"+": "sadd.with.overflow.i64.i64",
"-": "ssub.with.overflow.i64.i64",
"*": "smul.with.overflow.i64.i64"
};
final readonly & map<llvm:IntArithmeticOp> intArithmeticOps = {
"+": "add",
"-": "sub",
"*": "mul"
};
final readonly & map<llvm:FloatArithmeticOp> floatArithmeticOps = {
"+": "fadd",
"-": "fsub",
"*": "fmul",
"/": "fdiv",
"%": "frem"
};
final readonly & map<string> decimalArithmeticFuncNames = {
"+": "add",
"-": "sub",
"*": "mul",
"/": "div",
"%": "rem"
};
// final readonly & map<llvm:BinaryIntOp> binaryIntOps = {
// "+": "add",
// "-": "sub",
// "*": "mul",
// "/": "sdiv",
// "%": "srem"
// };
// function buildBinaryIntOp(bir:ArithmeticBinaryOp op) returns llvm:BinaryIntOp {
// return <llvm:BinaryIntOp>binaryIntOps[op];
// }
function buildBinaryIntIntrinsic(bir:ArithmeticBinaryOp op) returns llvm:IntrinsicFunctionName? {
return binaryIntIntrinsics[op];
}
// Build a value as REPR_FLOAT
function buildFloat(llvm:Builder builder, Scaffold scaffold, bir:FloatOperand operand) returns llvm:Value {
if operand is bir:FloatConstOperand {
return constFloat(scaffold, operand.value);
}
else {
return builder.load(scaffold.address(operand));
}
}
function buildDecimal(llvm:Builder builder, Scaffold scaffold, bir:DecimalOperand operand) returns llvm:Value {
if operand is bir:DecimalConstOperand {
return buildConstDecimal(builder, scaffold, operand.value);
}
else {
return builder.load(scaffold.address(operand));
}
}