-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmod.rs
More file actions
191 lines (150 loc) · 4.55 KB
/
mod.rs
File metadata and controls
191 lines (150 loc) · 4.55 KB
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
pub use self::classfile::*;
pub use self::io::*;
pub mod classfile;
pub mod io;
/*
// --
pub enum BytecodeError {
NotImplemented
}
pub type BytecodeResult<T> = Result<T, BytecodeError>;
// -- Stream
pub struct BytecodeReader<'a> {
source: &'a Read,
}
impl <'a> BytecodeReader<'a> {
pub fn new(source: &'a Read) -> BytecodeReader {
BytecodeReader { source: source }
}
pub fn read_u16(&self) -> Option<u16> {
None
}
}
pub struct BytecodeWriter<'a> {
target: &'a mut Write
}
impl<'a> BytecodeWriter<'a> {
/// Create a new bytecode writer that outputs the generated bytecode to the specified target
pub fn new(target: &'a mut Write) -> BytecodeWriter {
BytecodeWriter { target: target }
}
pub fn write_bytecode<T>(&mut self, bytecode: &T) -> Result<usize, Error> where T: Bytecode {
bytecode.write_bytecode(self)
}
pub fn write_u64(&mut self, value: u64) -> Result<usize, Error> {
self.target.write(&*vec![
((value & 0xFF << 56) >> 56) as u8,
((value & 0xFF << 48) >> 48) as u8,
((value & 0xFF << 40) >> 40) as u8,
((value & 0xFF << 32) >> 32) as u8,
((value & 0xFF << 24) >> 24) as u8,
((value & 0xFF << 16) >> 16) as u8,
((value & 0xFF << 8) >> 8) as u8,
(value & 0xFF) as u8
])
}
pub fn write_u32(&mut self, value: u32) -> Result<usize, Error> {
self.target.write(&*vec![
((value & 0xFF << 24) >> 24) as u8,
((value & 0xFF << 16) >> 16) as u8,
((value & 0xFF << 8) >> 8) as u8,
(value & 0xFF) as u8
])
}
pub fn write_u16(&mut self, value: u16) -> Result<usize, Error> {
self.target.write(&*vec![ ((value & 0xFF00) >> 8) as u8, (value & 0xFF) as u8 ])
}
pub fn write_u8(&mut self, value: u8) -> Result<usize, Error> {
self.target.write(&*vec![value])
}
pub fn write_n(&mut self, value: Vec<u8>) -> Result<usize, Error> {
value.iter().map(|v| self.write_u8(*v)).fold(Ok(0), |acc, x| {
match (acc, x) {
(Ok(i), Ok(s)) => Ok(i + s),
(e@Err(_), _) => e,
(_, Err(err)) => Err(err)
}
})
}
}
pub trait Bytecode: Sized {
fn read_bytecode(reader: &BytecodeReader) -> Result<Self, BytecodeError>;
fn write_bytecode(&self, writer: &mut BytecodeWriter) -> Result<usize, Error>;
}
// -- Constants --
pub struct ConstantPool {
}
impl ConstantPool {
}
pub struct ConstantPoolIndex {
pub index: u16
}
impl Bytecode for ConstantPoolIndex {
fn read_bytecode(reader: &BytecodeReader) -> Result<Self, BytecodeError> {
match reader.read_u16() {
Some(index) => Ok(ConstantPoolIndex { index: index }),
None => Err(BytecodeError::NotImplemented)
}
}
fn write_bytecode(&self, writer: &mut BytecodeWriter) -> Result<usize, Error> {
writer.write_u16(self.index)
}
}
// -- Attributes --
pub enum Attribute {
ConstantValue(ConstantValue),
Code(Code)
}
impl Bytecode for Attribute {
fn read_bytecode(reader: &BytecodeReader) -> Result<Self, BytecodeError> {
Err(BytecodeError::NotImplemented)
}
fn write_bytecode(&self, writer: &mut BytecodeWriter) -> Result<usize, Error> {
match self {
&Attribute::ConstantValue(ref val) => val.write_bytecode(writer),
&Attribute::Code(ref val) => val.write_bytecode(writer)
}
}
}
pub struct ConstantValue {
pub index: ConstantPoolIndex
}
impl Bytecode for ConstantValue {
fn read_bytecode(bytes: &BytecodeReader) -> Result<Self, BytecodeError> {
Err(BytecodeError::NotImplemented)
}
fn write_bytecode(&self, writer: &mut BytecodeWriter) -> Result<usize, Error> {
Ok(0)
}
}
pub struct Code {
pub max_stack: u16,
pub max_locals: u16,
pub code: Vec<u8>,
pub exception_table: Vec<ExceptionHandler>,
pub attributes: Vec<Attribute>
}
impl Bytecode for Code {
fn read_bytecode(bytes: &BytecodeReader) -> Result<Self, BytecodeError> {
Err(BytecodeError::NotImplemented)
}
fn write_bytecode(&self, writer: &mut BytecodeWriter) -> Result<usize, Error> {
Ok(0)
}
}
pub struct ExceptionHandler {
pub start_pc: u16,
pub end_pc: u16,
pub handler_pc: u16,
pub catch_type: u16
}
pub struct StackMapTable {
pub entries: Vec<StackMapFrame>
}
pub enum StackMapFrame {
// TODO incomplete
}
pub struct Exceptions {
pub exception_index_table: Vec<ConstantPoolIndex>
}
*/