CHECKPOINT: Everything builds again.

This commit is contained in:
2024-06-03 20:36:31 -07:00
parent afff04259c
commit 88d128df9f
39 changed files with 1514 additions and 1129 deletions

View File

@@ -1,7 +1,7 @@
use crate::backend::error::BackendError;
use crate::backend::Backend;
use crate::eval::PrimitiveType;
use crate::ir::{Expression, Primitive, Program, TopLevel, Type, Value, ValueOrRef, Variable};
use crate::ir::{Expression, Name, Primitive, Program, Type, Value, ValueOrRef};
use crate::syntax::{ConstantType, Location};
use cranelift_codegen::ir::{
self, entities, types, AbiParam, Function, GlobalValue, InstBuilder, MemFlags, Signature,
@@ -11,7 +11,6 @@ use cranelift_codegen::isa::CallConv;
use cranelift_codegen::Context;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_module::{DataDescription, FuncId, Linkage, Module};
use internment::ArcIntern;
use std::collections::{hash_map, HashMap};
const VOID_REPR_TYPE: types::Type = types::I64;
@@ -60,17 +59,16 @@ impl<M: Module> Backend<M> {
/// are no such statements, the function will do nothing.)
pub fn compile_program(
&mut self,
function_name: &str,
function_name: Name,
program: Program<Type>,
) -> Result<FuncId, BackendError> {
let mut generated_body = vec![];
let mut variables = HashMap::new();
for (top_level_name, top_level_type) in program.get_top_level_variables() {
match top_level_type {
Type::Function(argument_types, return_type) => {
let func_id = self.declare_function(
top_level_name.as_str(),
top_level_name.current_name(),
Linkage::Export,
argument_types,
*return_type,
@@ -80,7 +78,7 @@ impl<M: Module> Backend<M> {
Type::Primitive(pt) => {
let data_id = self.module.declare_data(
top_level_name.as_str(),
top_level_name.current_name(),
Linkage::Export,
true,
false,
@@ -94,7 +92,7 @@ impl<M: Module> Backend<M> {
Type::Structure(mut fields) => {
let data_id = self.module.declare_data(
top_level_name.as_str(),
top_level_name.current_name(),
Linkage::Export,
true,
false,
@@ -109,21 +107,24 @@ impl<M: Module> Backend<M> {
}
let void = Type::Primitive(PrimitiveType::Void);
let main_func_id =
self.declare_function(function_name, Linkage::Export, vec![], void.clone())?;
let main_func_id = self.declare_function(
function_name.current_name(),
Linkage::Export,
vec![],
void.clone(),
)?;
self.defined_functions
.insert(ArcIntern::new(function_name.to_string()), main_func_id);
.insert(function_name.clone(), main_func_id);
for item in program.items {
match item {
TopLevel::Function(name, args, rettype, body) => {
self.compile_function(&mut variables, name.as_str(), &args, rettype, body)?;
}
TopLevel::Statement(stmt) => {
generated_body.push(stmt);
}
}
for (_, function) in program.functions.into_iter() {
let func_id = self.compile_function(
&mut variables,
function.name.clone(),
&function.arguments,
function.return_type,
function.body,
)?;
self.defined_functions.insert(function.name, func_id);
}
self.compile_function(
@@ -131,7 +132,7 @@ impl<M: Module> Backend<M> {
function_name,
&[],
void.clone(),
Expression::Block(Location::manufactured(), void, generated_body),
program.body,
)
}
@@ -168,9 +169,9 @@ impl<M: Module> Backend<M> {
#[tracing::instrument(level = "debug", skip(self, variables, body))]
pub fn compile_function(
&mut self,
variables: &mut HashMap<Variable, ReferenceBuilder>,
function_name: &str,
arguments: &[(Variable, Type)],
variables: &mut HashMap<Name, ReferenceBuilder>,
function_name: Name,
arguments: &[(Name, Type)],
return_type: Type,
body: Expression<Type>,
) -> Result<FuncId, BackendError> {
@@ -195,13 +196,12 @@ impl<M: Module> Backend<M> {
// return to the user. For now, we declare all functions defined by this
// function as public/global/exported, although we may want to reconsider
// this decision later.
let interned_name = ArcIntern::new(function_name.to_string());
let func_id = match self.defined_functions.entry(interned_name) {
let func_id = match self.defined_functions.entry(function_name.clone()) {
hash_map::Entry::Occupied(entry) => *entry.get(),
hash_map::Entry::Vacant(vac) => {
tracing::warn!(name = ?function_name, "compiling undeclared function");
tracing::warn!(name = ?function_name.current_name(), "compiling undeclared function");
let func_id = self.module.declare_function(
function_name,
function_name.current_name(),
Linkage::Export,
&basic_signature,
)?;
@@ -277,7 +277,7 @@ impl<M: Module> Backend<M> {
fn compile_expression(
&mut self,
expr: Expression<Type>,
variables: &mut HashMap<Variable, ReferenceBuilder>,
variables: &mut HashMap<Name, ReferenceBuilder>,
builder: &mut FunctionBuilder,
) -> Result<(entities::Value, types::Type), BackendError> {
match expr {
@@ -379,7 +379,8 @@ impl<M: Module> Backend<M> {
panic!("Got to backend with non-structure type in structure construction?!");
};
let global_allocator = ArcIntern::new("__global_allocation_pointer__".to_string());
let global_allocator =
Name::new("__global_allocation_pointer__", Location::manufactured());
let Some(ReferenceBuilder::Global(_, allocator_variable)) =
variables.get(&global_allocator)
else {
@@ -635,7 +636,7 @@ impl<M: Module> Backend<M> {
fn compile_value_or_ref(
&self,
value_or_ref: ValueOrRef<Type>,
variables: &HashMap<Variable, ReferenceBuilder>,
variables: &HashMap<Name, ReferenceBuilder>,
builder: &mut FunctionBuilder,
) -> Result<(entities::Value, types::Type), BackendError> {
match value_or_ref {