Clean up warnings and formatting.

This commit is contained in:
2022-12-29 14:50:56 -08:00
committed by Adam Wick
parent e6a488012c
commit a426a4754a
10 changed files with 46 additions and 53 deletions

View File

@@ -118,7 +118,7 @@ impl<Annotation> Expression<Annotation> {
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Primitive {
Plus,
Minus,
@@ -141,7 +141,7 @@ where
}
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Value {
Number(Option<u8>, i128),
}
@@ -172,7 +172,7 @@ where
#[derive(Clone, Copy)]
struct CommaSep {}
impl<'a, 'b, D, A> Pretty<'a, D, A> for CommaSep
impl<'a, D, A> Pretty<'a, D, A> for CommaSep
where
A: 'a,
D: ?Sized + DocAllocator<'a, A>,

View File

@@ -10,10 +10,7 @@ pub struct Program<Annotation> {
}
impl<Annotation> Program<Annotation> {
pub fn pretty<'a, A, D>(
&self,
allocator: &'a D,
) -> DocBuilder<'a, D, A>
pub fn pretty<'a, A, D>(&self, allocator: &'a D) -> DocBuilder<'a, D, A>
where
A: 'a,
D: ?Sized + DocAllocator<'a, A>,
@@ -71,13 +68,16 @@ impl<Annotation> Statement<Annotation> {
.append(prim.pretty(variable_map, allocator))
}
Statement::Print(_, idx, var) => {
let name = strings.get(idx).cloned().unwrap_or_else(||"<unknown>".to_string());
Statement::Print(_, idx, _var) => {
let name = strings
.get(idx)
.cloned()
.unwrap_or_else(|| "<unknown>".to_string());
allocator
.text("print")
.append(allocator.space())
.append(allocator.text(name.to_string()))
.append(allocator.text(name))
}
}
}

View File

@@ -3,15 +3,12 @@ use codespan_reporting::diagnostic::Diagnostic;
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use cranelift_codegen::ir::{types, AbiParam, Signature};
use cranelift_codegen::isa::CallConv;
use cranelift_codegen::{isa, settings, CodegenError};
use cranelift_module::{default_libcall_names, Linkage, Module, ModuleError};
use cranelift_module::{default_libcall_names, ModuleError};
use cranelift_object::{object, ObjectBuilder, ObjectModule};
use ngr::asts::lil;
use ngr::passes::{run_front_end, BackendError};
use ngr::runtime::RuntimeFunctions;
use pretty::Arena;
use std::io;
use target_lexicon::Triple;
use thiserror::Error;
@@ -78,7 +75,7 @@ fn main() -> Result<(), MainError> {
if let Some((hil_tree, variable_map)) = hil_conversion_result.result {
//let arena = Arena::new();
let lil_tree = lil::Program::convert(hil_tree, variable_map.clone());
let lil_tree = lil::Program::convert(hil_tree, variable_map);
let isa = isa::lookup(Triple::host())?.finish(settings::Flags::new(settings::builder()))?;
let object_builder = ObjectBuilder::new(isa, "example", default_libcall_names())?;

View File

@@ -1,9 +1,7 @@
use std::collections::HashMap;
use std::string;
use crate::asts::hil;
use crate::asts::lil;
use crate::variable_map::VariableMap;
use std::collections::HashMap;
impl<Annotation: Clone> lil::Program<Annotation> {
pub fn convert(hil_program: hil::Program<Annotation>, mut variable_map: VariableMap) -> Self {
@@ -30,7 +28,7 @@ impl<Annotation: Clone> lil::Statement<Annotation> {
fn convert(
hil_statement: hil::Statement<Annotation>,
variable_map: &mut VariableMap,
strings: &mut HashMap<usize, String>
strings: &mut HashMap<usize, String>,
) -> Vec<Self> {
match hil_statement {
hil::Statement::Binding(annotation, var, expr) => match expr {
@@ -111,9 +109,9 @@ impl<Annotation: Clone> lil::Statement<Annotation> {
hil::Statement::Print(annotation, var) => {
let string_idx = strings.keys().max().unwrap_or(&1) + 1;
let zero_termed = format!("{}\0", variable_map.get_name(var).unwrap());
strings.insert(string_idx,zero_termed);
strings.insert(string_idx, zero_termed);
vec![lil::Statement::Print(annotation, string_idx, var)]
},
}
}
}
}

View File

@@ -7,7 +7,7 @@ use cranelift_codegen::ir::{entities, types, Function, InstBuilder, Signature, U
use cranelift_codegen::isa::CallConv;
use cranelift_codegen::Context;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
use cranelift_module::{FuncId, Linkage, Module, ModuleCompiledFunction, ModuleError, DataContext};
use cranelift_module::{DataContext, Linkage, Module, ModuleCompiledFunction, ModuleError};
use thiserror::Error;
#[derive(Debug, Error)]
@@ -22,7 +22,7 @@ pub enum BackendError {
impl<Annotation> Program<Annotation> {
pub fn into_cranelift<M: Module>(
&self,
mut self,
module: &mut M,
rtfuns: &RuntimeFunctions,
) -> Result<ModuleCompiledFunction, BackendError> {
@@ -38,19 +38,19 @@ impl<Annotation> Program<Annotation> {
Function::with_name_signature(UserFuncName::user(0, func_id.as_u32()), basic_signature);
let mut variable_name_global_values = HashMap::new();
for (index, value) in self.strings.iter() {
let global_id = module.declare_data(
&format!("local-string-{}", index),
Linkage::Local,
false,
false,
)?;
let mut data_context = DataContext::new();
data_context.set_align(8);
data_context.define(value.to_owned().into_boxed_str().into_boxed_bytes());
module.define_data(global_id, &data_context)?;
let local_data = module.declare_data_in_func(global_id, &mut ctx.func);
variable_name_global_values.insert(index, local_data);
for (index, value) in self.strings.drain() {
let global_id = module.declare_data(
&format!("local-string-{}", index),
Linkage::Local,
false,
false,
)?;
let mut data_context = DataContext::new();
data_context.set_align(8);
data_context.define(value.into_boxed_str().into_boxed_bytes());
module.define_data(global_id, &data_context)?;
let local_data = module.declare_data_in_func(global_id, &mut ctx.func);
variable_name_global_values.insert(index, local_data);
}
let print_func_ref = rtfuns.include_runtime_function("print", module, &mut ctx.func)?;
@@ -60,17 +60,17 @@ impl<Annotation> Program<Annotation> {
let main_block = builder.create_block();
builder.switch_to_block(main_block);
for stmt in self.statements.iter() {
for stmt in self.statements.drain(..) {
match stmt {
Statement::Print(_ann, name_idx, var) => {
let local_data = *variable_name_global_values.get(name_idx).unwrap();
let local_data = *variable_name_global_values.get(&name_idx).unwrap();
let var_name = builder.ins().symbol_value(types::I64, local_data);
let val = builder.use_var(Variable::new(*var));
let val = builder.use_var(Variable::new(var));
builder.ins().call(print_func_ref, &[var_name, val]);
}
Statement::ResultBinding(_, varnum, value) => {
let var = Variable::new(*varnum);
let var = Variable::new(varnum);
builder.declare_var(var, types::I64);
let val = match value {
@@ -102,7 +102,7 @@ impl<Annotation> Program<Annotation> {
}
Statement::VariableBinding(_, varnum, exp) => {
let var = Variable::new(*varnum);
let var = Variable::new(varnum);
builder.declare_var(var, types::I64);
let val = exp.into_cranelift(&mut builder);
builder.def_var(var, val);
@@ -119,13 +119,13 @@ impl<Annotation> Program<Annotation> {
}
impl<Annotation> SimpleExpression<Annotation> {
fn into_cranelift(&self, builder: &mut FunctionBuilder) -> entities::Value {
fn into_cranelift(self, builder: &mut FunctionBuilder) -> entities::Value {
match self {
SimpleExpression::Constant(_, value) => match value {
Value::Number(_base, numval) => builder.ins().iconst(types::I64, *numval as i64),
Value::Number(_base, numval) => builder.ins().iconst(types::I64, numval as i64),
},
SimpleExpression::Reference(_, num) => builder.use_var(Variable::new(*num)),
SimpleExpression::Reference(_, num) => builder.use_var(Variable::new(num)),
}
}
}

View File

@@ -1,6 +1,4 @@
use cranelift_codegen::ir::{
types, AbiParam, ArgumentExtension, ArgumentPurpose, FuncRef, Function, Signature,
};
use cranelift_codegen::ir::{types, AbiParam, FuncRef, Function, Signature};
use cranelift_codegen::isa::CallConv;
use cranelift_module::{FuncId, Linkage, Module, ModuleResult};
use std::collections::HashMap;

View File

@@ -97,7 +97,7 @@ where
}
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Value {
Number(Option<u8>, i128),
}
@@ -128,7 +128,7 @@ where
#[derive(Clone, Copy)]
struct CommaSep {}
impl<'a, 'b, D, A> Pretty<'a, D, A> for CommaSep
impl<'a, D, A> Pretty<'a, D, A> for CommaSep
where
A: 'a,
D: ?Sized + DocAllocator<'a, A>,

View File

@@ -17,7 +17,7 @@ impl<'s> TokenStream<'s> {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Location {
InFile(usize, usize),
Manufactured,
@@ -44,7 +44,7 @@ impl Default for Location {
}
}
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error, PartialEq, Eq)]
pub enum LexerError {
#[error("Failed lexing at {0}")]
LexFailure(Location),

View File

@@ -3,7 +3,7 @@ use logos::{Lexer, Logos};
use std::fmt;
use std::num::ParseIntError;
#[derive(Logos, Clone, Debug, PartialEq)]
#[derive(Logos, Clone, Debug, PartialEq, Eq)]
pub enum Token {
#[token("=")]
Equals,

View File

@@ -1,7 +1,7 @@
use crate::syntax::Location;
use codespan_reporting::diagnostic::{Diagnostic, Label};
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Warning {
ShadowedVariable(Location, Location, String),
}