]>
Witch of Git - ivy/blob - src/trans/x64.rs
2 use crate::trans
::{Code
, FnName
, Func
, Program
, SsaName
, Var
};
10 #[cfg(target_os = "macos")]
11 macro_rules
! extern_label
{
13 Label
::Extern(concat
!("_", $l
))
16 #[cfg(not(target_os = "macos"))]
17 macro_rules
! extern_label
{
23 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28 Builtin(&'
static str),
31 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
58 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
63 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
80 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
81 pub struct Definition
{
83 body
: Cow
<'
static, [Inst
]>,
92 builtins
: &HashMap
<String
, ast
::Name
>,
96 let (entry
, globals
, defns
) = compile(&prog
);
102 let builtins
= builtins
105 let number
= id
.global_number().unwrap
();
106 let (params
, name
, code
) = builtin(&name
);
110 name
: Label
::Builtin(name
),
111 body
: Cow
::Borrowed(code
),
116 .collect
::<Vec
<_
>>();
117 writeln
!(out
, ".data")?
;
118 for global
in globals
{
119 writeln
!(out
, "{}", global
)?
;
131 for builtin
in &builtins
{
139 mov [rip + {}], rax"#,
142 extern_label
!("ivy_make_lam"),
143 Label
::Global(builtin
.number
),
155 extern_label
!("ivy_exit"),
157 for builtin
in &builtins
{
158 writeln
!(out
, "{}", builtin
.defn
)?
;
162 writeln
!(out
, "{}", defn
)?
;
168 pub fn compile(prog
: &Program
) -> (Label
, Vec
<Global
>, Vec
<Definition
>) {
172 .map(|&global
| Global
{
173 name
: Label
::Global(global
),
180 .map(|func
| compile_func(func
))
182 (Label
::Function(prog
.top
), globals
, defns
)
185 fn even_up(x
: usize) -> usize {
189 fn compile_func(func
: &Func
) -> Definition
{
190 fn stack(x
: &SsaName
) -> Addr
{
191 Addr
::Off(Reg
::Rsp
, x
.0 as i32 * 8)
194 const HEADER_SIZE
: i32 = 0x18;
196 fn param(x
: u16) -> Addr
{
197 Addr
::Off(Reg
::Rbx
, HEADER_SIZE
+ x
as i32 * 8)
200 let upvar
= move |x
: u16| {
201 let params
= func
.params
as i32 * 8;
202 Addr
::Off(Reg
::Rbx
, HEADER_SIZE
+ params
+ x
as i32 * 8)
205 let mut body
= Vec
::new();
206 let stack_slots
= even_up(func
.order
.len());
207 body
.push(Inst
::Push(Reg
::Rbx
));
208 body
.push(Inst
::Cfi(Cfi
::Def(Reg
::Rsp
, 16)));
209 body
.push(Inst
::Mov(Reg
::Rbx
, Reg
::Rdi
));
210 body
.push(Inst
::SubI(Reg
::Rsp
, stack_slots
as u32 * 8));
211 body
.push(Inst
::Cfi(Cfi
::Def(Reg
::Rsp
, stack_slots
as i32 * 8 + 16)));
212 let address
= |v
| match v
{
213 Var
::Global(x
) => Addr
::Rip(Label
::Global(x
)),
214 Var
::Param(x
) => param(x
),
215 Var
::Upvar(x
) => upvar(x
),
216 Var
::Ssa(s
) => stack(&s
),
218 let get
= |r
, s
| match func
.block
[s
] {
219 Code
::Load(v
) => Inst
::Load(r
, address(v
)),
220 Code
::Num(v
) => Inst
::Imm(r
, (v
<< 1) | 1),
221 _
=> Inst
::Load(r
, stack(s
)),
223 for ssa
in &func
.order
{
224 match &func
.block
[ssa
] {
225 // We don't need to generate code for loads or nums themselves, they're consulted by other accesses
228 Code
::StoreGlobal(x
, s
) => {
229 body
.push(get(Reg
::Rax
, s
));
230 body
.push(Inst
::Store(Addr
::Rip(Label
::Global(*x
)), Reg
::Rax
));
237 body
.push(Inst
::Lea(Reg
::Rdi
, Addr
::Rip(Label
::Function(*name
))));
238 body
.push(Inst
::Imm(Reg
::Rsi
, *params
as i64));
239 body
.push(Inst
::Imm(Reg
::Rdx
, upvars
.len() as i64));
240 body
.push(Inst
::Call(extern_label
!("ivy_make_lam")));
241 body
.push(Inst
::Store(stack(ssa
), Reg
::Rax
));
242 for (i
, s
) in upvars
.iter
().enumerate() {
243 body
.push(get(Reg
::Rdi
, s
));
244 body
.push(Inst
::Store(
245 Addr
::Off(Reg
::Rax
, HEADER_SIZE
+ (*params
as i32) * 8 + i
as i32 * 8),
248 body
.push(Inst
::Call(extern_label
!("ivy_incref")));
249 body
.push(Inst
::Load(Reg
::Rax
, stack(ssa
)));
252 Code
::App(terms
) => {
253 let mut terms
= terms
.iter
();
254 body
.push(get(Reg
::Rdi
, terms
.next().expect("a function to apply")));
256 Some(s
) => body
.push(get(Reg
::Rsi
, s
)),
257 None
=> body
.push(Inst
::Imm(Reg
::Rsi
, 0)),
259 body
.push(Inst
::Call(extern_label
!("ivy_app")));
260 while let Some(term
) = terms
.next() {
261 body
.push(Inst
::Mov(Reg
::Rdi
, Reg
::Rax
));
262 body
.push(get(Reg
::Rsi
, term
));
263 body
.push(Inst
::Call(extern_label
!("ivy_app")));
265 body
.push(Inst
::Store(stack(ssa
), Reg
::Rax
));
269 match func
.block
[&func
.result
] {
270 Code
::Load(v
) => body
.push(Inst
::Load(Reg
::Rax
, address(v
))),
271 Code
::Num(n
) => body
.push(Inst
::Imm(Reg
::Rax
, (n
<< 1) | 1i64)),
274 body
.push(Inst
::AddI(Reg
::Rsp
, stack_slots
as u32 * 8));
275 body
.push(Inst
::Cfi(Cfi
::Def(Reg
::Rsp
, 16)));
276 body
.push(Inst
::Pop(Reg
::Rbx
));
277 body
.push(Inst
::Cfi(Cfi
::Def(Reg
::Rsp
, 8)));
278 body
.push(Inst
::Ret
);
280 name
: Label
::Function(func
.name
),
281 body
: Cow
::Owned(body
),
285 impl fmt
::Display
for Definition
{
286 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
287 writeln
!(fmt
, " .globl {}", self.name
)?
;
288 match self.body
.len() {
289 0 => write
!(fmt
, "{}:", self.name
),
290 1 => write
!(fmt
, "{}: {}", self.name
, self.body
[0]),
292 writeln
!(fmt
, " .cfi_startproc")?
;
293 writeln
!(fmt
, "{}:", self.name
)?
;
294 for inst
in self.body
.as_ref() {
295 writeln
!(fmt
, " {}", inst
)?
;
297 write
!(fmt
, " .cfi_endproc")
303 impl fmt
::Display
for Global
{
304 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
305 write
!(fmt
, "{}: .quad {}", self.name
, self.value
)
309 impl fmt
::Display
for Label
{
310 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
312 Label
::Global(g
) => write
!(fmt
, "IVY_GLOBAL${}", g
),
313 Label
::Function(f
) => write
!(fmt
, "ivy_fn${}", f
.0),
314 Label
::Extern(l
) => write
!(fmt
, "{}", l
),
315 Label
::Builtin(l
) => write
!(fmt
, "ivy_builtin${}", l
),
320 impl fmt
::Display
for Reg
{
321 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
323 Reg
::Rax
=> write
!(fmt
, "rax"),
324 Reg
::Rbx
=> write
!(fmt
, "rbx"),
325 Reg
::Rcx
=> write
!(fmt
, "rcx"),
326 Reg
::Rdx
=> write
!(fmt
, "rdx"),
327 Reg
::Rsi
=> write
!(fmt
, "rsi"),
328 Reg
::Rdi
=> write
!(fmt
, "rdi"),
329 Reg
::Rbp
=> write
!(fmt
, "rbp"),
330 Reg
::Rsp
=> write
!(fmt
, "rsp"),
331 Reg
::R8
=> write
!(fmt
, "r8"),
332 Reg
::R9
=> write
!(fmt
, "r9"),
333 Reg
::R10
=> write
!(fmt
, "r10"),
334 Reg
::R11
=> write
!(fmt
, "r11"),
335 Reg
::R12
=> write
!(fmt
, "r12"),
336 Reg
::R13
=> write
!(fmt
, "r13"),
337 Reg
::R14
=> write
!(fmt
, "r14"),
338 Reg
::R15
=> write
!(fmt
, "r15"),
343 impl fmt
::Display
for Addr
{
344 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
346 Addr
::Rip(l
) => write
!(fmt
, "[rip + {}]", l
),
347 Addr
::Off(r
, o
) => write
!(fmt
, "[{} + 0x{:02x}]", r
, o
),
352 impl fmt
::Display
for Cfi
{
353 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
355 Cfi
::Def(r
, i
) => write
!(fmt
, ".cfi_def_cfa {}, {}", r
, i
),
360 impl fmt
::Display
for Inst
{
361 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
363 Inst
::Push(r
) => write
!(fmt
, "push {}", r
),
364 Inst
::Pop(r
) => write
!(fmt
, "pop {}", r
),
365 Inst
::Cfi(c
) => write
!(fmt
, "{}", c
),
366 Inst
::Mov(d
, s
) => write
!(fmt
, "mov {}, {}", d
, s
),
367 Inst
::Load(r
, a
) => write
!(fmt
, "mov {}, {}", r
, a
),
368 Inst
::Store(a
, r
) => write
!(fmt
, "mov {}, {}", a
, r
),
369 Inst
::Imm(r
, i
) => write
!(fmt
, "mov {}, {}", r
, i
),
370 Inst
::Lea(r
, a
) => write
!(fmt
, "lea {}, {}", r
, a
),
371 Inst
::Call(l
) => write
!(fmt
, "call {}", l
),
372 Inst
::Jmp(l
) => write
!(fmt
, "jmp {}", l
),
373 Inst
::AddI(r
, x
) => write
!(fmt
, "add {}, {}", r
, x
),
374 Inst
::SubI(r
, x
) => write
!(fmt
, "sub {}, {}", r
, x
),
375 Inst
::Ret
=> write
!(fmt
, "ret"),
380 fn builtin(name
: &str) -> (u16, &'
static str, &'
static [Inst
]) {
386 Inst
::Load(Reg
::Rdi
, Addr
::Off(Reg
::Rdi
, 0x18)),
387 Inst
::Jmp(extern_label
!("ivy_debug")),
390 name
=> panic
!("Unsupported builtin '{}' for x64", name
),