aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.rs
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2021-09-14 00:19:40 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2021-09-14 00:19:40 +0200
commit9e6c0a92dbedb5b8801772802e2e5d2e56cb9bcf (patch)
tree96e798b847138a2edffadad02ef6ec74cfd8c739 /src/main.rs
parent96e9dd5f4ae46b5705b8063a43bb8576e1e5b7b0 (diff)
downloadllvm-kaleidoscope-rs-9e6c0a92dbedb5b8801772802e2e5d2e56cb9bcf.tar.gz
llvm-kaleidoscope-rs-9e6c0a92dbedb5b8801772802e2e5d2e56cb9bcf.zip
ch3: added LLVM IR code genchapter3
- Added safe wrapper around LLVM C API - Added codegen module to emit LLVM IR for the AST - Update the main repl loop to codegen LLVM IR
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs53
1 files changed, 40 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs
index 2646873..7160e04 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,16 +1,24 @@
-mod lexer;
-mod parser;
+use llvm_kaleidoscope_rs::{
+ codegen::Codegen,
+ lexer::{Lexer, Token},
+ llvm,
+ parser::Parser,
+ Either,
+};
-use lexer::{Lexer, Token};
-use parser::Parser;
use std::io::Read;
-fn handle_definition<I>(p: &mut Parser<I>)
+fn handle_definition<I>(p: &mut Parser<I>, module: &llvm::Module)
where
I: Iterator<Item = char>,
{
match p.parse_definition() {
- Ok(expr) => println!("Parse 'def'\n{:?}", expr),
+ Ok(func) => {
+ println!("Parse 'def'\n{:?}", func);
+ if let Ok(func) = Codegen::compile(module, Either::B(&func)) {
+ func.dump();
+ }
+ }
Err(err) => {
eprintln!("Error: {:?}", err);
p.get_next_token();
@@ -18,12 +26,17 @@ where
}
}
-fn handle_extern<I>(p: &mut Parser<I>)
+fn handle_extern<I>(p: &mut Parser<I>, module: &llvm::Module)
where
I: Iterator<Item = char>,
{
match p.parse_extern() {
- Ok(expr) => println!("Parse 'extern'\n{:?}", expr),
+ Ok(proto) => {
+ println!("Parse 'extern'\n{:?}", proto);
+ if let Ok(proto) = Codegen::compile(module, Either::A(&proto)) {
+ proto.dump();
+ }
+ }
Err(err) => {
eprintln!("Error: {:?}", err);
p.get_next_token();
@@ -31,12 +44,17 @@ where
}
}
-fn handle_top_level_expression<I>(p: &mut Parser<I>)
+fn handle_top_level_expression<I>(p: &mut Parser<I>, module: &llvm::Module)
where
I: Iterator<Item = char>,
{
match p.parse_top_level_expr() {
- Ok(expr) => println!("Parse top-level expression\n{:?}", expr),
+ Ok(func) => {
+ println!("Parse top-level expression\n{:?}", func);
+ if let Ok(func) = Codegen::compile(module, Either::B(&func)) {
+ func.dump();
+ }
+ }
Err(err) => {
eprintln!("Error: {:?}", err);
p.get_next_token();
@@ -49,16 +67,22 @@ fn main() {
println!("ENTER to parse current input.");
println!("C-d to exit.");
+ // Create lexer over stdin.
let lexer = Lexer::new(std::io::stdin().bytes().filter_map(|v| {
let v = v.ok()?;
Some(v.into())
}));
+ // Create parser for kaleidoscope.
let mut parser = Parser::new(lexer);
// Throw first coin and initialize cur_tok.
parser.get_next_token();
+ // Initialize LLVM module with its own context.
+ // We will emit LLVM IR into this module.
+ let module = llvm::Module::new();
+
loop {
match *parser.cur_tok() {
Token::Eof => break,
@@ -66,9 +90,12 @@ fn main() {
// Ignore top-level semicolon.
parser.get_next_token()
}
- Token::Def => handle_definition(&mut parser),
- Token::Extern => handle_extern(&mut parser),
- _ => handle_top_level_expression(&mut parser),
+ Token::Def => handle_definition(&mut parser, &module),
+ Token::Extern => handle_extern(&mut parser, &module),
+ _ => handle_top_level_expression(&mut parser, &module),
}
}
+
+ // Dump all the emitted LLVM IR to stdout.
+ module.dump();
}