Skip to content

Commit

Permalink
[ast][mir] Int -> Int32 (#1193)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 authored Apr 14, 2024
1 parent 3f3abba commit 892ff09
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 42 deletions.
75 changes: 39 additions & 36 deletions crates/samlang-ast/src/mir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::hir;

use super::hir::{BinaryOperator, GlobalVariable};
use super::hir;
use enum_as_inner::EnumAsInner;
use itertools::Itertools;
use samlang_heap::{Heap, ModuleReference, PStr};
Expand Down Expand Up @@ -62,7 +60,7 @@ impl TypeName {
for t in &self.suffix {
collector.push('_');
match t {
Type::Int => collector.push_str("int"),
Type::Int32 => collector.push_str("int"),
Type::Id(id) => id.write_encoded(collector, heap, table),
}
}
Expand Down Expand Up @@ -195,7 +193,7 @@ impl SymbolTable {

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, EnumAsInner)]
pub enum Type {
Int,
Int32,
Id(TypeNameId),
}

Expand All @@ -206,13 +204,13 @@ impl Type {

pub fn pretty_print(&self, heap: &Heap, table: &SymbolTable) -> String {
match self {
Type::Int => "int".to_string(),
Type::Int32 => "int".to_string(),
Type::Id(id) => id.encoded_for_test(heap, table),
}
}
}

pub const INT_TYPE: Type = Type::Int;
pub const INT_TYPE: Type = Type::Int32;

#[derive(Debug, Clone)]
pub struct ClosureTypeDefinition {
Expand Down Expand Up @@ -431,7 +429,7 @@ pub const ONE: Expression = Expression::IntLiteral(1);
#[derive(Debug, Clone)]
pub struct Binary {
pub name: PStr,
pub operator: BinaryOperator,
pub operator: hir::BinaryOperator,
pub e1: Expression,
pub e2: Expression,
}
Expand Down Expand Up @@ -536,82 +534,87 @@ pub enum Statement {
impl Statement {
pub fn binary_unwrapped(
name: PStr,
operator: BinaryOperator,
operator: hir::BinaryOperator,
e1: Expression,
e2: Expression,
) -> Binary {
match (operator, &e2) {
(BinaryOperator::MINUS, Expression::IntLiteral(n)) if *n != -2147483648 => {
Binary { name, operator: BinaryOperator::PLUS, e1, e2: Expression::int(-n) }
(hir::BinaryOperator::MINUS, Expression::IntLiteral(n)) if *n != -2147483648 => {
Binary { name, operator: hir::BinaryOperator::PLUS, e1, e2: Expression::int(-n) }
}
_ => Binary { name, operator, e1, e2 },
}
}

pub fn binary_flexible_unwrapped(
name: PStr,
operator: BinaryOperator,
operator: hir::BinaryOperator,
e1: Expression,
e2: Expression,
) -> Binary {
let (operator, e1, e2) = Self::flexible_order_binary(operator, e1, e2);
Self::binary_unwrapped(name, operator, e1, e2)
}

pub fn binary(name: PStr, operator: BinaryOperator, e1: Expression, e2: Expression) -> Statement {
pub fn binary(
name: PStr,
operator: hir::BinaryOperator,
e1: Expression,
e2: Expression,
) -> Statement {
Statement::Binary(Self::binary_unwrapped(name, operator, e1, e2))
}

pub fn flexible_order_binary(
operator: BinaryOperator,
operator: hir::BinaryOperator,
e1: Expression,
e2: Expression,
) -> (BinaryOperator, Expression, Expression) {
) -> (hir::BinaryOperator, Expression, Expression) {
let Binary { name: _, operator: op, e1: normalized_e1, e2: normalized_e2 } =
Self::binary_unwrapped(PStr::INVALID_PSTR, operator, e1, e2);
match op {
BinaryOperator::DIV
| BinaryOperator::MOD
| BinaryOperator::MINUS
| BinaryOperator::SHL
| BinaryOperator::SHR => (op, normalized_e1, normalized_e2),
BinaryOperator::MUL
| BinaryOperator::PLUS
| BinaryOperator::LAND
| BinaryOperator::LOR
| BinaryOperator::XOR
| BinaryOperator::EQ
| BinaryOperator::NE => {
hir::BinaryOperator::DIV
| hir::BinaryOperator::MOD
| hir::BinaryOperator::MINUS
| hir::BinaryOperator::SHL
| hir::BinaryOperator::SHR => (op, normalized_e1, normalized_e2),
hir::BinaryOperator::MUL
| hir::BinaryOperator::PLUS
| hir::BinaryOperator::LAND
| hir::BinaryOperator::LOR
| hir::BinaryOperator::XOR
| hir::BinaryOperator::EQ
| hir::BinaryOperator::NE => {
if normalized_e1 > normalized_e2 {
(op, normalized_e1, normalized_e2)
} else {
(op, normalized_e2, normalized_e1)
}
}
BinaryOperator::LT => {
hir::BinaryOperator::LT => {
if normalized_e1 < normalized_e2 {
(BinaryOperator::GT, normalized_e2, normalized_e1)
(hir::BinaryOperator::GT, normalized_e2, normalized_e1)
} else {
(op, normalized_e1, normalized_e2)
}
}
BinaryOperator::LE => {
hir::BinaryOperator::LE => {
if normalized_e1 < normalized_e2 {
(BinaryOperator::GE, normalized_e2, normalized_e1)
(hir::BinaryOperator::GE, normalized_e2, normalized_e1)
} else {
(op, normalized_e1, normalized_e2)
}
}
BinaryOperator::GT => {
hir::BinaryOperator::GT => {
if normalized_e1 < normalized_e2 {
(BinaryOperator::LT, normalized_e2, normalized_e1)
(hir::BinaryOperator::LT, normalized_e2, normalized_e1)
} else {
(op, normalized_e1, normalized_e2)
}
}
BinaryOperator::GE => {
hir::BinaryOperator::GE => {
if normalized_e1 < normalized_e2 {
(BinaryOperator::LE, normalized_e2, normalized_e1)
(hir::BinaryOperator::LE, normalized_e2, normalized_e1)
} else {
(op, normalized_e1, normalized_e2)
}
Expand Down Expand Up @@ -890,7 +893,7 @@ impl Function {
#[derive(Debug)]
pub struct Sources {
pub symbol_table: SymbolTable,
pub global_variables: Vec<GlobalVariable>,
pub global_variables: Vec<hir::GlobalVariable>,
pub closure_types: Vec<ClosureTypeDefinition>,
pub type_definitions: Vec<TypeDefinition>,
pub main_function_names: Vec<FunctionName>,
Expand Down
4 changes: 2 additions & 2 deletions crates/samlang-ast/src/mir_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ mod tests {
format!("{:?}", INT_TYPE.cmp(&INT_TYPE));
assert!(ZERO.as_int_literal().is_some());
Type::Id(table.create_type_name_for_test(PStr::UPPER_A)).as_id();
Type::Id(table.create_type_name_for_test(PStr::UPPER_A)).is_int();
assert!(INT_TYPE.is_int());
Type::Id(table.create_type_name_for_test(PStr::UPPER_A)).is_int32();
assert!(INT_TYPE.is_int32());
assert!(INT_TYPE.as_id().is_none());
format!(
"{:?}",
Expand Down
2 changes: 1 addition & 1 deletion crates/samlang-compiler/src/lir_lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::{BTreeMap, HashSet};

fn lower_type(type_: mir::Type) -> lir::Type {
match type_ {
mir::Type::Int => lir::Type::Primitive(lir::PrimitiveType::Int),
mir::Type::Int32 => lir::Type::Primitive(lir::PrimitiveType::Int),
mir::Type::Id(name) => lir::Type::Id(name),
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/samlang-compiler/src/mir_generics_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl Rewriter {
generics_replacement_map: &HashMap<PStr, mir::Type>,
) -> mir::Type {
match type_ {
hir::Type::Int => mir::Type::Int,
hir::Type::Int => mir::Type::Int32,
hir::Type::Id(id) => self.rewrite_id_type(heap, id, generics_replacement_map),
}
}
Expand Down Expand Up @@ -584,7 +584,7 @@ impl Rewriter {
fn type_permit_enum_boxed_optimization(&self, type_: &mir::Type) -> bool {
match type_ {
// We cannot distinguish unboxed int from tags
mir::Type::Int => false,
mir::Type::Int32 => false,
mir::Type::Id(type_id) => {
match &self.specialized_type_definitions.get(type_id).unwrap().mappings {
// Structs are always pointers.
Expand Down
2 changes: 1 addition & 1 deletion crates/samlang-compiler/src/mir_type_deduplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn rewrite_id_type_name(state: &State, id: TypeNameId) -> TypeNameId {

fn rewrite_type(state: &State, type_: Type) -> Type {
match type_ {
Type::Int => Type::Int,
Type::Int32 => Type::Int32,
Type::Id(id) => Type::Id(rewrite_id_type_name(state, id)),
}
}
Expand Down

0 comments on commit 892ff09

Please sign in to comment.