Skip to content

Commit

Permalink
Fix detection of extern functions defined in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegTheCat committed Apr 8, 2019
1 parent b541566 commit 5c1ce9e
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions examples/kaleidoscope/main.rs
Expand Up @@ -286,12 +286,12 @@ pub struct Prototype {
pub prec: usize
}

/// Defines a user-defined function.
/// Defines a user-defined or external function.
#[derive(Debug)]
pub struct Function {
pub prototype: Prototype,
pub body: Expr,
pub is_anon: bool
pub body: Option<Expr>,
pub is_anon: bool,
}

/// Represents the `Expr` parser.
Expand Down Expand Up @@ -502,8 +502,8 @@ impl<'a> Parser<'a> {
// Return new function
Ok(Function {
prototype: proto,
body: body,
is_anon: false
body: Some(body),
is_anon: false,
})
}

Expand All @@ -515,11 +515,10 @@ impl<'a> Parser<'a> {
// Parse signature of extern function
let proto = self.parse_prototype()?;

// Return signature of extern function
Ok(Function {
prototype: proto,
body: Expr::Number(std::f64::NAN),
is_anon: false
body: None,
is_anon: false,
})
}

Expand Down Expand Up @@ -820,9 +819,10 @@ impl<'a> Parser<'a> {
args: vec![],
is_op: false,
prec: 0

},
body: expr,
is_anon: true
body: Some(expr),
is_anon: true,
})
},

Expand Down Expand Up @@ -1114,6 +1114,12 @@ impl<'a> Compiler<'a> {
fn compile_fn(&mut self) -> Result<FunctionValue, &'static str> {
let proto = &self.function.prototype;
let function = self.compile_prototype(proto)?;

// got external function, returning only compiled prototype
if self.function.body.is_none() {
return Ok(function);
}

let entry = self.context.append_basic_block(&function, "entry");

self.builder.position_at_end(&entry);
Expand All @@ -1134,7 +1140,7 @@ impl<'a> Compiler<'a> {
}

// compile body
let body = self.compile_expr(&self.function.body)?;
let body = self.compile_expr(self.function.body.as_ref().unwrap())?;

self.builder.build_return(Some(&body));

Expand Down Expand Up @@ -1183,24 +1189,22 @@ macro_rules! print_flush {
};
}

// Greg <6A>: 2017-10-03
// The two following functions are supposed to be found by the JIT
// using the 'extern' keyword, but it currently does not work on my machine.
// I tried using add_symbol, add_global_mapping, and simple extern declaration,
// but nothing worked.
// However, extern functions such as cos(x) and sin(x) can be imported without any problem.
// Other lines related to this program can be found further down.

pub extern fn putchard(x: f64) -> f64 {
#[no_mangle]
pub extern "C" fn putchard(x: f64) -> f64 {
print_flush!("{}", x as u8 as char);
x
}

pub extern fn printd(x: f64) -> f64 {
#[no_mangle]
pub extern "C" fn printd(x: f64) -> f64 {
println!("{}", x);
x

}

#[used]
static EXTERNAL_FNS: [extern fn(f64) -> f64; 2] = [putchard, printd];

/// Entry point of the program; acts as a REPL.
pub fn main() {
// use self::inkwell::support::add_symbol;
Expand Down

0 comments on commit 5c1ce9e

Please sign in to comment.