libjit-sys 0.2.1

Just-In-Time Compilation in Rust using LibJIT bindings
Documentation
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
 * jit-cfg.c - Control Flow Graph routines for the JIT.
 *
 * Copyright (C) 2006  Southern Storm Software, Pty Ltd.
 *
 * This file is part of the libjit library.
 *
 * The libjit library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * The libjit library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the libjit library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "jit-internal.h"
#include "jit-cfg.h"

static void
init_node(_jit_node_t node, jit_block_t block)
{
	node->block = block;
	if(block)
	{
		jit_block_set_meta(block, _JIT_BLOCK_CFG_NODE, node, 0);
	}
	node->flags = 0;
	node->succs = 0;
	node->num_succs = 0;
	node->preds = 0;
	node->num_preds = 0;

	_jit_bitset_init(&node->live_in);
	_jit_bitset_init(&node->live_out);
	_jit_bitset_init(&node->live_use);
	_jit_bitset_init(&node->live_def);

	node->dfn = -1;
}

static void
init_edge(_jit_edge_t edge)
{
	edge->src = 0;
	edge->dst = 0;
	edge->flags = 0;
}

static void
init_value_entry(_jit_value_entry_t value)
{
	value->value = 0;
}

static _jit_node_t
create_node()
{
	_jit_node_t node;
	node = jit_new(struct _jit_node);
	if(node)
	{
		init_node(node, 0);
	}
	return node;
}

_jit_cfg_t
create_cfg(jit_function_t func)
{
	_jit_cfg_t cfg;

	cfg = jit_new(struct _jit_cfg);
	if(!cfg)
	{
		return 0;
	}

	cfg->entry = create_node();
	if(!cfg->entry)
	{
		jit_free(cfg);
		return 0;
	}

	cfg->exit = create_node();
	if(!cfg->exit)
	{
		jit_free(cfg->entry);
		jit_free(cfg);
		return 0;
	}

	cfg->func = func;
	cfg->nodes = 0;
	cfg->num_nodes = 0;
	cfg->edges = 0;
	cfg->num_edges = 0;
	cfg->post_order = 0;
	cfg->values = 0;
	cfg->num_values = 0;
	cfg->max_values = 0;

	return cfg;
}

static int
build_nodes(_jit_cfg_t cfg, jit_function_t func)
{
	int count;
	jit_block_t block;

	count = 0;
	block = 0;
	while((block = jit_block_next(func, block)) != 0)
	{
		++count;
	}

	cfg->num_nodes = count;
	cfg->nodes = jit_malloc(count * sizeof(struct _jit_node));
	if(!cfg->nodes)
	{
		return 0;
	}

	count = 0;
	block = 0;
	while((block = jit_block_next(func, block)) != 0)
	{
		init_node(&cfg->nodes[count++], block);
	}

	return 1;
}

static _jit_node_t
get_next_node(_jit_cfg_t cfg, _jit_node_t node)
{
	int index = (node - cfg->nodes) + 1;
	if(index < cfg->num_nodes)
	{
		return cfg->nodes + index;
	}
	else
	{
		return cfg->exit;
	}
}

static _jit_node_t
get_label_node(_jit_cfg_t cfg, jit_label_t label)
{
	jit_block_t block;
	block = jit_block_from_label(cfg->func, label);
	if(!block)
	{
		return 0;
	}
	return jit_block_get_meta(block, _JIT_BLOCK_CFG_NODE);
}

static _jit_node_t
get_catcher_node(_jit_cfg_t cfg)
{
	jit_label_t label;
	label = cfg->func->builder->catcher_label;
	if(label == jit_label_undefined)
	{
		return cfg->exit;
	}
	return get_label_node(cfg, label);
}

static void
enum_edge(_jit_cfg_t cfg, _jit_node_t src, _jit_node_t dst, int flags, int create)
{
	if(!cfg || !src || !dst)
	{
		return;
	}

	if(create)
	{
		cfg->edges[cfg->num_edges].src = src;
		cfg->edges[cfg->num_edges].dst = dst;
		cfg->edges[cfg->num_edges].flags = flags;
		src->succs[src->num_succs] = &cfg->edges[cfg->num_edges];
		dst->preds[dst->num_preds] = &cfg->edges[cfg->num_edges];
	}

	++(cfg->num_edges);
	++(src->num_succs);
	++(dst->num_preds);
}

static void
enum_node_edges(_jit_cfg_t cfg, _jit_node_t node, int create)
{
	jit_insn_t insn;
	jit_label_t label;
	jit_label_t *labels;
	int index, num_labels;

	/* TODO: Handle catch, finally, filter blocks and calls. */

	insn = _jit_block_get_last(node->block);
	if(!insn)
	{
		/* empty block: create a fall-through edge */
		enum_edge(cfg, node, get_next_node(cfg, node), 0, create);
	}
	else if(insn->opcode == JIT_OP_BR)
	{
		label = (jit_label_t) insn->dest;
		enum_edge(cfg, node, get_label_node(cfg, label), 0, create);
	}
	else if(insn->opcode >= JIT_OP_BR_IFALSE && insn->opcode <= JIT_OP_BR_NFGE_INV)
	{
		label = (jit_label_t) insn->dest;
		enum_edge(cfg, node, get_label_node(cfg, label), 0, create);
		enum_edge(cfg, node, get_next_node(cfg, node), 0, create);
	}
	else if(insn->opcode >= JIT_OP_RETURN && insn->opcode <= JIT_OP_RETURN_SMALL_STRUCT)
	{
		enum_edge(cfg, node, cfg->exit, 0, create);
	}
	else if(insn->opcode == JIT_OP_THROW)
	{
		enum_edge(cfg, node, get_catcher_node(cfg), 0, create);
	}
	else if(insn->opcode == JIT_OP_RETHROW)
	{
		enum_edge(cfg, node, cfg->exit, 0, create);
	}
	else if(insn->opcode == JIT_OP_JUMP_TABLE)
	{
		labels = (jit_label_t *) insn->value1->address;
		num_labels = (int) insn->value2->address;
		for(index = 0; index < num_labels; index++)
		{
			enum_edge(cfg, node, get_label_node(cfg, labels[index]), 0, create);
		}
		enum_edge(cfg, node, get_next_node(cfg, node), 0, create);
	}
	else
	{
		/* otherwise create a fall-through edge */
		enum_edge(cfg, node, get_next_node(cfg, node), 0, create);
	}
}

static void
enum_all_edges(_jit_cfg_t cfg, jit_function_t func, int create)
{
	int index;

	if(cfg->num_nodes == 0)
	{
		enum_edge(cfg, cfg->entry, cfg->exit, 0, create);
	}
	else
	{
		enum_edge(cfg, cfg->entry, &cfg->nodes[0], 0, create);
		for(index = 0; index < cfg->num_nodes; index++)
		{
			enum_node_edges(cfg, &cfg->nodes[index], create);
		}
	}
}

static int
create_edges(_jit_cfg_t cfg, jit_function_t func)
{
	int index;

	if(cfg->num_edges == 0)
	{
		return 1;
	}

	cfg->edges = jit_malloc(cfg->num_edges * sizeof(struct _jit_edge));
	if(!cfg->edges)
	{
		return 0;
	}
	for(index = 0; index < cfg->num_edges; index++)
	{
		init_edge(&cfg->edges[index]);
	}
	for(index = 0; index < cfg->num_nodes; index++)
	{
		if(cfg->nodes[index].num_succs > 0)
		{
			cfg->nodes[index].succs = jit_calloc(cfg->nodes[index].num_succs,
							     sizeof(_jit_edge_t));
			if(!cfg->nodes[index].succs)
			{
				return 0;
			}
			cfg->nodes[index].num_succs = 0;
		}
		if(cfg->nodes[index].num_preds > 0)
		{
			cfg->nodes[index].preds = jit_calloc(cfg->nodes[index].num_preds,
							     sizeof(_jit_edge_t));
			if(!cfg->nodes[index].preds)
			{
				return 0;
			}
			cfg->nodes[index].num_preds = 0;
		}
	}

	cfg->num_edges = 0;
	return 1;
}

static int
build_edges(_jit_cfg_t cfg, jit_function_t func)
{
	enum_all_edges(cfg, func, 0);
	if(!create_edges(cfg, func))
	{
		return 0;
	}
	enum_all_edges(cfg, func, 1);
	return 1;
}

static int
compute_depth_first_order(_jit_cfg_t cfg)
{
	struct stack_entry
	{
		_jit_node_t node;
		int index;
	} *stack;
	_jit_node_t node;
	_jit_node_t succ;
	int post_order_num;
	int sp;
	int index;

	if(cfg->post_order)
	{
		return 1;
	}

	stack = jit_malloc((cfg->num_nodes + 1) * sizeof(struct stack_entry));
	if(!stack)
	{
		return 0;
	}

	cfg->post_order = jit_calloc(cfg->num_nodes, sizeof(_jit_node_t));
	if(!cfg->post_order)
	{
		jit_free(stack);
		return 0;
	}

	post_order_num = 0;

	stack[0].node = cfg->entry;
	stack[0].index = 0;
	sp = 1;

	while(sp)
	{
		node = stack[sp - 1].node;
		index = stack[sp - 1].index;

		succ = node->succs[index]->dst;
		if(succ != cfg->exit && (succ->flags & _JIT_NODE_VISITED) == 0)
		{
			succ->flags |= _JIT_NODE_VISITED;
			if(succ->num_succs > 0)
			{
				stack[sp].node = succ;
				stack[sp].index = 0;
				++sp;
			}
			else
			{
				cfg->post_order[post_order_num++] = succ;
			}
		}
		else
		{
			if(index < node->num_succs)
			{
				stack[sp - 1].index = index + 1;
			}
			else
			{
				if(node != cfg->entry)
				{
					cfg->post_order[post_order_num++] = node;
				}
				--sp;
			}
		}
	}

	jit_free(stack);
	return 1;
}

static jit_value_t
get_dest(jit_insn_t insn)
{
	if(insn->opcode == JIT_OP_NOP
	   || (insn->flags & JIT_INSN_DEST_OTHER_FLAGS) != 0
	   || (insn->dest && insn->dest->is_constant))
	{
		return 0;
	}
	return insn->dest;
}

static jit_value_t
get_value1(jit_insn_t insn)
{
	if(insn->opcode == JIT_OP_NOP
	   || (insn->flags & JIT_INSN_VALUE1_OTHER_FLAGS) != 0
	   || (insn->value1 && insn->value1->is_constant))
	{
		return 0;
	}
	return insn->value1;
}

static jit_value_t
get_value2(jit_insn_t insn)
{
	if(insn->opcode == JIT_OP_NOP
	   || (insn->flags & JIT_INSN_VALUE2_OTHER_FLAGS) != 0
	   || (insn->value2 && insn->value2->is_constant))
	{
		return 0;
	}
	return insn->value2;
}

static int
create_value_entry(_jit_cfg_t cfg, jit_value_t value)
{
	_jit_value_entry_t values;
	int max_values;

	if(value->index >= 0)
	{
		return 1;
	}

	if(cfg->num_values == cfg->max_values)
	{
		if(cfg->max_values == 0)
		{
			max_values = 20;
			values = jit_malloc(max_values * sizeof(struct _jit_value_entry));
		}
		else
		{
			max_values = cfg->max_values * 2;
			values = jit_realloc(cfg->values, max_values * sizeof(struct _jit_value_entry));
		}
		if(!values)
		{
			return 0;
		}
		cfg->values = values;
		cfg->max_values = max_values;
	}

	value->index = cfg->num_values++;
	init_value_entry(&cfg->values[value->index]);

	return 1;
}

static int
use_value(_jit_cfg_t cfg, _jit_node_t node, jit_value_t value)
{
	if(value->index < 0)
	{
		return 1;
	}
	if(_jit_bitset_is_allocated(&node->live_def)
	   && _jit_bitset_test_bit(&node->live_def, value->index))
	{
		return 1;
	}
	if(!_jit_bitset_is_allocated(&node->live_use)
	   && !_jit_bitset_allocate(&node->live_use, cfg->num_values))
	{
		return 0;
	}
	_jit_bitset_set_bit(&node->live_use, value->index);
	return 1;
}

static int
def_value(_jit_cfg_t cfg, _jit_node_t node, jit_value_t value)
{
	if(!_jit_bitset_is_allocated(&node->live_def)
	   && !_jit_bitset_allocate(&node->live_def, cfg->num_values))
	{
		return 0;
	}
	_jit_bitset_set_bit(&node->live_def, value->index);
	return 1;
}

static int
create_value_entries(_jit_cfg_t cfg)
{
	int index;
	_jit_node_t node;
	jit_insn_iter_t iter;
	jit_insn_t insn;
	jit_value_t dest;
	jit_value_t value1;
	jit_value_t value2;

	for(index = 0; index < cfg->num_nodes; index++)
	{
		node = &cfg->nodes[index];
		jit_insn_iter_init(&iter, node->block);
		while((insn = jit_insn_iter_next(&iter)) != 0)
		{
			dest = get_dest(insn);
			value1 = get_value1(insn);
			value2 = get_value1(insn);

			if(dest && !create_value_entry(cfg, dest))
			{
				return 0;
			}
			if(value1 && !create_value_entry(cfg, value1))
			{
				return 0;
			}
			if(value2 && !create_value_entry(cfg, value2))
			{
				return 0;
			}
		}
	}

	return 1;
}

static int
compute_local_live_sets(_jit_cfg_t cfg)
{
	int index;
	_jit_node_t node;
	jit_insn_iter_t iter;
	jit_insn_t insn;
	jit_value_t dest;
	jit_value_t value1;
	jit_value_t value2;

	for(index = 0; index < cfg->num_nodes; index++)
	{
		node = &cfg->nodes[index];
		jit_insn_iter_init(&iter, node->block);
		while((insn = jit_insn_iter_next(&iter)) != 0)
		{
			dest = get_dest(insn);
			value1 = get_value1(insn);
			value2 = get_value2(insn);

			if(value1 && !use_value(cfg, node, value1))
			{
				return 0;
			}
			if(value2 && !use_value(cfg, node, value2))
			{
				return 0;
			}
			if(dest)
			{
				if((insn->flags & JIT_INSN_DEST_IS_VALUE) != 0)
				{
					if(!use_value(cfg, node, dest))
					{
						return 0;
					}
				}
				else
				{
					if(!def_value(cfg, node, dest))
					{
						return 0;
					}
				}
			}
		}
	}

	return 1;
}

static int
compute_global_live_sets(_jit_cfg_t cfg)
{
	int change;
	int index, succ_index;
	_jit_node_t node;
	_jit_node_t succ;
	_jit_bitset_t bitset;

	if(!_jit_bitset_allocate(&bitset, cfg->num_values))
	{
		return 0;
	}

	do
	{
		change = 0;
		for(index = 0; index < cfg->num_nodes; index++)
		{
			node = cfg->post_order[index];
			if(!node)
			{
				continue;
			}

			_jit_bitset_clear(&bitset);
			for(succ_index = 0; succ_index < node->num_succs; succ_index++)
			{
				succ = node->succs[succ_index]->dst;
				if(_jit_bitset_is_allocated(&succ->live_in))
				{
					_jit_bitset_add(&bitset, &succ->live_in);
				}
			}
			if(!_jit_bitset_is_allocated(&node->live_out)
			   && !_jit_bitset_allocate(&node->live_out, cfg->num_values))
			{
				_jit_bitset_free(&bitset);
				return 0;
			}
			if(_jit_bitset_copy(&node->live_out, &bitset))
			{
				change = 1;
			}

			_jit_bitset_sub(&bitset, &node->live_def);
			_jit_bitset_add(&bitset, &node->live_use);
			if(!_jit_bitset_is_allocated(&node->live_in)
			   && !_jit_bitset_allocate(&node->live_in, cfg->num_values))
			{
				_jit_bitset_free(&bitset);
				return 0;
			}
			if(_jit_bitset_copy(&node->live_in, &bitset))
			{
				change = 1;
			}
		}
	}
	while(change);

	_jit_bitset_free(&bitset);
	return 1;
}

void
_jit_cfg_free(_jit_cfg_t cfg)
{
	int index;

	if(cfg->nodes)
	{
		for(index = 0; index < cfg->num_nodes; index++)
		{
			if(cfg->nodes[index].succs)
			{
				jit_free(cfg->nodes[index].succs);
			}
			if(cfg->nodes[index].preds)
			{
				jit_free(cfg->nodes[index].preds);
			}
		}
		jit_free(cfg->nodes);
	}
	if(cfg->edges)
	{
		jit_free(cfg->edges);
	}
	if(cfg->post_order)
	{
		jit_free(cfg->post_order);
	}
	if(cfg->values)
	{
		jit_free(cfg->values);
	}
	jit_free(cfg->entry);
	jit_free(cfg->exit);
	jit_free(cfg);
}

_jit_cfg_t
_jit_cfg_build(jit_function_t func)
{
	_jit_cfg_t cfg;

	cfg = create_cfg(func);
	if(!cfg)
	{
		return 0;
	}
	if(!build_nodes(cfg, func) || !build_edges(cfg, func))
	{
		_jit_cfg_free(cfg);
		return 0;
	}
	if(!compute_depth_first_order(cfg))
	{
		_jit_cfg_free(cfg);
		return 0;
	}

	return cfg;
}

int
_jit_cfg_compute_liveness(_jit_cfg_t cfg)
{
	return (create_value_entries(cfg)
		&& compute_local_live_sets(cfg)
		&& compute_global_live_sets(cfg));
}