Skip to content

Commit

Permalink
Split analyze::Var
Browse files Browse the repository at this point in the history
  • Loading branch information
lemolatoon committed Mar 14, 2023
1 parent b7bb7a5 commit 39ce0ac
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod scope;
pub mod stmt;
pub mod types;
pub mod util;
pub mod variables;
45 changes: 1 addition & 44 deletions src/analyze/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use super::{
stmt::{ConvStmt, LoopControlKind},
types::{BaseType, InCompleteKind, Type},
util::{align_to, aligned_offset},
variables::{EnumVariant, GVar, LVar, Var},
};

#[derive(PartialEq, Eq, Clone, Debug)]
Expand Down Expand Up @@ -2261,19 +2262,6 @@ pub enum CastContext {
// Binary,
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct LVar {
/// Used like `mov rax, [rbp - offset]`
pub offset: usize,
pub ty: Type,
}

impl LVar {
pub const fn new_raw(offset: usize, ty: Type) -> Self {
Self { offset, ty }
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum Taged {
Struct(StructTagKind),
Expand Down Expand Up @@ -2362,37 +2350,6 @@ pub struct Enum {
pub members: BTreeMap<String, usize>,
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct GVar {
pub name: String,
pub ty: Type,
pub init: Option<ConstInitializer>,
pub is_extern: bool,
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct EnumVariant {
pub name: String,
pub value: usize,
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum Var {
GVar(GVar),
LVar(LVar),
EnumVariant(EnumVariant),
}

impl Var {
pub fn ty(&self) -> Type {
match self {
Var::GVar(gvar) => gvar.ty.clone(),
Var::LVar(lvar) => lvar.ty.clone(),
Var::EnumVariant(_) => Type::Base(BaseType::Int),
}
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct Func {
pub name: String,
Expand Down
3 changes: 2 additions & 1 deletion src/analyze/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use crate::{
};

use super::{
analyze::{ConstInitializer, GVar, LVar},
analyze::ConstInitializer,
stmt::ConvStmt,
types::{BaseType, Type},
variables::{GVar, LVar},
};

#[derive(PartialEq, Eq, Clone, Debug)]
Expand Down
5 changes: 2 additions & 3 deletions src/analyze/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use crate::{
};

use super::{
analyze::{
ConstInitializer, Enum, EnumTagKind, EnumVariant, GVar, LVar, StructTagKind, Taged, Var,
},
analyze::{ConstInitializer, Enum, EnumTagKind, StructTagKind, Taged},
types::{InCompleteKind, Type},
util::aligned_offset,
variables::{EnumVariant, GVar, LVar, Var},
};

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
Expand Down
48 changes: 48 additions & 0 deletions src/analyze/variables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::{
analyze::ConstInitializer,
types::{BaseType, Type},
};

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum Var {
GVar(GVar),
LVar(LVar),
EnumVariant(EnumVariant),
}

impl Var {
pub fn ty(&self) -> Type {
match self {
Var::GVar(gvar) => gvar.ty.clone(),
Var::LVar(lvar) => lvar.ty.clone(),
Var::EnumVariant(_) => Type::Base(BaseType::Int),
}
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct GVar {
pub name: String,
pub ty: Type,
pub init: Option<ConstInitializer>,
pub is_extern: bool,
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct LVar {
/// Used like `mov rax, [rbp - offset]`
pub offset: usize,
pub ty: Type,
}

impl LVar {
pub const fn new_raw(offset: usize, ty: Type) -> Self {
Self { offset, ty }
}
}

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub struct EnumVariant {
pub name: String,
pub value: usize,
}
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::analyze::analyze::GVar;
use crate::analyze::expr::ConstExpr;
use crate::analyze::expr::ConvExpr;
use crate::analyze::expr::ConvExprKind;
use crate::analyze::expr::FuncCallTargetKind;
use crate::analyze::types::Type;
use crate::analyze::variables::GVar;
use crate::preprocess::tokenkind::TokenKind as PreprocessTokenKind;
use crate::tokenize::debug_infos::DebugInfo;
use crate::tokenize::debug_infos::Position;
Expand Down
3 changes: 2 additions & 1 deletion src/generate/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::io::{BufWriter, Write};

use crate::{
analyze::{
analyze::{ConstInitializer, ConvFuncDef, ConvProgram, ConvProgramKind, GVar, LVar},
analyze::{ConstInitializer, ConvFuncDef, ConvProgram, ConvProgramKind},
expr::{
CastKind, ConstExpr, ConstExprKind, ConvBinOpKind, ConvBinary, ConvExpr, ConvExprKind,
ConvUnaryOp, FuncCallTargetKind,
},
stmt::{ConvStmt, LoopControlKind},
types::{BaseType, Type},
variables::{GVar, LVar},
},
error::{CompileError, UnexpectedTypeSizeStatus},
parse::expr::BinOpKind,
Expand Down
3 changes: 2 additions & 1 deletion src/parse/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::collections::BTreeSet;

use crate::{
analyze::{
analyze::{Analyzer, ConstInitializer, LVar},
analyze::{Analyzer, ConstInitializer},
expr::{ConstExpr, ConvExpr},
stmt::ConvStmt,
types::Type,
variables::LVar,
},
error::CompileError,
tokenize::{debug_infos::DebugInfo, tokenize::AssignBinOpToken},
Expand Down
1 change: 1 addition & 0 deletions tests/analysis_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ironcc::analyze::expr::{
use ironcc::analyze::stmt::ConvStmt;
use ironcc::analyze::types::{BaseType, Type};
use ironcc::analyze::util::aligned_offset;
use ironcc::analyze::variables::LVar;
use ironcc::parse::declaration::{DirectDeclarator, TypeSpecifier};
use ironcc::parse::expr::{BinOpKind, UnaryOp};
use ironcc::parse::parse::*;
Expand Down

0 comments on commit 39ce0ac

Please sign in to comment.