Skip to content

Commit

Permalink
Early returns and bools fixed!
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Mar 12, 2024
1 parent c1de5d0 commit c227b4f
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 4,376 deletions.
34 changes: 14 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ clap-verbosity-flag = "2.2.0"
clap_complete = "4.5.0"
const_format = "0.2.32"
cranelift-codegen = { version = "0.105.2", features = ["all-arch", "anyhow", "core", "bincode", "disas"] }
cranelift-frontend = "0.105.2"
cranelift-module = "0.105.2"
cranelift-native = "0.105.2"
cranelift-control = "0.105.2"
Expand Down Expand Up @@ -57,7 +58,6 @@ qsc-linker = { path = "./crates/qsc-linker" }
qsc-lsp = { path = "./crates/qsc-lsp" }
qsc-object = { path = "./crates/qsc-object" }
qsc-processor = { path = "./crates/qsc-processor" }
qsc-frontend = { path = "./crates/qsc-frontend" }

[profile.release]
strip = true
Expand All @@ -74,7 +74,6 @@ members = [
"crates/qsc-codegen",
"crates/qsc-compiler",
"crates/qsc-core",
"crates/qsc-frontend",
"crates/qsc-jit",
"crates/qsc-lexer",
"crates/qsc-linker",
Expand Down
2 changes: 1 addition & 1 deletion crates/qsc-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition.workspace = true
[dependencies]
miette.workspace = true
cranelift-codegen.workspace = true
cranelift-frontend.workspace = true
cranelift-module.workspace = true
cranelift-native.workspace = true
log.workspace = true
Expand All @@ -19,7 +20,6 @@ qsc-ast.workspace = true
qsc-core.workspace = true
qsc-jit.workspace = true
qsc-object.workspace = true
qsc-frontend.workspace = true
parking_lot.workspace = true
libc.workspace = true
thiserror.workspace = true
3 changes: 1 addition & 2 deletions crates/qsc-codegen/src/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::{
context::{CodegenContext, CompilerContext},
generator::{unify::BackendInternal, vars::func::FunctionCompiler, Backend},
};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_module::{default_libcall_names, DataDescription, DataId, Linkage, Module};
use qsc_ast::ast::{decl::func::FunctionNode, AbstractTree};
use qsc_frontend::{FunctionBuilder, FunctionBuilderContext};
use qsc_object::{ObjectBuilder, ObjectModule, ObjectProduct};
use target_lexicon::Triple;

Expand Down Expand Up @@ -143,7 +143,6 @@ impl AotGenerator {
values: HashMap::new(),
ret: func.ret.clone(),
func: func.clone(),
end: None,
};

Self::compile_fn(&self.ctx, ctx, func)?;
Expand Down
5 changes: 2 additions & 3 deletions crates/qsc-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::{
};

use cranelift_codegen::{
ir::{Block, Function, Value},
ir::{Function, Value},
CompiledCode, Context,
};
use cranelift_frontend::{FunctionBuilder, Variable};
use cranelift_module::{DataDescription, DataId, Module};
use qsc_frontend::{FunctionBuilder, Variable};

use miette::NamedSource;
use parking_lot::RwLock;
Expand All @@ -22,7 +22,6 @@ pub struct CodegenContext<'a, 'b> {
pub builder: &'b RwLock<FunctionBuilder<'a>>,
pub ret: Option<TypeNode>,
pub func: FunctionNode,
pub end: Option<Block>,
}

#[derive(Debug)]
Expand Down
6 changes: 3 additions & 3 deletions crates/qsc-codegen/src/generator/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use cranelift_codegen::{
entity::EntityRef,
ir::{InstBuilder, Value},
};
use cranelift_frontend::Variable;
use cranelift_module::Module;
use miette::Result;
use parking_lot::RwLock;
use qsc_frontend::Variable;

use crate::{
alias::DeclareAliasedFunction,
Expand Down Expand Up @@ -70,11 +70,11 @@ impl<'a, 'b, M: Module + DeclareAliasedFunction, T: Backend<'a, 'b, M>> ReturnCo

let val = bctx.use_var(ref_);

bctx.ins().jump(ctx.end.unwrap(), &[]);
bctx.ins().return_(&[val]);

Ok(val)
} else {
ctx.builder.write().ins().jump(ctx.end.unwrap(), &[]);
ctx.builder.write().ins().return_(&[]);

Ok(Self::null(ctx))
}
Expand Down
43 changes: 3 additions & 40 deletions crates/qsc-codegen/src/generator/vars/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
generator::{Backend, RETURN_VAR},
};

use qsc_ast::ast::{decl::func::FunctionNode, node::sym::SymbolNode};
use qsc_ast::ast::decl::func::FunctionNode;

use super::var::VariableCompiler;

Expand All @@ -27,22 +27,18 @@ impl<'a, 'b, M: Module, T: Backend<'a, 'b, M>> FunctionCompiler<'a, 'b, M> for T
func: &FunctionNode,
) -> Result<Value> {
let entry;
let end;

debug!("Creating entry block for function: {}", func.name);

{
let mut bctx = ctx.builder.write();
entry = bctx.create_block();
end = bctx.create_block();

entry = bctx.create_block();
bctx.append_block_params_for_function_params(entry);
bctx.switch_to_block(entry);
bctx.seal_block(entry);
}

ctx.end = Some(end);

debug!("Declaring argument variables for function: {}", func.name);

for (idx, arg) in func.args.iter().enumerate() {
Expand All @@ -58,42 +54,9 @@ impl<'a, 'b, M: Module, T: Backend<'a, 'b, M>> FunctionCompiler<'a, 'b, M> for T
Self::compile(cctx, ctx, node)?;
}

// Is this needed?
// ctx.builder.write().ins().jump(end, &[]);

// debug_assert!(
// self.position.is_none()
// || self.is_unreachable()
// || self.is_pristine(self.position.unwrap())
// || self.is_filled(self.position.unwrap()),
// "you have to fill your block before switching"
// );

if !ctx.builder.read().position.is_none()
&& !ctx.builder.read().is_unreachable()
&& !ctx.builder.read().is_pristine(entry)
&& !ctx.builder.read().is_filled(entry)
{
ctx.builder.write().ins().jump(end, &[]);
}

ctx.builder.write().switch_to_block(end);
ctx.builder.write().seal_block(end);

debug!("Compiled all nodes for function: {}", func.name);

if ctx.vars.contains_key(RETURN_VAR) {
let val = Self::compile_named_var(
cctx,
ctx,
SymbolNode {
value: RETURN_VAR.to_string(),
span: func.span.clone(),
},
)?;

ctx.builder.write().ins().return_(&[val]);
} else {
if !ctx.vars.contains_key(RETURN_VAR) {
ctx.builder.write().ins().return_(&[]);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/qsc-codegen/src/generator/vars/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use cranelift_codegen::{
entity::EntityRef,
ir::{InstBuilder, Value},
};
use cranelift_frontend::Variable;
use cranelift_module::{DataId, Linkage, Module};
use miette::{IntoDiagnostic, Result};
use qsc_frontend::Variable;

use parking_lot::RwLock;
use qsc_ast::ast::{decl::var::VariableNode, node::sym::SymbolNode};
Expand Down
3 changes: 1 addition & 2 deletions crates/qsc-codegen/src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use cranelift_codegen::{
settings::{self, Configurable, Flags},
Context,
};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_module::{default_libcall_names, DataDescription, DataId, Linkage, Module};
use miette::{IntoDiagnostic, NamedSource, Result};
use parking_lot::RwLock;
use qsc_ast::ast::{decl::func::FunctionNode, AbstractTree};
use qsc_frontend::{FunctionBuilder, FunctionBuilderContext};
use qsc_jit::{JITBuilder, JITModule};
use target_lexicon::Triple;

Expand Down Expand Up @@ -132,7 +132,6 @@ impl JitGenerator {
values: HashMap::new(),
ret: func.ret.clone(),
func: func.clone(),
end: None,
};

Self::compile_fn(&self.ctx, ctx, func)?;
Expand Down
25 changes: 0 additions & 25 deletions crates/qsc-frontend/Cargo.toml

This file was deleted.

Loading

0 comments on commit c227b4f

Please sign in to comment.