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
// This file is part of assembler. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/assembler/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2017 The developers of assembler. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/assembler/master/COPYRIGHT.


/// Hints to use when creating an instruction stream.
///
/// Also can be retrieved using `InstructionStream.hints_for_next_instance()`, to fine-tune future instances to avoid reallocations.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InstructionStreamHints
{
	/// Number of labels.
	pub number_of_labels: usize,
	
	/// Number of 8-bit jumps.
	pub number_of_8_bit_jumps: usize,
	
	/// Number of 32-bit jumps.
	pub number_of_32_bit_jumps: usize,
	
	/// Number of emitted labels.
	pub number_of_emitted_labels: usize,
}

impl Default for InstructionStreamHints
{
	#[inline(always)]
	fn default() -> Self
	{
		InstructionStreamHints
		{
			number_of_labels: Self::MinimumValue,
			number_of_8_bit_jumps: Self::MinimumValue,
			number_of_32_bit_jumps: Self::MinimumValue,
			number_of_emitted_labels: Self::MinimumValue,
		}
	}
}

impl InstructionStreamHints
{
	const MinimumValue: usize = 4096;
	
	/// Ensures that sensible minimals are present.
	#[inline(always)]
	pub fn adjust(&mut self)
	{
		self.number_of_labels = Self::adjust_value(self.number_of_labels);
		self.number_of_8_bit_jumps = Self::adjust_value(self.number_of_8_bit_jumps);
		self.number_of_32_bit_jumps = Self::adjust_value(self.number_of_32_bit_jumps);
		self.number_of_emitted_labels = Self::adjust_value(self.number_of_emitted_labels);
	}
	
	/// Ensures that a maximum high-water-mark is kept.
	#[inline(always)]
	pub fn maximize(&mut self, mut newer_unadjusted_hints: Self)
	{
		newer_unadjusted_hints.adjust();
		
		if newer_unadjusted_hints.number_of_labels > self.number_of_labels
		{
			self.number_of_labels = newer_unadjusted_hints.number_of_labels
		}
		
		if newer_unadjusted_hints.number_of_8_bit_jumps > self.number_of_8_bit_jumps
		{
			self.number_of_8_bit_jumps = newer_unadjusted_hints.number_of_8_bit_jumps
		}
		
		if newer_unadjusted_hints.number_of_32_bit_jumps > self.number_of_32_bit_jumps
		{
			self.number_of_32_bit_jumps = newer_unadjusted_hints.number_of_32_bit_jumps
		}
		
		if newer_unadjusted_hints.number_of_emitted_labels > self.number_of_emitted_labels
		{
			self.number_of_emitted_labels = newer_unadjusted_hints.number_of_emitted_labels
		}
	}
	
	fn adjust_value(value: usize) -> usize
	{
		if value < Self::MinimumValue
		{
			Self::MinimumValue
		}
		else
		{
			value.next_power_of_two()
		}
	}
}