-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
217 lines (180 loc) · 6.68 KB
/
Copy pathmain.rs
File metadata and controls
217 lines (180 loc) · 6.68 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
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
//! This example shows how to lower aggregate types such as structs.
//!
//! We'll go over how they can be constructed, optimized, and passed across the
//! function parameter/return boundary.
//!
//! The input we'll be working with is an AST-like `Type` type and a lookup table.
//!
//! Things to keep in mind for your own compiler:
//!
//! * Usually, things like field names and stringly identifiers would've already been desugared in
//! a previous IR before they are lower into LLVM/Cranelift IR.
//!
//! * This example will *not* go over alignment. Which makes it inefficient and incompatible with ABI's.
//! See the `struct-layouts` example for suggestions on alignment.
//!
//! `$ cargo run --example lowering-structs -- -o lowering-structs.o`
//! `$ clang lowering-structs.o -o lowering-structs`
//! `$ ./lowering-structs; echo $?`
use cranelift::{
codegen::Context,
prelude::{self as cl, FunctionBuilderContext, InstBuilder},
};
use cranelift_examples::{signature_from_decl, skip_boilerplate};
use cranelift_module::{FuncId, Linkage, Module};
mod lower;
mod types;
use cranelift_object::ObjectModule;
use lower::FuncLower;
use types::{LookupTable, Type};
// The `VirtualValue` enum keeps track of how our original values are mapped to Cranelift values.
//
// One value in our source language might be split across *multiple* Cranelift values.
// The same value in our source language can even be represented in different ways in Cranelift.
#[derive(Clone, Debug)]
enum VirtualValue {
// A singular value, will generally end up being passed around in registers.
Scalar(cl::Value),
// Our primary way of storing structs will be to create stackslots and write the fields at
// offsets of the stackslot pointers.
StackStruct {
type_: &'static str,
ptr: cl::Value,
},
// Instead of writing structs to stack pointers right away, we can try holding on to them in
// registers for a bit in-case they're temporary or will be written to other struct pointers.
UnstableStruct {
type_: &'static str,
fields: Vec<VirtualValue>,
},
}
impl VirtualValue {
#[track_caller]
fn as_scalar(&self) -> cl::Value {
match self {
VirtualValue::Scalar(value) => *value,
_ => panic!("not an scalar value"),
}
}
}
fn main() {
skip_boilerplate(b"lowering-structs", |ctx, fctx, module, _args| {
let mut types = types::LookupTable::hardcoded(module.isa().pointer_bytes() as u32);
let main_func_id = declare_main(module, &types);
let move_right_func_id = declare_move_right(module, &types);
types.function_names.insert(main_func_id, "main");
types
.function_names
.insert(move_right_func_id, "move_right");
define_main(module, &types, ctx, fctx, move_right_func_id, main_func_id);
define_move_right(module, &types, ctx, fctx, move_right_func_id);
});
}
// fn main() -> int;
fn declare_main(module: &mut ObjectModule, types: &LookupTable) -> FuncId {
let call_conv = module.isa().default_call_conv();
let sig = types.create_signature(call_conv, "main");
module
.declare_function("main", Linkage::Export, &sig)
.unwrap()
}
// fn move_right(p: Player, by: int) -> Player;
fn declare_move_right(module: &mut ObjectModule, types: &LookupTable) -> FuncId {
let call_conv = module.isa().default_call_conv();
let sig = types.create_signature(call_conv, "move_right");
module
.declare_function("move_right", Linkage::Export, &sig)
.unwrap()
}
// fn main() -> int {
// move_right(Player {
// id: 5,
// position: Point { x: 10, y: 20 },
// }, 2);
// return 0;
// }
fn define_main(
module: &mut ObjectModule,
types: &LookupTable,
ctx: &mut Context,
fctx: &mut FunctionBuilderContext,
move_right_func_id: FuncId,
id: FuncId,
) {
let mut builder = cl::FunctionBuilder::new(&mut ctx.func, fctx);
builder.func.signature = signature_from_decl(module, id);
let mut lower = FuncLower::new(&types, &mut builder, module);
let (entry, _vparams) = lower.create_entry_block(&[]);
lower.fbuilder.switch_to_block(entry);
let player: VirtualValue = {
let id = lower.int(5);
let position = {
let x = lower.int(10);
let y = lower.int(20);
lower.construct_struct("Point", &[("x", x), ("y", y)])
};
lower.construct_struct("Player", &[("id", id), ("position", position)])
};
let _moved_player: VirtualValue = {
let two = lower.ins().iconst(cl::types::I32, 2);
lower.call_func(move_right_func_id, vec![player, VirtualValue::Scalar(two)])
};
let exit_code = lower.int(0);
lower.return_(exit_code);
builder.finalize();
println!("fn main:\n{}", &ctx.func);
module.define_function(id, ctx).unwrap();
ctx.clear();
}
// fn move_right(p: Player, by: int) -> Player {
// Player {
// id: p.id,
// position: Point {
// x: p.position.x + by,
// y: p.position.y,
// }
// }
// }
//
// // -- Although what we'll actually be lowering it into is something more like -- //
//
// fn move_right(ret: *Player, p: *Player, by: int) -> () {
// *(ret+0) = *(p+0);
// *(ret+8) = *(p+8) + by;
// *(ret+16) = *(p+16);
// }
fn define_move_right(
module: &mut ObjectModule,
types: &LookupTable,
ctx: &mut Context,
fctx: &mut FunctionBuilderContext,
id: FuncId,
) {
ctx.func.signature = signature_from_decl(module, id);
let mut builder = cl::FunctionBuilder::new(&mut ctx.func, fctx);
let mut lower = FuncLower::new(&types, &mut builder, module);
let (entry, vparams) = lower.create_entry_block(&[Type::Struct("Player"), Type::Int]);
lower.fbuilder.switch_to_block(entry);
let player = {
let id = lower.destruct_field(&vparams[0], types.resolve_field("Player", "id"));
let position = {
let p_position =
lower.destruct_field(&vparams[0], types.resolve_field("Player", "position"));
let x = {
let x = lower
.destruct_field(&p_position, types.resolve_field("Point", "x"))
.as_scalar();
let by = vparams[1].as_scalar();
VirtualValue::Scalar(lower.ins().iadd(x, by))
};
let y = lower.destruct_field(&p_position, types.resolve_field("Point", "y"));
lower.construct_struct("Point", &[("x", x), ("y", y)])
};
lower.construct_struct("Player", &[("id", id), ("position", position)])
};
lower.return_(player);
builder.finalize();
println!("fn move_right:\n{}", &ctx.func);
module.define_function(id, ctx).unwrap();
ctx.clear();
}