Skip to content

Commit be232cb

Browse files
committed
Refactored some IR code
1 parent 46f6fe9 commit be232cb

File tree

5 files changed

+201
-186
lines changed

5 files changed

+201
-186
lines changed

Diff for: src/ir/global.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use super::Type;
2+
3+
#[derive(Clone, Debug)]
4+
pub struct Global {
5+
pub mangled_name: String,
6+
pub ir_type: Type,
7+
pub is_foreign: bool,
8+
pub is_thread_local: bool,
9+
}

Diff for: src/ir/instr.rs

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
use super::{FloatOrSign, FuncRef, GlobalVarRef, IntegerSign, Type, Value};
2+
use crate::asg::{FloatOrInteger, IntegerBits};
3+
4+
#[derive(Clone, Debug)]
5+
pub enum Instr {
6+
Return(Option<Value>),
7+
Call(Call),
8+
Alloca(Type),
9+
Store(Store),
10+
Load((Value, Type)),
11+
Malloc(Type),
12+
MallocArray(Type, Value),
13+
Free(Value),
14+
SizeOf(Type),
15+
Parameter(u32),
16+
GlobalVariable(GlobalVarRef),
17+
Add(BinaryOperands, FloatOrInteger),
18+
Checked(OverflowOperation, BinaryOperands),
19+
Subtract(BinaryOperands, FloatOrInteger),
20+
Multiply(BinaryOperands, FloatOrInteger),
21+
Divide(BinaryOperands, FloatOrSign),
22+
Modulus(BinaryOperands, FloatOrSign),
23+
Equals(BinaryOperands, FloatOrInteger),
24+
NotEquals(BinaryOperands, FloatOrInteger),
25+
LessThan(BinaryOperands, FloatOrSign),
26+
LessThanEq(BinaryOperands, FloatOrSign),
27+
GreaterThan(BinaryOperands, FloatOrSign),
28+
GreaterThanEq(BinaryOperands, FloatOrSign),
29+
And(BinaryOperands),
30+
Or(BinaryOperands),
31+
BitwiseAnd(BinaryOperands),
32+
BitwiseOr(BinaryOperands),
33+
BitwiseXor(BinaryOperands),
34+
LeftShift(BinaryOperands),
35+
ArithmeticRightShift(BinaryOperands),
36+
LogicalRightShift(BinaryOperands),
37+
Bitcast(Value, Type),
38+
ZeroExtend(Value, Type),
39+
SignExtend(Value, Type),
40+
FloatExtend(Value, Type),
41+
Truncate(Value, Type),
42+
TruncateFloat(Value, Type),
43+
IntegerToPointer(Value, Type),
44+
PointerToInteger(Value, Type),
45+
FloatToInteger(Value, Type, IntegerSign),
46+
IntegerToFloat(Value, Type, IntegerSign),
47+
Member {
48+
struct_type: Type,
49+
subject_pointer: Value,
50+
index: usize,
51+
},
52+
ArrayAccess {
53+
item_type: Type,
54+
subject_pointer: Value,
55+
index: Value,
56+
},
57+
StructLiteral(Type, Vec<Value>),
58+
IsZero(Value, FloatOrInteger),
59+
IsNonZero(Value, FloatOrInteger),
60+
Negate(Value, FloatOrInteger),
61+
BitComplement(Value),
62+
Break(Break),
63+
ConditionalBreak(Value, ConditionalBreak),
64+
Phi(Phi),
65+
InterpreterSyscall(InterpreterSyscallKind, Vec<Value>),
66+
}
67+
68+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
69+
pub enum OverflowOperator {
70+
Add,
71+
Subtract,
72+
Multiply,
73+
}
74+
75+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
76+
pub struct OverflowOperation {
77+
pub operator: OverflowOperator,
78+
pub sign: IntegerSign,
79+
pub bits: IntegerBits,
80+
}
81+
82+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
83+
pub enum InterpreterSyscallKind {
84+
Println,
85+
BuildAddProject,
86+
BuildSetAdeptVersion,
87+
BuildLinkFilename,
88+
BuildLinkFrameworkName,
89+
Experimental,
90+
ImportNamespace,
91+
DontAssumeIntAtLeast32Bits,
92+
UseDependency,
93+
}
94+
95+
#[derive(Clone, Debug)]
96+
pub struct Phi {
97+
pub ir_type: Type,
98+
pub incoming: Vec<PhiIncoming>,
99+
}
100+
101+
#[derive(Clone, Debug)]
102+
pub struct PhiIncoming {
103+
pub basicblock_id: usize,
104+
pub value: Value,
105+
}
106+
107+
#[derive(Clone, Debug)]
108+
pub struct Break {
109+
pub basicblock_id: usize,
110+
}
111+
112+
#[derive(Clone, Debug)]
113+
pub struct ConditionalBreak {
114+
pub true_basicblock_id: usize,
115+
pub false_basicblock_id: usize,
116+
}
117+
118+
#[derive(Clone, Debug)]
119+
pub struct BinaryOperands {
120+
pub left: Value,
121+
pub right: Value,
122+
}
123+
124+
impl BinaryOperands {
125+
pub fn new(left: Value, right: Value) -> Self {
126+
Self { left, right }
127+
}
128+
}
129+
130+
#[derive(Clone, Debug)]
131+
pub struct Call {
132+
pub func: FuncRef,
133+
pub args: Box<[Value]>,
134+
pub unpromoted_variadic_arg_types: Box<[Type]>,
135+
}
136+
137+
#[derive(Clone, Debug)]
138+
pub struct Store {
139+
pub new_value: Value,
140+
pub destination: Value,
141+
}

Diff for: src/ir/mod.rs

+9-185
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
mod funcs;
2+
mod global;
3+
mod instr;
24
mod structs;
5+
mod value;
36

47
pub use crate::asg::{FloatOrSign, GlobalVarRef, IntegerSign};
5-
use crate::{
6-
asg::{FloatOrInteger, IntegerBits},
7-
data_units::ByteUnits,
8-
source_files::Source,
9-
target::Target,
10-
};
8+
use crate::{data_units::ByteUnits, source_files::Source, target::Target};
119
use derivative::Derivative;
12-
use derive_more::{Deref, DerefMut, IsVariant, Unwrap};
10+
use derive_more::{Deref, DerefMut, IsVariant};
1311
use funcs::Funcs;
14-
use std::{collections::HashMap, ffi::CString};
12+
pub use global::Global;
13+
pub use instr::*;
14+
use std::collections::HashMap;
1515
pub use structs::Structs;
16+
pub use value::*;
1617

1718
pub struct Module {
1819
pub target: Target,
@@ -32,14 +33,6 @@ impl std::fmt::Debug for Module {
3233
}
3334
}
3435

35-
#[derive(Clone, Debug)]
36-
pub struct Global {
37-
pub mangled_name: String,
38-
pub ir_type: Type,
39-
pub is_foreign: bool,
40-
pub is_thread_local: bool,
41-
}
42-
4336
#[derive(Clone, Debug)]
4437
pub struct Func {
4538
pub mangled_name: String,
@@ -75,145 +68,6 @@ pub struct BasicBlock {
7568
pub instructions: Vec<Instr>,
7669
}
7770

78-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
79-
pub enum OverflowOperator {
80-
Add,
81-
Subtract,
82-
Multiply,
83-
}
84-
85-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
86-
pub struct OverflowOperation {
87-
pub operator: OverflowOperator,
88-
pub sign: IntegerSign,
89-
pub bits: IntegerBits,
90-
}
91-
92-
#[derive(Clone, Debug)]
93-
pub enum Instr {
94-
Return(Option<Value>),
95-
Call(Call),
96-
Alloca(Type),
97-
Store(Store),
98-
Load((Value, Type)),
99-
Malloc(Type),
100-
MallocArray(Type, Value),
101-
Free(Value),
102-
SizeOf(Type),
103-
Parameter(u32),
104-
GlobalVariable(GlobalVarRef),
105-
Add(BinaryOperands, FloatOrInteger),
106-
Checked(OverflowOperation, BinaryOperands),
107-
Subtract(BinaryOperands, FloatOrInteger),
108-
Multiply(BinaryOperands, FloatOrInteger),
109-
Divide(BinaryOperands, FloatOrSign),
110-
Modulus(BinaryOperands, FloatOrSign),
111-
Equals(BinaryOperands, FloatOrInteger),
112-
NotEquals(BinaryOperands, FloatOrInteger),
113-
LessThan(BinaryOperands, FloatOrSign),
114-
LessThanEq(BinaryOperands, FloatOrSign),
115-
GreaterThan(BinaryOperands, FloatOrSign),
116-
GreaterThanEq(BinaryOperands, FloatOrSign),
117-
And(BinaryOperands),
118-
Or(BinaryOperands),
119-
BitwiseAnd(BinaryOperands),
120-
BitwiseOr(BinaryOperands),
121-
BitwiseXor(BinaryOperands),
122-
LeftShift(BinaryOperands),
123-
ArithmeticRightShift(BinaryOperands),
124-
LogicalRightShift(BinaryOperands),
125-
Bitcast(Value, Type),
126-
ZeroExtend(Value, Type),
127-
SignExtend(Value, Type),
128-
FloatExtend(Value, Type),
129-
Truncate(Value, Type),
130-
TruncateFloat(Value, Type),
131-
IntegerToPointer(Value, Type),
132-
PointerToInteger(Value, Type),
133-
FloatToInteger(Value, Type, IntegerSign),
134-
IntegerToFloat(Value, Type, IntegerSign),
135-
Member {
136-
struct_type: Type,
137-
subject_pointer: Value,
138-
index: usize,
139-
},
140-
ArrayAccess {
141-
item_type: Type,
142-
subject_pointer: Value,
143-
index: Value,
144-
},
145-
StructLiteral(Type, Vec<Value>),
146-
IsZero(Value, FloatOrInteger),
147-
IsNonZero(Value, FloatOrInteger),
148-
Negate(Value, FloatOrInteger),
149-
BitComplement(Value),
150-
Break(Break),
151-
ConditionalBreak(Value, ConditionalBreak),
152-
Phi(Phi),
153-
InterpreterSyscall(InterpreterSyscallKind, Vec<Value>),
154-
}
155-
156-
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
157-
pub enum InterpreterSyscallKind {
158-
Println,
159-
BuildAddProject,
160-
BuildSetAdeptVersion,
161-
BuildLinkFilename,
162-
BuildLinkFrameworkName,
163-
Experimental,
164-
ImportNamespace,
165-
DontAssumeIntAtLeast32Bits,
166-
UseDependency,
167-
}
168-
169-
#[derive(Clone, Debug)]
170-
pub struct Phi {
171-
pub ir_type: Type,
172-
pub incoming: Vec<PhiIncoming>,
173-
}
174-
175-
#[derive(Clone, Debug)]
176-
pub struct PhiIncoming {
177-
pub basicblock_id: usize,
178-
pub value: Value,
179-
}
180-
181-
#[derive(Clone, Debug)]
182-
pub struct Break {
183-
pub basicblock_id: usize,
184-
}
185-
186-
#[derive(Clone, Debug)]
187-
pub struct ConditionalBreak {
188-
pub true_basicblock_id: usize,
189-
pub false_basicblock_id: usize,
190-
}
191-
192-
#[derive(Clone, Debug)]
193-
pub struct BinaryOperands {
194-
pub left: Value,
195-
pub right: Value,
196-
}
197-
198-
impl BinaryOperands {
199-
pub fn new(left: Value, right: Value) -> Self {
200-
Self { left, right }
201-
}
202-
}
203-
204-
#[derive(Clone, Debug)]
205-
pub struct Call {
206-
pub func: FuncRef,
207-
pub args: Box<[Value]>,
208-
pub unpromoted_variadic_arg_types: Box<[Type]>,
209-
}
210-
211-
#[derive(Clone, Debug)]
212-
pub struct Store {
213-
pub new_value: Value,
214-
pub destination: Value,
215-
}
216-
21771
#[derive(Derivative, Clone, Debug)]
21872
#[derivative(Hash, PartialEq, Eq)]
21973
pub struct Field {
@@ -446,36 +300,6 @@ pub struct TypeFunc {
446300
pub is_cstyle_variadic: bool,
447301
}
448302

449-
#[derive(Clone, Debug)]
450-
pub enum Value {
451-
Literal(Literal),
452-
Reference(ValueReference),
453-
}
454-
455-
#[derive(Clone, Debug, Unwrap, IsVariant)]
456-
pub enum Literal {
457-
Void,
458-
Boolean(bool),
459-
Signed8(i8),
460-
Signed16(i16),
461-
Signed32(i32),
462-
Signed64(i64),
463-
Unsigned8(u8),
464-
Unsigned16(u16),
465-
Unsigned32(u32),
466-
Unsigned64(u64),
467-
Float32(f32),
468-
Float64(f64),
469-
NullTerminatedString(CString),
470-
Zeroed(Type),
471-
}
472-
473-
#[derive(Clone, Debug)]
474-
pub struct ValueReference {
475-
pub basicblock_id: usize,
476-
pub instruction_id: usize,
477-
}
478-
479303
impl Module {
480304
pub fn new(target: Target) -> Self {
481305
Self {

0 commit comments

Comments
 (0)