-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathruntime-header.ll
610 lines (538 loc) · 23.7 KB
/
runtime-header.ll
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
; Start of malc header.ll
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32, i32 }
%struct.timespec = type { i64, i64 }
%struct.stat = type { i64, i64, i64, i32, i32, i32, i32, i64, i64, i64, i64, %struct.timespec, %struct.timespec, %struct.timespec, [3 x i64] }
declare i32 @puts(i8*)
declare i32 @snprintf(i8*, i64, i8* readonly, ...)
declare i32 @exit(i32)
declare i32 @memcmp(i8*, i8*, i32)
declare i32 @strlen(i8*)
declare i32 @gettimeofday(%struct.timeval*, %struct.timezone*)
declare i32 @stat(i8*, %struct.stat*)
declare i32 @open(i8*, i32, ...)
declare i64 @read(i32, i8*, i64)
declare i32 @close(i32)
declare void @llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1)
declare void @llvm.memset.p0i8.i64(i8*, i8, i64, i32, i1)
declare i8* @readline(i8*) ; Link with -lreadline
declare void @GC_init() ; Link with -lgc
declare i8* @GC_malloc(i64)
declare i8* @GC_malloc_atomic(i64)
declare i64 @GC_get_heap_size()
declare i64 @GC_get_total_bytes()
%mal_obj = type i64
; i32 - obj_type
; i32 - len (bytes/elements)
; %mal_obj - meta object
; i8* - points to data
%mal_obj_header_t = type { i32, i32, %mal_obj, i8* }
;
; Exception handling
;
%struct._Unwind_Exception = type { i64, void (i32, %struct._Unwind_Exception*)*, i64, i64 }
declare i32 @_Unwind_RaiseException(%struct._Unwind_Exception*)
declare i32 @__gxx_personality_v0(...) ; Link with -lstdc++
%mal_exn_t = type { %mal_obj, %struct._Unwind_Exception }
@global_argv = global i8** null
@global_argc = global i32 0
define private %mal_obj @identity(%mal_obj %obj) {
ret %mal_obj %obj
}
define private %mal_obj @bool_to_mal(i1 %cond) {
br i1 %cond, label %IfEqual, label %IfUnequal
IfEqual:
%1 = call %mal_obj @make_true()
ret %mal_obj %1
IfUnequal:
%2 = call %mal_obj @make_false()
ret %mal_obj %2
}
define private %mal_obj @mal_integer_q(%mal_obj %obj) {
%1 = and i64 %obj, 1
%2 = icmp eq i64 %1, 1
%3 = call %mal_obj @bool_to_mal(i1 %2)
ret %mal_obj %3
}
define private %mal_obj @mal_nil_q(%mal_obj %obj) {
%1 = icmp eq i64 %obj, 2
%2 = call %mal_obj @bool_to_mal(i1 %1)
ret %mal_obj %2
}
define private %mal_obj @mal_false_q(%mal_obj %obj) {
%1 = icmp eq i64 %obj, 4
%2 = call %mal_obj @bool_to_mal(i1 %1)
ret %mal_obj %2
}
define private %mal_obj @mal_true_q(%mal_obj %obj) {
%1 = icmp eq i64 %obj, 6
%2 = call %mal_obj @bool_to_mal(i1 %1)
ret %mal_obj %2
}
; Returns nil, false, true - for these constants
; 1 - integer
; 17/18/19 - symbol/string/keyword
; 33/34/35 - list/vector/hash-map
; 49 - atom
; 65 - env
; 66 - function
define private %mal_obj @mal_get_type(%mal_obj %obj) {
%1 = and i64 %obj, 1
%2 = icmp eq i64 %1, 1
br i1 %2, label %IfInt, label %IfConstOrObj
IfConstOrObj:
%3 = icmp ugt i64 %obj, 6
br i1 %3, label %IfObj, label %IfConst
IfObj:
%4 = inttoptr %mal_obj %obj to %mal_obj_header_t*
%5 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %4, i32 0, i32 0
%6 = load i32, i32* %5
%7 = sext i32 %6 to i64
%8 = call %mal_obj @make_integer(i64 %7)
ret %mal_obj %8
IfConst:
ret %mal_obj %obj
IfInt:
%9 = call %mal_obj @make_integer(i64 1)
ret %mal_obj %9
}
define private i32 @mal_get_len_i32(%mal_obj %obj) {
%1 = inttoptr %mal_obj %obj to %mal_obj_header_t*
%2 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 1
%3 = load i32, i32* %2
ret i32 %3
}
define private %mal_obj @mal_get_len(%mal_obj %obj) {
%1 = call i32 @mal_get_len_i32(%mal_obj %obj)
%2 = sext i32 %1 to i64
%3 = call %mal_obj @make_integer(i64 %2)
ret %mal_obj %3
}
define private %mal_obj @mal_get_meta(%mal_obj %obj) {
%1 = inttoptr %mal_obj %obj to %mal_obj_header_t*
%2 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 2
%3 = load %mal_obj, %mal_obj* %2
ret %mal_obj %3
}
define private i8* @mal_get_array_ptr_i8(%mal_obj %obj) {
%1 = inttoptr %mal_obj %obj to %mal_obj_header_t*
%2 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 3
%3 = load i8*, i8** %2
ret i8* %3
}
define private %mal_obj* @mal_get_array_ptr_mal_obj(%mal_obj %obj) {
%1 = inttoptr %mal_obj %obj to %mal_obj_header_t*
%2 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 3
%3 = bitcast i8** %2 to %mal_obj**
%4 = load %mal_obj*, %mal_obj** %3
ret %mal_obj* %4
}
define private %mal_obj @mal_integer_equal_q(%mal_obj %a, %mal_obj %b) {
%1 = icmp eq %mal_obj %a, %b
%2 = call %mal_obj @bool_to_mal(i1 %1)
ret %mal_obj %2
}
define private %mal_obj @mal_integer_gt_q(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = icmp sgt i64 %1, %2
%4 = call %mal_obj @bool_to_mal(i1 %3)
ret %mal_obj %4
}
define private %mal_obj @mal_integer_gte_q(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = icmp sge i64 %1, %2
%4 = call %mal_obj @bool_to_mal(i1 %3)
ret %mal_obj %4
}
define private %mal_obj @mal_integer_lt_q(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = icmp slt i64 %1, %2
%4 = call %mal_obj @bool_to_mal(i1 %3)
ret %mal_obj %4
}
define private %mal_obj @mal_integer_lte_q(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = icmp sle i64 %1, %2
%4 = call %mal_obj @bool_to_mal(i1 %3)
ret %mal_obj %4
}
define private %mal_obj @make_integer(i64 %x) {
%1 = shl i64 %x, 1
%2 = or i64 %1, 1
ret %mal_obj %2
}
define private i64 @mal_integer_to_raw(%mal_obj %obj) {
%1 = ashr i64 %obj, 1
ret i64 %1
}
define private %mal_obj @make_nil() {
ret %mal_obj 2
}
define private %mal_obj @make_false() {
ret %mal_obj 4
}
define private %mal_obj @make_true() {
ret %mal_obj 6
}
define private %mal_obj_header_t* @alloc_obj_header() {
%mal_obj_header_temp = getelementptr %mal_obj_header_t, %mal_obj_header_t* null, i64 1
%mal_obj_header_t_size = ptrtoint %mal_obj_header_t* %mal_obj_header_temp to i64
%1 = call i8* @GC_malloc(i64 %mal_obj_header_t_size)
%2 = bitcast i8* %1 to %mal_obj_header_t*
ret %mal_obj_header_t* %2
}
; len_bytes doesn't include the extra NULL char
define private %mal_obj @mal_make_bytearray_obj(i32 %objtype, i32 %len_bytes, i8* %bytes) {
%1 = call %mal_obj_header_t* @alloc_obj_header()
%2 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 0
store i32 %objtype, i32* %2 ; store type
%3 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 1
store i32 %len_bytes, i32* %3 ; store len
%4 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 2
%nil_obj = call %mal_obj @make_nil()
store %mal_obj %nil_obj, %mal_obj* %4 ; store meta
%5 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 3
store i8* %bytes, i8** %5 ; store pointer to byte array (string)
%new_obj = ptrtoint %mal_obj_header_t* %1 to %mal_obj
ret %mal_obj %new_obj
}
; len_bytes doesn't include the extra NULL char
define private %mal_obj @mal_empty_bytearray_obj(%mal_obj %objtype, %mal_obj %len_bytes) {
%objtype.i64 = call i64 @mal_integer_to_raw(%mal_obj %objtype)
%objtype.i32 = trunc i64 %objtype.i64 to i32
%len_bytes.i64 = call i64 @mal_integer_to_raw(%mal_obj %len_bytes)
%len_bytes.i32 = trunc i64 %len_bytes.i64 to i32
%buf_len = add i64 %len_bytes.i64, 1 ; space for terminating NULL char
%strptr = call i8* @GC_malloc_atomic(i64 %buf_len)
call void @llvm.memset.p0i8.i64(i8* %strptr, i8 0, i64 %buf_len, i32 0, i1 0)
%strobj = call %mal_obj @mal_make_bytearray_obj(i32 %objtype.i32, i32 %len_bytes.i32, i8* %strptr)
ret %mal_obj %strobj
}
define private %mal_obj @mal_set_bytearray_range(%mal_obj %dstobj, %mal_obj %offset, %mal_obj %len, %mal_obj %srcobj) {
%dst_buf = call i8* @mal_get_array_ptr_i8(%mal_obj %dstobj)
%offset.i64 = call i64 @mal_integer_to_raw(%mal_obj %offset)
%len.i64 = call i64 @mal_integer_to_raw(%mal_obj %len)
%len.i32 = trunc i64 %len.i64 to i32
%src_buf = call i8* @mal_get_array_ptr_i8(%mal_obj %srcobj)
%dst_buf_offset_ptr = getelementptr i8, i8* %dst_buf, i64 %offset.i64
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst_buf_offset_ptr, i8* %src_buf, i32 %len.i32, i32 0, i1 0)
ret %mal_obj %dstobj
}
define private %mal_obj @mal_set_bytearray_char(%mal_obj %dstobj, %mal_obj %offset, %mal_obj %ascii_value) {
%dst_buf = call i8* @mal_get_array_ptr_i8(%mal_obj %dstobj)
%offset.i64 = call i64 @mal_integer_to_raw(%mal_obj %offset)
%dst_buf_offset_ptr = getelementptr i8, i8* %dst_buf, i64 %offset.i64
%ascii_value.i64 = call i64 @mal_integer_to_raw(%mal_obj %ascii_value)
%ascii_byte = trunc i64 %ascii_value.i64 to i8
store i8 %ascii_byte, i8* %dst_buf_offset_ptr
ret %mal_obj %dstobj
}
define private %mal_obj @mal_get_bytearray_char(%mal_obj %dstobj, %mal_obj %offset) {
%dst_buf = call i8* @mal_get_array_ptr_i8(%mal_obj %dstobj)
%offset.i64 = call i64 @mal_integer_to_raw(%mal_obj %offset)
%dst_buf_offset_ptr = getelementptr i8, i8* %dst_buf, i64 %offset.i64
%ascii_value = load i8, i8* %dst_buf_offset_ptr
%ascii_value.i64 = zext i8 %ascii_value to i64
%ascii_value.obj = call %mal_obj @make_integer(i64 %ascii_value.i64)
ret %mal_obj %ascii_value.obj
}
define private %mal_obj @mal_raw_obj_to_integer(%mal_obj %obj) {
%1 = bitcast %mal_obj %obj to i64
%2 = call %mal_obj @make_integer(i64 %1)
ret %mal_obj %2
}
@snprintf_format_ld = private unnamed_addr constant [4 x i8] c"%ld\00"
define private %mal_obj @mal_integer_to_string(%mal_obj %intobj) {
%num = call i64 @mal_integer_to_raw(%mal_obj %intobj)
%vector = alloca [32 x i8]
%buf = getelementptr inbounds [32 x i8], [32 x i8]* %vector, i64 0, i64 0
%len_bytes_without_nullchar = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %buf, i64 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @snprintf_format_ld, i32 0, i32 0), i64 %num)
%len_bytes = add i32 %len_bytes_without_nullchar, 1
%len_bytes.i64 = sext i32 %len_bytes to i64
%bufcopy = call i8* @GC_malloc_atomic(i64 %len_bytes.i64)
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %bufcopy, i8* %buf, i32 %len_bytes, i32 0, i1 0)
%resstr = call %mal_obj @mal_make_bytearray_obj(i32 18, i32 %len_bytes_without_nullchar, i8* %bufcopy)
ret %mal_obj %resstr
}
; Compare two bytearrays. Length must be equal.
define private %mal_obj @mal_bytearray_equal_q(%mal_obj %a, %mal_obj %b) {
%a_len = call i32 @mal_get_len_i32(%mal_obj %a)
%a_buf = call i8* @mal_get_array_ptr_i8(%mal_obj %a)
%b_buf = call i8* @mal_get_array_ptr_i8(%mal_obj %b)
%res = call i32 @memcmp(i8* %a_buf, i8* %b_buf, i32 %a_len)
%is_equal = icmp eq i32 %res, 0
%mal_bool_res = call %mal_obj @bool_to_mal(i1 %is_equal)
ret %mal_obj %mal_bool_res
}
define private %mal_obj @mal_make_elementarray_obj(%mal_obj %objtype, %mal_obj %len_elements) {
%objtype.i64 = call i64 @mal_integer_to_raw(%mal_obj %objtype)
%objtype.i32 = trunc i64 %objtype.i64 to i32
%len_elements.i64 = call i64 @mal_integer_to_raw(%mal_obj %len_elements)
%len_elements.i32 = trunc i64 %len_elements.i64 to i32
%bytes = mul i64 %len_elements.i64, 8
%1 = call %mal_obj_header_t* @alloc_obj_header()
%2 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 0
store i32 %objtype.i32, i32* %2 ; store type
%3 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 1
store i32 %len_elements.i32, i32* %3 ; store len
%4 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 2
%nil_obj = call %mal_obj @make_nil()
store %mal_obj %nil_obj, %mal_obj* %4 ; store meta
%elementarrayptr = call i8* @GC_malloc(i64 %bytes)
%5 = getelementptr %mal_obj_header_t, %mal_obj_header_t* %1, i32 0, i32 3
store i8* %elementarrayptr, i8** %5 ; store pointer to %mal_obj array
%new_obj = ptrtoint %mal_obj_header_t* %1 to %mal_obj
ret %mal_obj %new_obj
}
define private %mal_obj @mal_set_elementarray_item(%mal_obj %obj, %mal_obj %item_index, %mal_obj %new_item) {
%elementarray = call %mal_obj* @mal_get_array_ptr_mal_obj(%mal_obj %obj)
%index_i64 = call i64 @mal_integer_to_raw(%mal_obj %item_index)
%ptr_to_element = getelementptr %mal_obj, %mal_obj* %elementarray, i64 %index_i64
store %mal_obj %new_item, %mal_obj* %ptr_to_element
ret %mal_obj %obj
}
define private %mal_obj @mal_get_elementarray_item(%mal_obj %obj, %mal_obj %item_index) {
%elementarray = call %mal_obj* @mal_get_array_ptr_mal_obj(%mal_obj %obj)
%index_i64 = call i64 @mal_integer_to_raw(%mal_obj %item_index)
%ptr_to_element = getelementptr %mal_obj, %mal_obj* %elementarray, i64 %index_i64
%res_obj = load %mal_obj, %mal_obj* %ptr_to_element
ret %mal_obj %res_obj
}
define private %mal_obj @mal_concat_elementarrays(%mal_obj %objtype, %mal_obj %a, %mal_obj %b) {
%a_len = call i32 @mal_get_len_i32(%mal_obj %a)
%a_len_bytes = mul nsw i32 %a_len, 8
%a_arrayptr = call i8* @mal_get_array_ptr_i8(%mal_obj %a)
%b_len = call i32 @mal_get_len_i32(%mal_obj %b)
%b_len_bytes = mul nsw i32 %b_len, 8
%b_arrayptr = call i8* @mal_get_array_ptr_i8(%mal_obj %b)
%new_len_i32 = add nsw i32 %a_len, %b_len
%new_len_i64 = sext i32 %new_len_i32 to i64
%new_len = call %mal_obj @make_integer(i64 %new_len_i64)
%new_obj = call %mal_obj @mal_make_elementarray_obj(%mal_obj %objtype, %mal_obj %new_len)
%new_arrayptr = call i8* @mal_get_array_ptr_i8(%mal_obj %new_obj)
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %new_arrayptr, i8* %a_arrayptr, i32 %a_len_bytes, i32 0, i1 0)
%next_arrayptr = getelementptr i8, i8* %new_arrayptr, i32 %a_len_bytes
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %next_arrayptr, i8* %b_arrayptr, i32 %b_len_bytes, i32 0, i1 0)
ret %mal_obj %new_obj
}
define private %mal_obj @mal_slice_elementarray(%mal_obj %newobjtype, %mal_obj %obj, %mal_obj %from, %mal_obj %len) {
%from_index = call i64 @mal_integer_to_raw(%mal_obj %from)
%obj_arrayptr = call i8* @mal_get_array_ptr_i8(%mal_obj %obj)
%obj_arrayptr_malobjs = bitcast i8* %obj_arrayptr to %mal_obj*
%obj_arrayptr_malobjs_start = getelementptr %mal_obj, %mal_obj* %obj_arrayptr_malobjs, i64 %from_index
%obj_arrayptr_start = bitcast %mal_obj* %obj_arrayptr_malobjs_start to i8*
%len_elements_i64 = call i64 @mal_integer_to_raw(%mal_obj %len)
%len_elements_i32 = trunc i64 %len_elements_i64 to i32
%len_bytes = mul nsw i32 %len_elements_i32, 8
%new_obj = call %mal_obj @mal_make_elementarray_obj(%mal_obj %newobjtype, %mal_obj %len)
%new_arrayptr = call i8* @mal_get_array_ptr_i8(%mal_obj %new_obj)
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %new_arrayptr, i8* %obj_arrayptr_start, i32 %len_bytes, i32 0, i1 0)
ret %mal_obj %new_obj
}
define private %mal_obj @mal_native_func_apply_list(%mal_obj %fn, %mal_obj %argslist) {
%i0 = call %mal_obj @make_integer(i64 0)
%i1 = call %mal_obj @make_integer(i64 1)
%i2 = call %mal_obj @make_integer(i64 2)
%args_names = call %mal_obj @fn_args_names(%mal_obj %fn)
%args_num = call i32 @mal_get_len_i32(%mal_obj %args_names)
%funcptr = call %mal_obj @fn_func_ptr(%mal_obj %fn)
switch i32 %args_num, label %TooMany [ i32 0, label %ZeroArgs
i32 1, label %OneArg
i32 2, label %TwoArgs
i32 3, label %ThreeArgs ]
ZeroArgs:
%casted_funcptr_0 = inttoptr %mal_obj %funcptr to %mal_obj()*
%result_0 = call %mal_obj %casted_funcptr_0()
ret %mal_obj %result_0
OneArg:
%arg_0_1 = call %mal_obj @mal_get_elementarray_item(%mal_obj %argslist, %mal_obj %i0)
%casted_funcptr_1 = inttoptr %mal_obj %funcptr to %mal_obj(%mal_obj)*
%result_1 = call %mal_obj %casted_funcptr_1(%mal_obj %arg_0_1)
ret %mal_obj %result_1
TwoArgs:
%arg_0_2 = call %mal_obj @mal_get_elementarray_item(%mal_obj %argslist, %mal_obj %i0)
%arg_1_2 = call %mal_obj @mal_get_elementarray_item(%mal_obj %argslist, %mal_obj %i1)
%casted_funcptr_2 = inttoptr %mal_obj %funcptr to %mal_obj(%mal_obj,%mal_obj)*
%result_2 = call %mal_obj %casted_funcptr_2(%mal_obj %arg_0_2, %mal_obj %arg_1_2)
ret %mal_obj %result_2
ThreeArgs:
%arg_0_3 = call %mal_obj @mal_get_elementarray_item(%mal_obj %argslist, %mal_obj %i0)
%arg_1_3 = call %mal_obj @mal_get_elementarray_item(%mal_obj %argslist, %mal_obj %i1)
%arg_2_3 = call %mal_obj @mal_get_elementarray_item(%mal_obj %argslist, %mal_obj %i2)
%casted_funcptr_3 = inttoptr %mal_obj %funcptr to %mal_obj(%mal_obj,%mal_obj,%mal_obj)*
%result_3 = call %mal_obj %casted_funcptr_3(%mal_obj %arg_0_3, %mal_obj %arg_1_3, %mal_obj %arg_2_3)
ret %mal_obj %result_3
TooMany:
call %mal_obj @mal_throw(%mal_obj %i0)
ret %mal_obj 0
}
define private %mal_obj @mal_func_apply_list(%mal_obj %fn, %mal_obj %argslist) {
%funcenv = call %mal_obj @fn_env(%mal_obj %fn)
%binds = call %mal_obj @fn_args_names(%mal_obj %fn)
%callenv = call %mal_obj @new_env(%mal_obj %funcenv, %mal_obj %binds, %mal_obj %argslist)
%funcptr = call %mal_obj @fn_func_ptr(%mal_obj %fn)
%casted_funcptr = inttoptr %mal_obj %funcptr to %mal_obj(%mal_obj)*
%result = call %mal_obj %casted_funcptr(%mal_obj %callenv)
ret %mal_obj %result
}
define private %mal_obj @mal_with_meta(%mal_obj %obj, %mal_obj %new_meta) {
%new_obj_header = call %mal_obj_header_t* @alloc_obj_header()
%new_obj_i8_ptr = bitcast %mal_obj_header_t* %new_obj_header to i8*
%obj_i8_ptr = inttoptr %mal_obj %obj to i8*
%mal_obj_header_temp = getelementptr %mal_obj_header_t, %mal_obj_header_t* null, i64 1
%mal_obj_header_t_size = ptrtoint %mal_obj_header_t* %mal_obj_header_temp to i64
%mal_obj_header_t_size_i32 = trunc i64 %mal_obj_header_t_size to i32
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %new_obj_i8_ptr, i8* %obj_i8_ptr, i32 %mal_obj_header_t_size_i32, i32 0, i1 0)
%meta_field_ptr = getelementptr %mal_obj_header_t, %mal_obj_header_t* %new_obj_header, i32 0, i32 2
store %mal_obj %new_meta, %mal_obj* %meta_field_ptr
%new_obj = ptrtoint %mal_obj_header_t* %new_obj_header to %mal_obj
ret %mal_obj %new_obj
}
define private %mal_obj @mal_add(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = add nsw i64 %1, %2
%4 = call %mal_obj @make_integer(i64 %3)
ret %mal_obj %4
}
define private %mal_obj @mal_sub(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = sub nsw i64 %1, %2
%4 = call %mal_obj @make_integer(i64 %3)
ret %mal_obj %4
}
define private %mal_obj @mal_mul(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = mul nsw i64 %1, %2
%4 = call %mal_obj @make_integer(i64 %3)
ret %mal_obj %4
}
define private %mal_obj @mal_div(%mal_obj %a, %mal_obj %b) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %a)
%2 = call i64 @mal_integer_to_raw(%mal_obj %b)
%3 = sdiv i64 %1, %2
%4 = call %mal_obj @make_integer(i64 %3)
ret %mal_obj %4
}
define private %mal_obj @mal_time_ms() {
%tv = alloca %struct.timeval, align 8
%1 = call i32 @gettimeofday(%struct.timeval* %tv, %struct.timezone* null)
%2 = getelementptr inbounds %struct.timeval, %struct.timeval* %tv, i32 0, i32 0
%3 = load i64, i64* %2, align 8
%4 = mul nsw i64 %3, 1000
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %tv, i32 0, i32 1
%6 = load i64, i64* %5, align 8
%7 = sdiv i64 %6, 1000
%8 = add nsw i64 %4, %7
%9 = call %mal_obj @make_integer(i64 %8)
ret %mal_obj %9
}
define private %mal_obj @mal_os_exit(%mal_obj %exitcode) {
%1 = call i64 @mal_integer_to_raw(%mal_obj %exitcode)
%2 = trunc i64 %1 to i32
call i32 @exit(i32 %2)
unreachable
ret %mal_obj 0
}
define private %mal_obj @mal_gc_get_heap_size() {
%1 = call i64 @GC_get_heap_size()
%2 = call %mal_obj @make_integer(i64 %1)
ret %mal_obj %2
}
define private %mal_obj @mal_gc_get_total_bytes() {
%1 = call i64 @GC_get_total_bytes()
%2 = call %mal_obj @make_integer(i64 %1)
ret %mal_obj %2
}
define private %mal_obj @mal_readline(%mal_obj %prompt) {
%promptstr = call i8* @mal_get_array_ptr_i8(%mal_obj %prompt)
%line = call i8* @readline(i8* %promptstr)
%islinenull = icmp eq i8* %line, null
br i1 %islinenull, label %GotEof, label %GotString
GotEof:
%resnil = call %mal_obj @make_nil()
ret %mal_obj %resnil
GotString:
%len_bytes_without_nullchar = call i32 @strlen(i8* %line)
%len_bytes = add i32 %len_bytes_without_nullchar, 1
%len_bytes.i64 = sext i32 %len_bytes to i64
%linecopy = call i8* @GC_malloc_atomic(i64 %len_bytes.i64)
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %linecopy, i8* %line, i32 %len_bytes, i32 0, i1 0)
%resstr = call %mal_obj @mal_make_bytearray_obj(i32 18, i32 %len_bytes_without_nullchar, i8* %linecopy)
ret %mal_obj %resstr
}
define private %mal_obj @mal_slurp(%mal_obj %filename) {
; Get the filename
%st = alloca %struct.stat, align 8
%filenamestr = call i8* @mal_get_array_ptr_i8(%mal_obj %filename)
; Get the file size in bytes
call i32 @stat(i8* %filenamestr, %struct.stat* %st)
%sizeptr = getelementptr inbounds %struct.stat, %struct.stat* %st, i32 0, i32 8
%sizebytes = load i64, i64* %sizeptr, align 8
%sizebytes_i32 = trunc i64 %sizebytes to i32
%sizebytes_with_nullchar = add i64 %sizebytes, 1
%bufsize_i32 = trunc i64 %sizebytes_with_nullchar to i32
; Allocate buffer for file content
%buf = call i8* @GC_malloc_atomic(i64 %sizebytes_with_nullchar)
; Open-Read-Close
%fd = call i32 (i8*, i32, ...) @open(i8* %filenamestr, i32 0)
call i64 @read(i32 %fd, i8* %buf, i64 %sizebytes)
call i32 @close(i32 %fd)
; Build a Mal string object with the file's content
%resstr = call %mal_obj @mal_make_bytearray_obj(i32 18, i32 %sizebytes_i32, i8* %buf)
ret %mal_obj %resstr
}
define private %mal_obj @mal_printbytearray(%mal_obj %obj) {
%buf = call i8* @mal_get_array_ptr_i8(%mal_obj %obj)
call i32 @puts(i8* %buf)
%nil_obj = call %mal_obj @make_nil()
ret %mal_obj %nil_obj
}
define private %mal_obj @mal_throw(%mal_obj %obj) {
%mal_exn_temp = getelementptr %mal_exn_t, %mal_exn_t* null, i64 1
%mal_exn_t_size = ptrtoint %mal_exn_t* %mal_exn_temp to i64
%exn_i8_ptr = call i8* @GC_malloc(i64 %mal_exn_t_size)
%exn_ptr = bitcast i8* %exn_i8_ptr to %mal_exn_t*
%obj_ptr = getelementptr %mal_exn_t, %mal_exn_t* %exn_ptr, i32 0, i32 0
store %mal_obj %obj, %mal_obj* %obj_ptr
%exn_class_ptr = getelementptr %mal_exn_t, %mal_exn_t* %exn_ptr, i32 0, i32 1, i32 0
store i64 4996829957792549888, i64* %exn_class_ptr ; 4996829957792549888 = 0x45584e5f4d414c00 = "EXN_MAL\0"
%unwind_exception_ptr = getelementptr %mal_exn_t, %mal_exn_t* %exn_ptr, i32 0, i32 1
%unused = call i32 @_Unwind_RaiseException(%struct._Unwind_Exception* %unwind_exception_ptr)
unreachable
ret %mal_obj 0
}
define private %mal_obj @mal_get_exception_from_landing_pad({i8*,i32} %landing_exn) {
%landing_exn_first = extractvalue {i8*,i32} %landing_exn, 0
%landing_exn_mal_obj_ptr = bitcast i8* %landing_exn_first to %mal_obj*
%exn_obj_ptr = getelementptr %mal_obj, %mal_obj* %landing_exn_mal_obj_ptr, i32 -1
%exn_obj = load %mal_obj, %mal_obj* %exn_obj_ptr
ret i64 %exn_obj
}
define private void @save_argc_argv(i32 %argc, i8** %argv) {
store i32 %argc, i32* @global_argc
store i8** %argv, i8*** @global_argv
ret void
}
define private %mal_obj @mal_c_argc() {
%argc.i32 = load i32, i32* @global_argc
%argc.i64 = sext i32 %argc.i32 to i64
%argcobj = call %mal_obj @make_integer(i64 %argc.i64)
ret %mal_obj %argcobj
}
define private %mal_obj @mal_c_argv_str(%mal_obj %argindex) {
%argindex.i64 = call i64 @mal_integer_to_raw(%mal_obj %argindex)
%argv = load i8**, i8*** @global_argv
%argviptr = getelementptr inbounds i8*, i8** %argv, i64 %argindex.i64
%argvistr = load i8*, i8** %argviptr
%argvilen = call i32 @strlen(i8* %argvistr)
%argviobj = call %mal_obj @mal_make_bytearray_obj(i32 18, i32 %argvilen, i8* %argvistr)
ret %mal_obj %argviobj
}
; End of malc header.ll