Skip to content

Commit

Permalink
Implement separate namespace for mnemonics and names
Browse files Browse the repository at this point in the history
  • Loading branch information
ytausky committed Oct 8, 2020
1 parent 72429a5 commit 91b7976
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 340 deletions.
8 changes: 4 additions & 4 deletions src/assembler/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ pub enum Directive {
impl BuiltinMnemonic {
pub fn binds_to_label(&self) -> bool {
match self {
BuiltinMnemonic::Directive(directive) => match directive {
Directive::Equ | Directive::Macro | Directive::Section => true,
_ => false,
},
BuiltinMnemonic::Directive(directive) => matches!(
directive,
Directive::Equ | Directive::Macro | Directive::Section
),
_ => false,
}
}
Expand Down
35 changes: 18 additions & 17 deletions src/assembler/semantics/cpu_instr/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use super::{Analysis, AtomKind, Backend, Condition, Expr, Fragment, Operand, Wid
use crate::assembler::keywords::{BranchKind, ExplicitBranch, ImplicitBranch};
use crate::diagnostics::{Diagnostics, EmitDiag, Message};
use crate::expr::{Atom, BinOp, ExprOp};
use crate::object::SymbolId;
use crate::span::WithSpan;
use crate::span::{Source, SpanSource};

impl<'a, 'b, I, D, S> Analysis<'a, 'b, I, D, S>
where
I: Iterator<Item = Result<Operand<D::SymbolId, S>, ()>>,
I: Iterator<Item = Result<Operand<S>, ()>>,
D: Backend<S> + Diagnostics<S>,
S: Clone,
{
Expand Down Expand Up @@ -70,7 +71,7 @@ where
}
}

fn collect_branch_operands(&mut self) -> Result<BranchOperands<D::SymbolId, S>, ()> {
fn collect_branch_operands(&mut self) -> Result<BranchOperands<S>, ()> {
let first_operand = self.next_operand()?;
Ok(
if let Some(Operand::Atom(AtomKind::Condition(condition), range)) = first_operand {
Expand All @@ -95,18 +96,18 @@ fn encode_condition(condition: Condition) -> u8 {
}) << 3
}

type BranchOperands<N, S> = (Option<(Condition, S)>, Option<BranchTarget<N, S>>);
type BranchOperands<S> = (Option<(Condition, S)>, Option<BranchTarget<S>>);

enum BranchTarget<N, S> {
enum BranchTarget<S> {
DerefHl(S),
Expr(Expr<N, S>),
Expr(Expr<SymbolId, S>),
}

impl<N, S: Clone> SpanSource for BranchTarget<N, S> {
impl<S: Clone> SpanSource for BranchTarget<S> {
type Span = S;
}

impl<N, S: Clone> Source for BranchTarget<N, S> {
impl<S: Clone> Source for BranchTarget<S> {
fn span(&self) -> Self::Span {
match self {
BranchTarget::DerefHl(span) => span.clone(),
Expand All @@ -115,10 +116,10 @@ impl<N, S: Clone> Source for BranchTarget<N, S> {
}
}

fn analyze_branch_target<N, D, S>(
target: Option<Operand<N, S>>,
fn analyze_branch_target<D, S>(
target: Option<Operand<S>>,
diagnostics: &mut D,
) -> Result<Option<BranchTarget<N, S>>, ()>
) -> Result<Option<BranchTarget<S>>, ()>
where
D: Diagnostics<S>,
S: Clone,
Expand All @@ -137,14 +138,14 @@ where
}
}

enum BranchVariant<N, S> {
PotentiallyConditional(Branch<N, S>),
enum BranchVariant<S> {
PotentiallyConditional(Branch<S>),
Unconditional(UnconditionalBranch),
}

#[derive(Clone, Debug, PartialEq)]
enum Branch<N, S> {
Explicit(ExplicitBranch, Expr<N, S>),
enum Branch<S> {
Explicit(ExplicitBranch, Expr<SymbolId, S>),
Ret,
}

Expand All @@ -153,11 +154,11 @@ enum UnconditionalBranch {
Reti,
}

fn analyze_branch_variant<N, D, S>(
fn analyze_branch_variant<D, S>(
kind: (BranchKind, &S),
target: Option<BranchTarget<N, S>>,
target: Option<BranchTarget<S>>,
diagnostics: &mut D,
) -> Result<BranchVariant<N, S>, ()>
) -> Result<BranchVariant<S>, ()>
where
D: Diagnostics<S>,
S: Clone,
Expand Down
78 changes: 37 additions & 41 deletions src/assembler/semantics/cpu_instr/ld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::span::{Source, SpanSource};

impl<'a, 'b, I, D, S> Analysis<'a, 'b, I, D, S>
where
I: Iterator<Item = Result<Operand<D::SymbolId, S>, ()>>,
I: Iterator<Item = Result<Operand<S>, ()>>,
D: Backend<S> + Diagnostics<S>,
S: Clone,
{
Expand Down Expand Up @@ -68,8 +68,8 @@ where

fn analyze_8_bit_ld(
&mut self,
dest: LdDest8<D::SymbolId, S>,
src: impl Into<LdOperand<D::SymbolId, LdDest8<D::SymbolId, S>, S>>,
dest: LdDest8<S>,
src: impl Into<LdOperand<LdDest8<S>, S>>,
) -> Result<(), ()> {
match (dest, src.into()) {
(
Expand Down Expand Up @@ -112,8 +112,8 @@ where

fn analyze_16_bit_ld(
&mut self,
dest: LdDest16<D::SymbolId, S>,
src: impl Into<LdOperand<D::SymbolId, LdDest16<D::SymbolId, S>, S>>,
dest: LdDest16<S>,
src: impl Into<LdOperand<LdDest16<S>, S>>,
) -> Result<(), ()> {
match (dest, src.into()) {
(LdDest16::Reg16(Reg16::Sp, _), LdOperand::Other(LdDest16::Reg16(Reg16::Hl, _))) => {
Expand Down Expand Up @@ -143,11 +143,7 @@ where
}
}

fn analyze_special_ld(
&mut self,
other: LdSpecial<D::SymbolId, S>,
direction: Direction,
) -> Result<(), ()> {
fn analyze_special_ld(&mut self, other: LdSpecial<S>, direction: Direction) -> Result<(), ()> {
match other {
LdSpecial::DerefPtrReg(ptr_reg, _) => self.session.emit_fragment(Fragment::Byte(
0x02 | encode_ptr_reg(ptr_reg) | (encode_direction(direction) >> 1),
Expand All @@ -164,8 +160,8 @@ where
}
}

impl<N, S: Clone> Operand<N, S> {
fn into_ld_dest<D>(self, diagnostics: &mut D) -> Result<LdDest<N, S>, ()>
impl<S: Clone> Operand<S> {
fn into_ld_dest<D>(self, diagnostics: &mut D) -> Result<LdDest<S>, ()>
where
D: Diagnostics<S>,
{
Expand All @@ -191,7 +187,7 @@ impl<N, S: Clone> Operand<N, S> {
})
}

fn into_ld_src<D>(self, diagnostics: &mut D) -> Result<LdOperand<N, LdDest<N, S>, S>, ()>
fn into_ld_src<D>(self, diagnostics: &mut D) -> Result<LdOperand<LdDest<S>, S>, ()>
where
D: Diagnostics<S>,
{
Expand All @@ -202,62 +198,62 @@ impl<N, S: Clone> Operand<N, S> {
}
}

enum LdOperand<N, T, S> {
Const(Expr<N, S>),
enum LdOperand<T, S> {
Const(Expr<SymbolId, S>),
Other(T),
}

impl<N, S> From<LdDest8<N, S>> for LdOperand<N, LdDest8<N, S>, S> {
fn from(dest: LdDest8<N, S>) -> Self {
impl<S> From<LdDest8<S>> for LdOperand<LdDest8<S>, S> {
fn from(dest: LdDest8<S>) -> Self {
LdOperand::Other(dest)
}
}

impl<N, S> From<LdDest16<N, S>> for LdOperand<N, LdDest16<N, S>, S> {
fn from(dest: LdDest16<N, S>) -> Self {
impl<S> From<LdDest16<S>> for LdOperand<LdDest16<S>, S> {
fn from(dest: LdDest16<S>) -> Self {
LdOperand::Other(dest)
}
}

enum LdDest<N, S> {
Byte(LdDest8<N, S>),
Word(LdDest16<N, S>),
DerefExpr(Expr<N, S>),
enum LdDest<S> {
Byte(LdDest8<S>),
Word(LdDest16<S>),
DerefExpr(Expr<SymbolId, S>),
}

enum LdDest8<N, S> {
enum LdDest8<S> {
Simple(M, S),
Special(LdSpecial<N, S>),
Special(LdSpecial<S>),
}

enum LdSpecial<N, S> {
Deref(Expr<N, S>),
enum LdSpecial<S> {
Deref(Expr<SymbolId, S>),
DerefC(S),
DerefPtrReg(PtrReg, S),
}

enum LdDest16<N, S> {
enum LdDest16<S> {
Reg16(Reg16, S),
DerefNn(Expr<N, S>),
DerefNn(Expr<SymbolId, S>),
}

trait DataWidth {
fn width(&self) -> Width;
}

impl<N, S> DataWidth for LdDest8<N, S> {
impl<S> DataWidth for LdDest8<S> {
fn width(&self) -> Width {
Width::Byte
}
}

impl<N, S> DataWidth for LdDest16<N, S> {
impl<S> DataWidth for LdDest16<S> {
fn width(&self) -> Width {
Width::Word
}
}

impl<N, S: Clone> LdOperand<N, LdDest8<N, S>, S> {
impl<S: Clone> LdOperand<LdDest8<S>, S> {
fn expect_a<D>(self, diagnostics: &mut D) -> Result<(), ()>
where
D: Diagnostics<S>,
Expand All @@ -269,7 +265,7 @@ impl<N, S: Clone> LdOperand<N, LdDest8<N, S>, S> {
}
}

impl<N, S: Clone> LdDest8<N, S> {
impl<S: Clone> LdDest8<S> {
fn expect_a<D>(self, diagnostics: &mut D) -> Result<(), ()>
where
D: Diagnostics<S>,
Expand All @@ -286,11 +282,11 @@ fn diagnose_not_a<T, D: EmitDiag<S, T>, S>(span: S, diagnostics: &mut D) -> Resu
Err(())
}

impl<N, T: Source<Span = S>, S: Clone> SpanSource for LdOperand<N, T, S> {
impl<T: Source<Span = S>, S: Clone> SpanSource for LdOperand<T, S> {
type Span = S;
}

impl<N, T: Source<Span = S>, S: Clone> Source for LdOperand<N, T, S> {
impl<T: Source<Span = S>, S: Clone> Source for LdOperand<T, S> {
fn span(&self) -> Self::Span {
match self {
LdOperand::Const(expr) => expr.span(),
Expand All @@ -299,11 +295,11 @@ impl<N, T: Source<Span = S>, S: Clone> Source for LdOperand<N, T, S> {
}
}

impl<N, S: Clone> SpanSource for LdDest8<N, S> {
impl<S: Clone> SpanSource for LdDest8<S> {
type Span = S;
}

impl<N, S: Clone> Source for LdDest8<N, S> {
impl<S: Clone> Source for LdDest8<S> {
fn span(&self) -> Self::Span {
use self::LdDest8::*;
match self {
Expand All @@ -313,11 +309,11 @@ impl<N, S: Clone> Source for LdDest8<N, S> {
}
}

impl<N, S: Clone> SpanSource for LdSpecial<N, S> {
impl<S: Clone> SpanSource for LdSpecial<S> {
type Span = S;
}

impl<N, S: Clone> Source for LdSpecial<N, S> {
impl<S: Clone> Source for LdSpecial<S> {
fn span(&self) -> Self::Span {
use self::LdSpecial::*;
match self {
Expand All @@ -327,11 +323,11 @@ impl<N, S: Clone> Source for LdSpecial<N, S> {
}
}

impl<N, S: Clone> SpanSource for LdDest16<N, S> {
impl<S: Clone> SpanSource for LdDest16<S> {
type Span = S;
}

impl<N, S: Clone> Source for LdDest16<N, S> {
impl<S: Clone> Source for LdDest16<S> {
fn span(&self) -> Self::Span {
match self {
LdDest16::Reg16(_, span) => span.clone(),
Expand Down
Loading

0 comments on commit 91b7976

Please sign in to comment.