From 1eac097706be2a579650fdfc5c769a6e65747c0e Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 30 Mar 2020 22:30:32 +0800 Subject: [PATCH] add simple output function --- src/main.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.rs b/src/main.rs index 61d181a90..7065da06a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,6 +98,10 @@ impl<'ctx> CodeGen<'ctx> { pass_manager.add_reassociate_pass(); pass_manager.initialize(); + let i32_type = context.i32_type(); + let fn_type = i32_type.fn_type(&[i32_type.into()], false); + module.add_function("output", fn_type, None); + CodeGen { context, module, pass_manager, builder: context.create_builder(), @@ -394,6 +398,12 @@ impl<'ctx> CodeGen<'ctx> { Ok(a.into()) } }, + + ("output", values::BasicValueEnum::IntValue(a)) => { + let fn_value = self.module.get_function("output").unwrap(); + Ok(self.builder.build_call(fn_value, &[a.into()], "call") + .try_as_basic_value().left().unwrap()) + }, _ => Err(self.compile_error(CompileErrorKind::Unsupported("unrecognized call"))) } } else { @@ -430,6 +440,7 @@ impl<'ctx> CodeGen<'ctx> { } } }, + Expression { expression } => { self.compile_expression(expression)?; }, If { test, body, orelse } => { let test = self.compile_expression(test)?; if test.get_type() != self.context.bool_type().into() {