compile/
compile.rs

1extern crate pcb;
2use pcb::{Ctxt, Function, Block};
3use pcb::ty::{self, Type};
4
5/*
6fn main() -> s32 {
7  foo()
8}
9fn foo() -> s32 {
10  0
11}
12*/
13
14fn main() {
15  let ctxt = Ctxt::new();
16
17  {
18    let s32_ty = Type::int(&ctxt, 32);
19    let fun_ty = ty::Function::new(vec![], s32_ty);
20    let foo = Function::new(&ctxt, "foo", fun_ty.clone());
21    let foo_start = Block::append(foo);
22    let foo_ret = foo_start.build_const_int(s32_ty, 0);
23    foo_start.build_return(foo_ret);
24
25    let main = Function::new(&ctxt, "main", fun_ty);
26    let main_start = Block::append(main); // the first block added is the entry
27                                          // block
28    let main_end = Block::append(main);
29    let main_ret = main_start.build_call(foo, &[]);
30    main_start.build_branch(main_end);
31    main_end.build_call(foo, &[]); // useless, but you can still do
32    main_end.build_return(main_ret);
33  }
34
35  println!("{}", ctxt);
36}